Index: bunco.py ================================================================== --- bunco.py +++ bunco.py @@ -1,14 +1,9 @@ # Test import csv, random -teammate = { - 1: 3, - 2: 4, - 3: 1, - 4: 2 -} +teammate_lookup = { 0: 2, 1: 3, 2: 0, 3: 1 } def TurnInProgress(): return -1 def roll_dice(): @@ -17,10 +12,11 @@ dice.append(random.randint(1,6)) return dice class Player: fuzzydie_holder = None + current_round = 1 def __init__(self, name, dex, math, speed): self.name = name self.dex = dex self.math_comprehension = math @@ -47,12 +43,12 @@ self.round_roll_counts.append(0) self.personal_roll_scores.append(0) self.turn_progress = 0 self.current_streak = 0 - def score_last_roll(self, current_round): - desired_num = current_round % 6 + def score_last_roll(self): + desired_num = self.current_round % 6 desired_num = 6 if desired_num == 0 else desired_num roll_score = 0 if all(die == desired_num for die in self.current_roll): @@ -66,21 +62,21 @@ roll_score += 1 if die == desired_num else 0 if roll_score > 0: self.current_streak += 1 self.max_streak = max(self.current_streak, self.max_streak) - self.round_scores[current_round - 1] += roll_score - self.personal_roll_scores[current_round - 1] += roll_score + self.round_scores[self.current_round - 1] += roll_score + self.personal_roll_scores[self.current_round - 1] += roll_score if roll_score == 21: self.bunco_count += 1 self.rolled_bunco = True else: self.current_streak = 0 return roll_score - def tick(self, current_round): + def tick(self): result = TurnInProgress() if self.fuzzydie_holder is self: self.current_fuzzydie_streak += 1 self.max_fuzzydie_streak = max(self.max_fuzzydie_streak, self.current_fuzzydie_streak) @@ -98,15 +94,15 @@ elif self.turn_progress < 75: # Reading the numbers # TODO: Incorporate MATH COMPREHENSION stat if not self.current_roll: self.current_roll = roll_dice() - self.round_roll_counts[current_round - 1] += 1 + self.round_roll_counts[self.current_round - 1] += 1 self.turn_progress += random.randint(12,25) else: # Finished reading the numbers -- good job, eyeballs and brain! - result = self.score_last_roll(current_round) + result = self.score_last_roll() self.current_roll = [] self.turn_progress = 0 return result class Table: @@ -122,40 +118,44 @@ def __repr__(self): names = ", ".join([p.name for p in self.players]) return f"" - def tick(self, current_round): + def update_teammate_score(self, score): + teammate = self.players[teammate_lookup[self.active_player]] + teammate.round_scores[Player.current_round - 1] += score + + def tick(self): if self.active_player == -1: # First tick for the table this round self.active_player = random.randint(1, len(self.players)) - 1 - result = self.players[self.active_player].tick(current_round) + result = self.players[self.active_player].tick() if result != TurnInProgress(): # Player rolled if result > 0: if (self.active_player % 2) == 0: self.team1_score += result else: self.team2_score += result - self.players[teammate[self.active_player + 1]-1].round_scores[current_round - 1] += result + self.update_teammate_score(result) else: self.active_player += 1 if self.active_player > (len(self.players) - 1): self.active_player = 0 - def roll_off(self, current_round): + def roll_off(self): """Attempt to settle a tie by going round the table once""" self.active_player = 0 # Let the first player go until they roll for 0 points while self.active_player == 0: - self.tick(current_round) + self.tick() # Now let the rest do the same while self.active_player != 0: - self.tick(current_round) + self.tick() def losers(self): if self.team1_score > self.team2_score: return self.players[1::2] else: @@ -202,11 +202,10 @@ self.team2_score = 0 self.active_player = -1 for player in self.players: player.prep_new_round() - def assign_teams(player_list): players_per_table = 4 tables = [] random.seed() @@ -237,15 +236,15 @@ class Game: def __init__(self, playerfile): self.players = load_players(playerfile) self.tables = assign_teams(self.players) - self.current_round = 1 + Player.current_round = 1 def tick(self): for table in self.tables: - table.tick(self.current_round) + table.tick() bunco_rollers = [p for p in self.players if p.rolled_bunco is True] # If multiple people rolled Bunco this tick, and one of them already has the # fuzzy die, they retain it. @@ -257,11 +256,11 @@ def print_status(self): for n, table in enumerate(self.tables): print(f"== TABLE {n+1} == Team 1:{table.team1_score} pts, Team 2:{table.team2_score} pts") for player in table.players: - print(f" {player.name} {player.round_scores[self.current_round - 1]} points, streak {player.max_streak} buncos {player.bunco_count}") + print(f" {player.name} {player.round_scores[Player.current_round - 1]} points, streak {player.max_streak} buncos {player.bunco_count}") def prep_next_round(self): # losers from head table move to next table headtable_losers = self.tables[0].losers() round_winners = self.tables[1].swap_for_winners(headtable_losers) @@ -273,11 +272,11 @@ self.tables[0].swap_for_losers(round_winners) for table in self.tables: table.prep_new_round() - self.current_round += 1 + Player.current_round += 1 def play_one_round(self): # Go until one of the head table teams reaches 21 pts while max(self.tables[0].team1_score, self.tables[0].team2_score) < 21: self.tick() @@ -287,14 +286,14 @@ # Finish up scoring for any players that have unscored rolls for player in self.players: if player.current_roll: print(f"Finishing up: {player}") while player.current_roll: - player.tick(self.current_round) + player.tick() # Settle ties at each table by doing a roll-off as many times as needed for table in self.tables: while table.team1_score == table.team2_score: print(f"Roll off! {table}") - table.roll_off(self.current_round) + table.roll_off() self.print_status()