diff --git a/simul.py b/simul.py index f8e6cea..7ea4b9d 100644 --- a/simul.py +++ b/simul.py @@ -1,7 +1,7 @@ import random NB_PLAYERS = 2 -NB_TRIES = 1000 +NB_TRIES = 10000 VERBOSE = True class Game(): @@ -10,6 +10,8 @@ class Game(): status: str winner: int nb_players: int + verbose: bool + MAX_HANDS = 10000 def shuffle_deal(nb_players): @@ -23,9 +25,10 @@ class Game(): return {i:elements[i:i + sublist_size] for i in range(0, nb_players)} - def __init__(self, nb_players, init_state=False): + def __init__(self, nb_players, init_state=False, verbose=True): self.status = 'playing' self.nb_players = nb_players + self.verbose = verbose if init_state: self.cards = init_state else: @@ -79,7 +82,8 @@ class Game(): if len(round_remaining_players) == 0 and len(test_cards) == 0: self.status = 'finished' self.winner = -1 - print(f'==== Game ended in a draw ====') + if self.verbose: + print(f'==== Game ended in a draw ====') elif len(round_remaining_players) == 0 and len(test_cards) > 0: self.status = 'finished' self.winner = hand_remaining_players[0] @@ -88,22 +92,30 @@ class Game(): self.winner = round_remaining_players[0] else: hand_winner = hand_remaining_players[0] - print(f'Player {hand_winner} won the hand!') + if self.verbose: + print(f'Player {hand_winner} won the hand!') # distribute the cards to the winning player self.cards[hand_winner].extend(trick_cards) def play_round(self): - print('--- Starting game ---') - print(str(self)) - i = 1 - while self.status == 'playing': - print(f'--- Playing hand {i} ---') - self.play_hand() + if self.verbose: + print('--- Starting game ---') print(str(self)) + i = 1 + while self.status == 'playing' and i <= Game.MAX_HANDS: + self.play_hand() + if self.verbose: + print(f'--- Playing hand {i} ---') + print(str(self)) if self.status == 'finished': - print(f'==== Game finished after {i} hands - Player {self.winner} won the game =====') + if self.verbose: + print(f'==== Game finished after {i} hands - Player {self.winner} won the game =====') return i + elif i == Game.MAX_HANDS: + if self.verbose: + print(f'Forced game to finish - infinite loop') + return 0 else: i += 1 @@ -112,19 +124,21 @@ class Game(): def simulate(n_games): nb_hands_to_finish_game = [] for _ in range(n_games): - g = Game(NB_PLAYERS) + g = Game(NB_PLAYERS,verbose=False) nb_hands = g.play_round() nb_hands_to_finish_game.append(nb_hands) del g return nb_hands_to_finish_game -#r = simulate(NB_TRIES) -#print(r) -#print(sum(r)/len(r)) -init_s = {0: [5, 3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3], - 1: [3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3, 5]} +r = simulate(NB_TRIES) +print(f"Number of games that didn't finish {len([e for e in r if e == 0])}") +print(f"Average number of hands to finish a game {sum(r)/len([e for e in r if e != 0])}") +print(f"Min-Max number of hands to finish a game {min([e for e in r if e != 0])} - {max(r)}") -g = Game(2, init_s) -nb_hands = g.play_round() -print(nb_hands) +# init_s = {0: [5, 3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3], +# 1: [3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3, 5]} + +# g = Game(2, init_s) +# nb_hands = g.play_round() +# print(nb_hands)