Ejemplo n.º 1
0
    def test_update_scores_win_diagonal(self):
        scores = [[3, -3, 5], [-2, 7, 0], [4, -1, 3]]
        board = TicTacToeBoard(3, False, None)
        board.board = [[0, 2, 1], [0, 1, 2], [1, 2, 1]]
        mc_update_scores(scores, board, PLAYERX)

        self.assertEqual(scores, [[3, -4, 6], [-2, 8, -1], [5, -2, 4]])
Ejemplo n.º 2
0
    def test_update_scores_tie(self):
        scores = [[4, -3, 5], [-2, 7, -3], [4, -1, 3]]
        board = TicTacToeBoard(3, False, None)
        board.board = [[2, 2, 1], [1, 1, 2], [2, 1, 1]]
        mc_update_scores(scores, board, PLAYERO)

        self.assertEqual(scores, [[4, -3, 5], [-2, 7, -3], [4, -1, 3]])
Ejemplo n.º 3
0
    def test(self):
        tictactoe_board = TicTacToeBoard(3, False, None)
        self.assertEqual(tictactoe_board.board,
                         [[0, 0, 0], [0, 0, 0], [0, 0, 0]])

        tictactoe_board = TicTacToeBoard(5, False, None)
        self.assertEqual(tictactoe_board.board,
                         [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                          [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])
Ejemplo n.º 4
0
    def test(self):
        board = TicTacToeBoard(3, False, None)
        original_state = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

        self.assertEqual(board.board, original_state)

        mc_trial(board, PLAYERX)
        
        self.assertNotEqual(board.board, original_state)
        self.assertNotEqual(board.evaluate_win_status(), None)
Ejemplo n.º 5
0
    def test_empty_board(self):
        scores = [[1, 2, 3], [7, 8, 9], [4, 5, 6]]
        board = TicTacToeBoard(3, False, None)
        board.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

        self.assertEqual(get_best_move(board, scores), (1, 2))

        scores = [[1, 2, 10], [7, 8, 9], [4, 5, 6]]
        board.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

        self.assertEqual(get_best_move(board, scores), (0, 2))
Ejemplo n.º 6
0
    def test(self):
        tictactoe_board = TicTacToeBoard(4, False, None)
        tictactoe_board.board[0][0] = 1
        tictactoe_board.board[1][2] = 2
        tictactoe_board.board[0][1] = 1
        tictactoe_board.board[2][2] = 2
        tictactoe_board.board[3][1] = 1
        tictactoe_board.board[2][3] = 2

        self.assertEqual(tictactoe_board.uleft_bright_diagonal(), [1, 0, 2, 0])
        self.assertEqual(tictactoe_board.bleft_uright_diagonal(), [0, 0, 2, 0])
Ejemplo n.º 7
0
    def test(self):
        tictactoe_board = TicTacToeBoard(4, False, None)
        tictactoe_board.move(0, 0, PLAYERX)
        tictactoe_board.move(3, 0, PLAYERX)
        tictactoe_board.move(1, 2, PLAYERO)
        tictactoe_board.move(1, 3, PLAYERO)
        tictactoe_board.move(1, 3, PLAYERX)

        self.assertEqual(tictactoe_board.square(0, 0), PLAYERX)
        self.assertEqual(tictactoe_board.square(3, 0), PLAYERX)
        self.assertEqual(tictactoe_board.square(1, 2), PLAYERO)
        self.assertEqual(tictactoe_board.square(1, 3), PLAYERO)
Ejemplo n.º 8
0
    def test_single_option(self):
        scores = [[3, -3, 5], [-2, 7, 0], [4, -1, 3]]
        board = TicTacToeBoard(3, False, None)
        board.board = [[0, 2, 1], [0, 1, 2], [1, 2, 1]]

        self.assertEqual(get_best_move(board, scores), (0, 0))

        board.board = [[0, 2, 1], [0, 0, 2], [1, 2, 1]]

        self.assertEqual(get_best_move(board, scores), (1, 1))

        board.board = [[2, 0, 1], [2, 1, 2], [1, 0, 1]]

        self.assertEqual(get_best_move(board, scores), (2, 1))
Ejemplo n.º 9
0
    def test(self):
        tictactoe_board = TicTacToeBoard(4, False, None)
        tictactoe_board.board[0][0] = 1
        tictactoe_board.board[1][2] = 2
        tictactoe_board.board[0][1] = 1
        tictactoe_board.board[2][2] = 2
        tictactoe_board.board[3][1] = 1
        tictactoe_board.board[2][3] = 2

        self.assertEqual(
            tictactoe_board.column_grid(),
            [[1, 0, 0, 0], [1, 0, 0, 1], [0, 2, 2, 0], [0, 0, 2, 0]])
Ejemplo n.º 10
0
    def test(self):
        board = TicTacToeBoard(3, False, None)
        index_grid = [[(row, col) for col in range(board.dimension)]
                      for row in range(board.dimension)]
        index_list = []
        for l_list in index_grid:
            index_list += l_list

        self.assertIsInstance(mc_move(board, PLAYERO, 30), tuple)
        self.assertIn(mc_move(board, PLAYERO, 30), index_list)
Ejemplo n.º 11
0
    def test(self):
        tictactoe_board = TicTacToeBoard(4, False, None)
        tictactoe_board.board[0][0] = 1
        tictactoe_board.board[1][2] = 2

        self.assertEqual(tictactoe_board.square(0, 0), PLAYERX)
        self.assertEqual(tictactoe_board.square(1, 2), PLAYERO)
        self.assertEqual(tictactoe_board.square(0, 1), EMPTY)
Ejemplo n.º 12
0
    def test_nonempty_board(self):
        tictactoe_board = TicTacToeBoard(4, False, None)
        tictactoe_board.board[0][0] = 1
        tictactoe_board.board[1][2] = 2
        tictactoe_board.board[2][1] = 1

        self.assertEqual(tictactoe_board.get_empty_squares(), [(0, 1), (0, 2),
                                                               (0, 3), (1, 0),
                                                               (1, 1), (1, 3),
                                                               (2, 0), (2, 2),
                                                               (2, 3), (3, 0),
                                                               (3, 1), (3, 2),
                                                               (3, 3)])

        tictactoe_board.board[1][3] = 2
        tictactoe_board.board[3][0] = 2
        tictactoe_board.board[2][3] = 1

        self.assertEqual(tictactoe_board.get_empty_squares(), [(0, 1), (0, 2),
                                                               (0, 3), (1, 0),
                                                               (1, 1), (2, 0),
                                                               (2, 2), (3, 1),
                                                               (3, 2), (3, 3)])
Ejemplo n.º 13
0
"""
Runs the monte carlo simulation for specified number of times
"""

from monte_carlo_tictactoe import mc_move
from tictactoe_board import (PLAYERX, switch_player, TicTacToeBoard)

BOARD = TicTacToeBoard(3, False, None)
NUM_TRIALS = 500

PLAYER = PLAYERX
win = None

while not win:
    MOVE = mc_move(BOARD, PLAYER, NUM_TRIALS)
    BOARD.move(MOVE[0], MOVE[1], PLAYER)
    PLAYER = switch_player(PLAYER)
    print BOARD.__str__() + '\n'
    win = BOARD.evaluate_win_status()

print "win: " + str(win)
Ejemplo n.º 14
0
    def test(self):
        tictactoe_board = TicTacToeBoard(4, False, None)
        tictactoe_board.board[0][0] = 1
        tictactoe_board.board[1][2] = 2
        tictactoe_board.board[0][1] = 1

        column_grid = tictactoe_board.column_grid()
        row_grid = tictactoe_board.board

        self.assertEqual(evaluate_grid_win(column_grid), [])
        self.assertEqual(evaluate_grid_win(row_grid), [])

        tictactoe_board.board[1][1] = 1
        tictactoe_board.board[2][1] = 1
        tictactoe_board.board[2][2] = 2
        tictactoe_board.board[3][1] = 1
        tictactoe_board.board[2][3] = 2

        column_grid = tictactoe_board.column_grid()

        self.assertEqual(evaluate_grid_win(column_grid), [PLAYERX])

        tictactoe_board.board[2][1] = 2
        tictactoe_board.board[2][0] = 2

        row_grid = tictactoe_board.board

        self.assertEqual(evaluate_grid_win(row_grid), [PLAYERO])
Ejemplo n.º 15
0
    def test_empty_two_by_two(self):
        board = TicTacToeBoard(2, False, None)
        scores = [[1, 2], [7, 8]]
        board.board = [[0, 0], [0, 0]]

        self.assertEqual(get_best_move(board, scores), (1, 1))
Ejemplo n.º 16
0
 def test(self):
     tictactoe_board = TicTacToeBoard(3, False, None)
     self.assertEqual(
         tictactoe_board.__str__(),
         "   |   |  \n-----------\n   |   |  \n-----------\n   |   |  ")
Ejemplo n.º 17
0
    def test_no_options(self):
        scores = [[3, -3, 5], [-2, 7, 0], [4, -1, 3]]
        board = TicTacToeBoard(3, False, None)
        board.board = [[1, 2, 1], [2, 1, 2], [1, 2, 1]]

        self.assertEqual(get_best_move(board, scores), None)
Ejemplo n.º 18
0
    def test(self):
        tictactoe_board = TicTacToeBoard(4, False, None)
        tictactoe_board.board[0][0] = 1
        tictactoe_board.board[1][2] = 2
        tictactoe_board.board[0][1] = 1

        uleft_bright = tictactoe_board.uleft_bright_diagonal()
        bleft_uright = tictactoe_board.bleft_uright_diagonal()

        self.assertEqual(evaluate_diagonal_win(uleft_bright), [])
        self.assertEqual(evaluate_diagonal_win(bleft_uright), [])

        tictactoe_board.board[1][1] = 1
        tictactoe_board.board[2][1] = 1
        tictactoe_board.board[2][2] = 1
        tictactoe_board.board[3][3] = 1

        uleft_bright = tictactoe_board.uleft_bright_diagonal()

        self.assertEqual(evaluate_diagonal_win(uleft_bright), [PLAYERX])

        tictactoe_board.board[2][1] = 2
        tictactoe_board.board[3][0] = 2
        tictactoe_board.board[0][3] = 2

        bleft_uright = tictactoe_board.bleft_uright_diagonal()

        self.assertEqual(evaluate_diagonal_win(bleft_uright), [PLAYERO])
Ejemplo n.º 19
0
    def test_multiple_options(self):
        scores = [[3, -3, 5], [-2, 7, 0], [4, -1, 3]]
        board = TicTacToeBoard(3, False, None)
        board.board = [[0, 2, 1], [0, 1, 2], [1, 0, 0]]

        self.assertIn(get_best_move(board, scores), ((0, 0), (2, 2)))
Ejemplo n.º 20
0
    def test_empty_board(self):
        tictactoe_board = TicTacToeBoard(3, False, None)
        tictactoe_board.board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

        self.assertEqual(tictactoe_board.evaluate_win_status(), None)
Ejemplo n.º 21
0
    def test(self):
        board = TicTacToeBoard(3, False, None)
        board.board = [[2, 2, 1], [2, 1, 2], [2, 1, 1]]

        self.assertEqual(get_score_values(board, PLAYERO), (1, -1))
        self.assertEqual(get_score_values(board, PLAYERX), (-1, 1))
Ejemplo n.º 22
0
    def test_winning_bleft_diagonal(self):
        tictactoe_board = TicTacToeBoard(3, False, None)
        tictactoe_board.board = [[0, 0, 2], [0, 2, 0], [2, 1, 1]]

        self.assertEqual(tictactoe_board.evaluate_win_status(), PLAYERO)
Ejemplo n.º 23
0
    def test_tie(self):
        tictactoe_board = TicTacToeBoard(3, False, None)
        tictactoe_board.board = [[1, 1, 2], [2, 2, 1], [1, 2, 1]]

        self.assertEqual(tictactoe_board.evaluate_win_status(), DRAW)
Ejemplo n.º 24
0
 def test(self):
     board = TicTacToeBoard(5, False, None)
     clone = board.clone()
     self.assertNotEqual(clone, board)
Ejemplo n.º 25
0
 def test(self):
     tictactoe_board = TicTacToeBoard(5, False, None)
     tictactoe_board.dimension = 3
     new_board = tictactoe_board.new_board()
     self.assertEqual(new_board, [[0, 0, 0], [0, 0, 0], [0, 0, 0]])
Ejemplo n.º 26
0
    def test_empty_board(self):
        tictactoe_board = TicTacToeBoard(2, False, None)

        self.assertEqual(tictactoe_board.get_empty_squares(), [(0, 0), (0, 1),
                                                               (1, 0), (1, 1)])