Introduction to OOP: Chapter 8: The Solitaire Game [next] [previous] [audio] [real] [text]

The class Card Pile

class CardPile {
	CardPile (int xl, int yl) { x = xl; y = yl; cardList = new LinkedList(); }

	public Card top() { return (Card) cardList.front(); }
	public boolean empty() { return cardList.empty(); }
	public Card pop() {
		Card result = (Card) cardList.front();
		cardList.pop();
		return result; }

		// the following are sometimes overridden

	public boolean includes (int tx, int ty) {
		return x <= tx && tx <= x + Card.width &&
			y <= ty && ty <= y + Card.height; }
	public void select (int tx, int ty) { }
	public void addCard (Card aCard) { cardList.add(aCard); }
	public void display (Graphics g) {
		g.setColor(Color.black);
		if (cardList.empty())
			g.drawRect(x, y, Card.width, Card.height);
		else
			top().draw(g, x, y); }

	public boolean canTake (Card aCard) { return false; }

		// coordinates of the card pile
	protected int x;
	protected int y;
	protected LinkedList cardList;
}
Intro OOP, Chapter 8, Slide 14