All pastes #929885 Raw Edit

Someone

public text v1 · immutable
·published 2008-03-06 02:55 UTC
rendered paste body
import java.util.ArrayList;

public class PlayerIMPL implements Player, Runnable
{	
	//fields
	private int gameNumber = 0;											//the number of the current game being played.
	private ArrayList <PlayerIMPL> players;								//the array of players
	private int chips = 0;												//the player's number of chips.
	private int playerNumber;											//the player's number.
	private int numCards;												//current number of cards given.
	private Card[] hand = new Card[Card.HAND_SIZE];						//the player's hand of cards.
	private int[] payment = new int[Game.numPlayers ];					//total of payments you owe.
	private static int numberForNewGame = 0;							//number of players ready for a new game											
	
	//monitors
	private Object readyToCompare = new Object();						//monitor for having all of the threads ready to compute.
	private Object myChipsLock = new Object();							//monitor for my chips
	private Object theirChipsLock = new Object();						//monitor for the other player's chips.
	private Object readyForNewGame = new Object();						//monitor for when the players are ready for a new game.
	
	//constructor 
	public PlayerIMPL(int chips, int playerNumber, ArrayList <PlayerIMPL> players)
	{
		this.players = players;
		this.chips = chips;
		this.playerNumber = playerNumber + 1;
	}
	
	//threaded method.
	public void run()
	{
		while(true)
		{
			//make sure only only the first thread creates a new deck.
			if(playerNumber == 1)
			{
				Game.dealer.newHand();
				gameNumber++;
			}
			
			//Get the cards for all of the players.
			for (int i=0; i < Card.HAND_SIZE; i++)
			{
				synchronized (readyToCompare)
				{
					hand[i] = Game.dealer.drawCard();
					numCards++;
					readyToCompare.notifyAll();
				}
			}
			
			//compare the hands
			int totalPayments = 0; //this will be used to get the total that the players paid out.
			for(int x=0; x < Game.numPlayers; x++)
			{
				if (x+1 != playerNumber)
				{
					//get the totals.
					PlayerIMPL opponent = players.get(x);
					payment[x] = opponent.compareHands(hand);
					
					//pay your opponent if you lost. (negative means the player lost)
					if (payment[x] < 0)
					{
						payment[x] *= -1; //make the amounts paid positive.	
						
						//remove from your stack
						synchronized(myChipsLock)
						{
							chips -= payment[x];
						}
						
						//add to their stack.
						opponent.receivePayment(payment[x]);
						
						//add to the total paid out
						totalPayments += payment[x];
					}
				}
			}
			
			//determine if the player is the winner
			String output = gameNumber + " " + playerNumber + " " + Card.toString(hand) + " " + chips;
			if(totalPayments == 0) //if the player won the round then they didn't pay anything out, unlike everyone else.
			{
				output += "*";
			}
			
			//output the final game info.
			System.out.println(output);
			
			
			//ready for a new game, is everyone else??
			synchronized(readyForNewGame)
			{
				numberForNewGame++; //update that there is yet another one ready.
				readyForNewGame.notifyAll();
				//not every player is ready for a new game.
				while(numberForNewGame < 5)
				{
					try {
						readyForNewGame.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public int compareHands(Card[] opponentsCards) 
	{	
		//wait until everony has a card to cp
		
		synchronized (readyToCompare)
		{
			//wait until every player has their cards.
			while (numCards < Card.HAND_SIZE)
			{
				try {
					readyToCompare.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			//return the amount of cash that is owed.
			return Card.compareHands(opponentsCards, hand);
		}
	}

	public boolean hasRetired() 
	{
		Game.playersActive--;
		Game.gameOver.notify();
		return false;
	}

	public void receivePayment(int payment) 
	{
		synchronized(theirChipsLock)
		{
			//give the payment to the opponent.
			chips = chips + payment;
		}
	}
}