/**
 * @author Yoshihiro Fujio
 * CS239 section 2
 * PA2
 * V1 date 2007/03/11
**/
/**********************************************************************
 * BridgeDriver.java
 *
 * This will welcome user, introduce the Bridge game, ask 4 player's
 * names, jump to each method, show the result of the game, tell the
 * player with highest hand, and give the end program message.
 ********************************************************************/
   import java.util.Scanner;

    public class BridgeDriver
   {
       public static void main(String args[])
      {
         final int PLAYERS = 4; // numbers of players
         String[] player; // Array of players
			Deck myDeck; // Deck of cards
			BridgeHand[] hand; // Array of BridgeHands
			Player[] play; // Array of Player
			Scanner keyb; // keyboard scanner
			
			keyb = new Scanner(System.in);
      	player = new String[PLAYERS]; // set array to 4
			myDeck = new Deck();
			hand = new BridgeHand[PLAYERS]; // set array to 4
			play = new Player[PLAYERS]; // set array to 4
			
			
			System.out.println("Welcome to Bridge Game.\n"); // welcome user
			// introducing program
			System.out.println("This program will ask 4 player's name,\n" +
			                    " play the Bridge Game, and Announce the\n" +
									  " player with the highest hand\n");
      

			
			// asking player's name
         for (int ii = 0; ii < PLAYERS; ii++)
         {			
            System.out.println("What is player " +  (ii + 1) + "'s name: ");
            player[ii]= keyb.nextLine();
         }
			
			for (int ii = 0; ii < PLAYERS; ii++)
			{
				hand[ii] = new BridgeHand(); // instantiate each BridgeHand
				play[ii] = new Player(player[ii], hand[ii]); // instantiate each Player
			}
			
			myDeck.shuffle(); // shuffle the deck
			System.out.println(myDeck); // check to make sure deck is shuffled
			
			
			// give cards to each players
			for (int ii = 0; ii < 13; ii++)
			{
				// give one card to player for each time until all player get 13 cards
				for ( int round = 0; round < PLAYERS; round++)
				{
					hand[round].putCardInHand(myDeck.deal());
					
				}
			}
			
			// show the result of players' hands and their total points
        for (int ii = 0; ii < PLAYERS; ii++)
		  {
		  		System.out.println(play[ii]);
		  }
		  
			
			// check if all player has same points
		  if (hand[0].evaluateHand() == hand[1].evaluateHand() &&	
		  			hand[0].evaluateHand() == hand[2].evaluateHand() &&
					hand[0].evaluateHand() == hand[3].evaluateHand())
		  	System.out.println("all players have the same points.");	
			
		  // check if three players have the highest card or not
		  else if (hand[0].evaluateHand() == hand[1].evaluateHand() && 
		  			hand[0].evaluateHand() == hand[2].evaluateHand() &&
					hand[0].evaluateHand() > hand[3].evaluateHand())
		  	System.out.println(player[0] + ", " + player[1] + " and " + player[2] + " has the highest hand.");
		  else if (hand[0].evaluateHand() == hand[1].evaluateHand() && 
		  			hand[0].evaluateHand() == hand[3].evaluateHand() &&
					hand[0].evaluateHand() > hand[2].evaluateHand())
		  	System.out.println(player[0] + ", " + player[1] + " and " + player[3] + " has the highest hand.");
		  else if (hand[0].evaluateHand() == hand[2].evaluateHand() && 
		  			hand[0].evaluateHand() == hand[3].evaluateHand() &&
					hand[0].evaluateHand() > hand[1].evaluateHand())
		  	System.out.println(player[0] + ", " + player[2] + " and " + player[3] + " has the highest hand.");
		  else if (hand[1].evaluateHand() == hand[2].evaluateHand() && 
		  			hand[1].evaluateHand() == hand[3].evaluateHand() &&
					hand[1].evaluateHand() > hand[0].evaluateHand())
		  	System.out.println(player[1] + ", " + player[2] + " and " + player[3] + " has the highest hand.");	
			
		  // check if two players have highest points or not
		  else if (hand[0].evaluateHand() == hand[1].evaluateHand() &&
		  			  		hand[0].evaluateHand() > hand[2].evaluateHand() &&
							hand[0].evaluateHand() > hand[3].evaluateHand())
		  	System.out.println(player[0] + " and " + player[1] + " has the highest hand.");	
		  else if (hand[0].evaluateHand() == hand[2].evaluateHand() &&
		  			  		hand[0].evaluateHand() > hand[1].evaluateHand() &&
							hand[0].evaluateHand() > hand[3].evaluateHand())
		  	System.out.println(player[0] + " and " + player[2] + " has the highest hand.");
		  else if (hand[0].evaluateHand() == hand[3].evaluateHand() &&
		  			  		hand[0].evaluateHand() > hand[1].evaluateHand() &&
							hand[0].evaluateHand() > hand[2].evaluateHand())
		  	System.out.println(player[0] + " and " + player[3] + " has the highest hand.");	
		  else if (hand[1].evaluateHand() == hand[2].evaluateHand() &&
		  			  		hand[1].evaluateHand() > hand[0].evaluateHand() &&
							hand[1].evaluateHand() > hand[3].evaluateHand())
		  	System.out.println(player[1] + " and " + player[2] + " has the highest hand.");
		  else if (hand[1].evaluateHand() == hand[3].evaluateHand() &&
		  			  		hand[1].evaluateHand() > hand[2].evaluateHand() &&
							hand[1].evaluateHand() > hand[0].evaluateHand())
		  	System.out.println(player[1] + " and " + player[3] + " has the highest hand.");	
		  else if (hand[2].evaluateHand() == hand[3].evaluateHand() &&
		  			  		hand[2].evaluateHand() > hand[1].evaluateHand() &&
							hand[2].evaluateHand() > hand[0].evaluateHand())
		  	System.out.println(player[2] + " and " + player[3] + " has the highest hand.");	
			
		  // check if player 1 has the highest card or not
		  else if (hand[0].evaluateHand() > hand[1].evaluateHand() &&	
		  			hand[0].evaluateHand() > hand[2].evaluateHand() &&
					hand[0].evaluateHand() > hand[3].evaluateHand())
		  	System.out.println(player[0] + " has the highest hand.");
		  // check if player 2 has the highest card or not
		  else if (hand[1].evaluateHand() > hand[0].evaluateHand() &&	
		  			hand[1].evaluateHand() > hand[2].evaluateHand() &&
					hand[1].evaluateHand() > hand[3].evaluateHand())
		  	System.out.println(player[1] + " has the highest hand.");
		  // check if player 3 has the highest card or not
		  else if (hand[2].evaluateHand() > hand[1].evaluateHand() &&	
		  			hand[2].evaluateHand() > hand[0].evaluateHand() &&
					hand[2].evaluateHand() > hand[3].evaluateHand())
		  	System.out.println(player[2] + " has the highest hand.");
		  // check if player 4 has the highest card or not
		  else if (hand[3].evaluateHand() > hand[1].evaluateHand() &&	
		  			hand[3].evaluateHand() > hand[2].evaluateHand() &&
					hand[3].evaluateHand() > hand[0].evaluateHand())
		  	System.out.println(player[3] + " has the highest hand.");
				
					
      System.out.println();
		System.out.println("this program ended normally"); // end message
      
      } // end main
   } // end class