def aPlayerHasWon(game): """ Check game state to see if a player has won """ game.playerAMoveCount = len(ai.getAllMovesForPlayer(game, True)) 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
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 test_getAllMovesForPlayer_good(self): """ Check that capture moves take precedence over non-captures """ board_description = [ " 1 2 3 4 5 6 7 8 9 0", "0 . . . . . 0", "9 . . . . . 9", "8 . . . . . 8", "7 . . . . . 7", "6 . b . . . 6", "5 . . a a . 5", "4 . . . . . 4", "3 . . . . . 3", "2 . . . . . 2", "1 . . . . . 1", " 1 2 3 4 5 6 7 8 9 0", ] board = boardParser.parseBoardInput(board_description) #capturingPiece = coordinate.Coordinate(3, 5) expectedLength = 1 movesList = ai.getAllMovesForPlayer(board, True) actualMovesListLength = len(movesList) self.assertEqual(expectedLength, actualMovesListLength)
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_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_getAllMovesForPlayer_king_cap_at_distance_multi_hop(self): """ Test case of king capturing at a distance""" # Given board = boardParser.parseBoardInput(helper.kingCapture4) # When actualResult = ai.getAllMovesForPlayer(board, False) # Then expectedLength = 1 self.assertEqual(len(actualResult), expectedLength)
def test_getPositionFromListOfMoves_issue_31(self): """ Tests fix for issue_31 where promotion rows were unselectable""" board = boardParser.parseBoardInput(helper.issue_31) userIsPlayerB = True listOfMoves = ai.getAllMovesForPlayer(board, not userIsPlayerB) actualListOfMoves = interface.getPositionFromListOfMoves( listOfMoves, "51", userIsPlayerB) expectedListLength = 1 self.assertEqual(expectedListLength, len(actualListOfMoves))
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)
if SEARCH_PLY == 0: game = ai.randomSearch(game, COMP_IS_PLAYER_A) else: game = ai.iterativeDeepeningSearch(game, COMP_IS_PLAYER_A, SEARCH_PLY) if DISPLAY_EVALUATION: print("Computer evaluation: {0}".format(game.score)) computersTurn = False game.print_board() if aPlayerHasWon(game): break legalMoves = ai.getAllMovesForPlayer(game, not COMP_IS_PLAYER_A) while(True): userInput = input('Enter a move: ') if userInput == 'm' or userInput == 'moves': print("These are your legal moves:") for move in legalMoves: move.print_board() print("--------------------------------") continue result = interface.getPositionFromListOfMoves(legalMoves, str(userInput), COMP_IS_PLAYER_A) if len(result) != 1: print("Unknown or invalid move, try again") continue else: