com.blackledge.david.game.solitaire
Class FreeCellGame

java.lang.Object
  extended by com.blackledge.david.game.solitaire.FreeCellGame

public class FreeCellGame
extends Object

FreeCell Card Game back end / core. Designed to have an interface built over it. Examine the various stacks and render your interface, then take user input and call the different "action" methods as appropriate - if they return true, you'll want to update your interface. Check won() to see if the game has been won. Also supports unlimited Undo. Game rules are defined by won(), the stack sizes/contents defined in newGame(), and the rule implementations in FreeCellDeck, FreeCellPlayStack, and FreeCellGoalStack. So you could create a new FreeCell back end variation by subclassing those classes, then overriding won() with any change in rules and overridding newGame() to use your subclasses.

Author:
David.Blackledge.com

Field Summary
 CardDeck deck
          the Main deck from which you draw cards.
 FreeCellGoalStack[] goalStack
          The stacks that are the object of the game - the user is trying to get all cards into the goal stacks.
 CardStack handStack
          When the user picks up a card or a stack of cards, they are held in this stack.
 FreeCellPlayStack[] playStack
          The stacks where the player may place cards while working to place them in the goal stacks.
 FreeCellSingleStack[] singleStack
          The stacks where the player may place cards while working to place them in the goal stacks.
 
Constructor Summary
FreeCellGame()
          Establish a game instance.
 
Method Summary
 boolean actionGoalStack(int i)
          Act on the identified stack.
 boolean actionPlayStack(int stackNum, int stackPosition)
          Act on the identified stack.
 boolean actionSingleStack(int i)
           
 boolean autoPlay()
          Play some things that can safely be played.
 int getFreeCount()
           
 void newGame()
          Replaces all stacks with null or new class instances, then draws cards to lay out the initial game state.
 boolean playHand()
          Attempt to play the currently held card(s) in either a goalStack or a playStack or a singleStack (other than the stack the hand is from)
 boolean putDownHand()
          put the cards currently in the handStack back where they were picked up from.
 boolean undo()
          Undo the last action.
 boolean won()
          Check if the game has been won - for FreeCell this means all 4 goalStacks have 13 cards each.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

deck

public CardDeck deck
the Main deck from which you draw cards.


playStack

public FreeCellPlayStack[] playStack
The stacks where the player may place cards while working to place them in the goal stacks.


singleStack

public FreeCellSingleStack[] singleStack
The stacks where the player may place cards while working to place them in the goal stacks.


goalStack

public FreeCellGoalStack[] goalStack
The stacks that are the object of the game - the user is trying to get all cards into the goal stacks.


handStack

public CardStack handStack
When the user picks up a card or a stack of cards, they are held in this stack.

Constructor Detail

FreeCellGame

public FreeCellGame()
Establish a game instance. Automatically calls newGame().

Method Detail

newGame

public void newGame()
Replaces all stacks with null or new class instances, then draws cards to lay out the initial game state.


undo

public boolean undo()
Undo the last action. Before every significant action, a memento is saved. If the action causes a significant change, that memento is saved on a stack. Undo pops the topmost memento and restores the game state with it.

Returns:
true if undo was performed (false if stack is empty)

actionGoalStack

public boolean actionGoalStack(int i)
Act on the identified stack. If there are cards in your hand, attempt to lay them on the stack, if there are not, attempt to pick up the top card.

Parameters:
i - index into goalStack
Returns:
true if the action had some effect

actionSingleStack

public boolean actionSingleStack(int i)

actionPlayStack

public boolean actionPlayStack(int stackNum,
                               int stackPosition)
Act on the identified stack. If there are cards in your hand, attempt to lay them on the stack, if there are not, attempt to pick up the stack of cards identified by the stackPosition.

Parameters:
stackNum - index into playStack
stackPosition - 0=top card.
Returns:
true if the action had some effect

getFreeCount

public int getFreeCount()
Returns:
the number of Free Cells

putDownHand

public boolean putDownHand()
put the cards currently in the handStack back where they were picked up from.

Returns:
true if the action had some effect

won

public boolean won()
Check if the game has been won - for FreeCell this means all 4 goalStacks have 13 cards each.

Returns:
true if the game has been won, false otherwise.

playHand

public boolean playHand()
Attempt to play the currently held card(s) in either a goalStack or a playStack or a singleStack (other than the stack the hand is from)

Returns:
true if the action had some effect

autoPlay

public boolean autoPlay()
Play some things that can safely be played. - all lowest and next higher cards that can go on goalstack. Loop as long as it returns true to autoplay all safe things.

Returns:
true if anything happened.