def test_interrogate_asks_for_two_cards_puts_the_range_in_history_and_display_the_result(self): try: old_raw_input = raw_input Interactive.raw_input = mock_raw_input('1', '3L', '7L') # opponent id, low card, high card player = HumanPlayer('joe') tom = AIPlayer('tom') tom._hand = [(1, 'L'), (3, 'L'), (6, 'L'), (2, 'H'), (8, '$'), (9, '$')] state = GameState() state.turn = 10 state.current_player = player state.players = [player, tom] state.question_cards = [(1, 'L'), (3, 'L'), (7, 'L')] with captured_output() as (out, err): turn_ended = interrogate_command(state) self.assertEqual('Interrogate\n' 'Question cards: 1L 3L 7L\n' 'Cards in this range: 2', output(out)) turn = state.history.pop() self.assertEqual(10, turn['turn']) self.assertEqual('joe', turn['player'].name) self.assertEqual('tom', turn['opponent'].name) self.assertEqual('interrogate', turn['action']) self.assertEqual(['L'], turn['range'].suits) self.assertEqual([3, 4, 5, 6, 7], turn['range'].ranks) self.assertEqual(2, turn['result']) self.assertTrue(turn_ended) finally: Interactive.raw_input = old_raw_input
def test_interrogate_for_the_same_two_cards_asks_for_rank_or_suit(self): try: old_raw_input = raw_input Interactive.raw_input = mock_raw_input('1', '3L', '3L', 'suit') # opponent id, low card, high card player = HumanPlayer('joe') tom = AIPlayer('Tom') tom._hand = [(1, 'L'), (3, 'L'), (6, 'L'), (2, 'H'), (8, '$'), (9, '$')] state = GameState() state.turn = 10 state.current_player = player state.players = [player, tom] state.question_cards = [(1, '$'), (3, 'L'), (3, 'L')] with captured_output() as (out, err): turn_ended = interrogate_command(state) self.assertEqual('Interrogate\n' 'Question cards: 1$ 3L 3L\n' 'Cards in this range: 3', output(out)) turn = state.history.pop() self.assertEqual(10, turn['turn']) self.assertEqual('joe', turn['player'].name) self.assertEqual('Tom', turn['opponent'].name) self.assertEqual('interrogate', turn['action']) self.assertEqual('3L->3L [suit]', str(turn['range'])) self.assertEqual(3, turn['result']) self.assertTrue(turn_ended) finally: Interactive.raw_input = old_raw_input
def test_secret_asks_for_two_cards_puts_the_range_in_history_and_display_the_result(self): try: old_raw_input = raw_input Interactive.raw_input = mock_raw_input('1', '9$', '1H') # opponent id, low card, high card player = HumanPlayer('joe') donna = AIPlayer('donna') donna._hand = [(1, 'L'), (3, 'L'), (6, 'L'), (2, 'H'), (8, '$'), (9, '$')] state = GameState() state.turn = 1 state.current_player = player state.players = [player, donna] state.question_cards = [(1, 'L'), (3, 'L'), (7, 'L')] with captured_output() as (out, err): turn_ended = secret_command(state) self.assertEqual('Secret\n' 'Cards in this range: 2', output(out)) turn = state.history.pop() self.assertEqual(1, turn['turn']) self.assertEqual('joe', turn['player'].name) self.assertEqual('donna', turn['opponent'].name) self.assertEqual('secret', turn['action']) self.assertEqual(['L', 'H', '$'], turn['range'].suits) self.assertEqual([9, 1], turn['range'].ranks) self.assertEqual(2, turn['result']) self.assertTrue(turn_ended) self.assertEqual(0, state.current_player.secret) finally: Interactive.raw_input = old_raw_input
def test_accuse_bad_guess_of_cards(self): try: old_raw_input = raw_input Interactive.raw_input = mock_raw_input('1', '1L', '2L') # opponent id, first card, second card joe = HumanPlayer('joe') joe._hand = [(4, 'L'), (8, 'L'), (5, 'H'), (8, 'H')] ai = AIPlayer('ai') ai._hand = [(1, 'L'), (3, 'L'), (1, 'H'), (9, '$')] state = GameState() state.turn = 23 state.current_player = joe state.players = [joe, ai] state.evidence_cards = [(5, '$'), (5, 'L')] with captured_output() as (out, err): self.assertTrue(accuse_command(state)) self.assertEqual('Accuse\n\n' 'Your guess is: Incorrect', output(out)) accusation = state.accusations.pop() self.assertEqual('joe', accusation['player'].name) self.assertEqual('ai', accusation['accused'].name) self.assertEqual([(1, 'L'), (2, 'L')], accusation['cards']) self.assertEqual('incorrect', accusation['outcome']) self.assertEqual('ended', state.status) finally: Interactive.raw_input = old_raw_input
def play_round(self): game = Game(snake_init_len=self.GAME_W // 3, width=self.GAME_W, height=self.GAME_H) run = True player = AIPlayer() while run and not game.game_over: pg.time.delay(self.PERIOD) for event in pg.event.get(): if event.type == pg.QUIT: run = False elif event.type == pg.KEYDOWN: continue dirs = { pg.K_UP: Game.UP, pg.K_DOWN: Game.DOWN, pg.K_RIGHT: Game.RIGHT, pg.K_LEFT: Game.LEFT } if event.key in dirs: game.change_direction(dirs[event.key]) self.draw_game(game.board) game.change_direction(player.get_next_move(game)) game.move_snake() return run
def test_accuse_bad_guess_of_murderer(self): try: old_raw_input = raw_input Interactive.raw_input = mock_raw_input('1', '3L', '5L') # opponent id, first card, second card joe = HumanPlayer('joe') joe._hand = [(4, 'L'), (7, 'L'), (5, 'H'), (8, 'H')] ai1 = AIPlayer('ai1') ai1._hand = [(1, 'L'), (3, 'L'), (1, 'H'), (9, '$')] ai2 = AIPlayer('ai2') ai2._hand = [(8, 'L'), (3, 'H'), (2, '$'), (3, '$')] state = GameState() state.turn = 23 state.current_player = joe state.players = [joe, ai1, ai2] state.evidence_cards = [(3, 'L'), (5, 'L')] with captured_output() as (out, err): self.assertTrue(accuse_command(state)) self.assertEqual('Accuse\n\n' 'Your guess is: Incorrect', output(out)) self.assertEqual('ended', state.status) finally: Interactive.raw_input = old_raw_input
def make_player(name, num): if name == 'ai': return AIPlayer(num) elif name == 'mcts': return AIPlayer(num, 'mcts') elif name == 'random': return RandomPlayer(num) elif name == 'human': return HumanPlayer(num)
def test_determine_murderer_when_its_another_player(self): state = GameState() john = HumanPlayer('john') jim = AIPlayer('jim') jack = AIPlayer('jack') state.current_player = john state.players = [john, jim, jack] state.extra_card = (8, 'L') john._hand = [(5, 'L'), (9, 'H'), (6, '$')] jim._hand = [(4, 'L'), (7, 'L'), (3, '$')] jack._hand = [(1, 'L'), (3, 'H'), (5, '$')] accusation_cards = [(5, 'L'), (7, '$')] # => murder card = 3H self.assertEqual(jack, determine_murderer(state, accusation_cards))
def setUp(self): self.thor = AIPlayer('thor') self.sigrid = AIPlayer('sigrid') self.erlend = HumanPlayer('erlend') self.state = GameState() self.state.players = [self.thor, self.sigrid, self.erlend]
def make_player(name, num): if name == 'ai': return AIPlayer(num) # TODO Change this back to hand in just board elif name == 'random': return RandomPlayer(num) elif name == 'human': return HumanPlayer(num)
def evaluate(self, n_games=10): # 定义两个棋手 current_mcts_player = AIPlayer(self.policy_value.policy_value_fn, self.c_puct, self.playout_num) pure_mcts_player = MCTSPlayer(c_puct=5, playout_num=self.mcts_search_num) win_cnt = defaultdict(int) for i in range(n_games): winner = self.game.start_play(current_mcts_player, pure_mcts_player, start_player=i % 2) win_cnt[winner] += 1 # 赢:1分,平局:0.5分 win_rate = 1.0 * (win_cnt[1] + 0.5 * win_cnt[-1]) / n_games print("num_playouts:{} ".format(self.mcts_search_num), end="") print("win:{},lose:{},tie:{}".format(win_cnt[1], win_cnt[2], win_cnt[-1])) # 胜率 return win_rate
def __init__(self, init_model=None): # 棋盘相关参数 self.board_width = board_width self.board_height = board_height # 用于游戏的棋盘 self.board = Board() self.game = Game(self.board) # 训练相关参数 self.lr = 2e-3 self.lr_coef = 1.0 self.buffer_size = 10000 self.batch_size = 512 self.buffer = deque(maxlen=self.buffer_size) self.kl_targ = 0.02 self.mcts_search_num = 2000 # 保存模型的频率 self.save_freq = 50 # 训练的次数 self.game_epoch = 1000 # 每一轮更新网络参数的次数 self.net_epoch = 5 # 自对弈相关参数 self.temp = 1.0 self.playout_num = 400 self.c_puct = 5 # 最大胜率 self.best_win_rate = 0.0 # 该局的落子数 self.move_num = 0 # 训练的价值策略网络 self.policy_value = PolicyValue(model_file=init_model) # 蒙特卡洛玩家 self.mcts_player = AIPlayer(self.policy_value.policy_value_fn, self.c_puct, self.playout_num, is_selfplay=1)
def game_flow(): board = Board() player1 = AIPlayer('X', (255, 255, 0)) player2 = HumanPlayer('O', (0, 0, 255)) current_player = player1 while True: # Animation loop (game flow loop) row, column = current_player.play_turn( board) # Get selected cell's row and col, for current player if row is None: # No move was selected continue board.update_board(row, column, current_player) # Update board with selected cell if has_won(board.board, 3 * row + column): # Check if last move was a wining move print("{} has won!".format(current_player.shape)) break if not board.empty_cells(): # Check for a tie print("TIE") break current_player = player2 if current_player is player1 else player1 # Update current player
def __init__(self, numHumanPlayers, numAIPlayers=0, deck=[]): #initialize and shuffle deck if deck == []: self.deck = ['6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣', 'A♣',\ '6♦', '7♦', '8♦', '9♦', '10♦', 'J♦', 'Q♦', 'K♦', 'A♦',\ '6♥', '7♥', '8♥', '9♥', '10♥', 'J♥', 'Q♥', 'K♥', 'A♥',\ '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♠'] random.shuffle(self.deck) else: self.deck = deck self.trump = self.deck[0] #initialize players self.players = [] for i in range(numHumanPlayers): self.players.append(HumanPlayer("Player " + str(i + 1))) #deal hands for _ in range(6): self.players[i].addCard(self.drawCard()) for i in range(numAIPlayers): self.players.append( AIPlayer("Player " + str(i + numHumanPlayers + 1))) #deal hands for _ in range(6): self.players[i + numHumanPlayers].addCard(self.drawCard())
import sys from PyQt5.QtWidgets import QApplication from Gui import GUI from Player import AIPlayer from PolicyValue import PolicyValue from Board import Board from config import * if __name__ == "__main__": app = QApplication(sys.argv) best_policy = PolicyValue(best_model_file) mcts_player = AIPlayer(best_policy.policy_value_fn, c_puct=5, playout_num=400) board = Board() gui = GUI(board=board, mcts=mcts_player, start_player=1) gui.show() sys.exit(app.exec_())
def test_prepare_card_pairs(self): player = AIPlayer('Ella') self.assertItemsEqual([((1, 'L'), (2, 'H')), ((1, 'L'), (3, '$')), ((2, 'H'), (3, '$')), ((2, 'H'), (1, 'L')), ((3, '$'), (1, 'L')), ((3, '$'), (2, 'H'))], player.prepare_card_pairs([(1, 'L'), (2, 'H'), (3, '$')]))
def test_prepare_card_pairs_should_include_rank_and_suit_for_identical_cards(self): player = AIPlayer('Ella') pairs = player.prepare_card_pairs([(1, 'L'), (1, 'L'), (3, '$')]) self.assertItemsEqual([((1, 'L'), (1, 'L'), 'rank'), ((1, 'L'), (1, 'L'), 'suit'), ((1, 'L'), (3, '$')), ((3, '$'), (1, 'L'))], pairs)
class TestAI(unittest.TestCase): def setUp(self): self.thor = AIPlayer('thor') self.sigrid = AIPlayer('sigrid') self.erlend = HumanPlayer('erlend') self.state = GameState() self.state.players = [self.thor, self.sigrid, self.erlend] def test_ai_player_setup_ai(self): self.thor._hand = [(1, 'L'), (8, '$')] self.sigrid._hand = [(7, 'L'), (4, 'H')] self.erlend._hand = [(9, 'H'), (9, '$')] self.state.extra_card = (5, 'L') self.thor.setup_ai(self.state) expected_sheet = sheet_table(excluded=[(1, 'L'), (8, '$'), (5, 'L')]) self.assertEqual(expected_sheet, self.thor.sheets['evidence'].table) self.assertEqual(expected_sheet, self.thor.sheets[self.sigrid].table) self.assertEqual(expected_sheet, self.thor.sheets[self.erlend].table) def test_ai_player_setup_ai_when_there_is_no_extra_card(self): self.state.extra_card = None try: self.thor.setup_ai(self.state) except TypeError: self.fail("When there is no extra card, setup_ai() should not raise an exception") def test_review_last_turn_marks_cards_as_excluded_when_result_is_zero(self): self.state.history.append({'turn': 1, 'player': self.thor, 'action': 'interrogate', 'opponent': self.sigrid, 'range': Range((2, 'H'), (6, 'H')), 'result': 0}) self.thor.setup_ai(self.state) self.thor.review_turn(self.state, -1) self.assertEqual(sheet_table(excluded=[(2, 'H'), (3, 'H'), (4, 'H'), (5, 'H'), (6, 'H')]), self.thor.sheets[self.sigrid].table) def test_review_last_turn_does_nothing_if_opponent_is_self(self): self.state.history.append({'turn': 1, 'player': self.sigrid, 'action': 'interrogate', 'opponent': self.thor, 'range': Range((2, 'H'), (6, 'H')), 'result': 0}) self.thor.setup_ai(self.state) try: self.thor.review_turn(self.state, -1) except KeyError: self.fail("review_last_interrogate() has tried to modify the player's own sheet") def test_review_last_turn_marks_cards_as_owned_when_results_equals_all_unknown_slots(self): self.thor._hand = [(5, 'H')] self.state.history.append({'turn': 1, 'player': self.thor, 'action': 'interrogate', 'opponent': self.sigrid, 'range': Range((2, 'H'), (6, 'H')), 'result': 4}) self.thor.setup_ai(self.state) self.thor.review_turn(self.state, -1) self.assertEqual(sheet_table(excluded=[(5, 'H')], owned=[(2, 'H'), (3, 'H'), (4, 'H'), (6, 'H')]), self.thor.sheets[self.sigrid].table) self.assertEqual(sheet_table(excluded=[(5, 'H'), (2, 'H'), (3, 'H'), (4, 'H'), (6, 'H')]), self.thor.sheets[self.erlend].table) self.assertEqual(sheet_table(excluded=[(5, 'H'), (2, 'H'), (3, 'H'), (4, 'H'), (6, 'H')]), self.thor.sheets[EVIDENCE_CARDS].table) def test_review_last_turn_takes_secret_into_account_if_current_player_was_the_asker(self): self.state.history.append({'turn': 1, 'player': self.thor, 'action': 'secret', 'opponent': self.sigrid, 'range': Range((9, '$'), (2, '$')), 'result': 0}) self.thor.setup_ai(self.state) self.thor.review_turn(self.state, -1) self.assertEqual(sheet_table(excluded=[(9, '$'), (1, '$'), (2, '$')]), self.thor.sheets[self.sigrid].table) self.state.history.append({'turn': 2, 'player': self.erlend, 'action': 'secret', 'opponent': self.sigrid, 'range': Range((3, 'L'), (3, '$')), 'result': 0}) self.thor.review_turn(self.state, -1) self.assertEqual(sheet_table(excluded=[(9, '$'), (1, '$'), (2, '$')]), self.thor.sheets[self.sigrid].table) def test_review_turn_gives_suit_total(self): self.state.history.append({'turn': 1, 'player': self.thor, 'action': 'interrogate', 'opponent': self.sigrid, 'range': Range((2, '$'), (8, '$')), 'result': 2}) self.thor.setup_ai(self.state) self.thor.sheets[self.sigrid].exclude_cards([(1, '$'), (4, '$')]) self.thor.sheets[self.sigrid].own_cards([(9, '$')]) self.thor.review_turn(self.state, -1) self.assertEqual(sheet_table(excluded=[(1, '$'), (4, '$')], owned=[(9, '$')], totals={'$': 3}), self.thor.sheets[self.sigrid].table) def test_review_turn_gives_rank_total(self): self.state.history.append({'turn': 1, 'player': self.thor, 'action': 'interrogate', 'opponent': self.sigrid, 'range': Range((3, 'L'), (3, 'H')), 'result': 2}) self.thor.setup_ai(self.state) self.thor.review_turn(self.state, -1) self.assertEqual(sheet_table(totals={3: 2}), self.thor.sheets[self.sigrid].table) def test_review_history_full(self): self.state.history.append({'turn': 1, 'player': self.thor, 'action': 'interrogate', 'opponent': self.sigrid, 'range': Range((9, '$'), (2, '$')), 'result': 0}) self.state.history.append({'turn': 2, 'player': self.sigrid, 'action': 'interrogate', 'opponent': self.erlend, 'range': Range((2, '$'), (2, 'L')), 'result': 0}) self.state.history.append({'turn': 3, 'player': self.erlend, 'action': 'interrogate', 'opponent': self.sigrid, 'range': Range((3, 'L'), (4, 'H')), 'result': 4}) self.thor._hand = [(3, '$'), (4, 'L')] self.thor.setup_ai(self.state) self.thor.review_history(self.state) self.assertEqual(sheet_table(excluded=[(9, '$'), (1, '$'), (2, '$'), (3, '$'), (4, 'L')], owned=[(3, 'L'), (3, 'H'), (4, '$'), (4, 'H')]), self.thor.sheets[self.sigrid].table) self.assertEqual(sheet_table(excluded=Range((2, 'L'), (4, '$')).cards(), totals={2: 0}), self.thor.sheets[self.erlend].table) def test_review_totals_suit_total_equals_owned_slots(self): self.thor.setup_ai(self.state) self.thor.sheets[self.sigrid].own_cards([(3, '$'), (8, '$')]) self.thor.sheets[self.sigrid].set_suit_total('$', 2) self.thor.review_totals() self.assertEqual(sheet_table(excluded=[(1, '$'), (2, '$'), (4, '$'), (5, '$'), (6, '$'), (7, '$'), (9, '$')], owned=[(3, '$'), (8, '$')], totals={'$': 2}), self.thor.sheets[self.sigrid].table) def test_review_totals_suit_total_equals_zero(self): self.thor.setup_ai(self.state) self.thor.sheets[self.sigrid].set_suit_total('L', 0) self.thor.review_totals() self.assertEqual(sheet_table(excluded=Range((1, 'L'), (9, 'L')).cards(), totals={'L': 0}), self.thor.sheets[self.sigrid].table) def test_review_totals_suit_total_equals_number_of_owned_plus_unknown_slots(self): self.thor.setup_ai(self.state) self.thor.sheets[self.sigrid].own_cards([(3, 'L')]) self.thor.sheets[self.sigrid].exclude_cards([(1, 'L'), (2, 'L'), (5, 'L'), (6, 'L'), (8, 'L'), (9, 'L')]) self.thor.sheets[self.sigrid].set_suit_total('L', 3) self.thor.review_totals() self.assertEqual(sheet_table(excluded=[(1, 'L'), (2, 'L'), (5, 'L'), (6, 'L'), (8, 'L'), (9, 'L')], owned=[(3, 'L'), (4, 'L'), (7, 'L')], totals={'L': 3}), self.thor.sheets[self.sigrid].table) def test_review_totals_does_nothing_with_suit_total_when_its_larger_than_owned_slots(self): self.thor.setup_ai(self.state) self.thor.sheets[self.sigrid].own_cards([(3, 'L')]) self.thor.sheets[self.sigrid].exclude_cards([(1, 'L'), (2, 'L')]) self.thor.sheets[self.sigrid].set_suit_total('L', 5) self.thor.review_totals() self.assertEqual(sheet_table(excluded=[(1, 'L'), (2, 'L')], owned=[(3, 'L')], totals={'L': 5}), self.thor.sheets[self.sigrid].table) def test_review_totals_rank_total_equals_owned_slots(self): self.thor.setup_ai(self.state) self.thor.sheets[self.sigrid].own_cards([(3, 'H'), (3, '$')]) self.thor.sheets[self.sigrid].set_rank_total(3, 2) self.thor.review_totals() self.assertEqual(sheet_table(excluded=[(3, 'L')], owned=[(3, 'H'), (3, '$')], totals={3: 2}), self.thor.sheets[self.sigrid].table) def test_review_totals_rank_total_equals_zero(self): self.thor.setup_ai(self.state) self.thor.sheets[self.sigrid].set_rank_total(5, 0) self.thor.review_totals() self.assertEqual(sheet_table(excluded=[(5, 'L'), (5, 'H'), (5, '$')], totals={5: 0}), self.thor.sheets[self.sigrid].table) def test_review_totals_rank_total_equals_number_of_owned_plus_unknown_slots(self): self.thor.setup_ai(self.state) self.thor.sheets[self.sigrid].own_cards([(5, 'L')]) self.thor.sheets[self.sigrid].exclude_cards([(5, '$')]) self.thor.sheets[self.sigrid].set_rank_total(5, 2) self.thor.review_totals() self.assertEqual(sheet_table(excluded=[(5, '$')], owned=[(5, 'L'), (5, 'H')], totals={5: 2}), self.thor.sheets[self.sigrid].table) def test_review_totals_does_nothing_with_rank_total_when_its_larger_than_owned_slots(self): self.thor.setup_ai(self.state) self.thor.sheets[self.sigrid].own_cards([(3, 'L')]) self.thor.sheets[self.sigrid].exclude_cards([(3, 'H')]) self.thor.sheets[self.sigrid].set_rank_total(3, 3) self.thor.review_totals() self.assertEqual(sheet_table(excluded=[(3, 'H')], owned=[(3, 'L')], totals={3: 3}), self.thor.sheets[self.sigrid].table)
if letter == 'O': square = o_player.get_move(game) else: square = x_player.get_move(game) if game.make_move(square, letter): # make_move returns a valid move or not # show players move in board if print_game: print(letter + " has moved to square {}\n".format(square)) game.print_board() print() game.print_board_numbers() # check for winner if game.current_winner: if print_game: print("\n", letter + " WINS !!") return letter letter = 'O' if letter == 'X' else 'X' # switch letter time.sleep(0.6) if print_game: print("\nDRAW!! There is no winner.") if __name__ == '__main__': x_player = AIPlayer('X') o_player = HumanPlayer('O') t = TicTacToe() play(t, x_player, o_player, print_game=True)
game.print_board_nums() letter = 'X' while game.empty(): if letter == 'O': square = o.get_move(game) else: square = x.get_move(game) if game.make_move(square, letter): if print_game: game.print_board() print('') if game.current_winner: print(letter + ' wins!') return letter letter = 'O' if letter == 'X' else 'X' time.sleep(.8) print('Its a tie') x = HumanPlayer('X') #o = RandomComputerPlayer('O') Play agaisnt AI or change to randomCompPlayer o = AIPlayer('O') t = TicTacToe() play(t, x, o, print_game=True)