def generate(num_games=25, num_rollouts=50, choose_method=None):

    if choose_method is None:
        choose_method = mcts.puct

    data = []
    for game in range(num_games):

        state = ttt.initial_state()
        for turn in it.count():
            print("game %d, turn %d..." % (game, turn))

            # Stop when game is over
            if state.is_leaf_leaf(): break

            # Act immediately if only one action available
            valid_actions = state.valid_actions()
            if len(valid_actions) == 1:
                state = state.perform_extra(valid_actions[0])
                continue

            # Otherwise, use MCTS
            node, a = mcts.mcts(state,
                                num_rollouts,
                                choose_method=choose_method)
            state = node.children()[a][0].state

            # Add child states and their values to the data
            Q = node.get_score_estimates()
            for c, (child, _) in enumerate(node.children()):
                data.append((child.state, Q[c]))

    return data
Esempio n. 2
0
def test_available_actions_after_move(coord, marker):
    init = ttt.initial_state()
    x, y = coord
    init[x][y] = marker
    actions = ttt.actions(init)
    assert len(actions) == 8
    assert (x, y) not in actions
Esempio n. 3
0
    def test_result_initial_state(self):
        board = initial_state()
        action = (0, 0)
        expected = [[X, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY],
                    [EMPTY, EMPTY, EMPTY]]

        self.assertEqual(result(board, action), expected)
Esempio n. 4
0
    def test_board_alternate(self):
        board = initial_state()
        value = X

        # Fill the board testing the alternation of turns
        for row in board:
            for i, cell in enumerate(row):
                row[i] = value
                value = X if value == O else O
Esempio n. 5
0
    def test_result(self):
        board = initial_state()
        result_board = [[EMPTY, EMPTY, EMPTY], [EMPTY, X, EMPTY],
                        [EMPTY, EMPTY, EMPTY]]
        self.assertEqual(result(board, (1, 1)), result_board)

        board = result_board
        result_board = [[EMPTY, EMPTY, EMPTY], [EMPTY, X, O],
                        [EMPTY, EMPTY, EMPTY]]
        self.assertEqual(result(board, (1, 2)), result_board)
Esempio n. 6
0
def test_board_move(coord):
    init = ttt.initial_state()
    new_state = ttt.result(init, coord)

    xs, os, empty = ttt._coords(new_state)
    assert len(xs) == 1
    assert len(os) == 0
    assert len(empty) == 8

    assert ttt._is_empty(init), "initial board unmodified"
Esempio n. 7
0
    def test_actions_alternate(self):
        test_set = set(self.full_set)
        board = initial_state()
        value = X

        for i, row in enumerate(board):
            for j, cell in enumerate(row):
                board[i][j] = value
                value = X if value == O else O
                test_set.remove((i, j))
                self.assertEqual(actions(board), test_set)
Esempio n. 8
0
def test_winner(marker, winner_move_idx):
    init = ttt.initial_state()
    assert ttt.winner(init) is None
    assert not ttt.terminal(init)

    # perform the winning move
    for i, j in ttt._winner_moves()[winner_move_idx]:
        init[i][j] = marker

    # and win!
    assert ttt.winner(init) is marker
    assert ttt.terminal(init)
Esempio n. 9
0
from tictactoe import initial_state, player, actions, result, winner, minimax

board = result(initial_state(), (0, 0))

print(minimax(board))
Esempio n. 10
0
def test_o_moves_after_any_x(coord):
    init = ttt.initial_state()
    x, y = coord
    init[x][y] = ttt.X
    assert ttt.player(init) == ttt.O
Esempio n. 11
0
def test_all_available_actions():
    init = ttt.initial_state()
    actions = ttt.actions(init)
    assert len(actions) == 9
Esempio n. 12
0
def test_board_is_not_empty(coord, marker):
    init = ttt.initial_state()
    x, y = coord
    init[x][y] = marker
    assert not ttt._is_empty(init)
Esempio n. 13
0
def test_x_moves_first():
    init = ttt.initial_state()
    assert ttt.player(init) == ttt.X
Esempio n. 14
0
from tictactoe import initial_state, player, actions, result, winner, terminal, utility, minimax
#import math

print('initial state = ', initial_state())
board = initial_state()
# board[0][0] = 'X'
# board[0][1] = 'O'
# board[1][0] = 'X'
# print('board = ', board)
# print('player = ', player(board))
# player = player(board)
# print('actions = ', actions(board))
# print('result = ', result(board, (2,0), player))
print('winner: ', winner(board))
print('terminal?: ', terminal(board))
# print('utility: ', utility(board))
# print('minimax: ', minimax(board))
#print(-math.inf < 0)
# print(float('-inf') < 0)
Esempio n. 15
0
def test_board_is_empty():
    init = ttt.initial_state()
    assert ttt._is_empty(init)
    b_size = int(
        input(
            'Please input the board_size, which in our Tictactoe game represent the obstacles in the board mark as P:(0-4) \n'
        ))
    game_times = int(input('Please input the number of games:\n'))

    net = tn.TictactoeNet(board_size)
    net.load_state_dict(tr.load("model%d.pth" % b_size))

    mode = int(
        input(
            'Please input\n1 for NN-Mcts-AI v.s Mcts AI:\n2 for NN-Mcts-AI v.s human player:\n'
        ))
    if mode == 2:
        state = tt.initial_state(b_size)
        for step in it.count():
            print(state.print_func())
            print("Step %d" % step)

            # Stop when game is over
            if state.is_leaf_leaf(): break

            # Act immediately if only one action available
            valid_actions = state.valid_actions()
            if len(valid_actions) == 1:
                state = state.perform_extra(valid_actions[0])
                continue

            # Otherwise, if it is the NN AI's turn (max), run NN_MCTS to decide its action
            if state.is_max_players_turn():
Esempio n. 17
0
import tictactoe as ttt

print(str(ttt.actions(ttt.initial_state())))

print(str(ttt.minimax(ttt.initial_state())))
Esempio n. 18
0
def test_flatten_board():
    init = ttt.initial_state()
    flat = ttt._flatten_board(init)
    assert isinstance(flat, list)
    assert len(flat) == 9
Esempio n. 19
0
 def test_player_initial_state(self):
     self.assertEqual(player(initial_state()), X)
Esempio n. 20
0
    def test_result_input_board_unmodified(self):
        board = initial_state()
        action = (0, 0)

        result(board, action)
        self.assertEqual(board, initial_state())
Esempio n. 21
0
    def test_actions_initial_state(self):
        expected = {(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0),
                    (2, 1), (2, 2)}

        self.assertEqual(actions(initial_state()), expected)
Esempio n. 22
0
 def test_terminal_initial_state(self):
     self.assertEqual(terminal(initial_state()), False)
Esempio n. 23
0
 def test_player_empty_board(self):
     self.assertEqual(player(initial_state()), X)
Esempio n. 24
0
from tictactoe import actions, terminal, utility, winner, player, initial_state

board = initial_state()

utility(board)
Esempio n. 25
0
pygame.init()
size = width, height = 600, 400

# Colors
black = (0, 0, 0)
white = (255, 255, 255)

screen = pygame.display.set_mode(size)

mediumFont = pygame.font.Font("OpenSans-Regular.ttf", 28)
largeFont = pygame.font.Font("OpenSans-Regular.ttf", 40)
moveFont = pygame.font.Font("OpenSans-Regular.ttf", 60)

user = None
board = tct.initial_state()
ai_turn = False

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(black)

    # Let user choose a player.
    if user is None:

        # Draw title
        title = largeFont.render("Play Tic-Tac-Toe", True, white)
Esempio n. 26
0
 def test_winner_initial_state(self):
     board = initial_state()
     self.assertEqual(winner(board), None)
Esempio n. 27
0
 def test_actions_method_empty_board(self):
     actions_to_do = actions(initial_state())
     for row in range(3):
         for col in range(3):
             self.assertTrue((row, col) in actions_to_do)
Esempio n. 28
0
import tictactoe as t

board = t.initial_state()

board[0][2] = t.X
board[1][1] = t.X
board[2][0] = t.X

print(t.utility(board))
print(t.terminal(board))
print(board)
for row in board:
	print(row.count(t.O))
print (t.player(board))
print(t.actions(board))

for a in t.actions(board):
	print(t.result(board,a))
Esempio n. 29
0
pygame.init()
size = width, height = 600, 400

# Colors
black = (0, 0, 0)
white = (255, 255, 255)

screen = pygame.display.set_mode(size)

mediumFont = pygame.font.Font("assets/OpenSans-Regular.ttf", 28)
largeFont = pygame.font.Font("assets/OpenSans-Regular.ttf", 40)
moveFont = pygame.font.Font("assets/OpenSans-Regular.ttf", 60)

user = None
board = ttt.initial_state()
ai_turn = False

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(black)

    # Let user choose a player.
    if user is None:

        # Draw title
        title = largeFont.render("Play Tic-Tac-Toe", True, white)
Esempio n. 30
0
import tictactoe as ttt
import itertools

X = "X"
O = "O"
EMPTY = None

# board mid-game
board = ttt.initial_state()
board[0][0] = X
board[0][1] = X
board[0][2] = X
board[1][1] = O
board[2][0] = O
board[2][2] = O
board[2][1] = X

# board empty
board_empty = ttt.initial_state()

# board full
board_full = [[X, X, X], [X, X, X], [X, X, X]]

print(ttt.player(board))
print(ttt.player(board))
print(ttt.player(board))

# for line in itertools.zip_longest(rows, cols, diags, fillvalue=[]):
#      winning_line()