def test_max_rank(self): children = { 1: tictactoe.Board(), 5: tictactoe.Board(), 3: tictactoe.Board() } children[1].rank = 0 children[5].rank = 1 children[3].rank = -1 maxKey = max(children.iterkeys(), key=(lambda key: children[key].rank)) self.assertEqual(5, maxKey)
def matching_diag(self): board1 = tictactoe.Board() board1.boardArr[0] = 'X' board1.boardArr[4] = 'X' board1.boardArr[8] = 'X' self.assertTrue(board1.matching_diag('X')) board2 = tictactoe.Board() board2.boardArr[2] = 'X' board2.boardArr[4] = 'X' board2.boardArr[6] = 'X' self.assertTrue(board2.matching_diag('X')) board_no_match = tictactoe.Board() self.assertFalse(board_no_match.matching_diag('X')) self.assertFalse(board_no_match.matching_diag('O'))
def test_switch_player(self): board = tictactoe.Board() board.current_plyr = 'X' board.switch_player() self.assertEqual(board.current_plyr, 'O') board.switch_player() self.assertEqual(board.current_plyr, 'X')
def test_full_square(self): board = tictactoe.Board() column = row = 0 player = 'X' board.mark_square(column, row, player) self.assertFalse(board.mark_square(column, row, player))
def test_DiagonalWinner(self): board = tictactoe.Board() win = Win_Checker() board.board[0][0] = 'X' board.board[1][1] = 'X' board.board[2][2] = 'X' self.assertEqual(True, win.has_winner(board.board))
def test_move_on_taken_cell(self): _ = tictactoe.EMPTY X = tictactoe.CROSS O = tictactoe.NOUGHT board = tictactoe.Board() newBoard = board.move(4).move(4) # should return None self.assertEqual(None, newBoard)
def test_build_tree_pre_win(self): _ = tictactoe.EMPTY X = tictactoe.CROSS O = tictactoe.NOUGHT board = tictactoe.Board([_, O, O, _, O, X, X, _, _]) board.nextMove = O tictactoe.buildGameTree(board) self.assertEqual(-1, board.rank)
def test_goodInput(self): #arrange board = tictactoe.Board() row = 0 column = 0 player = 'X' marker = Square_Marker() #act and assert self.assertEqual(True, marker.mark_square(board.board, column, row, player))
def test_move_onEmpty(self): _ = tictactoe.EMPTY X = tictactoe.CROSS O = tictactoe.NOUGHT board = tictactoe.Board() newBoard = board.move(4) # center cells self.assertEqual([_] * 9, board.cells) self.assertEqual([_, _, _, _, O, _, _, _, _], newBoard.cells) self.assertEqual(X, newBoard.nextMove)
def test_badPlayer(self): #arrange board = tictactoe.Board() row = 0 column = 0 player = 'b' marker = Square_Marker() #act and assert self.assertEqual(False, marker.mark_square(board.board, column, row, player))
def test_outOfBounds(self): #arrange board = tictactoe.Board() row = 4 column = 3 player = 'X' marker = Square_Marker() #act and assert self.assertEqual(False, marker.mark_square(board.board, column, row, player))
def test_move_cross(self): _ = tictactoe.EMPTY X = tictactoe.CROSS O = tictactoe.NOUGHT board = tictactoe.Board() newBoard = board.move(4).move( 0) # O in the center, X in the top left corner self.assertEqual([_] * 9, board.cells) self.assertEqual([X, _, _, _, O, _, _, _, _], newBoard.cells) self.assertEqual(O, newBoard.nextMove)
def test_has_winner(self): player = 'X' winner_board = tictactoe.Board() winner_board.boardArr[0] = 'X' winner_board.boardArr[1] = 'X' winner_board.boardArr[2] = 'X' self.assertTrue(winner_board.has_winner(player)) col_board = tictactoe.Board() col_board.boardArr[0] = 'X' col_board.boardArr[3] = 'X' col_board.boardArr[6] = 'X' self.assertTrue(col_board.has_winner(player)) diag_board = tictactoe.Board() diag_board.boardArr[0] = 'X' diag_board.boardArr[4] = 'X' diag_board.boardArr[8] = 'X' self.assertTrue(diag_board.has_winner(player))
def random_v_random(n=1): """ Plays two random players against each other """ p1_strategy = strategies.RandomStrategy() p2_strategy = strategies.RandomStrategy() p1 = player.Player('X', p1_strategy) p2 = player.Player('O', p2_strategy) board = tictactoe.Board() game = rl_game.Game(p1, p2, board) game.play_one()
class ifCharacter(unittest.TestCase): board = tt.Board() def test_not_chara(self): self.assertEqual(self.board.if_character('Y'), False) def test_is_chara(self): self.assertEqual(self.board.if_character('X'), True) def test_isnot_chara(self): self.assertEqual(self.board.if_character('NOT'), False)
def random_v_q(n=1000): """ Trains the RL agent against random """ for i in range(5): p1_strategy = strategies.RandomStrategy() p2_strategy = strategies.QStrategy('O') p1 = player.Player('X', p1_strategy) p2 = player.Player('O', p2_strategy) board = tictactoe.Board() game = rl_game.Game(p1, p2, board) game.play_many(n)
class InBounds(unittest.TestCase): board = tt.Board() def test_Out_of_Bound(self): self.assertEqual(self.board.in_bound(3, 3), False) def test_in_Bound(self): self.assertEqual(self.board.in_bound(2, 2), True) def test_Out_of_Bound2(self): self.assertEqual(self.board.in_bound(3, 12), False)
class Test_Has_Winner(unittest.TestCase): board = BoardClass.Board() def test_Horizontal_Win(self): print("Testing horizontal win") self.board.game_board = [['0', "X", '0'], ['0', "X", '0'], ['0', "X", '0']] self.assertEqual(self.board.has_winner(), "0")
def q_v_q(): """ Plays two Q-strategy players against each other """ p1_strategy = strategies.QStrategy('X') p2_strategy = strategies.QStrategy('O') p1 = player.Player('X', p1_strategy) p2 = player.Player('O', p2_strategy) board = tictactoe.Board() game = rl_game.Game(p1, p2, board) game.play_one() p1.strategy.save_q() p2.strategy.save_q()
def text_matching_col(self): board1 = tictactoe.Board() board1.boardArr[0] = 'X' board1.boardArr[3] = 'X' board1.boardArr[6] = 'X' self.assertTrue(board1.matching_col('X')) board2 = tictactoe.Board() board2.boardArr[1] = 'X' board2.boardArr[4] = 'X' board2.boardArr[7] = 'X' self.assertTrue(board2.matching_col('X')) board3 = tictactoe.Board() board3.boardArr[2] = 'X' board3.boardArr[5] = 'X' board3.boardArr[8] = 'X' self.assertTrue(board3.matching_col('X')) board_no_match = tictactoe.Board() self.assertFalse(board_no_match.matching_col('X')) self.assertFalse(board_no_match.matching_col('O'))
def test_NoWinnerTie(self): board = tictactoe.Board() win = Win_Checker() board.board[0][0] = 'X' board.board[0][1] = 'O' board.board[0][2] = 'X' board.board[1][0] = 'X' board.board[1][1] = 'O' board.board[1][2] = 'O' board.board[2][0] = 'O' board.board[2][1] = 'X' board.board[2][2] = 'O' self.assertEqual(False, win.has_winner(board.board))
def train_q(n=1000): """ Trains the RL agent """ for i in range(50): p1_strategy = strategies.QStrategy('X') p2_strategy = strategies.QStrategy('O') p1 = player.Player('X', p1_strategy) p2 = player.Player('O', p2_strategy) board = tictactoe.Board() game = rl_game.Game(p1, p2, board) game.play_many(n) p1.strategy.save_q() p2.strategy.save_q()
class Test_check_unoccupied(unittest.TestCase): board = BoardClass.Board() def test_unoccupied(self): print("Testing unoccupied") self.board.game_board[2][2] = '0' self.assertTrue(self.board.check_unoccupied(2, 2)) def test_occupied(self): print("Testing occupied") self.board.game_board[2][2] = 'X' self.assertFalse(self.board.check_unoccupied(2, 2))
class Test_check_inbounds(unittest.TestCase): game_board = BoardClass.Board() def test_both_valid_equal(self): print("Testing valid equal") self.assertTrue(self.game_board.check_inbounds(2, 2)) def test_both_valid(self): print("Testing both valid") self.assertTrue(self.game_board.check_inbounds(2, 1)) def test_both_invalid(self): print("Testing both invalid") self.assertFalse(self.game_board.check_inbounds(4, 4)) def test_one_valid_one_invalid(self): print("Testing valid and invalid") self.assertFalse(self.game_board.check_inbounds(4, 1))
def test_space_marked(self): board = tictactoe.Board() board.mark_square(0, 'X') self.assertEqual(board.boardArr[0], "X") board.mark_square(1, 'X') self.assertEqual(board.boardArr[1], "X") board.mark_square(2, 'X') self.assertEqual(board.boardArr[2], "X") board.mark_square(3, 'X') self.assertEqual(board.boardArr[3], "X") board.mark_square(4, 'X') self.assertEqual(board.boardArr[4], "X") board.mark_square(5, 'X') self.assertEqual(board.boardArr[5], "X") board.mark_square(6, 'X') self.assertEqual(board.boardArr[6], "X") board.mark_square(7, 'X') self.assertEqual(board.boardArr[7], "X") board.mark_square(8, 'X') self.assertEqual(board.boardArr[8], "X")
def human_v_q(human_player=1): """ Allows human to play versus q-learning agent """ if human_player == 1: p1_strategy = strategies.Human() p2_strategy = strategies.QStrategy('O') else: human_player = 2 p1_strategy = strategies.QStrategy('X') p2_strategy = strategies.Human() p1 = player.Player('X', p1_strategy) p2 = player.Player('O', p2_strategy) board = tictactoe.Board() message = 'Welcome to tic tac toe!\n'+\ 'You are playing against a random opponent and you are player '+str(human_player)+'.\n'+\ 'To make a move, enter the number of the square which you would like to play, labelled as:\n'+\ '1, 2, 3\n4, 5, 6\n7, 8, 9\n\n' print(message) game = rl_game.Game(p1, p2, board) game.play_one()
def play_training_game(network, c_puct, iterations, display=False): initial_state = game.Board() current_node = MCTS_node(initial_state, network) draft_training_data = [] while current_node.state.is_end() == False: for iter in range(iterations): MCTS_tree_search(current_node, network, c_puct) improved_policy = {} for move in current_node.moves: improved_policy[move] = float( current_node.N[move]) / (current_node.visits - 1) draft_training_data.append((current_node.state, improved_policy, 0)) move = get_move(improved_policy) current_node = current_node.children[move] if display: print("\n\nMOVING FORWARD") current_node.state.print_board() print("Network value estimate:%s" % current_node.network_value) game.print_policy(improved_policy) draft_training_data.append((current_node.state, {(0, 0): 1}, 0)) reward = current_node.state.value() training_data = [] for (state, policy, draft_reward) in draft_training_data: training_data.append((state, policy, reward)) reward = -reward #training_data[-1][0].print_board() #print(training_data[-1][2]) #raw_input() return training_data
class Test_check_valid_move(unittest.TestCase): board = BoardClass.Board() def test_valid_move(self): print("Testing valid") testMove = MoveClass.move(2, 2, 'X') self.assertTrue(self.board.check_valid_move(testMove)) def test_valid_occupied(self): print("Testing valid occupied") self.board.game_board[2][2] = 'X' testMove = MoveClass.move(2, 2, 'O') self.assertFalse(self.board.check_valid_move(testMove)) def test_invalid(self): print("Testing invalid") testMove = MoveClass.move(3, 3, 'O') self.assertFalse(self.board.check_valid_move(testMove))
def test_check_diag_left_diag_winner_player1(self): board = tictactoe.Board() board.board = [['X', 0, 0], [0, 'X', 0], [0, 0, 'X']] winner = board.check_diag(board.board, 3) self.assertEqual(winner, board.player1)
def test_column_no_winner(self): board = tictactoe.Board() board.board = [['X', 0, 0], ['O', 0, 0], ['X', 0, 0]] winner = board.winner_in_column() self.assertIsNone(winner)