def test_apply_player_action_ab(): test_board1 = cm.initialize_game_state() test_board2 = cm.initialize_game_state() player_map, mask_map = cm.board_to_bitmap(test_board1, cm.PLAYER1) n_rows = test_board1.shape[0] moves = np.array([3, 4, 3, 2, 2, 2, 2, 4, 1, 3, 4, 1, 0, 5]) player = cm.PLAYER1 for mv in moves: print('\n') cm.apply_player_action(test_board2, mv, player) player = cm.BoardPiece(player % 2 + 1) test2_map = cm.board_to_bitmap(test_board2, player)[1] print(bin(test2_map)) player_map, mask_map = cm.apply_player_action_ab( player_map, mask_map, mv, n_rows) print(bin(mask_map)) test_board1 = cm.initialize_game_state() player_map, mask_map = cm.board_to_bitmap(test_board1, cm.PLAYER1) n_rows = test_board1.shape[0] moves = np.array([3, 3, 3, 3, 3, 3, 3]) for mv in moves: print('\n') player_map, mask_map = cm.apply_player_action_ab( player_map, mask_map, mv, n_rows) print(bin(mask_map))
def test_scoring_function(): """ Test of the score evaluation of the hole board weights_array = np.array([4, 2, 5, 1000, -2, -100]) weight_middle, weight_2, weight_3, weight_win, weight_opp_2, weight_opp_3 """ from agents.common import initialize_game_state, scoring_function # weight_2 and opp_2 weights_array = np.array([4, 2, 5, 1000, -2, -100]) ret = initialize_game_state() ret[0, 2] = PLAYER2 ret[0, 1] = PLAYER2 assert scoring_function(ret, weights_array, PLAYER2) == 4 assert scoring_function(ret, weights_array, PLAYER1) == -4 # weight_3 and opp_3 ret2 = initialize_game_state() ret2[0, 1] = PLAYER2 ret2[1, 1] = PLAYER2 ret2[2, 1] = PLAYER2 assert scoring_function(ret2, weights_array, PLAYER2) == 7 assert scoring_function(ret2, weights_array, PLAYER1) == -102 # weight_win ret3 = initialize_game_state() ret3[0, 0] = PLAYER2 ret3[1, 1] = PLAYER2 ret3[2, 2] = PLAYER2 ret3[3, 3] = PLAYER2 assert scoring_function(ret3, weights_array, PLAYER2) == 1007
def test_number_of_connected(): board = initialize_game_state() board[0, 0:7] = [1, 2, 2, 1, 0, 0, 1] board[1, 0:7] = [0, 0, 0, 0, 0, 0, 1] board[2, 0:7] = [0, 0, 0, 0, 0, 0, 0] board[3, 0:7] = [0, 0, 0, 0, 0, 0, 0] board[4, 0:7] = [0, 0, 0, 0, 0, 0, 0] board[5, 0:7] = [0, 0, 0, 0, 0, 0, 0] p, m = get_position_mask_bitmap(2, board) pp, mm = get_position_mask_bitmap(1, board) assert number_of_connected(p, m) == 1 assert number_of_connected(pp, mm) == 2 board = initialize_game_state() board[0, 0:7] = [2, 2, 2, 1, 2, 1, 1] board[1, 0:7] = [0, 0, 0, 0, 2, 1, 1] board[2, 0:7] = [0, 0, 0, 0, 1, 2, 2] board[3, 0:7] = [0, 0, 0, 0, 0, 0, 0] board[4, 0:7] = [0, 0, 0, 0, 0, 0, 0] board[5, 0:7] = [0, 0, 0, 0, 0, 0, 0] p, m = get_position_mask_bitmap(2, board) pp, mm = get_position_mask_bitmap(1, board) assert number_of_connected(p, m) == 1 assert number_of_connected(pp, mm) == 3 board = initialize_game_state() board[0, 0:7] = [2, 2, 2, 2, 0, 1, 1] board[1, 0:7] = [2, 0, 0, 0, 0, 0, 1] board[2, 0:7] = [1, 0, 0, 0, 0, 0, 2] board[3, 0:7] = [0, 0, 0, 0, 0, 0, 0] board[4, 0:7] = [0, 0, 0, 0, 0, 0, 0] board[5, 0:7] = [0, 0, 0, 0, 0, 0, 0] p, m = get_position_mask_bitmap(2, board) pp, mm = get_position_mask_bitmap(1, board) assert number_of_connected(p, m) == 4 assert number_of_connected(pp, mm) == 2 board = initialize_game_state() board[0, 0:7] = [2, 1, 2, 2, 1, 1, 1] board[1, 0:7] = [2, 0, 1, 1, 2, 0, 1] board[2, 0:7] = [1, 0, 0, 1, 2, 0, 2] board[3, 0:7] = [0, 0, 0, 0, 1, 0, 0] board[4, 0:7] = [0, 0, 0, 0, 0, 0, 0] board[5, 0:7] = [0, 0, 0, 0, 0, 0, 0] p, m = get_position_mask_bitmap(2, board) pp, mm = get_position_mask_bitmap(1, board) assert number_of_connected(p, m) == 2 assert number_of_connected(pp, mm) == 4 board = initialize_game_state() board[0, 0:7] = [2, 1, 2, 2, 1, 1, 1] board[1, 0:7] = [2, 0, 1, 1, 2, 0, 1] board[2, 0:7] = [1, 0, 0, 1, 2, 0, 2] board[3, 0:7] = [0, 0, 0, 0, 2, 0, 0] board[4, 0:7] = [0, 0, 0, 0, 2, 0, 0] board[5, 0:7] = [0, 0, 0, 0, 0, 0, 0] p, m = get_position_mask_bitmap(2, board) pp, mm = get_position_mask_bitmap(1, board) assert number_of_connected(p, m) == 4 assert number_of_connected(pp, mm) == 2
def test_apply_player_action(): from agents.common import initialize_game_state from agents.common import apply_player_action board = initialize_game_state() board[0, 1] = BoardPiece(1) board[0, 2] = BoardPiece(2) board[1, 1] = BoardPiece(1) ret = apply_player_action(board, 2, BoardPiece(2), False) assert ret.dtype == BoardPiece assert ret.shape == (6, 7) assert isinstance(ret, np.ndarray) assert ret[1, 2] == BoardPiece(2) board = initialize_game_state() ret = apply_player_action(board, 2, BoardPiece(2), False) assert ret[0, 2] == BoardPiece(2) ret = apply_player_action(board, 2, BoardPiece(1), False) assert ret[1, 2] == BoardPiece(1) ret = apply_player_action(board, 3, BoardPiece(2), False) assert ret[0, 3] == BoardPiece(2) ret = apply_player_action(board, 3, BoardPiece(1), False) assert ret[1, 3] == BoardPiece(1) ret = apply_player_action(board, 2, BoardPiece(2), False) assert ret[2, 2] == BoardPiece(2) assert isinstance(ret, np.ndarray) assert ret.dtype == BoardPiece assert ret.shape == (6, 7)
def test_MCTS(): # Selection board = initialize_game_state() child_board = initialize_game_state() child_board[0, 0] = PLAYER1 current_node = Node(state=board) child_node = Node(state=child_board, parent=current_node) current_node.untriedMoves = [0, 3, 4] current_node.children = [child_node] selected_node = Node.selection(current_node) assert selected_node == current_node # Expand current_node.untriedMoves = [0, 3, 4] explored_node = Node.expand(current_node) assert len(current_node.untriedMoves) == 2 assert explored_node != current_node # rollout current_node = Node(state=board, player=PLAYER1) won = connected_four(current_node.state, PLAYER1) assert won #backpropagate Node.update(current_node, result=[-1, 1]) assert current_node.visits == 1 selectedColumn = MCTS(board) assert selectedColumn
def human_vs_agent( generate_move_1: GenMove, generate_move_2: GenMove, player_1: str = "Player 1", player_2: str = "Player 2", args_1: tuple = (), args_2: tuple = (), init_1: Callable = lambda board, player: None, init_2: Callable = lambda board, player: None, ): import time from agents.common import PLAYER1, PLAYER2, PLAYER1_PRINT, PLAYER2_PRINT, GameState from agents.common import initialize_game_state, pretty_print_board, apply_player_action, check_end_state players = (PLAYER1, PLAYER2) for play_first in (1, -1): for init, player in zip((init_1, init_2)[::play_first], players): init(initialize_game_state(), player) saved_state = {PLAYER1: None, PLAYER2: None} board = initialize_game_state() gen_moves = (generate_move_1, generate_move_2)[::play_first] player_names = (player_1, player_2)[::play_first] gen_args = (args_1, args_2)[::play_first] if play_first == 1: machine_player = PLAYER1 else: machine_player = PLAYER2 playing = True while playing: for player, player_name, gen_move, args in zip( players, player_names, gen_moves, gen_args, ): print(player, player_name) t0 = time.time() print(pretty_print_board(board)) print( f'{player_name} you are playing with {PLAYER1_PRINT if player == PLAYER1 else PLAYER2_PRINT}' ) action, saved_state[player] = gen_move(board.copy(), player, saved_state[player], *args) print(f"Move time: {time.time() - t0:.3f}s") apply_player_action(board, action, player) end_state = check_end_state(board, player) if end_state != GameState.STILL_PLAYING: print(pretty_print_board(board)) if end_state == GameState.IS_DRAW: print("Game ended in draw") else: print( f'{player_name} won playing {PLAYER1_PRINT if player == PLAYER1 else PLAYER2_PRINT}' ) playing = False break
def testPrettyPrintBoard(self): from agents.common import pretty_print_board, string_to_board board1 = np.ones((6, 7)) board1[1:, 2:] = np.zeros((5, 5)) * player board2 = np.zeros((6, 7)) board2[:4, :4] = np.eye(4) * PLAYER2 board3 = initialize_game_state() board3[0, 0] = player #checks that (0,0) is bottom left corner #representation of empty board: str0 = '|===============================|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|===============================|\n' \ '|\t0\t1\t2\t3\t4\t5\t6\t|\n' #representation of board1: str1 = '|===============================|\n' \ '|\tX\tX\t \t \t \t \t \t|\n' \ '|\tX\tX\t \t \t \t \t \t|\n' \ '|\tX\tX\t \t \t \t \t \t|\n' \ '|\tX\tX\t \t \t \t \t \t|\n' \ '|\tX\tX\t \t \t \t \t \t|\n' \ '|\tX\tX\tX\tX\tX\tX\tX\t|\n' \ '|===============================|\n' \ '|\t0\t1\t2\t3\t4\t5\t6\t|\n' #representation of board2: str2 ='|===============================|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \tO\t \t \t \t|\n' \ '|\t \t \tO\t \t \t \t \t|\n' \ '|\t \tO\t \t \t \t \t \t|\n' \ '|\tO\t \t \t \t \t \t \t|\n' \ '|===============================|\n' \ '|\t0\t1\t2\t3\t4\t5\t6\t|\n' #representation of board3: str3 = '|===============================|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\t \t \t \t \t \t \t \t|\n' \ '|\tX\t \t \t \t \t \t \t|\n' \ '|===============================|\n' \ '|\t0\t1\t2\t3\t4\t5\t6\t|\n' self.assertEqual(pretty_print_board(initialize_game_state()), str0) self.assertEqual(pretty_print_board(board1), str1) self.assertEqual(pretty_print_board(board2), str2) self.assertEqual(pretty_print_board(board3), str3)
def test_generate_move_minimax(): # win on the first move board = initialize_game_state() apply_player_action(board, 2, BoardPiece(2), False) apply_player_action(board, 2, BoardPiece(1), False) apply_player_action(board, 3, BoardPiece(2), False) apply_player_action(board, 2, BoardPiece(1), False) apply_player_action(board, 3, BoardPiece(2), False) apply_player_action(board, 2, BoardPiece(1), False) apply_player_action(board, 3, BoardPiece(2), False) action, save_state = generate_move_minimax(board, BoardPiece(1), None) assert action == 2 # block opponent board1 = initialize_game_state() apply_player_action(board1, 2, BoardPiece(2), False) apply_player_action(board1, 0, BoardPiece(1), False) apply_player_action(board1, 3, BoardPiece(2), False) apply_player_action(board1, 2, BoardPiece(1), False) apply_player_action(board1, 3, BoardPiece(2), False) apply_player_action(board1, 0, BoardPiece(1), False) apply_player_action(board1, 3, BoardPiece(2), False) action1, save_state = generate_move_minimax(board1, BoardPiece(1), None) assert action1 == 3 # with alpha-betta board2 = initialize_game_state() board2[0, 0:7] = [0, 1, 1, 2, 1, 0, 0] board2[1, 0:7] = [0, 0, 0, 2, 0, 0, 0] board2[2, 0:7] = [0, 0, 0, 1, 0, 0, 0] board2[3, 0:7] = [0, 0, 0, 2, 0, 0, 0] board2[4, 0:7] = [0, 0, 0, 0, 0, 0, 0] board2[5, 0:7] = [0, 0, 0, 0, 0, 0, 0] action2, save_state = generate_move_minimax(board2, BoardPiece(1), None) assert action2 == 2 board3 = initialize_game_state() board3[0, 0:7] = [1, 0, 2, 1, 2, 0, 1] board3[1, 0:7] = [0, 0, 1, 2, 0, 0, 0] board3[2, 0:7] = [0, 0, 0, 0, 0, 0, 0] board3[3, 0:7] = [0, 0, 0, 0, 0, 0, 0] board3[4, 0:7] = [0, 0, 0, 0, 0, 0, 0] board3[5, 0:7] = [0, 0, 0, 0, 0, 0, 0] action3, save_state = generate_move_minimax(board3, BoardPiece(2), False) assert action3 == 2 board4 = initialize_game_state() board4[0, 0:7] = [1, 2, 2, 1, 1, 2, 2] board4[1, 0:7] = [1, 2, 2, 2, 1, 1, 2] board4[2, 0:7] = [2, 1, 1, 1, 2, 2, 1] board4[3, 0:7] = [1, 1, 2, 1, 1, 2, 2] board4[4, 0:7] = [2, 1, 2, 2, 1, 1, 1] board4[5, 0:7] = [1, 2, 2, 1, 2, 0, 2] action4, save_state = generate_move_minimax(board4, BoardPiece(2), False) assert action4 == 5
def test_connected_four(): from agents.common import connected_four, initialize_game_state board = initialize_game_state() assert not connected_four(board, PLAYER2) board[2:6, 0] = PLAYER1 assert connected_four(board, PLAYER1) board = initialize_game_state() board[2:6, 0] = PLAYER2 assert connected_four(board, PLAYER2)
def test_evaluate_end_state(): from agents.agent_negamax.negamax import evaluate_end_state dummy_board = initialize_game_state() assert evaluate_end_state(dummy_board, PLAYER1) == 0 assert evaluate_end_state(dummy_board, PLAYER2) == 0 win_player_1 = initialize_game_state() win_player_1[:4, 0] = PLAYER1 assert evaluate_end_state(win_player_1, PLAYER1) == np.inf assert evaluate_end_state(win_player_1, PLAYER2) == -np.inf
def test_generate_move_minimax_alphabeta(): """ Test for the first movement of the minimax alpha-beta agent """ ret = initialize_game_state() column, state = generate_move_minimax_alphabeta(ret, PLAYER1) assert int(column) == 3 ret2 = initialize_game_state() column, state = generate_move_minimax_alphabeta(ret2, PLAYER2) assert int(column) == 3
def test_generate_move_scored(): """ Test for the first movement of the scored agent """ from agents.agent_score.score_aa import generate_move_scored from agents.common import initialize_game_state ret = initialize_game_state() column, state = generate_move_scored(ret, PLAYER1, []) assert int(column) == 3 ret2 = initialize_game_state() column, state = generate_move_scored(ret2, PLAYER2, []) assert int(column) == 3
def test_generate_move(): board = initialize_game_state() ret = generate_move(board, PLAYER1, board) #first move should be in column 3 and saved state not modified assert ret == (3, board)
def test_low_row_heuristic(): from agents.agent_negamax.negamax import low_row_heuristic from agents.common import initialize_game_state dummy_board = initialize_game_state() assert low_row_heuristic(dummy_board, PLAYER1) == 0 assert low_row_heuristic(dummy_board, PLAYER2) == 0 dummy_board[0, 0] = PLAYER1 assert low_row_heuristic(dummy_board, PLAYER1) == 5 assert low_row_heuristic(dummy_board, PLAYER2) == 0 dummy_board[0, 1] = PLAYER2 assert low_row_heuristic(dummy_board, PLAYER1) == 5 assert low_row_heuristic(dummy_board, PLAYER2) == 5 dummy_board[0, 2] = PLAYER2 assert low_row_heuristic(dummy_board, PLAYER1) == 5 assert low_row_heuristic(dummy_board, PLAYER2) == 10 dummy_board[0, 3] = PLAYER1 assert low_row_heuristic(dummy_board, PLAYER1) == 10 assert low_row_heuristic(dummy_board, PLAYER2) == 10 dummy_board[1, 0] = PLAYER1 assert low_row_heuristic(dummy_board, PLAYER1) == 14 assert low_row_heuristic(dummy_board, PLAYER2) == 10 dummy_board[1, 1] = PLAYER2 assert low_row_heuristic(dummy_board, PLAYER1) == 14 assert low_row_heuristic(dummy_board, PLAYER2) == 14
def test_negamax(): from agents.agent_negamax.negamax import negamax from agents.common import initialize_game_state dummy_board = initialize_game_state() assert negamax(dummy_board, PLAYER1, 0) == 0 assert negamax(dummy_board, PLAYER2, 0) == 0 win_player_1 = initialize_game_state() win_player_1[:4, 0] = PLAYER1 assert negamax(win_player_1, PLAYER1, 0) == np.inf assert negamax(win_player_1, PLAYER2, 0) == -np.inf near_1_win = initialize_game_state() near_1_win[:3, 0] = PLAYER1 assert negamax(near_1_win, PLAYER1, 1) == np.inf
def testApplyPlayerAction(self): from agents.common import apply_player_action move = np.random.randint(7) board = apply_player_action(initialize_game_state(), move, player) self.assertIsInstance(board, np.ndarray) self.assertEqual(board.shape, (6, 7)) self.assertEqual(board[0, move], player) if move in np.arange(7)\ else self.assertTrue(np.array_equal(board, np.zeros((6, 7)))) #apply move in the same column to see if pieces stack within column: board = apply_player_action(board, move, player) self.assertEqual(board[0, move], board[1, move], player) #test whether appropriate Exceptions are raised: board[:, move] = np.ones(6) * player with self.assertRaises(ColumnError): apply_player_action(board, move, player) illegalMove = 8 #illegal move should raise Exception with self.assertRaises(BoardError): apply_player_action(board, illegalMove, player)
def test_add_node(): board = initialize_game_state() board[0, 0:7] = [0, 1, 1, 2, 1, 0, 0] board[1, 0:7] = [0, 0, 0, 2, 0, 0, 0] board[2, 0:7] = [0, 0, 0, 1, 0, 0, 0] board[3, 0:7] = [0, 0, 0, 2, 0, 0, 0] board[4, 0:7] = [0, 0, 0, 0, 0, 0, 0] board[5, 0:7] = [0, 0, 0, 0, 0, 0, 0] root = Node(12, board) root.add_node() child = root.children[0] child.add_node() child2 = child.children[0] child.add_node() child3 = child.children[1] assert child.children[0] == child2 assert child2.parent == child assert child.parent == root assert root.children[0] == child assert child.children[1] == child3 assert not child.children[0] == child3 assert not root == child2 assert not child2.parent == root assert not root == child assert not child == child2
def testInitializeGameState(self): board = initialize_game_state() self.assertIsInstance(board, np.ndarray) self.assertEqual(board.dtype, BoardPiece) self.assertTrue(np.array_equal(board, np.zeros((6, 7))))
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_apply_player_action(): board_ = apply_player_action(board=initialize_game_state(), action=3, player=np.int8(np.random.choice([1,2]))) #CHECK WHETHER THE CHANG IS WHAT IS EXPECTED board_config_1 = np.zeros([6,7]) board_config_1[0,1] = np.int8(1) board_config_1 = apply_player_action(board=board_config_1, action=1, player=np.int8(0)) # breakpoint() expected_board_1 = np.zeros([6,7], dtype=int) expected_board_1[0,1] = np.int8(1) expected_board_1[1,1] = np.int8(0) #print(board_) #print('Expected Board') # print(expected_board_1) # print('\n') # print('Board after player action') # print(board_config_1) assert isinstance(board_, np.ndarray) assert np.all(board_config_1 == expected_board_1)
def test_initialize_game_state(): ret = initialize_game_state() assert isinstance(ret, np.ndarray) assert ret.dtype == BoardPiece assert ret.shape == (6, 7) assert np.all(ret == NO_PLAYER)
def test_check_end_state(): test_board = cm.initialize_game_state() test_board = np.ones(test_board.shape) * 3 board_map, board_mask = cm.board_to_bitmap(test_board, cm.PLAYER1) empty_board = mpz('0' * test_board.size) board_map = mpz(board_map) board_mask = mpz(board_mask) assert cm.check_end_state(board_map, board_mask, empty_board, test_board.shape) == cm.GameState.IS_DRAW test_board[0, 1] = cm.PLAYER1 test_board[1, 2] = cm.PLAYER1 test_board[2, 3] = cm.PLAYER1 test_board[3, 4] = cm.PLAYER1 board_map, board_mask = cm.board_to_bitmap(test_board, cm.PLAYER1) empty_board = mpz('0' * test_board.size) board_map = mpz(board_map) board_mask = mpz(board_mask) assert cm.check_end_state(board_map, board_mask, empty_board, test_board.shape) == cm.GameState.IS_WIN board_map, board_mask = cm.board_to_bitmap(test_board, cm.PLAYER1) empty_board = mpz('0' * test_board.size) board_map = mpz(board_map) board_mask = mpz(board_mask) assert cm.check_end_state(board_map, board_mask, empty_board, test_board.shape) == cm.GameState.IS_WIN
def test_pretty_print_board(): from agents.common import pretty_print_board from agents.common import initialize_game_state dummy_board = initialize_game_state() ret = pretty_print_board(dummy_board) # Split string at newlines rows = ret.split('\n') # 1. Evaluate board area # number of rows (excluding borders) = 1st dim of board assert len(rows[1:-2]) == dummy_board.shape[0] # length of each row (excluding border rows) = 2nd dim of board assert all((len(r) - 1) / 2 == dummy_board.shape[1] for r in rows[1:-2]) # div by 2 to account for space # 2. Evaluate border area borderForm = '|{}|'.format('=' * (2 * (dummy_board.shape[1]) - 1)) column_label_form = '|{}|'.format(' '.join( [str(n) for n in range(dummy_board.shape[1])])) assert rows[0] == borderForm assert rows[-2] == borderForm assert rows[-1] == column_label_form
def test_agents(): """ Test that the agents minimax and MCTS take immediate wins and block immediate losses""" empty_board = initialize_game_state() n_rows, n_cols = empty_board.shape for player in players: opponent = PLAYER1 if player == PLAYER2 else PLAYER2 # Test for immediate wins (p=player) and immediate losses (p=opponent) for p in (player, opponent): board_col = empty_board.copy() board_row = empty_board.copy() # Check for win and loss in a row and column for i in range(CONNECT_N - 1): board_row = apply_player_action(board_row, PlayerAction(i), p) board_col = apply_player_action(board_col, PlayerAction(CONNECT_N - 1), p) # Check that both agents make the right move (always column CONNECT_N -1 = 3) for move_agent in move_agents: for board in [board_row, board_col]: action = move_agent(board, player, None)[0] assert action == PlayerAction(CONNECT_N - 1) # Test that the agent blocks a certain win of the opponent (two free player pieces in the middle of # the board generate a certain win if the player does not put his piece to the right of left) board = empty_board.copy() board[-1, 1:3] = opponent for move_agent in move_agents: action = move_agent(board, player, None)[0] assert action == PlayerAction(0) or action == PlayerAction(3)
def generate_full_board(player, empty_spaces=0): # Generate an empty board arr_board = cm.initialize_game_state() # Convert board to bitmap bit_board, bit_mask = cm.board_to_bitmap(arr_board, player) # Calculate the board shape bd_shp = arr_board.shape # While the board is not full, continue placing pieces while popcount(bit_mask) != bd_shp[0] * bd_shp[1] - empty_spaces: # Select a random move in a column that is not full move = -1 while not (0 <= move < bd_shp[1]): move = np.random.choice(bd_shp[1]) try: move = cm.PlayerAction(move) cm.top_row(arr_board, move) except IndexError: move = -1 # Apply the move to both boards cm.apply_action(arr_board, move, player) bit_board, bit_mask = cm.apply_action_cp(bit_board, bit_mask, move, bd_shp) # Switch to the next player player = cm.BoardPiece(player % 2 + 1) return arr_board, bit_board, bit_mask, player
def root_state(player: cm.BoardPiece): ''' Initializes starting state with starting player :param player: the starting player :return: the root state of the game ''' return State(cm.initialize_game_state(), player, cm.GameState.STILL_PLAYING, None)
def test_connected_four_horizontal(self): c4_yes = common.initialize_game_state() common.apply_player_action(c4_yes, PlayerAction(0), common.PLAYER1) common.apply_player_action(c4_yes, PlayerAction(1), common.PLAYER1) common.apply_player_action(c4_yes, PlayerAction(2), common.PLAYER1) common.apply_player_action(c4_yes, PlayerAction(3), common.PLAYER1) c4_no = common.initialize_game_state() common.apply_player_action(c4_no, PlayerAction(0), common.PLAYER1) common.apply_player_action(c4_no, PlayerAction(1), common.PLAYER1) common.apply_player_action(c4_no, PlayerAction(2), common.PLAYER2) common.apply_player_action(c4_no, PlayerAction(3), common.PLAYER1) assert common.connected_four(c4_yes, PLAYER1) == True assert common.connected_four(c4_yes, PLAYER1, PlayerAction(3)) == True assert common.connected_four(c4_no, PLAYER1) == False assert common.connected_four(c4_no, PLAYER1, PlayerAction(3)) == False
def test_pretty_print_board(): board = initialize_game_state() pp_string = pretty_print_board(board) assert isinstance(pp_string, str) print(pp_string) return pp_string
def test_generate_move_minimax(): """ Must return a player action """ for ply in [PLAYER1, PLAYER2]: out = generate_move(board=cc.initialize_game_state(), player=ply, saved_state=None) assert type(out) == tuple assert isinstance(out[0], PlayerAction)
def test_get_valid_moves(): from agents.common import get_valid_moves from agents.common import initialize_game_state dummy_board = initialize_game_state() all_moves = np.arange(dummy_board.shape[1]) assert np.all(get_valid_moves(dummy_board) == all_moves)