public class WarGame
{
    private Player player1;
    private Player player2;

    private static final int NUM_CARDS = 20;

    // constructor: creates a new game of War

    public WarGame()
    {
	Deck deck = new Deck(NUM_CARDS); // no need for an instance var here
	deck.shuffle();

	Pile p1Pile = new Pile();
	Pile p2Pile = new Pile();

	for (int deal = 0; deal < NUM_CARDS / 2; deal++)
	    {
		// deal a card to player 1
		Card p1Card = deck.getTopCard();
		p1Pile.addCard(p1Card);

		// deal a card to player 2
		Card p2Card = deck.getTopCard();
		p2Pile.addCard(p2Card);
	    }

	// make players and give them cards 
	player1 = new Player(p1Pile);
	player2 = new Player(p2Pile);
    }

    // playTurn: plays one turn

    public void playTurn()
    {
	Pile played = new Pile();

	// play cards until not a tie or someone ran out of cards


	Card one, two;

	do
	    {
		one = player1.playCard();
		two = player2.playCard();

		System.out.println("Player 1 plays the " + one);
		System.out.println("Player 2 plays the " + two);

		played.addCard(one);
		played.addCard(two);
	    }
	while (one.compareTo(two) == 0 && !checkGameOver());

	// see who won this turn

	if (one.compareTo(two) > 0)
	    {
		player1.winCards(played);
		System.out.println("Player 1 wins this turn");
	    }
	else if (one.compareTo(two) < 0)
	    {
		player2.winCards(played);
		System.out.println("Player 2 wins this turn");
	    }

	System.out.println("Cards left: player 1-" + player1.countCards()
			   + " player2-" + player2.countCards());
    }

    // checkGameOver: returns true exactly when at least one player is
    // out of cards

    public boolean checkGameOver()
    {
	boolean answer;

	if (player1.countCards() == 0 || player2.countCards() == 0)
	    answer = true;
	else
	    answer = false;

	return answer;
    }

    // getWinner: returns the player who won
    // must not be called until the game is over

    public Player getWinner()
    {
	Player winner;

	if (player1.countCards() == 0 && player2.countCards() == 0)
	    winner = null;
	else if (player1.countCards() == 0)
	    winner = player1;
	else
	    winner = player2;

	return winner;
    }
}

	    

