// Arup Guha
// 12/8/09
// This class runs the main program, poker.

import java.util.*;
public class Poker {

	private CardList Deck;
	private CardList Hand;

	public Poker() {

		// Create the deck and shuffle it.
		Deck = new CardList();
		Deck.makeDeck();
		Deck.shuffle();

		// Create a hand and add in five cards from the deck.
		Hand = new CardList();
		for (int i=0; i<5; i++)
			Hand.addCard(Deck.removeCard());

	}

	public void playGame() {

		Scanner stdin = new Scanner(System.in);

		// shows hand
		System.out.println("Here is your hand:\n"+Hand);

		// Asks user which cards to exchange
		System.out.println("How many cards do you want to exchange?");
		int numCards = stdin.nextInt();

		// Imposes limits on exchange
		if (numCards > 5) numCards = 5;
		if (numCards < 0) numCards = 0;

		// Creates array for cards to be exchanged
		int[] exchangeList = new int[numCards];
		int cardNum = 0;

		// Runs as long as user wants to exchange cards
		while (cardNum < numCards) {

			System.out.println("Enter the number of the next card you want to exchange.");
			int val = stdin.nextInt();

			if (Poker.in(exchangeList,cardNum, val))
				System.out.println("Sorry, you've chosen that card already. Try again.");
			else {
				exchangeList[cardNum] = val-1;
				cardNum++;
			}
		}
		
		Arrays.sort(exchangeList);

		// Removes card - we need to remove them from the back so the indexes don't get screwed up.
		for (int i=cardNum-1; i>=0; i--)
			Hand.removeCard(exchangeList[i]);

		// Adds card
		for (int i=0; i<cardNum; i++)
			Hand.addCard(Deck.removeCard());

		// Prints out final hand
		System.out.println("Here is your final hand:\n"+Hand);

		// Checks if user has four of a kind
		boolean FourOfAKind = false;
		boolean ThreeOfAKind = false;
		boolean Pair = false;
		
		for (String s: Card.kinds) {
			int matches = Hand.numMatches(s);
			
			if (matches == 4)
				FourOfAKind = true;
			else if (matches == 3)
				ThreeOfAKind = true;
			else if (matches == 2)
				Pair = true;
		}

		// Prints if user has full house or pairs
		// Prints if user has four of a kind
		if (FourOfAKind)
			System.out.println("Congrats, you got a four of a kind!");		
		else if (ThreeOfAKind && Pair)
			System.out.println("Congrats, you got a full house!");
		else if (ThreeOfAKind)
			System.out.println("Congrats, you got a three of a kind!");
		else if (Pair == true)
			System.out.println("Congrats, you got a pair!");
		else
			System.out.println("You did not get a pair.");
	}


	public static boolean in(int[] array, int length, int searchval) {
		for (int i=0; i<length; i++)
			if (array[i] == searchval)
				return true;
		return false;
	}

	public static void main(String[] args) {
		Poker game = new Poker();
        game.playGame();
	}
}