com.blackledge.david.game.solitaire
Class SolitaireGame

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

public class SolitaireGame
extends Object

Solitaire 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 SolitaireDeck, SolitairePlayStack, and SolitaireGoalStack. So you could create a new Solitaire 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
 SolitaireDeck deck
          the Main deck from which you draw cards.
 DeckStack deckStack
          The stack on which drawn cards are lain.
 SolitaireGoalStack[] 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.
 SolitairePlayStack[] playStack
          The stacks where the player may place cards while working to place them in the goal stacks.
 
Constructor Summary
SolitaireGame()
          Establish a game instance.
 
Method Summary
 boolean actionDeck()
          Act on the deck.
 boolean actionDeckStack()
          Act on the identified stack.
 boolean actionGoalStack(int i)
          Act on the identified stack.
 boolean actionPlayStack(int stackNum, int stackPosition)
          Act on the identified stack.
 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 (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 Solitaire 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 SolitaireDeck deck
the Main deck from which you draw cards.


deckStack

public DeckStack deckStack
The stack on which drawn cards are lain.


playStack

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


goalStack

public SolitaireGoalStack[] 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

SolitaireGame

public SolitaireGame()
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

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

actionDeckStack

public boolean actionDeckStack()
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.

Returns:
true if the action had some effect

actionDeck

public boolean actionDeck()
Act on the deck. That is, draw cards from the deck onto the deckStack, or if the deck is empty, turn the deckStack over and use it as the deck.

Returns:
true if the action had some effect

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 Solitaire 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 (other than the stack the hand is from)

Returns:
true if the action had some effect