Compare commits

..

1 commit
main ... master

Author SHA1 Message Date
b55d73a7c8 Created using Colab 2025-05-15 10:26:31 +02:00
3 changed files with 1355 additions and 60 deletions

BIN
030.pdf

Binary file not shown.

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,7 @@
import random import random
NB_PLAYERS = 4 NB_PLAYERS = 2
NB_TRIES = 10000 NB_TRIES = 1000
VERBOSE = True VERBOSE = True
class Game(): class Game():
@ -10,8 +10,6 @@ class Game():
status: str status: str
winner: int winner: int
nb_players: int nb_players: int
verbose: bool
MAX_HANDS = 10000
def shuffle_deal(nb_players): def shuffle_deal(nb_players):
@ -25,10 +23,9 @@ class Game():
return {i:elements[i:i + sublist_size] for i in range(0, nb_players)} return {i:elements[i:i + sublist_size] for i in range(0, nb_players)}
def __init__(self, nb_players, init_state=False, verbose=True): def __init__(self, nb_players, init_state=False):
self.status = 'playing' self.status = 'playing'
self.nb_players = nb_players self.nb_players = nb_players
self.verbose = verbose
if init_state: if init_state:
self.cards = init_state self.cards = init_state
else: else:
@ -44,10 +41,9 @@ class Game():
def keys_of_max_values(hand): def keys_of_max_values(hand):
max_indexes = []
if hand.values():
max_value = max(hand.values()) max_value = max(hand.values())
max_indexes = [k for k, v in hand.items() if v == max_value] max_indexes = [k for k, v in hand.items() if v == max_value]
return max_indexes return max_indexes
@ -57,9 +53,8 @@ class Game():
def play_hand(self): def play_hand(self):
# at the beginning of the hand we assume all players should have at least 1 card in their stack # at the beginning of the hand we assume all players should have at least 1 card in their stack
rrp = self.get_round_remaining_players() test_cards = {k: self.cards[k][0] for k in self.cards.keys()}
test_cards = {k: self.cards[k][0] for k in rrp} trick_cards = [self.cards[k].pop(0) for k in self.cards.keys()]
trick_cards = [self.cards[k].pop(0) for k in rrp]
hand_remaining_players = Game.keys_of_max_values(test_cards) hand_remaining_players = Game.keys_of_max_values(test_cards)
@ -80,48 +75,35 @@ class Game():
trick_cards.extend(list(test_cards.values())) trick_cards.extend(list(test_cards.values()))
hand_remaining_players = Game.keys_of_max_values(test_cards) hand_remaining_players = Game.keys_of_max_values(test_cards)
rrp = self.get_round_remaining_players() round_remaining_players = self.get_round_remaining_players()
print(rrp) if len(round_remaining_players) == 0 and len(test_cards) == 0:
print(hand_remaining_players)
if len(rrp) == 0 and len(test_cards) == 0:
self.status = 'finished' self.status = 'finished'
self.winner = -1 self.winner = -1
if self.verbose:
print(f'==== Game ended in a draw ====') print(f'==== Game ended in a draw ====')
elif len(rrp) == 0 and len(test_cards) > 0: elif len(round_remaining_players) == 0 and len(test_cards) > 0:
self.status = 'finished' self.status = 'finished'
self.winner = hand_remaining_players[0] self.winner = hand_remaining_players[0]
elif len(rrp) == 1 and rrp == hand_remaining_players: elif len(round_remaining_players) == 1:
self.status = 'finished' self.status = 'finished'
self.winner = rrp[0] self.winner = round_remaining_players[0]
elif len(rrp) == 1 and len(hand_remaining_players) == 0:
self.status = 'finished'
self.winner = rrp[0]
else: else:
hand_winner = hand_remaining_players[0] hand_winner = hand_remaining_players[0]
if self.verbose:
print(f'Player {hand_winner} won the hand!') print(f'Player {hand_winner} won the hand!')
# distribute the cards to the winning player # distribute the cards to the winning player
self.cards[hand_winner].extend(trick_cards) self.cards[hand_winner].extend(trick_cards)
def play_round(self): def play_round(self):
if self.verbose:
print('--- Starting game ---') print('--- Starting game ---')
i = 1
while self.status == 'playing' and i <= Game.MAX_HANDS:
if self.verbose:
print(f'--- Playing hand {i} ---')
print(str(self)) print(str(self))
i = 1
while self.status == 'playing':
print(f'--- Playing hand {i} ---')
self.play_hand() self.play_hand()
print(str(self))
if self.status == 'finished': if self.status == 'finished':
if self.verbose:
print(f'==== Game finished after {i} hands - Player {self.winner} won the game =====') print(f'==== Game finished after {i} hands - Player {self.winner} won the game =====')
return i return i
elif i == Game.MAX_HANDS:
if self.verbose:
print(f'Forced game to finish - infinite loop')
return 0
else: else:
i += 1 i += 1
@ -130,33 +112,19 @@ class Game():
def simulate(n_games): def simulate(n_games):
nb_hands_to_finish_game = [] nb_hands_to_finish_game = []
for _ in range(n_games): for _ in range(n_games):
g = Game(NB_PLAYERS,verbose=True) g = Game(NB_PLAYERS)
nb_hands = g.play_round() nb_hands = g.play_round()
nb_hands_to_finish_game.append(nb_hands) nb_hands_to_finish_game.append(nb_hands)
del g del g
return nb_hands_to_finish_game return nb_hands_to_finish_game
#r = simulate(NB_TRIES)
#print(r)
#print(sum(r)/len(r))
# r = simulate(100) init_s = {0: [5, 3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3],
# print(f"Number of games that didn't finish {len([e for e in r if e == 0])}") 1: [3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3, 5]}
# 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)}")
# init_s = {0: [5, 3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3], g = Game(2, init_s)
# 1: [3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3, 5]}
# init_s = {0: [6, 1, 6, 5, 3, 3, 2],
# 1: [1, 2, 5, 3, 3, 3, 3, 2, 3, 3, 2, 6, 2, 3, 1, 3, 6, 3, 6, 1, 1, 6, 3],
# 2: [6, 1, 6, 1, 3],
# 3: [1]}
init_s = {
0: [2, 3, 3, 3, 4, 4, 2, 2, 4, 2, 3, 3],
1: [3, 3, 3, 4, 4, 2, 2, 5],
2: [3, 3, 4, 4, 2, 2, 5, 5],
3: [3, 4, 4, 2, 2, 5, 5, 4]
}
g = Game(4, init_s)
nb_hands = g.play_round() nb_hands = g.play_round()
print(nb_hands) print(nb_hands)