/** 
 * GolfGame.java
 methgod in the class include
 DEAL
 USER DECIDE
 DISCARD
 */
package Golf;

import java.util.*;
import java.io.*;

public class GolfGame {

	//These are our thingies! Yay thingies!
	Vector deck = new Vector(53); 
	Vector playerHand = new Vector(4);
	Vector computerHand = new Vector(4);
//	Vector discard = new Vector(1,1);		//just one card that the user can see
	Card discard = new Card();
	
	//default Constructor
	GolfGame() {
		makeDeck(deck);
	}
	
	
//plays the frikken game...
//loops one round of turns until boolean knock = true
//then it decides who wins and outputs the winner...
// goes back to main...or something....maybe its in a giant loop...i dunno...

void playNewGame()
{	
	//this is the arbiter of the method, if someone knocks then the game is over and we see who wins.
	boolean knock = false;
	int win = 0;
	//makes a temp card for the userdecide method...it needs one apperently	
	Card cardHolder = new Card();	
	
	
	//deals cards out to the player and computer
	System.out.println("dealing, please wait...");
	deal();

while(knock == false)
{	

	//makes the user go, gives choices  of pickup a card chose which card to discard ect.

	System.out.println("the user will go first, becuase humans rock"); 
	userDecide();
	//humanknowkdecision <<<----------FINISH!!


	//this makes the computer go:

	//decide where to pick up -
	//pick up -
	//replace. -
	//discard -

	System.out.println("now the lowly computer will go");

	cardHolder = whereToPickUp();
	replace();
	knock = computerKnockDecide();
}

//checks to see who wins!

win = winCheck();

if (win == 1)
{
	System.out.println("THE HUMAN HAS TRIUMPHED OVER THE MACHINE, CONGRATULATIONS!!"); 
}
else
{
	System.out.println("THE MACHINE HAS TRIUMPHED OVER THE HUMAN, SORRY, YOU FAILED.");
}

}//end method
		


	
	public void deal()	{				
			//supposed to give the comp 4 card and the player 4 cards and display them
		for (int i = 0; i <= 4; i++)	{
		
			playerHand.setElementAt(dealCard(), i);
			computerHand.setElementAt(dealCard(), i);
	
		}//end for loop
	}//end deal


	public static Card dealCard() {
		return new Card();
	} //end deal card
	
	public void showPlayerHand()	{
		System.out.println("--------   --------");
		System.out.println("|      |   |      |");
		System.out.println("|  ?   |   |  ?   |");
		System.out.println("| -1-  |   | -2-  |");
		System.out.println("|      |   |      |");
		System.out.println("--------   --------");
		System.out.println("                   ");
		System.out.println("--------   --------");
		System.out.println("|      |   |      |");
		System.out.println("|  -    |   |  -    |");
		System.out.println("| -3-  |   | -4-  |");
		System.out.println("|      |   |      |");
		System.out.println("--------   --------");
	}// end show player hand
	

	
//this method will decide where to pick up from (either the discard pile or the deck) then it will pick a card up from that place
//and return it.
public Card whereToPickUp()
{
	Card temp = new Card();

	Card c1 = (Card)computerHand.elementAt(1);
	Card c2 = (Card)computerHand.elementAt(2);
	Card c3 = (Card)computerHand.elementAt(3);
	Card c4 = (Card)computerHand.elementAt(4);
	//checks to mkae sure that discard pile is more than all of the computers cards before picking from the deck
	if ((discard.getValue() > c3.getValue()) &&
 	   (discard.getValue() > c4.getValue() ) && 
	   (discard.getValue() > c1.getValue() + 2 ) && 		   
	   (discard.getValue() > c2.getValue() + 2 ))
	{	
		
		temp = dealCard();
	}

	else
	{
		temp = discard; 		
	}

	return temp;		
}
	
	
	
	public void userDecide()	{
		
		int arbiter;
		char controller;
		Card cardHolder = new Card();
		Card temp = new Card();
		Card cardInHand = new Card(); //hold a  card for the user to decide what to do with

		
		System.out.println("What do you want to do?");
		System.out.println("[k] - knock");
		System.out.println("DRAW");
		System.out.println("[d] -from the draw pile");
		System.out.println("[s] -from the discard pile");
		controller = SavitchIn.readChar();
				

		//the following is a switch statment to let the user decide either to discard the cardinhand or replace one of the 4 hand cards
			
			switch (controller)	{			
	//discard or keep card
				
		
				//If you want to knock!!!!
				case 'k':
				case 'K':
				

				break;
					
					
			//IF U PICK UP FROM THE DRAW!!	
				case 'd':
				case 'D':					
				//this keep the card for you to discard a hand card, implies that you are taking a card from the draw pile
					
					System.out.println("ahhh, we have risk taker");
					System.out.println("now what to do with your new card:");
					System.out.println("[n] if you want it to go strait to the discard");
					System.out.println("[number of the card you wish to throw away] if you want to keep ur new card and discard another");
					arbiter = SavitchIn.readChar();
					
					
					cardInHand = cardHolder; 
					//calls a(card.number) method from the card class to make card in hand 
									
					//the following is a switch statment to let the user decide either to discard the cardinhand or replace one of the 4 hand cards
					
					switch (arbiter)	{	
	//discard what?
						
						case '1':
							temp = (Card)playerHand.elementAt(1);
							discard(deck, temp);
							//discards the 1 card
							deck.setElementAt(cardInHand, 1);
							//this is supposed to replace the card you want to discard with the card you drew
							break;
							
						case '2':
							temp = (Card)playerHand.elementAt(2);
							discard(deck, temp);
							//discards the 2 card
							deck.setElementAt(cardInHand, 2);
							//this is supposed to replace the card you want to discard with the card you drew
							break;
							
						case '3':
							temp = (Card)playerHand.elementAt(3);
							discard(deck, temp);
								
							//discards the 3 card		
							deck.setElementAt(cardInHand, 3);
							//this is supposed to replace the card you want to discard with the card you drew
							break;
							
						case '4':
							temp = (Card)playerHand.elementAt(4);
							discard(deck, temp);
						//discards the 4 card
							deck.setElementAt(cardInHand, 4);
							//this is supposed to replace the card you want to discard with the card you drew
							break;
							
						case 'n':
						case 'N':
							discard(deck, cardInHand);
							//this little dude right up here discard the card that u picked up from the draw pile, NOT THE DISCARD PILE 
							break;
							
									
	}//end sub d switch
				
								//IF U PICK UP FROM DISCARD!!
				case 's':
				case 'S':			
				//you want to take a card from the discard pile, then decide what to do with it, you have no choice but to replace one of your cards with the card inHand
					
					cardHolder = discard; 	
				//draws from the discard pile and puts it in the card holder
				
					System.out.println("Please enter the number of the card you wish to throw away");
					arbiter = SavitchIn.readChar();
					
					switch (arbiter)	{
						
						case '1':
							temp = (Card)playerHand.elementAt(1);
							discard(deck, temp);	//discards the 1 card
							deck.setElementAt(cardInHand, 1); //this is supposed to replace the card you want to discard with the card you drew
							break;
							
						case '2':
							temp = (Card)playerHand.elementAt(2);
							discard(deck, temp);
									//discards the 2 card
							deck.setElementAt(cardInHand, 2);
							break;
							
						case '3':
							temp = (Card)playerHand.elementAt(3);
							discard(deck, temp);	
		//discards the 3 card
							deck.setElementAt(cardInHand, 3);
							break;
							
						case '4':
							temp = (Card)playerHand.elementAt(4);
							discard(deck, temp);	
		//discards the 4 card
							deck.setElementAt(cardInHand, 4);
							break;		
									
	}//end sub s switch	
							}//end CONTROL switch
	}//end userdecide
	
	
	
	
	static void discard(Vector deck, Card card)	{					
	//put a card back into the deck, to be used to cards not needed for drawing
	
		int cardnumber;						
	//holds the card
		cardnumber = card.number;
		deck.setElementAt(card, cardnumber);
	}//end discard




// makeDeck initialises a deck of 52 cards (deck without jokers) 
	// the same as a normal deck except it also makes
	// a blank card as a place holder which is at the end
	
	public static void makeDeck(Vector suparDeck) 
	{
		int number = 0;//the number card out of 52	
		char suit; //the current suit
		int changesuit = 0;//decides the current suit		
		

		//loops 4 times, 1 for each suit
		for(int k = 0; k <= 4; k++)
		{	
			changesuit++;
			
			//loops 13 times, each time creating a card in vector deck
			for(int i = 0; i <= 13; i++)
			{	
				number++;
				
				Card temp = new Card();
				
				 
				//assigns a number out of 52 to each card
		
				temp.setNumber(number);

				//assigns a value to each card (1-13)
				temp.setValue(i++);

				//assigns a point value to each card
				//in this case it happens to match face value
				temp.setPoints(i);
				
				//decides which suit to use
				if (changesuit == 1)
				{	
					suit = 'S';
				} else if (changesuit == 2)
				{	
					suit = 'C';
				} else if (changesuit == 3)
				{	
					suit = 'H';
				} else 	{	
					suit = 'D';
				}

				temp.setSuit(suit);
				suparDeck.setElementAt(temp, i);
			}
			
		}
		}



// winCheck will check to see which player (comp or computer player) wins.
// it compares the hands and sees which is lowest. then it returns true if
// the human player wins and false if the human player loses (ie: the copmuter wins.)


public int winCheck()

{	
	int accumulte = 0;
	int ptotal = 0;
	int ctotal = 0;
	Card temp;
	
	
	int pwin = 0;

	//adds up the computers total
	for(int i = 0; i <= 3; i++)
	{ 	 

		temp = (Card)computerHand.elementAt(i);
		accumulte = temp.getValue();
		ctotal = ctotal + accumulte;
	}
	
	//adds up the player's total
	for(int i = 0; i <= 3; i++)
	{ 	 

		temp = (Card)computerHand.elementAt(i);
		accumulte = temp.getValue();
		ptotal = ptotal + accumulte;
	}

	//compares the totals
	if (ptotal == ctotal)
	{
		pwin = 0; 
	}
	else if (ptotal < ctotal)
	{
		pwin = 1;
	}
	else
	{
		pwin = 0;
	}

	return pwin; 

}




public void replace()
{
	int biggest = 0;
	int max = 0;
	int i;
	Card temp = new Card();
	
	Card cHand = new Card();

	for(i = 0; i < 3; i++)
	cHand = (Card)computerHand.elementAt(i);
	{
		if (cHand.getValue() > max)
		{
			max = cHand.getValue();
			biggest = i;
		}
	}
	
	temp = (Card)computerHand.elementAt(biggest);
	computerHand.setElementAt(biggest, pickup);
	discard = temp;
}


public boolean computerKnockDecide()
{

	Card c1 = (Card)computerHand.elementAt(1);
	Card c2 = (Card)computerHand.elementAt(2);
	Card c3 = (Card)computerHand.elementAt(3);
	Card c4 = (Card)computerHand.elementAt(4);

	if (((c1.getvalue()+2) + (c2.getvalue()+2) + (c3.getValue()) + (c4.getvalue())) < 10)
	{
		return true;
	}
}

	
}//end class

