All pastes #401124 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-03-19 03:33 UTC
rendered paste body
/*
 * 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 + "<no elements>";
        for (int i=0; i<size; i++)
            result += "|" + pile[i];
        return result + "|";
    }
    
    // This main program is used to perform unit testing on the CardPile class
    public static void main(String args[]) {
        TableUI ui = new TableUI();
        CardPile mypile = new CardPile(2);
        System.out.println("\nCreated a new pile:\n"+mypile);
        System.out.println((mypile.isEmpty()?"the pile is empty":"the pile is not empty"));
        System.out.println("topCard returns:" +mypile.topCard());
        System.out.println("takeTopCard returns: "+mypile.takeTopCard());
        mypile.addCard(new Card(1,Card.CLUB));
        System.out.println("\nAdded 1 of clubs to pile:\n"+mypile);
        System.out.println((mypile.isEmpty()?"the pile is empty":"the pile is not empty"));
        System.out.println("topCard returns:" +mypile.topCard());
        System.out.println("\ntakeTopCard returns: "+mypile.takeTopCard());
        System.out.println("the resulting pile is:\n"+mypile);
        mypile.addCard(new Card(1,Card.CLUB));        
        mypile.addCard(new Card(2,Card.HEART));     
        System.out.println("\nAdded 1 of Clubs and 2 of hearts to pile:\n"+mypile);
        System.out.println((mypile.isEmpty()?"the pile is empty":"the pile is not empty"));
        System.out.println("topCard returns:" +mypile.topCard());
        System.out.println("\ntakeTopCard returns: "+mypile.takeTopCard());
        System.out.println("the resulting pile is:\n"+mypile);
        mypile.addCard(new Card(2,Card.HEART));   
        mypile.addCard(new Card(3,Card.HEART));  
        System.out.println("\nAdded 2 and 3 of hearts to pile:\n"+mypile);
        System.out.println("3 of hearts should not have been added: size would exceed maxsize.");
    }    
}