# Jared Wasserman
# 6/4/2013
# This class runs the main program, poker.
# Python translation of Arup's Java code

from CardList import CardList
from Card import Card

class Poker:

    def __init__(self): 
        # Create the deck and shuffle it.
        self.Deck = CardList()
        self.Deck.make_deck()
        self.Deck.shuffle()

        # Create a hand and add in five cards from the deck.
        self.Hand = CardList()
        for i in range(5):
            self.Hand.add_card(self.Deck.remove_card())

    
    def play_game(self):
        # shows hand
        print("Here is your hand:\n" + str(self.Hand))

        # Asks user which cards to exchange
        print("How many cards do you want to exchange?")
        num_cards = int(input())

        # Imposes limits on exchange
        if (num_cards > 5):
            num_cards = 5
        if (num_cards < 0):
            num_cards = 0

        # Creates array for cards to be exchanged
        exchangeList = [None] * num_cards
        card_num = 0

        # Runs as long as user wants to exchange cards
        while (card_num < num_cards):

            print("Enter the number of the next card you want to exchange.")
            val = int(input())

            if (Poker.is_in(exchangeList, card_num, val)):
                print("Sorry, you've chosen that card already. Try again.")
            else:
                exchangeList[card_num] = val - 1
                card_num += 1
        
        exchangeList.sort()

        # Removes card - we need to remove them from the back so the indexes don't get screwed up.
        for i in reversed(range(card_num)):
            self.Hand.remove_card(exchangeList[i])

        # Adds card
        for i in range(card_num):
            self.Hand.add_card(self.Deck.remove_card())

        # Prints out final hand
        print("Here is your final hand:\n" + str(self.Hand))

        # Checks if user has four of a kind
        FourOfAKind = False
        ThreeOfAKind = False
        Pair = False
        
        for s in Card.kinds:
            matches = self.Hand.num_matches(s)
            
            if (matches == 4):
                FourOfAKind = True
            elif (matches == 3):
                ThreeOfAKind = True
            elif (matches == 2):
                Pair = True

        # Prints if user has full house or pairs
        # Prints if user has four of a kind
        if (FourOfAKind):
            print("Congrats, you got a four of a kind!")        
        elif (ThreeOfAKind and Pair):
            print("Congrats, you got a full house!")
        elif (ThreeOfAKind):
            print("Congrats, you got a three of a kind!")
        elif (Pair == True):
            print("Congrats, you got a pair!")
        else:
            print("You did not get a pair.")
    
    
    @staticmethod
    def is_in(array, length, searchval):
        for i in range(length):
            if (array[i] == searchval):
                return True
        return False
    

def main(): 
    game = Poker()
    game.play_game()
    
    
# runs main module on startup
if __name__ == "__main__":
    main()
    
    
