All pastes #404486 Raw Edit

Untitled

public text v1 · immutable
·published 2007-03-21 01:33 UTC
rendered paste body
public class CardPile {
    

    Stack<Card> 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] ; 
        size = 0;
    }
    
    public boolean isEmpty() {
           
        return (size==0);
    }
    
    public boolean addCard(Card card) {
        if (size == maxsize)    return false; 
        Card temcard = pile.push(Card card);          //  < ?
        (Card)pile[size]=card;
        size++; 
        return true; 
    }
        
    
    public Card takeTopCard() {
        if (isEmpty())
            return null;
        else {
            Card tempcard=(Card)pile.pop();
   //       pile[size-1] = null;
            size--;
            return tempcard;
        }
    }
    
    public Card topCard() {
        /*
        if (isEmpty())
            return null;
        else
            return pile[size-1];
         * OR
         **/
        return (isEmpty()? null: (Card)pile.peek());
    }