Player.java
This is the syntax highlighted version of Player.java
from 3.5 Inheritance of
Introduction to Computer Science by
Robert Sedgewick and Kevin Wayne.
/*************************************************************************
* Compilation: javac Player.java
* Execution: java -classpath .:cards.jar Player
* Dependencies: Card.java Draw.java
*
* Implement a player that holds a pile of cards.
*
*************************************************************************/
public class Player {
final static int MAX_CARDS = 52; // max number of cards in a hand
private Card[] cards = new Card[MAX_CARDS]; // the cards
private int N = 0; // number of cards
private String name; // player's name
private int x, y; // location to draw
public Player(String name, int x, int y) {
this.name = name;
this.x = x;
this.y = y;
}
public Card peak() { return cards[0]; } // return first card
public void dealTo(Card c) { cards[N++] = c; } // insert card
public void reset() { N = 0; } // discard all cards
// compute value of blackjack hand
public int value() {
int val = 0;
boolean hasAce = false;
for (int i = 0; i < N; i++) {
val = val + cards[i].rank();
if (cards[i].isAce()) hasAce = true;
}
if (hasAce && (val <= 11)) val = val + 10; // handle ace = 1 or 11
return val;
}
// draw the pile of cards, with the first one centered at (x, y)
public void draw(Draw d) {
d.go(x, y);
for (int i = 0; i < N; i++) {
cards[i].draw(d);
d.goForward(25); // 25 seems like a good intercard distance
}
}
// print out cards in player's hand (for debugging)
public String toString() {
String s = name + " (" + value() + ") ";
for (int i = 0; i < N; i++)
s += cards[i] + " ";
return s;
}
}
Last updated: Tue Jun 22 11:30:01 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.