Explorar el Código

Add game state

master
Noëlle hace 2 años
padre
commit
73a8814ac0
No se encontró ninguna clave conocida en la base de datos para esta firma
Se han modificado 1 ficheros con 54 adiciones y 15 borrados
  1. 54
    15
      brontes.py

+ 54
- 15
brontes.py Ver fichero

@@ -1,7 +1,7 @@
from random import randint, shuffle

class Sibling:
def __init__(self):
def __init__(self, state=None):
self.tragedy = 0
self.masterpiece = 0
self.brother = 0
@@ -10,6 +10,7 @@ class Sibling:
self.alive = True
self.rounds = 0
self.success = False
self.state = state

def round_check(self):
if self.tragedy >= 10:
@@ -39,6 +40,7 @@ class Sibling:
def publication_attempt(self):
if randint(1,6) == 6:
return True
self.state.failed_manuscripts += 1
return False

def __str__(self):
@@ -48,15 +50,21 @@ class Sibling:
return out_str

class Game:
def __init__(self, siblings=None, common_brother=None, brother_count=None):
self.num_siblings = int(siblings) if siblings else 3
self.common_brother = bool(common_brother) if common_brother else False
self.brother_count = int(brother_count) if brother_count else self.num_siblings
def __init__(self, siblings=None, common_brother=None, brother_count=None, verbose=None, state=None):
self.num_siblings = int(siblings) if siblings != None else 3
self.common_brother = bool(common_brother) if common_brother != None else False
self.brother_count = int(brother_count) if brother_count != None else self.num_siblings
self.verbose = bool(verbose) if verbose != None else True
self.state = state
self.siblings = []
for _ in range(self.num_siblings):
self.siblings.append(Sibling())
self.siblings.append(Sibling(state=self.state))
if self.common_brother:
self.brother_ok = True
self.per_sibling_brother = []
for _ in range(self.num_siblings):
self.per_sibling_brother.append(True)
self.success = False

def is_brother_ok(self, sibling):
if self.common_brother and self.brother_ok:
@@ -72,6 +80,9 @@ class Game:
elif event_type <= 5:
self.walk_moors(sibling)
else:
# inc_chance = randint(1,6)
# if inc_chance == 6:
# sibling.masterpiece += 1
sibling.masterpiece += 1

def quiet_day(self, sibling):
@@ -123,31 +134,61 @@ class Game:
if sibling.alive:
break
else:
print("Everyone has died.")
self.vprint("Everyone has died.")
for sibling in self.siblings:
print(sibling)
self.vprint(sibling)
self.success = False
return False
temp_sibs = self.siblings[:]
shuffle(temp_sibs)
for current_sibling in temp_sibs:
cur_sib_num = self.siblings.index(current_sibling) + 1
cur_sib_num = self.siblings.index(current_sibling)
if current_sibling.alive:
success = self.turn(current_sibling)
if success:
print(f"Sibling #{cur_sib_num} has published their masterpiece!")
self.vprint(f"Sibling #{cur_sib_num+1} has published their masterpiece!")
for sibling in self.siblings:
print(sibling)
self.vprint(sibling)
self.success = True
return False
if self.common_brother and self.brother_ok and not current_sibling.brother_ok:
if self.common_brother and self.brother_ok and not current_sibling.brother_ok and self.per_sibling_brother[cur_sib_num]:
self.brother_count -= 1
self.per_sibling_brother[cur_sib_num] = False
if self.brother_count <= 0:
self.brother_ok = False
return True

def vprint(self, text):
if self.verbose:
print(text)

def play_game(self):
continue_playing = True
while continue_playing:
continue_playing = self.round()
return self.success

class GameState:
def __init__(self, rounds=None, siblings=None, common_brother=None, brother_count=None, verbose=None):
self.rounds = int(rounds) if rounds else 10000
self.successes = 0
self.failures = 0
self.failed_manuscripts = 0
for _ in range(self.rounds):
game = Game(siblings=siblings, common_brother=common_brother, brother_count=brother_count, verbose=verbose, state=self)
success = game.play_game()
if success:
self.successes += 1
else:
self.failures += 1
out_str = f"Across {self.rounds} families of {siblings} sisters and "
if common_brother:
out_str += "one brother"
else:
out_str += "as many brothers"
out_str += f", there were {self.successes} successfully-published masterpieces, and {self.failures} tragic deaths for everybody involved."
out_str += f" In addition, there were {self.failed_manuscripts} manuscripts submitted that were refused by all publishers."
print(out_str)

if __name__ == "__main__":
# scores = {"Tragedy": {"Results": 0, "Brother OK": 0}, "Masterpiece": {"Results": 0, "Brother OK": 0}}
@@ -156,8 +197,6 @@ if __name__ == "__main__":
# scores[result]["Results"] = scores[result]["Results"] + 1
# if brother:
# scores[result]["Brother OK"] = scores[result]["Brother OK"] + 1

game = Game()
game.play_game()
gs = GameState(rounds=100000, siblings=3, common_brother=True, verbose=False)

# print(scores)

Cargando…
Cancelar
Guardar