cs4099
Class Pile

java.lang.Object
  extended by cs4099.Pile

public class Pile
extends java.lang.Object

A class that represents a pile in a Solitaire game. Piles hold cards, and have specific rules as to the cards that can be placed on them at any time.

Author:
David Newton
See Also:
Card, State

Constructor Summary
Pile(int id, int capacity, int order, int uniquesuit, boolean alternatecolours, boolean samesuit, boolean removable, boolean addable, boolean isfoundation, int stocknumber, int cardstodeal, boolean isstock, int stockcapacity, int startvalue, boolean oneaway, boolean oneawaynowrap, boolean fourteens, int displayx, int displayy, boolean straightstack, boolean backwards)
          Creates a new instance of Pile.
 
Method Summary
 boolean addCard(Card card)
          Adds a card on to a pile, if it does not go against the constraints of the pile.
 void buryKings()
          Buries the Kings on this pile (sends all cards with value 13 to the beginning).
 java.lang.String canAddCard(Card card)
          Determines whether a card can be added to the pile.
 boolean canRemoveCard()
          Verifies whether the top card can be removed, without actually removing it.
 int compareTo(Pile pile)
          Returns "true" if this pile is found to be "greater than" another, based on its attributes and contents but not its ID number.
 int contains(Card[] cards)
          Returns information on whether any of a set of cards exist in this pile.
 boolean dealCard(Card card)
          Forces adding a card to a pile - used in dealing the cards rather than playing them.
 boolean equalsByContents(Pile otherpile)
          A method to compare the contents of this pile with another, and to return "true" if the contents are equal.
 Card getTopCard()
          Returns the top card of this pile - a shortcut for getting its suit and value.
 boolean isequivalent(Pile pile)
          Examines whether a pile's properties are equivalent to another, including the suits and values of the cards in them.
 Card removeCard()
          Returns the topmost card, or a null if the topmost card cannot be removed.
 java.lang.String toString()
          Returns a String representation of this pile for output.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Pile

public Pile(int id,
            int capacity,
            int order,
            int uniquesuit,
            boolean alternatecolours,
            boolean samesuit,
            boolean removable,
            boolean addable,
            boolean isfoundation,
            int stocknumber,
            int cardstodeal,
            boolean isstock,
            int stockcapacity,
            int startvalue,
            boolean oneaway,
            boolean oneawaynowrap,
            boolean fourteens,
            int displayx,
            int displayy,
            boolean straightstack,
            boolean backwards)
Creates a new instance of Pile.

Parameters:
id - The ID number of the pile.
capacity - The pile's capacity (length of the array used to hold cards)
order - The numerical order in which cards must be placed - 1 specifies building up, -1 to build down.
uniquesuit - Should be set to 0-3 if the pile should only accept one suit, -1 otherwise.
alternatecolours - Set to true if cards must be placed on the pile in alternating colours.
samesuit - Set to true if cards added to the pile must be of the same suit as the card below.
removable - Set to true if cards can be removed from this pile (unless the pile is a stock).
addable - Set to true if cards can be added from this pile.
isfoundation - Set to true if this pile is to be regarded as a goal pile.
stocknumber - The number of the stock that this pile is filled from (-1 if none).
cardstodeal - The number of random cards to deal to this pile at the start of the game.
isstock - Set to true if this pile should behave as a stock pile.
stockcapacity - The number of cards to deal from this pile (if a stock) at one time.
startvalue - The value that a card must be to fill this pile if it is empty (0 if any).
oneaway - Set to true if cards with adjacent values must be added to this pile (wrapping between A-K allowed).
oneawaynowrap - Set to true if cards with adjacent values must be added to this pile (wrapping between A-K disallowed).
fourteens - Set to true if this pile must accept cards in pairs that total fourteen numerically.
displayx - The X position for this pile on the desktop.
displayy - The Y position for this pile on the desktop.
straightstack - Set to true if only the top card is to be displayed.
backwards - Set to true if this pile should be drawn upwards instead of downwards.
Method Detail

toString

public java.lang.String toString()
Returns a String representation of this pile for output.

Overrides:
toString in class java.lang.Object
Returns:
The string representation of card suits and values, separated by tabs, or [Empty] if empty.

addCard

public boolean addCard(Card card)
Adds a card on to a pile, if it does not go against the constraints of the pile.

Returns:
True if the addition was successful.

dealCard

public boolean dealCard(Card card)
Forces adding a card to a pile - used in dealing the cards rather than playing them. The pile cannot exceed its capacity.

Returns:
True if the deal was successful.

canAddCard

public java.lang.String canAddCard(Card card)
Determines whether a card can be added to the pile. For the purposes of error output, the method has to return a String that explains the state of the move - this string will be empty if the move is valid.

Parameters:
card - The card to attempt to add to the pile.
Returns:
The error message, or an empty string if the move is valid.

removeCard

public Card removeCard()
Returns the topmost card, or a null if the topmost card cannot be removed.

Returns:
The topmost card, or null.

canRemoveCard

public boolean canRemoveCard()
Verifies whether the top card can be removed, without actually removing it.

Returns:
True if it can be removed, False otherwise.

getTopCard

public Card getTopCard()
Returns the top card of this pile - a shortcut for getting its suit and value.

Returns:
The top card, or null if there are no cards on the pile.

isequivalent

public boolean isequivalent(Pile pile)
Examines whether a pile's properties are equivalent to another, including the suits and values of the cards in them.

Parameters:
pile - The pile to compare against.
Returns:
True if the piles are identical by properties, False if not.

contains

public int contains(Card[] cards)
Returns information on whether any of a set of cards exist in this pile. The return type is int rather than boolean, as this also provides the position that the card has in the pile relative to the top card.

Parameters:
cards - The array of cards to find.
Returns:
The number of cards below the surface at which a card is found, or -1 if there was no match.

compareTo

public int compareTo(Pile pile)
Returns "true" if this pile is found to be "greater than" another, based on its attributes and contents but not its ID number.

Parameters:
pile - The pile to compare to
Returns:
1 if this pile is greater, -1 if the passed pile is greater, 0 if they are equivalent

equalsByContents

public boolean equalsByContents(Pile otherpile)
A method to compare the contents of this pile with another, and to return "true" if the contents are equal.

Parameters:
otherpile - The pile to compare to.
Returns:
"True" if the contents are equal, "False" otherwise.

buryKings

public void buryKings()
Buries the Kings on this pile (sends all cards with value 13 to the beginning). This is used in the dealing phase of some games.