-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
46 lines (38 loc) · 1.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import random
from game import Game, Move, Player
from stable_baselines3 import PPO
from utils import decode_action
class RandomPlayer(Player):
def __init__(self) -> None:
super().__init__()
def make_move(self, game: 'Game') -> tuple[tuple[int, int], Move]:
from_pos = (random.randint(0, 4), random.randint(0, 4))
move = random.choice([Move.TOP, Move.BOTTOM, Move.LEFT, Move.RIGHT])
return from_pos, move
class MyPlayer(Player):
def __init__(self, path) -> None:
super().__init__()
self.path = path
self.model = PPO.load(path)
def make_move(self, game: 'Game') -> tuple[tuple[int, int], Move]:
action, _ = self.model.predict(game.get_board())
pos, slid = decode_action(action)
while not game.move(pos, slid, 0):
action, _ = self.model.predict(game.get_board())
pos, slid = decode_action(action)
# print("Invalid move!!")
return decode_action(action)
if __name__ == '__main__':
g = Game()
g.print()
path = './/old_results//quixo_ppo_random_opponent_longest2'
player1 = MyPlayer(path=path)
player2 = MyPlayer(path=path)
counter = 0
for i in range(1000):
winner = g.play(player1, player2)
if winner == 0:
counter += 1
print(f"Player 1 won {counter} times out of 1000 games.")
g.print()
print(f"Winner: Player {winner}")