/* * CardPile.java * * Created on March 9, 2007, 12:45 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package FreeCell; import java.util.Stack; /** * * @author cps109 */ public class CardPile { Stack pile; // array pile of cards int maxsize; // maximum size of pile int size; // index of next card to be added /** Creates a new instance of CardPile */ public CardPile(int maxsize) { this.maxsize = maxsize; // Card pile[] = new Card[maxsize]; // Stack stack = new Stack(); size = 0; } public boolean isEmpty() { /* if (size==0) return true; else return false; ** OR return (size==0? true: false); ** OR */ return (size==0); } public boolean addCard(Card card) { if (size == maxsize) return false; Card card = pile.push(card) ; size++; return true; } public Card takeTopCard() { if (isEmpty()) return null; else { Card mycard = (Card)pile.pop(); /** Card tempcard = pile[size-1]; pile[size-1] = null; size--; return tempcard; */ } } public Card topCard() { /* if (isEmpty()) return null; else return pile[size-1]; * OR **/ // return (isEmpty()? null: pile[size-1]); return (isEmpty()? null: (Card)pile.peek() ); } public String toString() { String result = "Card pile of max size "+maxsize+" and current size "+size+" with elements: "; if (isEmpty()) return result + ""; for (int i=0; i