def testPlayerOverPlayer(self): for playerId in range(2): b = GameBoard() for y in range(3): for x in range(3): b.makeMove((x,y), playerId) self.assertRaises(InvalidMoveException, b.makeMove, (x,y), range(2)[playerId-1])
class EmptyAndFullTest(unittest.TestCase): def setUp(self): self.board = GameBoard() def tearDown(self): self.board = None # verify that an initial board is empty def testBoardEmpty(self): self.assertEquals(True, self.board.isEmpty()) # verify that isFull() returns True when the board is full def testBoardFull0(self): for y in range(3): for x in range(3): self.board.makeMove((x,y), 0) self.assertEquals(True, self.board.isFull()) # verify that isFull() returns True when the board is full def testBoardFull1(self): for y in range(3): for x in range(3): self.board.makeMove((x,y), 1) self.assertEquals(True, self.board.isFull())
def testComputerNeverLoses(self): board = GameBoard() self.count = 0 for cell in board.getEmptyCells(): self._makeMove(board, cell) # computer makes first move for cell in board.getEmptyCells(): board.makeMove(cell, game.ID_COMPUTER) for cell in board.getEmptyCells(): self._makeMove(board, cell)
def testNonWins(self): for non_win in self.non_wins: for playerId in range(2): b = GameBoard() for move in non_win: # move player b.makeMove(move, playerId) # move opponent for cell in b.getEmptyCells(): if cell not in non_win: b.makeMove(cell, range(2)[playerId-1]) break self.assertEqual(False, b.checkForWin(playerId)) # test opponent non-wins for non_win in self.non_wins: for playerId in range(2): b = GameBoard() for move in non_win: # move player for cell in b.getEmptyCells(): if cell not in non_win: b.makeMove(cell, playerId) break # move opponent b.makeMove(move, range(2)[playerId-1]) self.assertEqual(False, b.checkForWin(range(2)[playerId-1]))
def testDefaultWins(self): for win in self.wins: for playerId in range(2): b = GameBoard() for move in win: # move player b.makeMove(move, playerId) # move opponent for cell in b.getEmptyCells(): if cell not in win: b.makeMove(cell, range(2)[playerId-1]) break self.assertEqual(True, b.checkForWin(playerId)) # test opponent wins for win in self.wins: for playerId in range(2): b = GameBoard() for move in win: # move player for cell in b.getEmptyCells(): if cell not in win: b.makeMove(cell, playerId) break # move opponenet b.makeMove(move, range(2)[playerId-1]) self.assertEqual(True, b.checkForWin(range(2)[playerId-1]))
def testEmptyCellCount(self): for playerId in range(2): b = GameBoard() for cellCountShouldBe in (9,8,7,6,5,4,3,2,1,0): self.assertEquals(len(b.getEmptyCells()), cellCountShouldBe) if cellCountShouldBe > 0: b.makeMove(b.getEmptyCells()[0], playerId)
def testMoveEmptyCellsByCell(self): for playerId in range(2): b = GameBoard() for cell in b.getEmptyCells(): b.makeMove(cell, playerId)
def testMoveEmptyCellsByXY(self): for playerId in range(2): b = GameBoard() for y in range(3): for x in range(3): b.makeMove((x,y), playerId)
def testInBounds(self): for playerId in range(2): b = GameBoard() for y in range(3): for x in range(3): b.makeMove((x,y), playerId)