Browse Source

Add game state

master
Noëlle 2 years ago
parent
commit
73a8814ac0
No known key found for this signature in database
1 changed files with 54 additions and 15 deletions
  1. 54
    15
      brontes.py

+ 54
- 15
brontes.py View File

from random import randint, shuffle from random import randint, shuffle


class Sibling: class Sibling:
def __init__(self):
def __init__(self, state=None):
self.tragedy = 0 self.tragedy = 0
self.masterpiece = 0 self.masterpiece = 0
self.brother = 0 self.brother = 0
self.alive = True self.alive = True
self.rounds = 0 self.rounds = 0
self.success = False self.success = False
self.state = state


def round_check(self): def round_check(self):
if self.tragedy >= 10: if self.tragedy >= 10:
def publication_attempt(self): def publication_attempt(self):
if randint(1,6) == 6: if randint(1,6) == 6:
return True return True
self.state.failed_manuscripts += 1
return False return False


def __str__(self): def __str__(self):
return out_str return out_str


class Game: 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 = [] self.siblings = []
for _ in range(self.num_siblings): for _ in range(self.num_siblings):
self.siblings.append(Sibling())
self.siblings.append(Sibling(state=self.state))
if self.common_brother: if self.common_brother:
self.brother_ok = True 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): def is_brother_ok(self, sibling):
if self.common_brother and self.brother_ok: if self.common_brother and self.brother_ok:
elif event_type <= 5: elif event_type <= 5:
self.walk_moors(sibling) self.walk_moors(sibling)
else: else:
# inc_chance = randint(1,6)
# if inc_chance == 6:
# sibling.masterpiece += 1
sibling.masterpiece += 1 sibling.masterpiece += 1


def quiet_day(self, sibling): def quiet_day(self, sibling):
if sibling.alive: if sibling.alive:
break break
else: else:
print("Everyone has died.")
self.vprint("Everyone has died.")
for sibling in self.siblings: for sibling in self.siblings:
print(sibling)
self.vprint(sibling)
self.success = False
return False return False
temp_sibs = self.siblings[:] temp_sibs = self.siblings[:]
shuffle(temp_sibs) shuffle(temp_sibs)
for current_sibling in 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: if current_sibling.alive:
success = self.turn(current_sibling) success = self.turn(current_sibling)
if success: 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: for sibling in self.siblings:
print(sibling)
self.vprint(sibling)
self.success = True
return False 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.brother_count -= 1
self.per_sibling_brother[cur_sib_num] = False
if self.brother_count <= 0: if self.brother_count <= 0:
self.brother_ok = False self.brother_ok = False
return True return True


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

def play_game(self): def play_game(self):
continue_playing = True continue_playing = True
while continue_playing: while continue_playing:
continue_playing = self.round() 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__": if __name__ == "__main__":
# scores = {"Tragedy": {"Results": 0, "Brother OK": 0}, "Masterpiece": {"Results": 0, "Brother OK": 0}} # scores = {"Tragedy": {"Results": 0, "Brother OK": 0}, "Masterpiece": {"Results": 0, "Brother OK": 0}}
# scores[result]["Results"] = scores[result]["Results"] + 1 # scores[result]["Results"] = scores[result]["Results"] + 1
# if brother: # if brother:
# scores[result]["Brother OK"] = scores[result]["Brother OK"] + 1 # 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) # print(scores)

Loading…
Cancel
Save