def test_destinationIsEmpty_true(self): """ Test when destination is Empty """ gnObject = gamenode.GameNode() pieceDestination = coordinate.Coordinate(4, 6) actualResult = ai.destinationIsEmpty(gnObject, pieceDestination) expectedResult = True self.assertEqual(actualResult, expectedResult)
def test_getAllMovesForPlayer_isPlayerB(self): """ Test getting all moves for player B """ gnObject = gamenode.GameNode() gnObject.createStartingPosition() actualResult = ai.getAllMovesForPlayer(gnObject, False) expectedResultLength = 9 self.assertEqual(len(actualResult), expectedResultLength)
def parseBoardInput(description): """ Takes a description of a board and returns the corresponding GameNode """ gnObject1 = gamenode.GameNode() # Remove any lines that are not part of the board cleanDescription = [] for line in description: if line[0] != ' ': cleanDescription.append(line) # Set board state where coordinate Y is even for i in range(5): for j in range(5): pieceChar = cleanDescription[2 * i][6 * j + 5] gnObject1.setState(coordinate.Coordinate(2 * j + 2, 10 - 2 * i), types.getPieceIntValueFromChar(pieceChar)) # Set board state where coordinate Y is odd for i in range(5): for j in range(5): pieceChar = cleanDescription[2 * i + 1][6 * j + 2] gnObject1.setState(coordinate.Coordinate(2 * j + 1, 9 - 2 * i), types.getPieceIntValueFromChar(pieceChar)) return gnObject1
def test_setState(self): """ Test setting the board state at a Coordinate with a value """ gnObject = gamenode.GameNode() testCoordinate = coordinate.Coordinate(5, 1) testValue = types.PLAYER_A_KING gnObject.setState(testCoordinate, testValue) self.assertEqual(gnObject.getState(testCoordinate), types.PLAYER_A_KING)
def test_destinationIsEmpty_false(self): """ Test when destination is not Empty """ gnObject = gamenode.GameNode() pieceDestination = coordinate.Coordinate(9, 3) gnObject.setState(pieceDestination, types.PLAYER_A_REGULAR) actualResult = ai.destinationIsEmpty(gnObject, pieceDestination) expectedResult = False self.assertEqual(actualResult, expectedResult)
def test_isCoordinateMatch_player_A_no_match(self): """ Test coordinate no match with user as player A """ userIsPlayerB = False gnObject = gamenode.GameNode() testCoordinate = coordinate.Coordinate(3, 7) self.assertFalse( interface.isCoordinateMatch(gnObject, testCoordinate, userIsPlayerB))
def transferNode(startNode): """ Copies input gamenode to a new one and returns it. """ resultNode = gamenode.GameNode() resultNode.pieceLastMoved = startNode.pieceLastMoved resultNode.deltaLastMoved = startNode.deltaLastMoved for x in range(0, 10): for y in range(0, 10): resultNode.gameState[x][y] = startNode.gameState[x][y] return resultNode
def test_getNoncaptureMovesForRegularPiece_2_moves(self): """ Test that regular piece has to possible moves """ gnObject = gamenode.GameNode() pieceLocation = coordinate.Coordinate(4, 4) gnObject.setState(pieceLocation, types.PLAYER_A_REGULAR) actualResult = ai.getNoncaptureMovesForRegularPiece( gnObject, pieceLocation) expectedResultLength = 2 self.assertEqual(len(actualResult), expectedResultLength)
def test_getPositionFromListOfMoves_none(self): """ Test getting position from single possible user input """ userIsPlayerB = True gnObject = gamenode.GameNode() gnObject.createStartingPosition() listOfMoves = ai.getAllMovesForPlayer(gnObject, not userIsPlayerB) actualValue = interface.getPositionFromListOfMoves( listOfMoves, "7775", userIsPlayerB) self.assertEqual(len(actualValue), 0)
def test_isCoordinateMatch_player_B_good(self): """ Test coordinate match with user as player B """ userIsPlayerB = True gnObject = gamenode.GameNode() testCoordinate = coordinate.Coordinate(3, 7) gnObject.setState(testCoordinate, types.PLAYER_B_REGULAR) self.assertTrue( interface.isCoordinateMatch(gnObject, testCoordinate, userIsPlayerB))
def test_getNoncaptureMovesForRegularPiece_1_move(self): """ Test regular piece on the edge of the board, which has 1 move """ gnObject = gamenode.GameNode() pieceLocation = coordinate.Coordinate(10, 4) gnObject.setState(pieceLocation, types.PLAYER_A_REGULAR) actualResult = ai.getNoncaptureMovesForRegularPiece( gnObject, pieceLocation) expectedResultLength = 1 self.assertEqual(len(actualResult), expectedResultLength)
def test_isCoordinateMatch_player_B_wrong_piece(self): """ Test coordinate match with user as player B but destination does not match""" userIsPlayerB = True gnObject = gamenode.GameNode() testCoordinate = coordinate.Coordinate(3, 7) gnObject.setState(testCoordinate, types.PLAYER_A_REGULAR) self.assertFalse( interface.isCoordinateMatch(gnObject, testCoordinate, userIsPlayerB))
def test_createStartingPosition(self): """ Check that the starting position is set correctly """ gnObject = gamenode.GameNode() gnObject.createStartingPosition() self.assertEqual(gnObject.getState(coordinate.Coordinate(2, 2)), types.PLAYER_A_REGULAR) self.assertEqual(gnObject.getState(coordinate.Coordinate(6, 10)), types.PLAYER_B_REGULAR) self.assertEqual(gnObject.getState(coordinate.Coordinate(9, 5)), types.EMPTY)
def test_matchMultipleCoordinatesToMoves_playerB_unambiguous(self): """ Match multi-coordinate playerB input to move """ userIsPlayerB = True gnObject = gamenode.GameNode() gnObject.createStartingPosition() listOfMoves = ai.getAllMovesForPlayer(gnObject, not userIsPlayerB) coordinates = interface.getCoordinatesFromUserInput("3746") actualValue = interface.matchMultipleCoordinatesToMoves( listOfMoves, coordinates, userIsPlayerB) self.assertEqual(len(actualValue), 1)
def test_getNoncaptureMovesForRegularPiece_0_moves(self): """ Test regular piece that is completely blocked from moving """ gnObject = gamenode.GameNode() gnObject.createStartingPosition() pieceLocation = coordinate.Coordinate(1, 1) gnObject.setState(pieceLocation, types.PLAYER_A_REGULAR) actualResult = ai.getNoncaptureMovesForRegularPiece( gnObject, pieceLocation) expectedResultLength = 0 self.assertEqual(len(actualResult), expectedResultLength)
def test_getAllMovesForPlayer_1_move_2_players(self): """ Test getting moves for a particular player when both players have legal moves """ # Given gnObject = gamenode.GameNode() pieceLocationA = coordinate.Coordinate(6, 2) gnObject.setState(pieceLocationA, types.PLAYER_A_REGULAR) pieceLocationB = coordinate.Coordinate(5, 7) gnObject.setState(pieceLocationB, types.PLAYER_B_REGULAR) # When actualResult = ai.getAllMovesForPlayer(gnObject, False) # Then expectedResultLength = 2 self.assertEqual(len(actualResult), expectedResultLength)
def test_print_board(self): """Check that print_board works""" with helper.captured_output() as out: gnObject = gamenode.GameNode() gnObject.print_board() actual_print = out.getvalue() expected_print = (" 1 2 3 4 5 6 7 8 9 0\n" "0 . . . . . 0\n" "9 . . . . . 9\n" "8 . . . . . 8\n" "7 . . . . . 7\n" "6 . . . . . 6\n" "5 . . . . . 5\n" "4 . . . . . 4\n" "3 . . . . . 3\n" "2 . . . . . 2\n" "1 . . . . . 1\n" " 1 2 3 4 5 6 7 8 9 0\n") self.assertEqual(actual_print, expected_print)
def test_makePieceMove_good(self): """ Test executing a happy-path piece move """ # Given gnObject = gamenode.GameNode() pieceDestination = coordinate.Coordinate(9, 3) pieceLocation = coordinate.Coordinate(8, 2) gnObject.setState(pieceLocation, types.PLAYER_A_REGULAR) # When actualResult = ai.makePieceMove(gnObject, pieceDestination, pieceLocation) # Then actualResultLocationType = actualResult.getState(pieceLocation) actualResultDestinationType = actualResult.getState(pieceDestination) expectedResultLocationType = types.EMPTY expectedResultDestinationType = types.PLAYER_A_REGULAR self.assertEqual(actualResultLocationType, expectedResultLocationType) self.assertEqual(actualResultDestinationType, expectedResultDestinationType)
def test_eq_not_same(self): """ Check equality function compares boards as not equal """ gnObject1 = gamenode.GameNode() gnObject1.setState(coordinate.Coordinate(5, 3), types.PLAYER_A_KING) gnObject2 = gamenode.GameNode() self.assertTrue(gnObject1 != gnObject2)
game.playerBMoveCount = len(ai.getAllMovesForPlayer(game, False)) if game.playerAWins(): print("Player A wins!") return True elif game.playerBWins(): print("Player B wins!") return True return False if __name__ == '__main__': """ Main game loop. Play alternates between user and computer. """ try: game = boardParser.parseBoardInput(debug.customPosition) except NameError: game = gamenode.GameNode() game.createStartingPosition() firstTurn = True if COMP_IS_PLAYER_A: computersTurn = True else: computersTurn = False while(True): if not firstTurn: game.print_board() print("--------------------------------") elif firstTurn and COMP_IS_PLAYER_A: game.print_board()
def test_default_instantiation(self): """ Test a known default instantiation """ gnObject = gamenode.GameNode() result = gnObject.gameState[0][0] self.assertEqual(result, types.EMPTY) self.assertFalse(gnObject.score)
def test_getState(self): """ Check getting the board state at a Coordinate """ gnObject = gamenode.GameNode() self.assertEqual(gnObject.getState(coordinate.Coordinate(3, 3)), types.EMPTY)
def test_playerWins(self): """ Check if a player has won the game """ gnObject = gamenode.GameNode() self.assertRaises(NotImplementedError, gnObject.playerWins)
def test_getPieceCount_bad_unassigned(self): """ Handle unknown player type """ gnObject = gamenode.GameNode() self.assertRaises(ValueError, gnObject.getPieceCount, None)
def test_eq_same(self): """ Check equality function compares boards as equal """ gnObject1 = gamenode.GameNode() gnObject2 = gamenode.GameNode() self.assertTrue(gnObject1 == gnObject2)