import java.util.Scanner;
/*****************
*this method is the 
*driver for the bridge
*game
*@author - Bryan Wiseman
*@version - 1
********************/
//Date: 3/13/07
//Section: 1
//PA: 2

public class Driver
{
	/******************
	*This method welcomes
	*user and runs the
	*bridge game
	********************/
	public static void main(String[] args)
	{
		String player1;		//the player 1 name
		String player2;		//the player 2 name
		String player3;		//the player 3 name
		String player4;		//the player 4 name
		String finalScore;	//the winner of the game
		Deck deck;				//the new deck
		BridgeHand hands1;	//player 1 hand
		BridgeHand hands2;	//player 2 hand
		BridgeHand hands3;	//player 3 hand
		BridgeHand hands4;	//player 4 hand
		Player pl1;				//player 1 stats
		Player pl2;				//player 2 stats 
		Player pl3;				//player 3 stats
		Player pl4;				//player 4 stats
		int score1;				//player 1 score
		int score2;				//player 2 score
		int score3;				//player 3 score
		int score4;				//player 4 score
		Scanner scan;
		
		scan = new Scanner(System.in);
		finalScore = "";
		//welcome		
		System.out.println("Welcome to the Bridge Game");
		//program explanation
		System.out.print("This program plays a game of Bridge with four people.\n");
		//gets player 1's name
		System.out.print("Please enter name of player 1: ");
		player1 = scan.nextLine();
		System.out.println();
		//gets player 2's name
		System.out.print("Please enter name of player 2: ");
		player2 = scan.nextLine();
		System.out.println();
		//gets player 3's name
		System.out.print("Please enter name of player 3: ");
		player3 = scan.nextLine();
		System.out.println();
		//gets player 4's name
		System.out.print("Please enter name of player 4: ");
		player4 = scan.nextLine();
		System.out.println();
		//echos players name
		System.out.println("The players are: "  + player1 + ", "
								 + player2 + ", " + player3 + ", " + 
								 player4 + ".\n");
		//gets new deck
		deck = new Deck();
		//shuffles new deck
		deck.shuffle();
		//activates players hands
		hands1 = new BridgeHand();
		hands2 = new BridgeHand();
		hands3 = new BridgeHand();
		hands4 = new BridgeHand();
		//deals the cards into their hands
		for(int num = 0; num < 13; num++)
		{
			hands1.putCardInHand(deck.deal(), num);
			hands2.putCardInHand(deck.deal(), num);
			hands3.putCardInHand(deck.deal(), num);
			hands4.putCardInHand(deck.deal(), num);
		}
		//gets the stats on each player
		pl1 = new Player( player1, hands1);
		pl2 = new Player( player2, hands2);
		pl3 = new Player( player3, hands3);
		pl4 = new Player( player4, hands4);
		//gets the scores for each player
		score1 = hands1.evaluateHand();
		score2 = hands2.evaluateHand();
		score3 = hands3.evaluateHand();
		score4 = hands4.evaluateHand();
		//sees who won the game
		if(score1 > score2)
		{
			if(score1 > score3)
			{
				if(score1 > score4)
				{
					finalScore = "Player 1 wins";
				}
				else if(score1 == score4)
				{
					finalScore = "Player 1 and Player 4 tie";
				}
				else if(score1 < score4)
				{
					finalScore = "Player 4 wins";
				}
			}
			else if(score1 == score3)
			{
				if(score1 > score4)
				{
					finalScore = "Player 1 and Player 3 tie";
				}
				else if(score1 == score4)
				{
					finalScore = "Player 1 and Player 3 and Player 4 tie";
				}
				else if(score1 < score4)
				{
					finalScore = "Player 4 wins";
				}
			}
			else if(score1 < score3)
			{
				if(score3 > score4)
				{
					finalScore = "Player 3 win";
				}
				else if(score3 == score4)
				{
					finalScore = "Player 3 and Player 4 tie";
				}
				else if(score3 < score4)
				{
					finalScore = "Player 4 wins";
				}
			}
		}
		else if(score1 == score2)
		{
			if(score1 > score3)
			{
				if(score1 > score4)
				{
					finalScore = "Player 1 and Player 2 tie";
				}
				else if(score1 == score4)
				{
					finalScore = "Player 1 and Player 2 and Player 4 tie";
				}
				else if(score1 < score4)
				{
					finalScore = "Player 4 wins";
				}
			}
			else if(score1 == score3)
			{
				if(score1 > score4)
				{
					finalScore = "Player 1 and Player 2 and Player 3 tie";
				}
				else if(score1 == score4)
				{
					finalScore = "Player 1 and Player 2 and Player 3 and Player 4 tie";
				}
				else if(score1 < score4)
				{
					finalScore = "Player 4 wins";
				}
			}
			else if(score1 < score3)
			{
				if(score3 > score4)
				{
					finalScore = "Player 3 win";
				}
				else if(score3 == score4)
				{
					finalScore = "Player 3 and Player 4 tie";
				}
				else if(score3 < score4)
				{
					finalScore = "Player 4 wins";
				}
			}
		}
		else if(score1 < score2)
		{
			if(score2 > score3)
			{
				if(score2 > score4)
				{
					finalScore = "Player 2 wins";
				}
				else if(score2 == score4)
				{
					finalScore = "Player 2 and Player 4 tie";
				}
				else if(score2 < score4)
				{
					finalScore = "Player 4 wins";
				}
			}
			else if(score2 == score3)
			{
				if(score2 > score4)
				{
					finalScore = "Player 2 and Player 3 tie";
				}
				else if(score2 == score4)
				{
					finalScore = "Player 2 and Player 3 and Player 4 tie";
				}
				else if(score1 < score4)
				{
					finalScore = "Player 4 wins";
				}
			}
			else if(score2 < score3)
			{
				if(score3 > score4)
				{
					finalScore = "Player 3 win";
				}
				else if(score3 == score4)
				{
					finalScore = "Player 3 and Player 4 tie";
				}
				else if(score3 < score4)
				{
					finalScore = "Player 4 wins";
				}
			}
		}
			//prints out players stats and the winner of the game
			System.out.println(pl1.toString());
			System.out.println();
			System.out.println(pl2.toString());
			System.out.println();
			System.out.println(pl3.toString());
			System.out.println();
			System.out.println(pl4.toString());
			System.out.println();
			System.out.println(finalScore);
			System.out.println("Program ended normally");		 
	}
}