cs4099
Class State

java.lang.Object
  extended by cs4099.State

public class State
extends java.lang.Object

This class represents a collection of Piles - it holds a specific state in a Solitaire game.

Author:
David Newton
See Also:
Pile

Constructor Summary
State(State parent)
          Creates a new State
 
Method Summary
 void addPile(Pile pile)
          Add a pile to this state (used in the interpretation stage).
 java.lang.String canPerformMove(Pile firstpile, Pile secondpile)
          Tests whether a move can be performed from this state, by considering two factors:
1.
 State copy()
          Create an exact copy of this state, with duplicates of all piles and cards.
 int countCards()
          Counts the number of cards in the entire state.
 int countCardsInFoundations()
          Counts the number of cards in foundations.
 boolean equals(State newstate)
          Method to test the state against another to check for duplicates.
 Card[] findrequiredcards()
          Finds the cards required by the foundations in this state.
 Move[] findValidMoves()
          Finds and returns all the valid moves from this state.
 State getSortedState()
          Returns a new state with piles in order of properties, based on this state.
 int hashCode()
          Returns a hash code for this state.
 void outputState()
          Output this state to the command line in the form "[ID]: [Cards]".
 void performMove(int firstpileno, int secondpileno)
          Performs a move on the two piles corresponding to the passed values.
 void updateFoundations()
          Updates the foundations of this state - gets all piles with the "isFoundation" attribute set to true, and adds their ID values into the foundations array for quick access.
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

State

public State(State parent)
Creates a new State

Parameters:
parent - The state from which the new state was created
Method Detail

addPile

public void addPile(Pile pile)
Add a pile to this state (used in the interpretation stage).

Parameters:
pile - The pile to add.

outputState

public void outputState()
Output this state to the command line in the form "[ID]: [Cards]".


copy

public State copy()
Create an exact copy of this state, with duplicates of all piles and cards.

Returns:
The copied state.

canPerformMove

public java.lang.String canPerformMove(Pile firstpile,
                                       Pile secondpile)
Tests whether a move can be performed from this state, by considering two factors:
1. Whether the source pile can have its top card removed
2. Whether the target pile can accept the top card from the first pile.
The return type is a String for error reporting reasons - it will be blank if the move could be made successfully, and a description of the problem if it could not.

Parameters:
firstpile - The source pile for the move
secondpile - The target pile for the move
Returns:
An empty string if the move can be performed, or an error message if it could not.

performMove

public void performMove(int firstpileno,
                        int secondpileno)
Performs a move on the two piles corresponding to the passed values.

Parameters:
firstpileno - The number of the source pile.
secondpileno - The number of the target pile.

equals

public boolean equals(State newstate)
Method to test the state against another to check for duplicates.

Parameters:
newstate - The state to compare with.
Returns:
True if the states are equivalent, False if not.

findrequiredcards

public Card[] findrequiredcards()
Finds the cards required by the foundations in this state.

Returns:
The array of required cards.

getSortedState

public State getSortedState()
Returns a new state with piles in order of properties, based on this state.

Returns:
The sorted state.

hashCode

public int hashCode()
Returns a hash code for this state.

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code.

updateFoundations

public void updateFoundations()
Updates the foundations of this state - gets all piles with the "isFoundation" attribute set to true, and adds their ID values into the foundations array for quick access.


countCardsInFoundations

public int countCardsInFoundations()
Counts the number of cards in foundations.

Returns:
The number of cards that are in foundations.

countCards

public int countCards()
Counts the number of cards in the entire state.

Returns:
The number of cards found.

findValidMoves

public Move[] findValidMoves()
Finds and returns all the valid moves from this state.

Returns:
The array of available moves.