"""
 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 ; finished by Jim at home.
 
 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.total = 0
        self.this_turn = 0

    def get_choice(self):
        """ Return True if this player chooses to roll again """
        # Strategy: stop once we have at least 7 on this roll.
        # 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):
        print()
        for player in self.players:
            print(f"{player.possessive} total is {player.total}.")

    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 """
        player.this_turn = 0
        print(f"\n{player.possessive} turn.")
        while True:
            can_continue = self.do_roll(player)
            if can_continue:
                choice = player.get_choice()
                if not choice:
                    # add this turn's dice rolls to total
                    player.total = player.total + player.this_turn
                    return
            else:
                return

    def is_finished(self):
        """ Return True if game is over. Print winner or tie. """
        winners = []
        for player in self.players:
            if player.total > total_to_win:
                print(player.winner_name)
                winners.append(player)
        if len(winners) > 1:
            print("Tie game!!!")
        return len(winners) > 0
                
    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():
                return

def main():
    Skunk().play()

if __name__ == '__main__':
    main()