def testAddMove(self): board = Board(2, 2) board.addMove(Square(0, 0), "X") self.assertEqual( board.getPossibleMoves(), [Square(0, 1), Square(1, 0), Square(1, 1)])
def testGetPossibleMoves(self): board = Board(2, 2) self.assertEqual( board.getPossibleMoves(), [Square(0, 0), Square(0, 1), Square(1, 0), Square(1, 1)])
def testValidateSquare(self): squareValidator = SquareValidator(2, 2) square = Square(0, 0) self.assertTrue(squareValidator.validateSquare(square)) square = Square(0, 3) try: squareValidator.validateSquare(square) self.assertEqual(False) except GameException as ge: self.assertEqual(str(ge), "Invalid column!\n")
def testIsWon(self): game = Game(2, 2) game.playerTurn(Square(0, 0), "X") self.assertTrue(game.isWon()) game = Game(2, 2) game.computerTurn("O") self.assertTrue(game.isWon())
def getPossibleMoves(self): ''' Returns a list containing the coordinates of the empty squares on the board, under the form of Square objects Input: square - Square object Output: possibleMoves - list of Square objects ''' possibleMoves = [] for row in range(self.__rows): for column in range(self.__columns): if self.__moves[row][column] == 0: possibleMoves.append(Square(row, column)) return possibleMoves
def readMove(self): while True: move = input( "Enter the coordinates of your next move (row, column): ") coordinates = move.strip().split(' ') try: if len(coordinates) == 2: row = int(coordinates[0]) column = int(coordinates[1]) break else: raise ValueError except ValueError: print("Invalid move!") return Square(row, column)
def testAddNeighbours(self): board = Board(2, 2) board.addNeighbours(Square(0, 0)) self.assertEqual(board.getPossibleMoves(), [Square(0, 0)])