Compare commits
4 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 31f1321c3d | |||
| 5d531a5cde | |||
| 7d42823c95 | |||
| 7822f7d51b |
3 changed files with 60 additions and 1355 deletions
BIN
030.pdf
Normal file
BIN
030.pdf
Normal file
Binary file not shown.
File diff suppressed because one or more lines are too long
88
simul.py
88
simul.py
|
|
@ -1,7 +1,7 @@
|
|||
import random
|
||||
|
||||
NB_PLAYERS = 2
|
||||
NB_TRIES = 1000
|
||||
NB_PLAYERS = 4
|
||||
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:
|
||||
|
|
@ -41,9 +44,10 @@ class Game():
|
|||
|
||||
|
||||
def keys_of_max_values(hand):
|
||||
max_value = max(hand.values())
|
||||
max_indexes = [k for k, v in hand.items() if v == max_value]
|
||||
|
||||
max_indexes = []
|
||||
if hand.values():
|
||||
max_value = max(hand.values())
|
||||
max_indexes = [k for k, v in hand.items() if v == max_value]
|
||||
return max_indexes
|
||||
|
||||
|
||||
|
|
@ -53,8 +57,9 @@ class Game():
|
|||
|
||||
def play_hand(self):
|
||||
# at the beginning of the hand we assume all players should have at least 1 card in their stack
|
||||
test_cards = {k: self.cards[k][0] for k in self.cards.keys()}
|
||||
trick_cards = [self.cards[k].pop(0) for k in self.cards.keys()]
|
||||
rrp = self.get_round_remaining_players()
|
||||
test_cards = {k: self.cards[k][0] for k in rrp}
|
||||
trick_cards = [self.cards[k].pop(0) for k in rrp]
|
||||
|
||||
hand_remaining_players = Game.keys_of_max_values(test_cards)
|
||||
|
||||
|
|
@ -75,35 +80,48 @@ class Game():
|
|||
trick_cards.extend(list(test_cards.values()))
|
||||
hand_remaining_players = Game.keys_of_max_values(test_cards)
|
||||
|
||||
round_remaining_players = self.get_round_remaining_players()
|
||||
if len(round_remaining_players) == 0 and len(test_cards) == 0:
|
||||
rrp = self.get_round_remaining_players()
|
||||
print(rrp)
|
||||
print(hand_remaining_players)
|
||||
if len(rrp) == 0 and len(test_cards) == 0:
|
||||
self.status = 'finished'
|
||||
self.winner = -1
|
||||
print(f'==== Game ended in a draw ====')
|
||||
elif len(round_remaining_players) == 0 and len(test_cards) > 0:
|
||||
if self.verbose:
|
||||
print(f'==== Game ended in a draw ====')
|
||||
elif len(rrp) == 0 and len(test_cards) > 0:
|
||||
self.status = 'finished'
|
||||
self.winner = hand_remaining_players[0]
|
||||
elif len(round_remaining_players) == 1:
|
||||
elif len(rrp) == 1 and rrp == hand_remaining_players:
|
||||
self.status = 'finished'
|
||||
self.winner = round_remaining_players[0]
|
||||
self.winner = rrp[0]
|
||||
elif len(rrp) == 1 and len(hand_remaining_players) == 0:
|
||||
self.status = 'finished'
|
||||
self.winner = rrp[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))
|
||||
if self.verbose:
|
||||
print('--- Starting game ---')
|
||||
i = 1
|
||||
while self.status == 'playing':
|
||||
print(f'--- Playing hand {i} ---')
|
||||
while self.status == 'playing' and i <= Game.MAX_HANDS:
|
||||
if self.verbose:
|
||||
print(f'--- Playing hand {i} ---')
|
||||
print(str(self))
|
||||
self.play_hand()
|
||||
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 +130,33 @@ 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=True)
|
||||
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(100)
|
||||
# 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)
|
||||
# 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]}
|
||||
|
||||
# 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()
|
||||
print(nb_hands)
|
||||
|
|
|
|||
Loading…
Reference in a new issue