-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_vs_bot.py
133 lines (106 loc) · 4.81 KB
/
play_vs_bot.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import argparse
from stable_baselines3 import PPO
from game.game_env import GameEnv
from game.players import HumanPlayer, bot_player_dict
def get_player_name():
"""
Welcome a player and get his name
"""
print("Hello and welcome to this version of the fighting phase of Rising Sun board game !")
player_name = input("\nEnter your name : ")
return player_name
def initialize_players(player_name, bot_behavior):
"""
Initialize players
:param player_name: (str) the name of the human player
:param bot_behavior: (str) the behavior of the bot
:return player: (Player) instance of the human player
:return bot_player: (Player) instance of the bot player
"""
player = HumanPlayer(name=player_name)
if bot_behavior in bot_player_dict:
bot_player = bot_player_dict[bot_behavior](name='bot_player')
elif bot_behavior == "trained":
try:
pass
except:
raise(ValueError("A trained agent with those parameters hasn't been found"))
else:
raise(ValueError("Unknown bot bahavior"))
return player, bot_player
def ask_displaying_rules():
"""
Ask the player if he wants the rules to be displayed
"""
print("\nDo you want to read the rules (Y/N) ?")
display_rules = None
while display_rules not in ["Y", "N"]:
display_rules = input()
return True if display_rules == "Y" else False
def display_rules():
"""
Display the rules if the player asked
"""
with open('utils/rules.txt', 'r') as file:
print("\n ---- RULES ----")
print(file.read())
input("Press any key to continue...")
def play_game(player, bot_player, fights_per_game, nb_games):
"""
Play against a bot player and return the number of victories at the end
:param player: (Player) instance of the human player
:param bot_player: (Player) instance of the bot player
:param fights_per_game: (int) Number of fights in a game
:param nb_games: (int) Number of games played vs the bot
:return player_won_games: (int) The number of games won by the human player
:return bot_won_games: (int) The number of games won by the bot player
"""
print(f"\nBeginning of the game")
player_won_games = bot_won_games = 0
env = GameEnv(player=player, bot_player=bot_player, fights_per_game=fights_per_game, verbose=True)
for _ in range(nb_games):
print("")
obs, info = env.reset()
done = False
player_ep_nb_points = []
bot_ep_nb_points = []
while not done:
action = player.choose_action()
obs, reward, done, truncated, info = env.step(action)
player_ep_nb_points.append(player.nb_points)
bot_ep_nb_points.append(bot_player.nb_points)
print(f"\nplayer points : {player.nb_points} bot_player points : {bot_player.nb_points}")
if player.nb_points > bot_player.nb_points:
player_won_games += 1
elif player.nb_points < bot_player.nb_points:
bot_won_games += 1
return player_won_games, bot_won_games
def print_game_result(player_won_games, bot_won_games):
"""
Print the results of the human vs bot confrontation
:param player_won_games: (int) The number of games won by the human player
:param bot_won_games: (int) The number of games won by the bot player
"""
print(f"\nFinal result : ")
if player_won_games == bot_won_games:
print(f"Equailty : you both won {player_won_games} fights")
elif player_won_games > bot_won_games:
print(f"Victory ! You won {player_won_games} fights and your opponent won {bot_won_games} fights")
elif player_won_games < bot_won_games:
print(f"Defeat ! You won {player_won_games} fights and your opponent won {bot_won_games} fights")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--nb_games", type=int, required=False, default=3)
parser.add_argument("--fights_per_game", type=int, required=False, default=2)
parser.add_argument("--bot_behavior", type=str, required=False, default="random")
parser.add_argument("--training_timesteps", type=int, required=False, default=100000)
parser.add_argument("--seed", type=int, required=False, default=42)
parser.add_argument("--algo", type=str, required=False, default="PPO")
args = parser.parse_args()
player_name = get_player_name()
player, bot_player = initialize_players(player_name, args.bot_behavior)
if ask_displaying_rules():
display_rules()
player_won_games, bot_won_games = play_game(player, bot_player, args)
print_game_result(player_won_games, bot_won_games)