Beispiel #1
0
def test_against_random(model):
    '''
    Evaluate the model against random performance
    '''

    wins = {0: 0, 1: 0, 2: 0}

    for _ in range(1000):
        game = TicTacToe(3)

        pid = np.random.random_integers(low=1, high=2, size=1)[0]
        winner = None
        while winner is None:

            board = game.get_board(pid)

            if pid == 1:
                x, y = random_choice(board)
            else:
                x, y = q_select(pid, board, model, game)

            game.place(pid, x, y)

            winner = game.check_win()

            pid = (pid % 2) + 1

        wins[winner] += 1

    print('Wins: %d Ties: %d Losses: %d' % (wins[2], wins[0], wins[1]))
    return (wins[2] / (wins[0] + wins[1] + wins[2]))
Beispiel #2
0
    def do_ttt(self, inp):
        """ /ttt <username> <x> <y> """
        inp = inp.split(' ')
        try:
            clientname = inp.pop(0)
            client = clients[clientname]
            if not client:
                self.send_msg("user not connected", [self])
                return
            x = int(inp.pop(0))
            y = int(inp.pop(0))
            if not (  0 <= x <= 2 and  0 <= x <= 2):
                self.send_msg("wrong position", [self])
                return
        except:
            self.send_msg("wrong entry", [self])

        # init game
        if self.ttt is None or self.ttt[1] != client:
            game = TicTacToe()
            self.ttt = (game, client)
            client.ttt = (game, self)
        else:
            game = self.ttt[0]

        if game.play(x, y):
            self.send_msg("\n" + game.__str__(), [client, self])
            if game.winner is not None:
                self.send_msg(f"{self.username} won", [client, self])
                self.ttt = None
            else:
                self.send_msg(f"[TTT] do /ttt {self.username} <x> <y>", [client])
        else:
            self.send_msg(f"Invalid pos {x} {y} \n" + game.__str__(), [self])
Beispiel #3
0
def debug_run(model):
    '''
    Shows the board and value for each step in a game
    '''
    game = TicTacToe(3)

    pid = np.random.random_integers(low=1, high=2, size=1)[0]
    winner = None
    while winner is None:

        board = game.get_board(pid)

        val = model.evaluate(game.get_input(pid))
        print(board)
        print(val)

        x, y = q_select(pid, board, model, game)

        game.place(pid, x, y)
        winner = game.check_win()

        pid = (pid % 2) + 1

    print(
        model.sess.run(model.probs,
                       feed_dict={model.states: game.get_input(pid)})[0])
Beispiel #4
0
def play_user(model):
    '''
    Test the model against human skill level
    '''
    game = TicTacToe(3)
        
    pid = np.random.random_integers(low=1, high=2, size=1)[0]
    winner = None

    while winner is None:

        board = game.get_board(pid)
        print(board)
        
        if pid == 2:
            x, y, prob = evaluate(model, game, pid, tau=.1)
            print(prob)
            print(model.evaluate(game.get_input(pid)))
        else:
            x = int(input('x: '))
            y = int(input('y: '))

        game.place(pid, x, y)
        winner = game.check_win()

        pid = (pid % 2) + 1

    print(game.get_input(1))
Beispiel #5
0
def chess_worker(connection, args):

    while True:

        game = TicTacToe()

        rewards = []

        pid = 1
        boards = [copy.deepcopy(game)]
        i = 0

        while game.check_win() is None:

            if random.random() > args.gamma:
                connection.send({'type': 'board', 'boards': boards[-8:]})
                move = connection.recv()
            else:
                move = random.choice(game.legal_moves)

            game = game.push(move)
            boards.append(copy.deepcopy(game))

            pid = (pid % 2 + 1)
            i += 1

        res = game.check_win()
        size = len(boards) - 1

        if res != 0:
            rewards = [1 if (i % 2) == 0 else 0 for i in range(size)]
            rewards = rewards[::-1]
        else:
            rewards = [0] * (size)

        rewards = np.expand_dims(np.array(rewards), 1)
        in_boards, in_targets = build_input(boards, rewards, args.history)

        connection.send({
            'type': 'data',
            'inp': in_boards,
            'rewards': in_targets
        })
        connection.send({'type': 'end'})
Beispiel #6
0
def debug_run(model):
    '''
    Shows the board and value for each step in a game
    '''
    game = TicTacToe()
        
    pid = np.random.random_integers(low=1, high=2, size=1)[0]
    winner = None
    while winner is None:

        board = game.get_board(pid)

        val = model.evaluate(board.reshape(1, 3, 3))
        print(board)
        print(val)

        x, y = q_select(board, model)

        game.place(pid, x, y)
        winner = game.check_win()

        pid = (pid % 2) + 1
Beispiel #7
0
def play_user(model):
    '''
    Test the model against human skill level
    '''
    game = TicTacToe()

    pid = np.random.random_integers(low=1, high=2, size=1)[0]
    winner = None
    while winner is None:

        board = game.get_board(pid)
        val = model.evaluate(board.reshape(1, 3, 3))
        print(board)

        if pid == 2:
            x, y = q_select(pid, board, model, game)
        else:
            x = int(input('x: '))
            y = int(input('y: '))

        game.place(pid, x, y)
        winner = game.check_win()

        pid = (pid % 2) + 1
Beispiel #8
0
def computer_move():
    best_move, best_value = None, float("-inf")
    for move in game.get_empty_cells():
        game_t = dc(game)
        game_t.act(move)
        value = minimax(game_t, False)

        if value > best_value:
            best_value = value
            best_move = move

    game.act(best_move)


if __name__ == "__main__":
    game = TicTacToe()

    choice = input("Do you want to first (y/Y)? ")
    go_first = choice == "y" or choice == "Y"

    while not game.is_gameover():
        if go_first:
            if game.get_turn() == 1:
                player_move()
            else:
                computer_move()
        else:
            if game.get_turn() == 1:
                computer_move()
            else:
                player_move()
Beispiel #9
0
 def __init__(self, board, last_turn):
     self.board = [TicTacToe(grid) for grid in board]
     self.winner = None  # None as nobody won
     self.last_turn = last_turn  # None because nobody played before
     self.previous_move = None  # None on the start of the game
Beispiel #10
0
def ttt():
    playerX = Player('X')
    playerO = Player('O')
    board = Board()
    ttt = TicTacToe(board, playerX, playerO)
    return ttt