def test_get_player_actions(): from agents.common import string_to_board from agents.agent_MCTS.MCTS import get_player_actions player_check = 2 board1 = string_to_board( '| - - - - - - - |\n| . . X O . . . |\n| . X O X . . . |\n| . X O O . . . |\n| . O X O O . . |\n| . X O X O . . |\n| . X X O O X . |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |' ) board2 = string_to_board( '| - - - - - - - |\n| X X X X X O . |\n| O X O X O X . |\n| O X X X O O X |\n| X O X O X O O |\n| O O X X O X X |\n| O X O O O X O |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |' ) board4 = np.zeros((6, 7), dtype=np.int8) board4[0, :] = np.array([0, 0, 0, 2, 1, 1, 0]) board4[1, :] = np.array([0, 0, 0, 2, 1, 1, 0]) board4[2, :] = np.array([0, 0, 0, 2, 1, 0, 0]) board4[3, :] = np.array([0, 0, 0, 2, 2, 0, 0]) ret1 = get_player_actions(board1, player_check) ret2 = get_player_actions(board2, player_check) ret3 = get_player_actions(board4, player_check, _last_action=3) assert isinstance(ret1, list) assert ret1 == [0, 1, 4, 5, 6] assert ret2 == [6] assert ret3 == []
def test_position_value(): #from agents.agent_minimax.minimax import window_value from agents.agent_minimax.minimax import position_value from agents.common import string_to_board board1 = string_to_board( "| - - - - - - - |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . . O . . . |\n| . . O X . . . |\n| . . X O . . . |\n| - - - - - - - |\n|0 1 2 3 4 5 6 |" ) board2 = string_to_board( "| - - - - - - - |\n| . . . . . . . |\n| . . . X . . . |\n| . . O O . . . |\n| . . X O . . . |\n| . . O X . . . |\n| . . X O . . . |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |" ) board3 = string_to_board( '| - - - - - - - |\n| . . X O . . . |\n| . X O X . . . |\n| . X O O . . . |\n| . O X O O . . |\n| . X O X O . . |\n| . X X O O X . |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |' ) board4 = string_to_board( "| - - - - - - - |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . . O . . . |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |" ) ret11 = position_value(board1, PLAYER1) ret12 = position_value(board1, PLAYER2) ret21 = position_value(board2, PLAYER1) ret22 = position_value(board2, PLAYER2) ret31 = position_value(board3, PLAYER1) ret32 = position_value(board3, PLAYER2) ret41 = position_value(board4, PLAYER1) ret42 = position_value(board4, PLAYER2) assert isinstance(ret11, int) assert ret11 == 33 and ret12 == -26 assert ret21 == 74 and ret22 == -73 assert ret31 == 223 and ret32 == -232 assert ret41 == 10 and ret42 == -5
def test_check_result(): from agents.common import string_to_board from agents.agent_MCTS.MCTS import check_result player_check = 2 board1 = string_to_board( '| - - - - - - - |\n| . . X O . . . |\n| . X O X . . . |\n| . X O O . . . |\n| . O X O O . . |\n| . X O X O . . |\n| . X X O O X . |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |' ) board2 = string_to_board( '| - - - - - - - |\n| X X X X X O . |\n| O X O X O X . |\n| O X X X O O X |\n| X O X O X O O |\n| O O X X O X X |\n| O X O O O X O |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |' ) board4 = np.zeros((6, 7), dtype=np.int8) board4[0, :] = np.array([0, 0, 0, 2, 1, 1, 0]) board4[1, :] = np.array([0, 0, 0, 2, 1, 1, 0]) board4[2, :] = np.array([0, 0, 0, 2, 1, 0, 0]) board4[3, :] = np.array([0, 0, 0, 2, 2, 0, 0]) board5 = np.zeros((6, 7), dtype=np.int8) board5[0, :] = np.array([0, 0, 0, 1, 2, 2, 0]) board5[1, :] = np.array([0, 0, 0, 1, 2, 2, 0]) board5[2, :] = np.array([0, 0, 0, 1, 2, 0, 0]) board5[3, :] = np.array([0, 0, 0, 1, 1, 0, 0]) ret1 = check_result(board1, player_check) ret2 = check_result(board2, player_check) ret3 = check_result(board4, player_check) ret4 = check_result(board5, player_check) assert isinstance(ret1, int) assert ret1 == 0 assert ret2 == 1 assert ret3 == 1 assert ret4 == -0.1
def test_string_to_board(): """ test for string_to_board(): - np.ndarray type - dimensions of (6, 7) - BoardPiece type - Correct conversion of ['O', 'X'] -> [PLAYER1, PLAYER2] types """ test_string = ( '|--------------|\n' '| |\n' '| |\n' '| |\n' '| |\n' '| X |\n' # PLAYER2 at (1, 3) '| O |\n' # PLAYER1 at (0, 3) '|--------------|\n' '|0 1 2 3 4 5 6 |\n') # Function output out = cc.string_to_board(test_string) # Test output assert isinstance(out, np.ndarray) assert out.dtype == BoardPiece assert out.shape == (6, 7) assert out[0, 3] == PLAYER1 assert out[1, 3] == PLAYER2
def test_string_to_board() -> str: board = initialize_game_state() board_test = pretty_print_board(board) ret = string_to_board(board_test) assert isinstance(ret, np.ndarray)
def test_board_score(): """ 5 patterns that give points (score) given a score_dict (score dictionary): [p, p, p, n] [p, p, n, p] [p, p, n, n] [p, n, p, n] [p, n, n, p] The way to check the scoring is done well... is to manually check so... check that the score are the same """ # Parameters n = NO_PLAYER score_dict = np.array([5, 4, 3, 2, 1]) test_board = ('|--------------|\n' '| |\n' '| |\n' '| |\n' '| X X O |\n' '| X X O O O |\n' '|O O O X O X X |\n' '|--------------|\n' '|0 1 2 3 4 5 6 |\n') test_board = cc.string_to_board(test_board) manual_score = [20, 14] # P1 and P2 scores assert(manual_score[0] == minimax.board_score(board=test_board, player=PLAYER1, score_dict=score_dict)) assert(manual_score[1] == minimax.board_score(board=test_board, player=PLAYER2, score_dict=score_dict))
def test_string_to_board(): from agents.common import string_to_board game_states_from_string = [string_to_board(board) for board in boards] for index, game_state in enumerate(game_states_from_string): assert isinstance(game_state, np.ndarray) assert (game_state == game_states[index]).all
def testStringtoBoard(self): from agents.common import string_to_board, pretty_print_board #It's enough to check that string_to_board ° pretty_print = id: board = np.random.choice([player, PLAYER2], ((6, 7))) self.assertTrue( np.array_equal(board, string_to_board(pretty_print_board(board))))
def test_alphabeta(): from agents.agent_minimax.minimax import alphabeta from agents.common import string_to_board board1 = string_to_board( "| - - - - - - - |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . . O . . . |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |" ) board2 = string_to_board( "| - - - - - - - |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . . . . . . |\n| . . O . . . . |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |" ) alpha = -999 beta = 999 ret1 = alphabeta(board1, alpha, beta, False, PLAYER1, depth=5) ret2 = alphabeta(board2, alpha, beta, False, PLAYER1, depth=5) assert (ret1 > ret2) == True
def test_get_player_actions(): from agents.common import string_to_board from agents.agent_minimax.minimax import get_player_actions board1 = string_to_board( '| - - - - - - - |\n| . . X O . . . |\n| . X O X . . . |\n| . X O O . . . |\n| . O X O O . . |\n| . X O X O . . |\n| . X X O O X . |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |' ) board2 = string_to_board( '| - - - - - - - |\n| X X X X X O . |\n| O X O X O X . |\n| O X X X O O X |\n| X O X O X O O |\n| O O X X O X X |\n| O X O O O X O |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |' ) ret1 = get_player_actions(board1) ret2 = get_player_actions(board2) assert isinstance(ret1, list) assert ret1 == [0, 1, 4, 5, 6] assert ret2 == [6]
def test_string_to_board(): from agents.common import string_to_board from agents.common import pretty_print_board, initialize_game_state board = initialize_game_state() board_str = pretty_print_board(board) ret = string_to_board(board_str) assert isinstance(ret, np.ndarray) assert ret.shape == (6, 7) assert np.all(ret == NO_PLAYER)
def test_string_to_board(): from agents.common import string_to_board from agents.common import pretty_print_board board1 = np.zeros((6, 7), dtype=np.int8) board1[0, :] = np.array([0, 1, 1, 1, 1, 1, 0]) board1[1, :] = np.array([0, 1, 1, 1, 1, 1, 0]) board1[2, :] = np.array([0, 1, 1, 1, 1, 1, 0]) board1[3, :] = np.array([0, 2, 2, 2, 2, 0, 0]) board2 = np.zeros((6, 7), dtype=np.int8) board2[:, 4] = np.array([2, 2, 2, 2, 0, 0]) ret = pretty_print_board(board1) ret1 = string_to_board(ret) ret2 = pretty_print_board(board2) ret3 = string_to_board(ret2) assert isinstance(ret1, np.ndarray) assert ret1.dtype == np.int8 assert (board1 == ret1).all() assert (board2 == ret3).all()
def test_string_to_board(): from agents.common import string_to_board b4_string = "|==============|\n" \ "| |\n" \ "| |\n" \ "| X X |\n" \ "| O X X |\n" \ "| O O X X |\n" \ "| O O O X X |\n" \ "|==============|\n" \ "|0 1 2 3 4 5 6 |\n" assert np.all(string_to_board(b4_string) == b4)
def test_string_to_board(board=board_to_test): """ Test the implementation of the function string to board :param board: Selected board """ from agents.common import string_to_board, pretty_print_board ret_test = string_to_board(pretty_print_board(board)) assert isinstance(ret_test, np.ndarray) assert ret_test.dtype == BoardPiece comparison = board == ret_test comparison.all() assert comparison.all()
def test_check_terminal(): from agents.agent_minimax.minimax import check_terminal from agents.common import string_to_board board1 = string_to_board( '| - - - - - - - |\n| X X X X X O 0 |\n| O X O X O X 0 |\n| O X X X O O X |\n| X O X O X O O |\n| O O X X O X X |\n| O X O O O X O |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |' ) board2 = string_to_board( '| - - - - - - - |\n| . . X O . . . |\n| . X O X . . . |\n| . X O O . . . |\n| . O X O O . . |\n| . X O X O . . |\n| . X X O O X . |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |' ) board3 = string_to_board( "| - - - - - - - |\n| . . . . . . . |\n| . . . . . . . |\n| . . . X . . . |\n| . O O O O . . |\n| . X X O O X . |\n| X X O O O X X |\n| - - - - - - - |\n| 0 1 2 3 4 5 6 |" ) ret1 = check_terminal(board1) ret2 = check_terminal(board2) ret3 = check_terminal(board3) assert isinstance(ret1, bool) assert ret1 == True assert ret2 == False assert ret3 == True
def test_string_to_board(): from agents.common import string_to_board from agents.common import initialize_game_state, pretty_print_board dummy_board = initialize_game_state() # pretty print the board to get a string dummy_string = pretty_print_board(dummy_board) # convert string back to board ret = string_to_board(dummy_string) # new board should match original board assert (ret == dummy_board).all()
def test_string_to_board(): from agents.common import string_to_board, pretty_print_board test_str = '|====|\n' test_str += '|' + NO_PLAYER_PRINT + ' ' + PLAYER1_PRINT + ' |\n' test_str += '|' + PLAYER2_PRINT + ' ' + NO_PLAYER_PRINT + ' |\n' test_str += '|====|\n' test_str += '|0 1 |' ret_arr = string_to_board(test_str) test_arr = np.full((2, 2), NO_PLAYER, dtype=BoardPiece) test_arr[0, 1] = PLAYER1 test_arr[1, 0] = PLAYER2 assert (ret_arr == test_arr).all()
def test_string_to_board(): test_board = cm.initialize_game_state() test_board = np.random.randint(0, 3, test_board.shape) print('') print(test_board) board_str = cm.pretty_print_board(test_board) print('') print(board_str) board_arr = cm.string_to_board(board_str) print('') print(board_arr) assert isinstance(test_board, np.ndarray) assert board_arr.dtype == np.int8 assert board_arr.shape == test_board.shape return
def test_even_odd_scores(): filled_board = string_to_board(still_playing_board) ret = even_odd_row_scores(filled_board, PLAYER1) # PLAYER1 has 14 pieces in odd rows assert ret == 14 * 2 ret = even_odd_row_scores(filled_board, PLAYER2) #PLAYER2 has 11 pieces in even rows assert ret == 11 * 2 #empty board should have 0 for both players empty_board = initialize_game_state() assert even_odd_row_scores(empty_board, PLAYER2) == 0 assert even_odd_row_scores(empty_board, PLAYER1) == 0
def test_center_column_score(): filled_board = string_to_board(still_playing_board) ret = center_column_score(filled_board, PLAYER1) #PLAYER1 has 1 piece in column so score should be 3 assert ret == 3 ret = center_column_score(filled_board, PLAYER2) #PLAYER2 has 4 pieces in column so score should be 15 assert ret == 4 * 3 empty_board = initialize_game_state() ret = center_column_score(empty_board, PLAYER1) #should be 0 assert ret == 0
def test_string_to_board(): from agents.common import string_to_board board = np.zeros((6, 7)) board[0, 0:7] = [1, 2, 2, 1, 1, 1, 2] board[1, 0:7] = [2, 1, 2, 1, 2, 2, 1] board[2, 0:7] = [1, 2, 1, 2, 0, 0, 0] board[3, 0:7] = [2, 2, 1, 0, 0, 0, 0] board[4, 0:7] = [1, 1, 0, 0, 0, 0, 0] board[5, 0:7] = [2, 0, 0, 0, 0, 0, 0] boardStr = '|==============|\n' \ '|O |\n' \ '|X X |\n' \ '|O O X |\n' \ '|X O X O |\n' \ '|O X O X O O X |\n' \ '|X O O X X X O |\n' \ '|==============|\n' \ '|0 1 2 3 4 5 6 |' assert np.all(string_to_board(boardStr) == board)
def test_pretty_print_board_and_string_to_board(): """Test the printing of a board and the transformation back to an array""" # Check that the print of the board to a string and the transformation back to the board works for every position on # the board for each player for player in players: board = initialize_game_state() n_rows, n_cols = board.shape for i in range(n_rows): for j in range(n_cols): board[i, j] = player # Transform the board into a pretty string pp_board = pretty_print_board(board) # Transform it back to an ndarray trans_board = string_to_board(pp_board) # Check that everything worked assert isinstance(pp_board, str) assert len(pp_board) > 42 assert np.array_equal(board, trans_board)
def test_string_to_board(): """ Test if the given string representation of the board maps to the correct Board configuration """ from agents.common import string_to_board test_board = np.array([[BoardPiece(2), BoardPiece(0), BoardPiece(2), BoardPiece(0), BoardPiece(1), BoardPiece(0), BoardPiece(1)], [BoardPiece(1), BoardPiece(2), BoardPiece(1), BoardPiece(2), BoardPiece(1), BoardPiece(2), BoardPiece(1)], [BoardPiece(2), BoardPiece(0), BoardPiece(2), BoardPiece(0), BoardPiece(1), BoardPiece(0), BoardPiece(1)], [BoardPiece(1), BoardPiece(2), BoardPiece(1), BoardPiece(2), BoardPiece(1), BoardPiece(2), BoardPiece(1)], [BoardPiece(2), BoardPiece(0), BoardPiece(2), BoardPiece(0), BoardPiece(1), BoardPiece(0), BoardPiece(1)], [BoardPiece(1), BoardPiece(2), BoardPiece(1), BoardPiece(2), BoardPiece(1), BoardPiece(2), BoardPiece(1)]]) test_string = '''|=======| |XOXOXOX| |O O X X| |XOXOXOX| |O O X X| |XOXOXOX| |O O X X| |=======| |0123456|''' assert (string_to_board(test_string) == test_board).all()
def test_string_to_board(): string = test_pretty_print_board() board_ = string_to_board(string) assert isinstance(board_, np.ndarray)
def test_string_to_board(): from agents.common import string_to_board, pretty_print_board board = np.zeros((6, 7), dtype=BoardPiece) ret = string_to_board(pretty_print_board(board)) assert isinstance(ret, np.ndarray)
"|==============|\n" + \ "|0 1 2 3 4 5 6 |" board2 = "|==============|\n" + \ "| |\n" + \ "| |\n" + \ "| X X |\n" + \ "| O X X |\n" + \ "| O X O O |\n" + \ "| O O X X |\n" + \ "|==============|\n" + \ "|0 1 2 3 4 5 6 |" boards: List[str] = [board1, board2] # np.ndarray representations of initial states game_state1 = initialize_game_state() game_state2 = string_to_board(board2) game_states: np.ndarray = [game_state1, game_state2] # list of actions and players to test on initial states actions = [PlayerAction(0), PlayerAction(3), PlayerAction(6)] players = [PLAYER1, PLAYER2] # games state 1 with all possible player action combo game_state1_with_action1_player1 = game_state1.copy() game_state1_with_action1_player1[5][0] = PLAYER1 game_state1_with_action2_player1 = game_state1.copy() game_state1_with_action2_player1[5][3] = PLAYER1 game_state1_with_action3_player1 = game_state1.copy() game_state1_with_action3_player1[5][6] = PLAYER1 game_state1_with_action1_player2 = game_state1.copy() game_state1_with_action1_player2[5][0] = PLAYER2
def test_easy_block_win(): """ Test that the agent blocks an opponent from winning and take winning moves Remember: 'O ': PLAYER1 'X ': PLAYER2 ' ': NO_PLAYER """ # Test horizontal block test_horz = ('|--------------|\n' '| |\n' '| |\n' '| |\n' '| |\n' '| |\n' '|O O O X |\n' '|--------------|\n' '|0 1 2 3 4 5 6 |\n') test_horz = cc.string_to_board(test_horz) horz_block = 3 # Test horizontal block test_dia1 = ('|--------------|\n' '| |\n' '| |\n' '| |\n' '| O O |\n' '| O X X |\n' '|O X X O |\n' '|--------------|\n' '|0 1 2 3 4 5 6 |\n') test_dia1 = cc.string_to_board(test_dia1) dia1_block = 3 # Test horizontal block test_dia2 = ('|--------------|\n' '| |\n' '| |\n' '| |\n' '|O O |\n' '|X X O |\n' '|O X X O |\n' '|--------------|\n' '|0 1 2 3 4 5 6 |\n') test_dia2 = cc.string_to_board(test_dia2) dia2_block = 0 # Test horizontal block test_vert = ('|--------------|\n' '| |\n' '| |\n' '| |\n' '|O |\n' '|O |\n' '|O |\n' '|--------------|\n' '|0 1 2 3 4 5 6 |\n') test_vert = cc.string_to_board(test_vert) vert_block = 0 # We asses if the agent plays at the target column and we will move this position along the horizontal axis. Then we # change make the same test but changing the player pieces to check the agent can play with both pieces. for shift in range(7): # horz, dia1 and dia2 can only be shifted 3 times if shift <= 3: for expected_block_column, test_board in zip([horz_block, dia1_block, dia2_block], [test_horz, test_dia1, test_dia2]): # Play for the win agent_action, _ = generate_move(np.roll(test_board, shift, axis=1), PLAYER1, None) assert (expected_block_column + shift == agent_action) # Play to block agent_action, _ = generate_move(np.roll(test_board, shift, axis=1), PLAYER2, None) assert(expected_block_column+shift == agent_action) # Change player, test with opposite player temp = test_board.copy() test_board[temp == PLAYER1] = PLAYER2 test_board[temp == PLAYER2] = PLAYER1 # Play to block agent_action, _ = generate_move(np.roll(test_board, shift, axis=1), PLAYER1, None) assert (expected_block_column + shift == agent_action) # Play to win agent_action, _ = generate_move(np.roll(test_board, shift, axis=1), PLAYER2, None) assert (expected_block_column + shift == agent_action) expected_block_colmn = vert_block test_board = test_vert # Play for the win agent_action, _ = generate_move(np.roll(test_board, shift, axis=1), PLAYER1, None) assert (expected_block_column + shift == agent_action) # Play to block agent_action, _ = generate_move(np.roll(test_board, shift, axis=1), PLAYER2, None) assert (expected_block_column + shift == agent_action) # Change player, test with opposite player temp = test_board.copy() test_board[temp == PLAYER1] = PLAYER2 test_board[temp == PLAYER2] = PLAYER1 # Play to block agent_action, _ = generate_move(np.roll(test_board, shift, axis=1), PLAYER1, None) assert (expected_block_column + shift == agent_action) # Play to win agent_action, _ = generate_move(np.roll(test_board, shift, axis=1), PLAYER2, None) assert (expected_block_column + shift == agent_action)