Beispiel #1
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 #2
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 #3
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 #4
0
def test_against_random(model, size=100):
    '''
    Evaluate the model against random performance
    '''
    
    wins = {0: 0, 1: 0, 2: 0}
    
    for _ in range(size):
        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:
                r_board = game.get_board_raw()
                x, y, _ = evaluate(model, game, pid, tau=1)

            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 #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
model = DQN()

games = 0
gamma = 1.0

EPOCH = 100
TEST_FRQ = 100 * EPOCH
win_rate = []
win_rate.append(test_against_random(model))

while True:
    games += 1

    #play game
    game = TicTacToe()

    boards = {1: [], 2: []}

    pid = 1
    opp_pid = 2
    winner = None
    while winner is None:
        
        board = game.get_board(pid)

        if random.random() < gamma:
            x, y = random_choice(board)
        else:
            x, y = q_select(board, model)
Beispiel #10
0
    for j in range(len(probs[2])):
        total = np.concatenate((total, dict_list[2][j]))
    
    return total

if __name__ == '__main__': 
    
    model = Zero()
    EPOCH = 300
    ITER = 30

    for it in range(ITER):
        for i in range(EPOCH):
            print('{}: {}/{}'.format(it, i, EPOCH), end='\r')
            game = TicTacToe(3)
            
            pid = np.random.random_integers(low=1, high=2, size=1)[0]
            winner = None

            inputs = {1: [], 2: []}
            probs = {1 : [], 2: []}
            while winner is None:

                board = game.get_board(pid)

                r_board = game.get_board_raw()
                x, y, prob = evaluate(model, game, (pid % 2) + 1)

                inputs[pid].append(copy.copy(game.get_input(pid)))
                probs[pid].append(prob)
from ttt import TicTacToe,AutoPlayer,HumanPlayer
from pprint import pprint
import operator



game = TicTacToe()

a1 = AutoPlayer(symb=1,greedy=True)
a2 = AutoPlayer(symb=2,epsilon=0.2,step_size=0.4)
h1 = HumanPlayer(symb=1)

game.play(a1,a2,iter=40000)

a1.setGreedy(False)
a1.setEpsilon(0.1)
a2.setStepSize(0.2)
game.play(a1,a2,iter=40000)

a1.setGreedy(True)
a2.setStepSize(0.1)
game.play(a1,a2,iter=40000)

#print('---- LIVE GAME ------')
game.reset_board()
a2.setGreedy(True)
game.play(h1, a2, iter=1, stats=False, printBoard=True)
Beispiel #12
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 #13
0
def ttt():
    playerX = Player('X')
    playerO = Player('O')
    board = Board()
    ttt = TicTacToe(board, playerX, playerO)
    return ttt