Skip to content

Commit

Permalink
Added dic_win_lose_type dictionary to compute types of victories for …
Browse files Browse the repository at this point in the history
…each player in test
  • Loading branch information
daniPclos committed Apr 16, 2024
1 parent 96d61c8 commit f14c603
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
11 changes: 10 additions & 1 deletion examples/player_list_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
nb_games = 1000
results = {} # We will count the number of victories for each player

# Initialize global victory type evaluator
dic_global_win_lose_type = {}

# Match all combinations of players
for i, player1_class in enumerate(players_classes):
# Get the name of the player
Expand All @@ -39,16 +42,22 @@
p1 = player1_class(1)
p2 = player2_class(2)

# Initialize victory type evaluator dic
dic_win_lose_type = {p1.name(): {}, p2.name(): {}}

# Get the name of the player 2
player2_name = p2.name()
results[player1_name][player2_name] = 0

print(f"\n\nPlaying {player1_name} vs {player2_name}:")

# Play 100 games
victories_number = tester.play_1v1(p1, p2, nb_games=nb_games)
victories_number, dic_global_win_lose_type[f"{p1.name()}vs{p2.name()}"] = \
tester.play_1v1(p1, p2, nb_games=nb_games, dic_win_lose_type=dic_win_lose_type)

results[player1_name][player2_name] = victories_number[player1_name]

print(f"dic_global_win_lose_type = \n{dic_global_win_lose_type}")

print()
print("Results:")
Expand Down
39 changes: 36 additions & 3 deletions santorinai/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def display_message(self, message, verbose_level=1):
if self.verbose_level >= verbose_level:
print(message)

def play_1v1(self, player1: Player, player2: Player, nb_games: int = 1):
def play_1v1(self, player1: Player, player2: Player, nb_games: int = 1, dic_win_lose_type=None):
"""
Play a 1v1 game between player1 and player2
Expand All @@ -39,6 +39,10 @@ def play_1v1(self, player1: Player, player2: Player, nb_games: int = 1):
NB_PAWNS = NB_PLAYERS * 2

nb_victories = [0, 0]
# Initialize empty dic_win_lose_type in not passed
if not dic_win_lose_type:
dic_win_lose_type = {player1.name(): {},
player2.name(): {}}

# Check if the players are objects of the Player class
if player1 is None or not isinstance(player1, Player):
Expand Down Expand Up @@ -85,6 +89,9 @@ def play_1v1(self, player1: Player, player2: Player, nb_games: int = 1):
f" Pawn placed at an invalid position: {reason}", 1
)
self.display_message(f" Player {player.name()} loses")
dic_win_lose_type[player.name()] = \
register_new_victory_type(dic_win_lose_type[player.name()],
f" Pawn placed at an invalid position: {reason}")
nb_victories[(pawn_nb + 2) % NB_PLAYERS] += 1
break

Expand Down Expand Up @@ -125,6 +132,9 @@ def play_1v1(self, player1: Player, player2: Player, nb_games: int = 1):
f" Pawn moved at an invalid position: {reason}", 1
)
self.display_message(f" Player {current_player.name()} loses")
dic_win_lose_type[current_player.name()] = \
register_new_victory_type(dic_win_lose_type[current_player.name()],
f" Player {current_player.name()} loses")
nb_victories[(current_player.player_number) % NB_PLAYERS] += 1
break

Expand All @@ -146,7 +156,11 @@ def play_1v1(self, player1: Player, player2: Player, nb_games: int = 1):
self.display_message(
f"Player {players[winner_number - 1].name()} wins!"
)
nb_victories[winner_number - 1] += 1
dic_win_lose_type[players[winner_number - 1].name()] = \
register_new_victory_type(dic_win_lose_type[players[winner_number - 1].name()],
f"Player {players[winner_number - 1].name()} wins!")

nb_victories[winner_number - 1] += 1

# Display the results
print("\nResults:")
Expand All @@ -168,4 +182,23 @@ def play_1v1(self, player1: Player, player2: Player, nb_games: int = 1):
return {
players[0].name(): nb_victories[0],
players[1].name(): nb_victories[1],
}
}, dic_win_lose_type

def register_new_victory_type(dic_win_lose_types, s_msg):
"""
Function that registers types of winning and loosing conditions
and keeps track on a counter for each type of already registered
type
Args:
dic_win_lose_types:
s_msg:
Returns:
"""
try:
dic_win_lose_types[s_msg] += 1
except KeyError:
dic_win_lose_types[s_msg] = 1

return dic_win_lose_types

0 comments on commit f14c603

Please sign in to comment.