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

/**********************************************************************
* This class plays a game of Bridge by asking for four players, giving
* each 13 cards from a shuffled deck, and determining the winner.
*
* @author Jonathan Herman
* @version 1
**********************************************************************/
// Elizabeth Adams
// Date: 3/06/07
// PA 2
// Section: 2

public class BridgeDriver
{
	/**
	* This method plays a game of Bridge by asking for four players, giving
	* each 13 cards from a shuffled deck, and determining the winner.
	*
	* @param args command line arguments-unused
	*/ 
	public static void main (String args[])
	{
		String name1, name2, name3, name4, winner;
		Player player1, player2, player3, player4;
		BridgeHand hand1, hand2, hand3, hand4, winningHand;
				
		Deck myDeck;
		Scanner keyboard;	
		
		myDeck = new Deck();
		keyboard = new Scanner (System.in);
		
		// welcomes the user and explains what the program will do
		System.out.println ("Welcome to the Bridge program. This program " +
								  "will first ask for four players. Then, it will \n" +
								  "shuffle a deck and deal out a hand of 13 cards " +
								  "to each player. After this is done, the program \n" +
								  "will determine which player(s) won the game and " +
								  "print out the results.\n");
		
		// gets the first player's name
		System.out.print ("What is the first player's name: ");
		name1 = keyboard.nextLine();
		System.out.println ("The first player's name is " + name1 + ".");
		
		// gets the second player's name
		System.out.print ("What is the second player's name: ");
		name2 = keyboard.nextLine();
		System.out.println ("The second player's name is " + name2 + ".");
		
		// gets the third player's name
		System.out.print ("What is the third player's name: ");
		name3 = keyboard.nextLine();
		System.out.println ("The third player's name is " + name3 + ".");
		
		// gets the fourth player's name
		System.out.print ("What is the fourth player's name: ");
		name4 = keyboard.nextLine();
		System.out.println ("The fourth player's name is " + name4 + ".\n");
		
		// creates the hands
		hand1 = new BridgeHand (name1);
		hand2 = new BridgeHand (name2);
		hand3 = new BridgeHand (name3);
		hand4 = new BridgeHand (name4);
		
		// shuffles the deck
		myDeck.shuffle();
		
		// deals out 13 cards to each player's hand
		for (int x = 0; x < 13; x++)
		{
			hand1.putCardInHand (myDeck.deal());
			hand2.putCardInHand (myDeck.deal());
			hand3.putCardInHand (myDeck.deal());
			hand4.putCardInHand (myDeck.deal());
		}
		
		// gives hands to players
		player1 = new Player (name1, hand1);
		player2 = new Player (name2, hand2);
		player3 = new Player (name3, hand3);
		player4 = new Player (name4, hand4);
		
		// prints out each player's hand and score.
		System.out.println (player1);
		System.out.println (player2);
		System.out.println (player3);
		System.out.println (player4);
		
		// finds the high score
		winningHand = hand1;
		if (hand2.evaluateHand() > winningHand.evaluateHand())
			winningHand = hand2;
		if (hand3.evaluateHand() > winningHand.evaluateHand())
			winningHand = hand3;
		if (hand4.evaluateHand() > winningHand.evaluateHand())
			winningHand = hand4;
		
		// determines the winner
		winner = winningHand.getName();
		
		// checks to see if there was a tie
		if (winningHand.equalScore (hand1))
			winner += " and " + hand1.getName();
		if (winningHand.equalScore (hand2))
			winner += " and " + hand2.getName();
		if (winningHand.equalScore (hand3))
			winner += " and " + hand3.getName();
		if (winningHand.equalScore (hand4))
			winner += " and " + hand4.getName();
		
		// prints out the results
		System.out.println (winner + " won the game.\n");		
		System.out.println ("This program dealt 4 hands for each player given " +
		                    "by the user and found the winner(s).\nThis program " +
								  "has ended normally.");
	} // end main method
		
} // end BridgeDriver class