Skip to content

Commit

Permalink
added methods and tests for Console
Browse files Browse the repository at this point in the history
  • Loading branch information
skim committed Nov 24, 2010
1 parent a8f81ec commit 0d5e097
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
87 changes: 87 additions & 0 deletions TicTacToe-Core/Console.st
@@ -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!'.
]
]
104 changes: 104 additions & 0 deletions TicTacToe-Core/ConsoleTest.st
@@ -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!').
]
]
3 changes: 3 additions & 0 deletions TicTacToe-Core/HumanTest.st
@@ -1,2 +1,5 @@
TicTacToeTestCase subclass: HumanTestCase [
testMakeMove [

]
]

0 comments on commit 0d5e097

Please sign in to comment.