""" skunk.py Simulate the 'skunk' dice game. Players take turns rolling two dice. They can continue rolling or stop. If a 1 comes up, that's "skunk", 0 for that turn. If a 1-1 comes up, "double skunk", entire total is reset to 0. Each has a total score so far, the sum of their rolls. First to 100 wins. Started in class --- UNFINISHED --------------------- This file has a number of typos, syntax errors, and programming errors. It's an earlier edition of the file that became the (mostly) working skunk.py in this same folder. If you'd like practice in debugging, see if you can fix the errors and trace through what *should* be happening .... --------- Jim Mahoney | May 2021 | cs.bennington.college | MIT License """ import random total_to_win = 100 class Dice: def roll(self): """ return (x,y) two random ints from 1 to 6 """ return (random.randint(1, 6), random.randint(1, 6)) class HumanPlayer: def __init__(self): self.possessive = 'Your' self.winner_name = 'You win!' self.total = 0 self.this_turn = 0 def get_choice(self): """ Return True if this player chooses to roll again """ print(f"Your previous rolls total {self.total}.") print(f"This turn you have {self.this_turn} so far.") choice = input("Roll again? (yes or no) ") return choice.lower()[0] == 'y' class ComputerPlayer: def __init__(self): self.possessive = "The computer's" self.winner_name = 'The computer wins!' self.score = 0 def get_choice(self): """ Return True if this player chooses to roll again """ # Strategy: keep rolling if we haven't reached 7 on this turn. # Is this a good approach? Hmmm. At least it's simple ... return self.this_turn >= 7 class Skunk: def __init__(self): self.players = [ HumanPlayer(), ComputerPlayer() ] self.dice = Dice() def print_summary(self): for player in self.players: print(f"\n{player.possessive} total is {player.total}.\n"} def do_roll(self, player): """ Roll; print; update player stats; return True if can continue """ (die1, die2) = self.dice.roll() print(f"Dice roll is ({die1}, {die2})", end='') if die1 == 1 and die2 == 1: # double skunk ? print("... double skunk!") player.this_turn = 0 player.total = 0 return False elif die1 == 1 or die2 == 1: # skunk ? print("... skunk!") player.this_turn = 0 return False else: print(".") player.this_turn = player.this_turn + die1 + die2 return True def do_turn(self, player): """ One turn for this player """ print(f"{player.possessive} turn.") while True: can_continue = self.do_roll(player) if can_continue: choice = player.get_choice() if not choice: return def is_finished(self): """ Return True if game is over. Print winner or tie. """ winners = [] for player in players: if player.total > total_to_win: print(player.winner_name) winners.append(player) if len(winners) > 1: print("Tie game!!!") return True else: return False def play(self): print(" Let's play *** SKUNK *** !! ") while True: self.print_summary() for player in self.players: self.do_turn(player) if self.is_finished(): p return def main(): Skunk().play() if __name__ == '__main__': main()