import java.awt.*; import BuildStack; import Deck; import Hand; import Card; import java.lang.Object; import java.util.Stack; /* The player structure consists of: a string for their name store - a Stack of Cards hand - an array of Cards discard - an array of Stacks of Cards. The player functions are responsible for moving the cards between the elements of the player and the build stacks which are global to all players. */ public class Player { protected Stack store = new Stack(); protected Stack discard [] = new Stack[4]; protected Hand hand; protected int size_of_hand = 5; public String name; public int storecount; public Player (String str) /* Function: Constructor. Alloacates memory for an array of four stacks. */ { int i; name = new String (str); for ( i = 0; i < 4; i++) { discard[i] = new Stack(); } } public boolean choosePlay (BuildStack b[], int gamestate[], Deck deck) /* Dummy routine to allow for dynamic binding of computer player's choosePlay routine. */ { return true; } public boolean fillHand (Deck d, int handsize) /* sets handsize if its not equal to five. then calls the hand classes' routine that deals a card to each empty slot of the hand array. */ { if (handsize != size_of_hand) size_of_hand = handsize; return hand.fillHand(d, handsize); } public boolean emptyHand () { return hand.isEmpty(); } public void fillStore (int storesize, int handsize, Deck source) //Fill the player's store stack and hand { int j = 0; storecount = storesize; hand = new Hand (handsize); hand.fillHand (source); while (j < storesize) { store.push (source.Deal ()); j++; } } public void showPlayer (int game[], BuildStack build[]) /* updates gamestate arry with all the player data in specific format. The gamestate array is used by the applet to identify the top card of the current players stacks and hand as well as the top card of each build stack. */ { int i; Card c; Object object; for (i = 0; i < 4; i++) { if (build[i].empty()) game[i] = 0; else { c = (Card) build[i].peek(); game[i] = c.showUseValue(); } } hand.getHandValues(game); if (!store.empty()) { object = store.peek (); c = (Card) object; game[9] = c.showUseValue(); } else game[9] = 0; for (i=0; i < 4; i++) { if (discard[i].empty()) { game[i+10] = 0; } else { object = discard[i].peek (); c = (Card) object; game[i+10] = c.showUseValue(); } } for (i = 0; i < 14; i++) //for testing purposes System.out.print (" " + game[i]); System.out.println(); } public boolean playStore (BuildStack b) /* Function: Attempts to play a card from the store stack to one of the build stacks. If the card is successfully played to a build stack, then the card is popped from the store stack into temp. */ { Card temp; if (!store.empty()) { Object object = store.peek (); temp = (Card) object; if (b.push(temp)) { //pop the card into oblivion object = store.pop (); return true; } } return false; } public boolean playHand (BuildStack b, int index) /* Function: Attempts to play a card from the hand to a build stack. If the card does not fit onto the stack then it is replaced in the hand. Returns: false if the hand is empty, true if cards remain. */ { Card temp = hand.getCard (index); if (!b.push(temp)) { hand.addCard (temp, index); } return hand.isEmpty (); } public boolean playHand (BuildStack b, int index, Deck deck, int size) /* Function: Attempts to play a card from the hand to a build stack. If the card does not fit onto the stack then it is replaced in the hand. Returns: false if the hand is empty, true if cards remain. */ { Card temp = hand.getCard (index); if (!b.push(temp)) { hand.addCard (temp, index); return false; } if (hand.isEmpty ()) { hand.fillHand (deck, size); } return true; } public boolean playDiscard (BuildStack b, int index) /* Function: Attempts to play from a discard stack to a build stack. The discard stack to play is specified in this case. The card is popped from the hand only if it will fit onto a build stack. Accepts: The array of build stacks, a value for wildcards, an index that specifies which discard stack to play from. */ { Card temp; if (!discard[index].empty()) { temp = (Card) discard[index].pop(); if (!b.push(temp)) { discard[index].push(temp); return false; } return true; } return false; } public boolean playFinish (int index1, int index2) /* Function: Finishes a players turn by playing a card from the hand to a specified discard stack. Accepts: The value of the card to play and the discard stack to play to. */ { Card temp = hand.getCard (index1); if (temp.showUseValue () > 0) { discard[index2].push (temp); return true; } else { hand.addCard (temp); return false; } } public boolean playToBuild (BuildStack b, Card c) /* Function: Attempts to play a card to one of the build stacks. Accepts: The card and the build stack. Returns: 1 if the card was successfully played to a stack. 0 if the card could not be played to any stack. */ { if (b.push (c)) { return true; } else return false; } public boolean storeEmpty () { return (store.empty()); } public void dispose () /*allows dynamic binding of computer player's dispose routine */ { } }