set max for infinite loop games
This commit is contained in:
parent
1fff2fc73f
commit
7822f7d51b
1 changed files with 34 additions and 20 deletions
40
simul.py
40
simul.py
|
|
@ -1,7 +1,7 @@
|
||||||
import random
|
import random
|
||||||
|
|
||||||
NB_PLAYERS = 2
|
NB_PLAYERS = 2
|
||||||
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:
|
||||||
|
|
@ -79,6 +82,7 @@ class Game():
|
||||||
if len(round_remaining_players) == 0 and len(test_cards) == 0:
|
if len(round_remaining_players) == 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(round_remaining_players) == 0 and len(test_cards) > 0:
|
||||||
self.status = 'finished'
|
self.status = 'finished'
|
||||||
|
|
@ -88,22 +92,30 @@ class Game():
|
||||||
self.winner = round_remaining_players[0]
|
self.winner = round_remaining_players[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))
|
print(str(self))
|
||||||
i = 1
|
i = 1
|
||||||
while self.status == 'playing':
|
while self.status == 'playing' and i <= Game.MAX_HANDS:
|
||||||
print(f'--- Playing hand {i} ---')
|
|
||||||
self.play_hand()
|
self.play_hand()
|
||||||
|
if self.verbose:
|
||||||
|
print(f'--- Playing hand {i} ---')
|
||||||
print(str(self))
|
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
|
||||||
|
|
||||||
|
|
@ -112,19 +124,21 @@ 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=False)
|
||||||
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(NB_TRIES)
|
||||||
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],
|
||||||
nb_hands = g.play_round()
|
# 1: [3, 5, 1, 1, 2, 4, 1, 4, 6, 2, 6, 3, 6, 4, 2, 5, 3, 5]}
|
||||||
print(nb_hands)
|
|
||||||
|
# g = Game(2, init_s)
|
||||||
|
# nb_hands = g.play_round()
|
||||||
|
# print(nb_hands)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue