def test_result_updates_uf_player_two_side_not_connected(): initial = game.initial_state(5) p2_side1 = 5 * 5 + 2 state = game.result(initial, 1) state = game.result(state, 2) result = game._connected(p2_side1, 2, state.uf) assert not result
def test_copy_game_board_equals(): initial = game.initial_state(11) state = game.result(game.result(game.result(initial, 0), 1), 2) copy = state.copy() actual = copy.board expected = state.board assert np.array_equal(actual, expected)
def test_result_adds_action_for_player_two(): initial = game.initial_state(4) state = game.result(initial, 1) state = game.result(state, 2) actual = state.board[2] expected = -1 assert actual == expected
def test_utility_player_one_won(): initial = game.initial_state(2) fst = game.result(initial, 0) snd = game.result(fst, 1) thd = game.result(snd, 2) actual = game.utility(thd, 1) expected = 1 assert actual == expected
def test_image_stack_with_0_step(): size = 77 initial = game.initial_state(size) state = game.result(initial, 55) state = game.result(state, 94) expected = np.zeros((2, size, size)) actual = game._image_stack(state.history, size, 0) assert np.array_equal(actual, expected)
def test_utility_player_one_lost(): initial = game.initial_state(2) fst = game.result(initial, 3) snd = game.result(fst, 0) thd = game.result(snd, 2) fth = game.result(thd, 1) actual = game.utility(fth, 1) expected = -1 assert actual == expected
def test_result_appends_history(): initial = game.initial_state(33) action_one = 17 action_two = 22 state = game.result(initial, action_one) state = game.result(state, action_two) expected = [action_one, action_two] actual = state.history assert expected == actual
def test_image_stack_player_one_perspective(): size = 3 action_one = 0 action_two = 1 initial = game.initial_state(size) state = game.result(initial, action_one) state = game.result(state, action_two) expected = np.zeros((2, size, size)) expected[0][action_one // size][action_one % size] = 1 # player one's action expected[1][action_two // size][action_two % size] = 1 # player two's action actual = game._image_stack(state.history, size, -1) assert np.array_equal(actual, expected)
def play_games(self, fst, snd, num_games, games_played=0): states = [game.initial_state(self.size) for _ in range(num_games)] fst_wins = 0 turn = 0 finished_games = games_played while states: if self.visual: self.gui.update(states[0]) if turn % 2 == 0: # it's first players' turn if fst: # fst is an AI actions = fst.decide_parallel(states) else: actions = self.human_input() else: if snd: # snd is an AI actions = snd.decide_parallel(states) else: actions = self.human_input() states = [game.result(s, a) for s, a in zip(states, actions)] if self.visual: self.gui.update(states[0]) fst_wins += sum(1 for s in states if game.utility(s, 1) == 1) new_states = [] for s in states: if game.terminal_test(s): finished_games += 1 #self.save_game_result(fst, snd, game.utility(s,1), finished_games) else: new_states.append(s) states = new_states # important step ;) turn += 1 return fst_wins
def test_result_updates_uf_player_one_sides_not_connected(): initial = game.initial_state(4) p1_side1 = 5 * 5 action = 5 state = game.result(initial, action) result = game._connected(p1_side1, action, state.uf) assert not result
def test_result_removes_correct_action(): initial = game.initial_state(19) action = 55 state = game.result(initial, action) actions = set(state.actions) result = action not in actions assert result
def test_actions_correct_count(): initial = game.initial_state(8) state = game.result(initial, 1) actions = game.actions(state) expected = 63 # 8 * 8 - 1 actual = len(actions) assert actual == expected
def expand(node): if node.untried_actions: action = node.untried_actions.pop() child = Node(game.result(node.state, action), action=action, parent=node) node.children.append(child) node = child return node
def test_image_stack_player_two_perspective(): size = 3 action = 1 initial = game.initial_state(size) state = game.result(initial, action) expected = np.zeros((2, size, size), dtype=np.float32) # player ones's action # notice we now use modulo first, and division second. # this results in a transposed matrix expected[1][action % size][action // size] = 1 actual = game._image_stack(state.history, size, 1) assert np.array_equal(actual, expected)
def test_result_player_change(): initial = game.initial_state(1) state = game.result(initial, 0) expected = -1 actual = game.player(state) assert actual == expected
def test_result_adds_action_for_player_one(): initial = game.initial_state(4) state = game.result(initial, 1) actual = state.board[1] expected = 1 assert actual == expected
def evaluate(state): while not game.terminal_test(state): actions = game.actions(state) rand_action = actions[random.randrange(0, len(actions))] state = game.result(state, rand_action) return state
def test_result_changes_player(): initial = game.initial_state(3) state = game.result(initial, 0) actual = state.player_to_play expected = -1 assert actual == expected