'''
Brian Crabtree
Uno.py
Simulate the game "Uno"
'''

#Import from CollectionOfUnoCards
from CollectionOfUnoCards import CollectionOfUnoCards

#Class to be used to simulate the game
class Uno:

    #Define the number of cards in a hand
    NUMCARDSHAND = 7

    #Constructor for an Uno game
    def __init__(self):

        #Create the deck and shuffle it
        self.deck = CollectionOfUnoCards()
        self.deck.makeDeck()
        self.deck.shuffle()

        #Create a discard pile
        self.discardPile = CollectionOfUnoCards()

        #Create hands for the two different players
        self.hand1 = CollectionOfUnoCards()
        self.hand2 = CollectionOfUnoCards()

        #Alternate adding cards to the 2 players' hands
        for i in range(0, Uno.NUMCARDSHAND + 1):
            self.hand1.addCard(self.deck.deck.pop())
            self.hand2.addCard(self.deck.deck.pop())

    #Run the game simulation
    def playGame(self):

        #Simulate the first turn for player 1 to start the game
        print("Player 1, here is your hand:\n" + str(self.hand1))
        card = int(input("Please input the number of the card you wish to discard: "))
        self.discardPile.addCard(self.hand1.deck.pop(card))
        self.hand1.numCards -= 1

        #Change who's turn it is
        turn = 2

        #Take turns while the game is still going on
        while (self.deck.getNumCards() > 0) and (self.hand1.getNumCards() > 0) and (self.hand2.getNumCards() > 0):
            self.playTurn(turn)

            if turn == 1:
                turn = 2
            else:
                turn = 1

        #Print the result of the game once it has ended
        printResult()

    #Print out the results of the game
    def printResult(self):
        if self.deck.getNumCards == 0:
            print("Sorry, the game has ended in a draw.")
        elif self.hand1.getNumCards() == 0:
            print("Player 1, you win =)")
        else:
            ("Player 2, you win =)")

    #Simulate one of the players taking a turn
    def playTurn(self, player):

        #Print the top card of the discard pile
        print("The card at the top of the discard pile is " + str(self.discardPile.getTopCard()))

        #Player 1's turn
        if player == 1:

            #Verify that player 1 has a valid move
            if self.hand1.canPlay(self.discardPile.getTopCard()):

                #Print out player 1's hand and ask for a discard choice
                print("Player 1, here is your hand:\n" + str(self.hand1))
                card = int(input("Please input the number of the card you wish to discard: "))

                #If a valid card is chosen, remove from the player's hand and place it in the discard pile
                if self.hand1.getCard(card).canPlay(self.discardPile.getTopCard()):
                    self.discardPile.addCard(self.hand1.deck.pop(card))
                    self.hand1.numCards -= 1

                #If an invalid card is chosen, the turn ends
                else:
                    print("Sorry that is not a valid card. You lost your opportunity to drop a card.")

                #Print out when the player has 1 card
                if self.hand1.getNumCards() == 1:
                    print("Player One says UNO!!!!")

            #Draw a card for the player if they cannot play and print out their new hand
            else:
                print("Sorry, you can't play on this card. A card has been drawn for you.")
                self.hand1.addCard(self.deck.deck.pop())
                self.deck.numCards -= 1
                print("Player 1, here is your resulting hand:\n" + str(self.hand1))

        #Player 2's turn
        else:

            #Verify that player 2 has a valid move
            if self.hand2.canPlay(self.discardPile.getTopCard()):

                #Print out player 2's hand and ask for a discard choice
                print("Player 2, here is your hand:\n" + str(self.hand2))
                card = int(input("Please input the number of the card you wish to discard: "))

                #If a valid card is chosen, remove from the player's hand and place it in the discard pile
                if self.hand2.getCard(card).canPlay(self.discardPile.getTopCard()):
                    self.discardPile.addCard(self.hand2.deck.pop(card))
                    self.hand2.numCards -= 1

                #If an invalid card is chosen, the turn ends
                else:
                    print("Sorry that is not a valid card. You lost your opportunity to drop a card.")

                #Print out when the player has 1 card
                if self.hand2.getNumCards() == 1:
                    print("Player Two says UNO!!!!")
                    
            #Draw a card for the player if they cannot play and print out their new hand
            else:
                print("Sorry, you can't play on this card. A card has been drawn for you.")
                self.hand2.addCard(self.deck.deck.pop())
                self.deck.numCards -= 1
                print("Player 2, here is your resulting hand:\n" + str(self.hand2))


#Run the game simulation
myGame = Uno()
myGame.playGame()
