Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
skim
committed
Nov 24, 2010
1 parent
a8f81ec
commit 0d5e097
Showing
3 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
Object subclass: Console [ | ||
| stdInput stdOutput lastDisplayedMessage | | ||
|
||
Console class >> new [ | ||
^super new initialize | ||
] | ||
|
||
initialize [ | ||
stdInput := stdin. | ||
stdOutput := Transcript. | ||
] | ||
|
||
stdInput [ | ||
^stdInput | ||
] | ||
|
||
stdInput: in [ | ||
stdInput := in. | ||
] | ||
|
||
stdOutput [ | ||
^stdOutput | ||
] | ||
|
||
stdOutput: output [ | ||
stdOutput := output. | ||
] | ||
|
||
userInput [ | ||
^stdInput nextLine. | ||
] | ||
|
||
playerType: piece [ | ||
stdOutput cr; | ||
show: 'Choose player type for ', piece asString, ' (enter ''h'' for human or ''u'' for unbeatable cpu) '. | ||
^self userInput | ||
] | ||
|
||
humanPlayerMove: piece [ | ||
stdOutput cr; | ||
show: 'Enter your move, player ', piece asString. | ||
^(self userInput asInteger) | ||
] | ||
|
||
playAgain [ | ||
^self userInput | ||
] | ||
|
||
displayBoard: list [ | ||
| bar space vSpace | | ||
bar := '---+---+---'. | ||
space := ' '. | ||
vSpace := ' | '. | ||
stdOutput cr; | ||
show: space, (list at: 1) asString, vSpace, (list at: 2) asString, vSpace, (list at: 3) asString; | ||
cr; | ||
show: bar; | ||
cr; | ||
show: space, (list at: 5) asString, vSpace, (list at: 5) asString, vSpace, (list at: 6) asString; | ||
cr; | ||
show: bar; | ||
cr; | ||
show: space, (list at: 7) asString, vSpace, (list at: 8) asString, vSpace, (list at: 9) asString. | ||
] | ||
|
||
displayNegamaxMoveMessage: piece [ | ||
stdOutput show: 'Player ''', piece asString, ''' is making a move'; | ||
cr. | ||
] | ||
|
||
displayWinner: piece [ | ||
stdOutput show: 'The winner is ', piece asString, '!!!'. | ||
] | ||
|
||
displayTryAgain [ | ||
stdOutput cr; cr; | ||
show: 'Do you want to play again? (''y'' or ''n'') '. | ||
] | ||
|
||
displayDrawMessage [ | ||
stdOutput show: 'The game is a draw.'. | ||
] | ||
|
||
displayExitMessage [ | ||
stdOutput show: 'Thanks for playing!'. | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,106 @@ | ||
Object subclass: FakeStdIn [ | ||
| nextLine | | ||
|
||
nextLine [ | ||
^nextLine | ||
] | ||
|
||
nextLine: input [ | ||
nextLine := input. | ||
] | ||
] | ||
|
||
Object subclass: FakeStdOut [ | ||
| message | | ||
|
||
message [ | ||
^message | ||
] | ||
|
||
cr [] | ||
|
||
show: newMessage [ | ||
(message isNil) | ||
ifTrue: [message := '']. | ||
message := message, newMessage. | ||
] | ||
] | ||
|
||
TicTacToeTestCase subclass: ConsoleTestCase [ | ||
| console fakeStdIn fakeStdOut | | ||
setUp [ | ||
fakeStdIn := FakeStdIn new. | ||
fakeStdOut := FakeStdOut new. | ||
console := Console new. | ||
console stdInput: fakeStdIn. | ||
console stdOutput: fakeStdOut. | ||
] | ||
|
||
testInputFromUser [ | ||
| testString | | ||
testString := 'hello world'. | ||
fakeStdIn nextLine: testString. | ||
self assert: (console userInput = testString). | ||
] | ||
|
||
testPlayerTypeInputFromUser [ | ||
| human | | ||
human := 'h'. | ||
fakeStdIn nextLine: human. | ||
self assert: ((console playerType: $X) = human). | ||
self assert: (fakeStdOut message = 'Choose player type for X (enter ''h'' for human or ''u'' for unbeatable cpu) ') | ||
] | ||
|
||
testHumanPlayerMove [ | ||
| move | | ||
move := '4'. | ||
fakeStdIn nextLine: move. | ||
self assert: ((console humanPlayerMove: $X) = move asInteger). | ||
] | ||
|
||
testTryAgainInputFromUser [ | ||
| yes | | ||
yes := 'y'. | ||
fakeStdIn nextLine: yes. | ||
self assert: ((console playAgain) = yes). | ||
] | ||
|
||
testDisplayBoardLayout [ | ||
| list board bar space vSpace expectedBoardLayout | | ||
list := Array new: 9 withAll: $O. | ||
board := ThreeByThree create: list. | ||
console displayBoard: board list. | ||
bar := '---+---+---'. | ||
space := ' '. | ||
vSpace := ' | '. | ||
expectedBoardLayout := space, (list at: 1) asString, vSpace, (list at: 2) asString, vSpace, (list at: 3) asString, bar, | ||
space, (list at: 5) asString, vSpace, (list at: 5) asString, vSpace, (list at: 6) asString, bar, | ||
space, (list at: 7) asString, vSpace, (list at: 8) asString, vSpace, (list at: 9) asString. | ||
self assert: (fakeStdOut message = expectedBoardLayout). | ||
] | ||
|
||
testNegamaxMoveMessage [ | ||
console displayNegamaxMoveMessage: $O. | ||
self assert: (fakeStdOut message = 'Player ''O'' is making a move'). | ||
] | ||
|
||
testDisplayWinner [ | ||
console displayWinner: $X. | ||
self assert: (fakeStdOut message = 'The winner is X!!!'). | ||
] | ||
|
||
testDisplayTryAgain [ | ||
console displayTryAgain. | ||
self assert: (fakeStdOut message = 'Do you want to play again? (''y'' or ''n'') '). | ||
] | ||
|
||
testDisplayDrawMessage [ | ||
console displayDrawMessage. | ||
self assert: (fakeStdOut message = 'The game is a draw.'). | ||
] | ||
|
||
testDisplayExitMessage [ | ||
console displayExitMessage. | ||
self assert: (fakeStdOut message = 'Thanks for playing!'). | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
TicTacToeTestCase subclass: HumanTestCase [ | ||
testMakeMove [ | ||
|
||
] | ||
] |