// Arup Guha
// 7/25/07
// Sample Program for BHCSI to illustrate arrays.
// This program generates a five card hand for poker, and then prints out
// the number of cards of the same kind and of the same suit, if there are
// repeats.

import java.util.*;

public class Poker {
	
	public static void main(String[] args) {
		
		int[] myhand = new int[5];
		Random r = new Random();
		
		// Fill hand with five distinct cards.
		int numcards = 0;
		while (numcards < 5) {
			
			// Generate a new card here.
			int newcard = r.nextInt(52);
			
			// Check to see if we have that card already.
			boolean duplicate = false;
			for (int i=0; i<numcards; i++) {
				if (newcard == myhand[i])
					duplicate = true;
			}
			
			// Add that card to our hand ONLY if we didn't already have it.
			if (!duplicate) {
				myhand[numcards] = newcard;
				numcards++;
			}	
		}
		
		// Print out the hand for the user.
		System.out.print("Here is your hand: ");
		for (int i=0; i<myhand.length; i++)
			System.out.print(getCard(myhand[i])+" ");
		System.out.println();
		
		// Print out if the user has a more than one of a kind.
		int[] freq = new int[13];
		for (int i=0; i<freq.length; i++)
			freq[i] = 0;
		
		// Count up how many of each card there is.	
		for (int i=0; i<myhand.length; i++)
			freq[myhand[i]%13]++;
			
		// Now we go through and print out any doubles, etc.
		for (int i=0; i<freq.length; i++) {
			if (freq[i] > 1)
				System.out.println("You have " + freq[i] + " number of " + getKind(i));
		}
		
		// Count up how many of each suit we have.
		freq = new int[4];
		for (int i=0; i<freq.length; i++)
			freq[i] = 0;
			
		// Count up how many of each card there is.	
		for (int i=0; i<myhand.length; i++)
			freq[myhand[i]/13]++;
			
		// Now we go through and print out any doubles, etc.
		for (int i=0; i<freq.length; i++) {
			if (freq[i] > 1)
				System.out.println("You have " + freq[i] + getSuit(13*i)+"s.");
		}
		
	}
	
	// Pre-conditions: cardnum is in between 0 and 51, inclusive.
	// Post-conditions: A character is returned that represents the suit of the
	//                  corresponding card. There is a one to one correspondence
	//                  of the integers 0 through 51 and each playing card.
	public static char getSuit(int cardnum) {
		
		// The first 13 cards are Clubs
		if (cardnum < 13)
			return 'C';
			
		// The next 13 are Diamonds
		else if (cardnum < 26)
			return 'D';
			
		// Then Hearts...
		else if (cardnum < 39)
			return 'H';
			
		// Or Spades.
		else
			return 'S';
	}
	
	// Pre-conditions: cardnum is in between 0 and 51, inclusive.
	// Post-conditions: A char is returned based on cardnum that represents
	//                  the kind of the corresponding card. There is a one
	//                  to one correspondence between the integers 0 to 51, 
	//					and the cards.
	public static char getKind(int cardnum) {
		
		// We'll assign these four cards as Kings.
		if (cardnum%13 == 0)
			return 'K';
			
		// These as Aces.
		else if (cardnum%13 == 1)
			return 'A'; 
			
		// These as Tens
		else if (cardnum%13 == 10)
			return 'T';
			
		// Jacks
		else if (cardnum%13 == 11)
			return 'J';
			
		// Queens
		else if (cardnum%13 == 12)
			return 'Q';
			
		// This naturally takes care of 2 through 9.
		else
			return (char)('0'+(cardnum%13));
	}
	
	// Pre-conditions: cardnum is in between 0 and 51, inclusive.
	// Post-conditions: A String is returned representing a playing card that
	//                  corresponds to cardnum. 
	public static String getCard(int cardnum) {
		char first = getKind(cardnum);
		char second = getSuit(cardnum);
		return "" + first + second;
	}
}