Compare commits
4 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 31f1321c3d | |||
| 5d531a5cde | |||
| 7d42823c95 | |||
| 7822f7d51b |
2 changed files with 60 additions and 28 deletions
BIN
030.pdf
Normal file
BIN
030.pdf
Normal file
Binary file not shown.
74
simul.py
74
simul.py
|
|
@ -1,7 +1,7 @@
|
||||||
import random
|
import random
|
||||||
|
|
||||||
NB_PLAYERS = 2
|
NB_PLAYERS = 4
|
||||||
NB_TRIES = 1000
|
NB_TRIES = 10000
|
||||||
VERBOSE = True
|
VERBOSE = True
|
||||||
|
|
||||||
class Game():
|
class Game():
|
||||||
|
|
@ -10,6 +10,8 @@ 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):
|
||||||
|
|
@ -23,9 +25,10 @@ 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):
|
def __init__(self, nb_players, init_state=False, verbose=True):
|
||||||
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:
|
||||||
|
|
@ -41,9 +44,10 @@ 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
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -53,8 +57,9 @@ 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
|
||||||
test_cards = {k: self.cards[k][0] for k in self.cards.keys()}
|
rrp = self.get_round_remaining_players()
|
||||||
trick_cards = [self.cards[k].pop(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 rrp]
|
||||||
|
|
||||||
hand_remaining_players = Game.keys_of_max_values(test_cards)
|
hand_remaining_players = Game.keys_of_max_values(test_cards)
|
||||||
|
|
||||||
|
|
@ -75,35 +80,48 @@ 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)
|
||||||
|
|
||||||
round_remaining_players = self.get_round_remaining_players()
|
rrp = self.get_round_remaining_players()
|
||||||
if len(round_remaining_players) == 0 and len(test_cards) == 0:
|
print(rrp)
|
||||||
|
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(round_remaining_players) == 0 and len(test_cards) > 0:
|
elif len(rrp) == 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(round_remaining_players) == 1:
|
elif len(rrp) == 1 and rrp == hand_remaining_players:
|
||||||
self.status = 'finished'
|
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:
|
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 ---')
|
||||||
print(str(self))
|
|
||||||
i = 1
|
i = 1
|
||||||
while self.status == 'playing':
|
while self.status == 'playing' and i <= Game.MAX_HANDS:
|
||||||
|
if self.verbose:
|
||||||
print(f'--- Playing hand {i} ---')
|
print(f'--- Playing hand {i} ---')
|
||||||
self.play_hand()
|
|
||||||
print(str(self))
|
print(str(self))
|
||||||
|
self.play_hand()
|
||||||
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
|
||||||
|
|
||||||
|
|
@ -112,19 +130,33 @@ 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)
|
g = Game(NB_PLAYERS,verbose=True)
|
||||||
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))
|
|
||||||
|
|
||||||
init_s = {0: [5, 3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3],
|
# r = simulate(100)
|
||||||
1: [3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3, 5]}
|
# 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()
|
nb_hands = g.play_round()
|
||||||
print(nb_hands)
|
print(nb_hands)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue