Skip to content

Commit

Permalink
all tests implemented in ThreeByThree
Browse files Browse the repository at this point in the history
  • Loading branch information
skim committed Nov 24, 2010
1 parent 3debe61 commit d367911
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
48 changes: 46 additions & 2 deletions TicTacToe-Core/ThreeByThree.st
Expand Up @@ -12,8 +12,8 @@ Board subclass: ThreeByThree [

initialize [
winningPatterns := #((1 2 3) (4 5 6) (7 8 9) (1 4 7) (2 5 8) (3 6 9) (1 5 9) (3 5 7)).
list := {' ' . ' ' . ' ' . ' ' . ' ' . ' ' . ' ' . ' ' . ' '}.
winner := self noWinner
list := Array new: 9 withAll: ' '.
self resetWinner.
]

winningPatterns [
Expand All @@ -28,6 +28,15 @@ Board subclass: ThreeByThree [
^list
]

listAsString [
| board |
board := ''.
list do: [ :each |
board := board, each asString.
].
^board
]

pieceIn: square [
^list at: square
]
Expand All @@ -48,14 +57,27 @@ Board subclass: ThreeByThree [
].
]

clear: square [
list at: square put: self emptySquare.
self resetWinner.
]

isOccupied: square [
^((list at: square) ~= self emptySquare)
]

isValidMove: square [
^((1 <= square) & (square <= list size) and: [(self isOccupied: square) not])
]

winner [
^winner
]

resetWinner [
winner := self noWinner.
]

findWinner [
self winningPatterns do: [ :pattern |
self findPattern: pattern.
Expand All @@ -77,6 +99,28 @@ Board subclass: ThreeByThree [
anyEmptySquares [
^((list indexOf: self emptySquare) ~= 0)
]

emptySquaresCount [
^(list count: [ :each | each = self emptySquare ])
]

movesMade [
^(self size - self emptySquaresCount)
]

emptySquares [
| squares counter |
squares := Array new: self emptySquaresCount withAll: ' '.
counter := 1.
1 to: self size do: [ :square |
((list at: square) = self emptySquare)
ifTrue: [
squares at: counter put: square.
counter := counter + 1.
].
].
^squares
]

isGameOver [
^((self someoneWinner | (self anyEmptySquares not)))
Expand Down
51 changes: 49 additions & 2 deletions TicTacToe-Core/ThreeByThreeTest.st
Expand Up @@ -35,7 +35,7 @@ TicTacToeTestCase subclass: ThreeByThreeTestCase [
board := ThreeByThree create: existingBoard.
self assert: (board ~= nil).
self assert: (board size = 9).
self assert: (board list asString = 'XOXOXOOOX').
self assert: (board listAsString = 'XOXOXOOOX').
]

testMovingPieceOnBoard [
Expand All @@ -59,7 +59,54 @@ TicTacToeTestCase subclass: ThreeByThreeTestCase [
board move: 7 piece: $X.
board move: 8 piece: $X.
board move: 9 piece: $O.
self assert: (board noWinner = board winner).
self assert: (board winner = board noWinner).
self assert: (board isGameOver).
]

testWinningGame [
board move: 1 piece: $X.
board move: 2 piece: $X.
board move: 3 piece: $X.
self assert: (board winner = $X).
self assert: (board isGameOver).
]

testValidMoves [
1 to: 9 do: [ :square |
self assert: (board isValidMove: square).
]
]

testInvalidMoves [
self assert: ((board isValidMove: board size + 1) not).
self assert: ((board isValidMove: 0) not).
]

testPieceIn [
board move: 1 piece: $X.
board move: 2 piece: $O.
self assert: ((board pieceIn: 1) = $X).
self assert: ((board pieceIn: 2) = $O).
self assert: ((board pieceIn: 3) = board emptySquare).
]

testEmptySquares [
self assert: (board emptySquares size = 9).
board move: 1 piece: $X.
self assert: (board emptySquares size = 8).
]

testClearMove [
board move: 1 piece: $X.
board clear: 1.
self assert: ((board isOccupied: 1) not).
self assert: (board listAsString = ' ').
]

testNumberOfMovesMade [
board move: 1 piece: $O.
board move: 2 piece: $X.
board move: 3 piece: $O.
self assert: (board movesMade = 3).
]
]

0 comments on commit d367911

Please sign in to comment.