//
//  CurrentPlay.java
//  cribbage
//
//  Created by Nathan Haug on Sun Mar 07 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//
import java.lang.Math;

public class CurrentPlay {
	static final int maxCards = 20;
	
	private Card currentPlay[];	
	private int numCards;
	private int total;
	
	// constructor
	public CurrentPlay () {
		currentPlay = new Card[maxCards];
		numCards = 0;
		total = 0;
	}
	
	// copy constructor
	public CurrentPlay (CurrentPlay myPlay) {
		currentPlay = new Card[maxCards];
		numCards = 0;
		total = 0;
		
		for (int i = 0; i < myPlay.numCards(); i++)
			this.playCard(myPlay.getCard(i));
	}
	
	public Card getCard(int pos) {
		//Pre:  pos in 0..maxCards-1
		//Post: Card is still in Hand
		return currentPlay[pos];
	}
	
	public int getTotal() {
		//Pre:  The currentPlay is created
		//Post: The total (summation of all cards) of the currentPlay is returned
		return total;
	}
	
	public int numCards() {
		//Pre:  The currentPlay is created
		//Post: The number of cards in currentPlay is returned
		return numCards;
	}
	
	public int playCard(Card aCard) {
		//Pre:  none
		//Post: aCard is now in Pegging play, total scored is returned
		
		int pointsScored;
		
		// Put the card in play
		currentPlay[numCards++] = aCard;
		
		// Add the value of the total in play
		int cardWorth = aCard.getRank();
		if (cardWorth > 10)
			cardWorth = 10;
		total += cardWorth;
		
		// Figure the amount scored on the play and return it
		pointsScored = pointsScored();

		
		return pointsScored;
	}
	
	public int pointsScored() {
		// Figures the amount of points scored on the latest card
		
		int pointsScored = 0;
		int seqCards = 1;
		int compCards = numCards;
		Card compCard;
		Card baseCard = this.getCard(numCards-1);
		
		// Look for pairs
			for (int j = (numCards-2); j >= 0; j--) {
				compCard = this.getCard(j);
				
				// For a pair (match in rank):
				if (baseCard.getRank() == compCard.getRank()) {
					pointsScored += 2; // Give two points
					//System.out.println("Pegs 2 points for a pair.");
				}
				else
					break;
			}
		
		// Look for a total of 15
			if (total == 15) {
				pointsScored += 2;
				//System.out.println("Pegs 2 points for 15.");
			}
			
		// Look for a total of 31
			if (total == 31) {
				pointsScored += 2;
				//System.out.println("Pegs 2 points for 31.");
			}
					
		// Lastly, check for runs:
			
			// compCards is the number of cards in the current test for sequences
			compCards = numCards; 
			
			// Start with the latest card played
seqCheck:   for (int i = 0; i < numCards; i++) {
				// Compare with the next card in sequence
				for (int j = i+1; j < numCards; j++) {
					// If they are not possibly in sequence with each other, break and try a smaller loop
					if (  ( !( Math.abs(currentPlay[i].getRank() - currentPlay[j].getRank() ) < (compCards) ) ) | //  the potential-sequence test
						  ( ( currentPlay[i].getRank() - currentPlay[j].getRank() ) == 0 )				 ){ //  check for non-equality
						compCards = (numCards - i - 1);
						seqCards = 1;
						break;
					}
					// Otherwise check if we've gone through all the cards
					else {
						// If we've already checked all the cards, exit the whole check
						if (j == (numCards-1) )
							seqCards++;
					}
				}
			}
			
			// System.out.println("Found sequence of "+seqCards+" cards");
			if (seqCards > 2)
				pointsScored += seqCards;
		
	return pointsScored;
	}
	
	// Testing function
	public static void main(String[] str) {
		CurrentPlay myPlay = new CurrentPlay();
		int score;
		
		// Define what cards we want:
		// Card(suit,rank) Club-0 Heart-1 Spade-2 Diamond-3
		Card card1 = new Card(1,1);
		Card card2 = new Card(2,4);
		Card card3 = new Card(3,2);
		Card card4 = new Card(3,3);
		Card card5 = new Card(1,1);
		
		// Play on the current cards
		System.out.print("Played "+card1.display());
		System.out.print("\n");
		score = myPlay.playCard(card1);
		
		System.out.println("The current play totals "+score+" points.");
		
		System.out.print("Played "+card2.display());
		System.out.print("\n");
		score = myPlay.playCard(card2);
		
		System.out.println("The current play totals "+score+" points.");
		
		System.out.print("Played "+card3.display());
		System.out.print("\n");
		score = myPlay.playCard(card3);
		
		System.out.println("The current play totals "+score+" points.");
	
		System.out.print("Played "+card4.display());
		System.out.print("\n");
		score = myPlay.playCard(card4);
		
		System.out.println("The current play totals "+score+" points.");
		
		System.out.print("Played "+card5.display());
		System.out.print("\n");
		score = myPlay.playCard(card5);
		
		System.out.println("The current play totals "+score+" points.");
		

		
	}

}
