Example #1
0
    def game(self, player1, player2):
        players = [player1, player2]

        game = Game(self.size)
        moves = []
        index = 0

        while not game.is_terminal() and len(moves) < self.size*50:
            move = players[index](game)
            end = time.time()
            game.play_game(*move)
            moves.append(move)
            index = 1 - index

        if len(moves) == self.size*50:
            return 0
        if index == 1:
            return 1
        else:
            return 2
Example #2
0
def f(offset, max_exp):
    if max_exp == 0:
        return offset

    for i in range(max_exp + 1):
        n_playout = offset + 2**i
        print(f"Number of playouts {n_playout}")

        draws = 0
        wins = [0, 0]

        player1 = MCTSPlayer(n_playout=n_playout, win_score=100)
        player2 = Negamax(depth=depth, tt=tt)
        players = [player1, player2]
        game = Game(9)
        while not game.is_terminal():
            move = players[game.index](game)
            game.make_move(move)

        end, player = game.game_end()
        if end:
            wins[player] += 1
            if player == 0:
                return min(n_playout, f(offset + 2**(i - 1) + 1, i - 1))
    return 10000000
Example #3
0
def create_players(parser):
    commands = parser.add_subparsers(title='sub-commands')
    parser.add_argument('--size', type=int)
    parser.add_argument('--wall_dist', type=str)

    for i in range(2):
        sub_parser = commands.add_parser(f'player{i+1}')
        sub_parser.add_argument('--type')
        sub_parser.add_argument('--depth', type=int)
        sub_parser.add_argument('--iterations', type=int)
        sub_parser.add_argument('--time_limit', type=int)
        sub_parser.add_argument('--tt', type=bool)
        sub_parser.add_argument('--n_playout', type=int)

    args = parse_args(parser, commands)
    players = [None] * 2
    args = vars(args)

    for i in range(2):
        args_player = args[f'player{i+1}']
        args_player = vars(args_player)
        player_type = args_player.pop("type")
        args_player = {k: v for k, v in args_player.items() if v is not None}
        if "tt" in args_player:
            args_player.pop("tt")
            player = players_dict[player_type](win_score=1000,
                                               tt=TT(),
                                               **args_player)
        else:
            player = players_dict[player_type](win_score=1000, **args_player)

        players[i] = player

    wall_dist = [int(x) for x in args['wall_dist'].split(",")
                 ] if args['wall_dist'] else [100, 100]
    return Game(args['size'], wall_dist=wall_dist), players
Example #4
0

for depth1 in [1,2,3,4,5]:
    for depth2 in [1,2,3,4,5]:
        white_wins = 0
        draws = 0
        curr_time = time.time()

        player1 = players_list_no_shuffle[depth1 - 1]
        player2 = players_list_yes_shuffle[depth2 - 1]

        players = [player1, player2]

        for _ in range(NUM_GAMES_EACH):

            game = Game(SIZE, wall_scores=[WALL_SCORE, WALL_SCORE], wall_counts=[SIZE, SIZE + 1])
            moves = []
            index = 0

            while not game.is_terminal() and len(moves) < SIZE*15:
                move = players[index](game)
                game.play_game(*move)
                moves.append(move)
                index = 1 - index

            if len(moves) == SIZE*15:
                draws += 1
            elif index == 1:
                white_wins += 1

        print(f"board size is {SIZE} and player1 depth is {depth1} and player2 depth is {depth2}")
Example #5
0
DEPTH = 1
SIZE = 5

player = create.Human()
pop = Population(DEPTH, SIZE)
#gene = [1.53276322, -0.31481973, 0.91032865, 0.10268689, 0.41397708, 0.93256447, 0.067119]
#gene = [2.17708616, 0.14143157, -0.39465167, 2.45134455, -1.29809957, 1.35924934, 0.1645196]
#gene = [ 2.17708616, 0.14143157, -0.39465167, 2.45134455, -1.29809957, -0.00416553, -0.38419192]
# gene = [2.17708616, 0.44166281, -0.39465167, 0.65901557, -1.29809957, -0.00416553, -0.38419192]
#gene = [ 2.17708616, 0.14143157, -0.39465167, 0.70410026, -1.29809957, -0.00416553, -0.38419192]
gene = [
    2.17708616, -1.0604653, -1.29051458, 0.70410026, -1.29809957, -0.00416553,
    -0.38419192
]
negamax_good = Negamax(depth=DEPTH, scoring=pop.scoring(gene), tt=TT())
gene = [1, 0, 0, 0, 0, 0, 0]
negamax_bad = Negamax(depth=3, scoring=pop.scoring(gene), tt=TT())


def main(size, game, players):
    root = tk.Tk()
    GUI(root, game, players)
    root.mainloop()


if __name__ == '__main__':
    game = Game(SIZE)
    #main(SIZE, game, [negamax_good, negamax_bad])
    main(SIZE, game, [Negamax(depth=1, tt=TT()), Negamax(depth=3, tt=TT())])
Example #6
0
parser.add_argument("id")
parser.add_argument("time")
args = parser.parse_args()

saved_game = json.load(open(f"{args.id}.json", 'r'))

size = saved_game['size']
moves = saved_game['moves']
players = saved_game['players']
date = datetime.strptime(args.id, "%d-%m-%Y-%H-%M-%S")

print(
    f"Playing saved game played of date {date} between {players[0]} and {players[1]}..."
)

game = Game(size)
root = tk.Tk()

gui = GUI(root, game, [None, None])


def play_move():
    if moves == []:
        return

    move = moves.pop(0)
    game.make_move(move)
    gui.refresh()

    root.after(int(args.time) * 1000, play_move)
Example #7
0
        players_list.append(Negamax(depth, tt=tt_loaded))
        print("loaded")
    except Exception as x:
        print(f"i dont have {wall_scores[i]} in memory")
        players_list.append(Negamax(depth, tt=TT()))

start_time = time.time()
#score_func consideres both player1 and player2!!! not only one player
for i1, player1 in enumerate(players_list):
    for i2, player2 in enumerate(players_list):
        if player1 == player2:
            continue
        curr_time = time.time()
        players = [players_list[i1], players_list[i2]]
        for j in range(NUM_GAMES_EACH):
            game = Game(SIZE, wall_scores=[wall_scores[i1], wall_scores[i2]])
            moves = []
            index = 0

            while not game.is_terminal() and len(moves) < SIZE*15:
                move = players[index](game)
                game.play_game(*move)
                moves.append(move)
                index = 1 - index

            if index == 1:
                player_scores[i1] += 1
            else:
                player_scores[i2] += 1
        print(f"finished {NUM_GAMES_EACH} games between player{i1} and player{i2}")
        print(f"That took {time.time() - curr_time} seconds")
Example #8
0
    #     print("loaded")
    # except Exception as x:
    #     print(f"i dont have depth {i + 1} in memory")
    players_list.append(Negamax(i + 1, tt=TT()))


print("NO WALL RESTRICTION!!! DONT USE TT ")
for size in [5]:
    for depth1 in depths[size]:
        for depth2 in depths[size]:
                wins_white = 0
                draws = 0
                for i in range(TOTAL_GAMES):
                    players = [players_list[depth1 - 1], players_list[depth2 - 1]]

                    game = Game(size, wall_dist=[100, 100])
                    moves = []
                    index = 0

                    while not game.is_terminal() and len(moves) < size*50:
                        move = players[index](game)
                        end = time.time()
                        game.play_game(*move)
                        moves.append(move)
                        index = 1 - index

                    if len(moves) == size*50:
                        draws += 1
                        table1[depth1 - 1][depth2 - 1] = 0
                    elif index == 1:
                        table1[depth1 - 1][depth2 - 1] = 1