public class Player
{
    private Pile supplyPile;
    private Pile winningsPile;

    // constructor: creates a player with the given hand

    public Player(Pile dealtCards)
    {
	supplyPile = dealtCards;
	winningsPile = new Pile();
    }

    // playCard: gets the top card from this player's hand
    // if this player had run through all the cards,
    // shuffles the pile of won cards and starts playng from those

    public Card playCard()
    {
	// see if we have any cards left
	int supplyCards = supplyPile.countCards();

	if (supplyCards == 0)
	    {
		// shuffle winnings
		winningsPile.shuffle();

		// swap two piles
		Pile temp = supplyPile;
		supplyPile = winningsPile;
		winningsPile = temp;
	    }

	// take top card off supplyPile
	Card playedCard = supplyPile.getTopCard();
	return playedCard;
    }

    // countCards: returns the total number of cards this player has
    // (counting cards won and cards yet to be played)

    public int countCards()
    {
	// find out how many cards are in each pile
	int supplyCards = supplyPile.countCards();
	int winningsCards = winningsPile.countCards();

	// add them up
	int total = supplyCards + winningsCards;

	// report the total
	return total;
    }

    // winCards: adds the given pile to this player's winnings
    public void winCards(Pile playedCards)
    {
	winningsPile.merge(playedCards);
    }
}

