import java.util.ArrayList;
public class PlayerIMPL implements Player, Runnable
{
//fields
private int chips = 0; //the player's number of chips.
private int playerNumber; //the player's number.
public Card[] hand = new Card[Card.HAND_SIZE]; //the player's hand of cards.
private static ArrayList <Card[]> hands = null; //This is the array of the player's hand
private static int playersWithCards = 0; //the number of cards that have all the card that they need.
public static Object readyToCompare = new Object(); //monitor for having all of the threads ready to compute.
//constructor
public PlayerIMPL(int chips, int playerNumber)
{
this.chips = chips;
this.playerNumber = playerNumber + 1;
//System.out.println("player " + playerNumber + " created");
}
//threaded method.
public void run()
{
//Get the cards for all of the players.
for (int i=0; i < Card.HAND_SIZE; i++)
{
synchronized(Game.dealer)
{
hand[i] = Game.dealer.drawCard();
}
}
compareHands(hand);
//TODO write the code for when the game is finished.
}
public int compareHands(Card[] cards)
{
synchronized(hands)
{
hands.add(cards);
}
//wait until everony has a card to cp
synchronized (readyToCompare)
{
playersWithCards++;
readyToCompare.notifyAll();
//wait until every player has their cards.
while (playersWithCards < Game.numPlayers)
{
try {
readyToCompare.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//compare the cards
String handString = "";
for (int i=0; i< Game.numPlayers; i++)
handString += "[" + hand[i] + "] ";
System.out.println("player " + playerNumber + ": " + handString);
//compare hands
}
//add the hand.
/*
int x = index + 1;
while(x != index){
if (x > (playerList.size()))
x = 0;
PlayerThread currentPlayer = playerList.get(x);
while(currentPlayer.isNotReady){
synchronized(currentPlayer){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
int result = Card.compareHands((Card[])this.hand.toArray(), (Card[])currentPlayer.getHand().toArray());
result *= -1;
System.out.println ( "player" + index + " = " + result); //##REMOVE ME######################
synchronized(currentPlayer){
currentPlayer.receivePayment(result);
}*/
return 0;
//make a new deck when finished with all cards.
}
public boolean hasRetired()
{
Game.playersActive--;
Game.gameOver.notify();
return false;
}
public void receivePayment(int payment)
{
//add the payment to the chips currently owned.
chips = chips + payment;
}
}