|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectks.common.model.Element
ks.common.model.Stack
Models a stack of cards.
A stack of cards is a linear sequence of cards with a bottom (position 0) and a top card (position n-1 where n is the number of cards in the stack). Each card maintains its own state (i.e., its value, whether it is face up, whether it is selected).
Cards can be added to the top of the stack with the add (Card c)
or the push (Stack s)
method. If a Stack
of cards is
pushed onto the top of the stack, the bottom-most card of the stack s is added
to the top of the target stack. Any state changing operation in the stack (add,
get, push, getSelected, select) resets the selected status of all cards in the
stack.
The Card get()
method removes and returns the top card in the stack.
The boolean select()
method selects just the top card in the stack.
The boolean select(int n)
method selects the top n cards, if
available. The selected state of the desired cards is altered.
The Stack getSelected()
method removes and returns a stack of cards,
s, from the top of the stack such that all cards in s are selected, if any are
selected.
Stack has some basic helper methods that may prove useful, such as
alternatingColors, ascending, descending, sameColor, sameRank,
sameSuit
. The methods rank()
and suit()
return
the rank and suit (respectively) of the topmost card in the Stack, if one exists;
if not, then Integer.MAX_VALUE is returned by default.
Creation date: (9/30/01 9:45:50 PM)
Constructor Summary | |
Stack()
Construct an empty stack with auto-generated name. |
|
Stack(java.lang.String name)
Create an empty Stack with the given name. |
Method Summary | |
void |
add(Card c)
Add a card into the stack. |
boolean |
alternatingColors()
Determines whether Cards in the Stack are all alternating in color. |
boolean |
alternatingColors(int start,
int end)
Determines whether Cards in the Stack are all alternating in color. |
boolean |
ascending()
Determines whether Cards in the Stack are all ascending in rank order. |
boolean |
ascending(int start,
int end)
Determines whether Cards in the Stack are all ascending in rank order. |
int |
count()
Returns the number of Cards in the Stack. |
boolean |
descending()
Determines whether Cards in the Stack are all descending in rank order. |
boolean |
descending(int start,
int end)
Determines whether Cards in the Stack are all descending in rank order. |
boolean |
deselect()
Reset all card selections. |
boolean |
empty()
Determines whether the Stack is empty. |
Card |
get()
Remove top card from the pile and return it to the callee (or return null if empty). |
int |
getNumSelectedCards()
Returns the number of Cards that are selected from the stack. |
Stack |
getSelected()
Return Stack of the selected cards. |
Card |
peek()
Peek and return a copy of the top-most card (or null if empty). |
Card |
peek(int idx)
Peek and return a copy of the card identified by idx in the stack. |
void |
push(Stack s)
Push Stack onto existing stack; the stack parameter object is unchanged. |
int |
rank()
Returns the rank of the top card. |
void |
removeAll()
Remove All cards from the stack. |
boolean |
sameColor()
Determines whether Cards in the Stack are all of the same color. |
boolean |
sameColor(int start,
int end)
Determines whether Cards in the Stack are all of the same color. |
boolean |
sameRank()
Determines whether Cards in the Stack are all of the same rank. |
boolean |
sameRank(int start,
int end)
Determines whether Cards in the Stack are all of the same rank. |
boolean |
sameSuit()
Determines whether Cards in the Stack are all of the same suit. |
boolean |
sameSuit(int start,
int end)
Determines whether Cards in the Stack are all of the same suit. |
boolean |
select()
Select the top-most card in the Stack. |
boolean |
select(int newNumSelectedCards)
Select the top n cards in the Stack, where n is a number from 1 - count(). |
int |
suit()
Return the suit of the topmost card in the Stack. |
java.lang.String |
toString()
Return String representation of Stack |
Methods inherited from class ks.common.model.Element |
getListener, getName, setListener, setName |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public Stack()
public Stack(java.lang.String name)
name
- the name of this Stack model element.Method Detail |
public void add(Card c)
Generates modelChanged action if c is non-null
c
- ks.common.model.Cardpublic boolean alternatingColors()
Returns true if the stack is empty.
public boolean alternatingColors(int start, int end)
Throws exception if Stack is empty.
start
- the start of the range (zero as first card in Stack)end
- the end of the range (not included in the check, and no greater than count())
public boolean ascending()
Returns true if the stack is empty.
public boolean ascending(int start, int end)
Throws exception if Stack is empty.
start
- the start of the range (zero as first card in Stack)end
- the end of the range (not included in the check, and no greater than count())
public int count()
public boolean descending()
Returns true if the stack is empty.
public boolean descending(int start, int end)
Throws Exception if the stack is empty.
start
- the start of the range (zero as first card in Stack)end
- the end of the range (not included in the check, and no greater than count())
public boolean deselect()
Equivalent to select (0).
Generates modelChanged action.
public boolean empty()
public Card get()
Generates modelChanged action only if stack is non-empty.
public int getNumSelectedCards()
public Stack getSelected()
The cards in the Stack returned by this function are all de-selected.
Generates modelChanged action only if there were some selected cards.
public Card peek()
public Card peek(int idx)
peek() == peek (count() - 1)
idx
- int (a number between 0 and numCards-1)
public void push(Stack s)
Generates modelChanged action only if Stack s is non-empty.
s
- ks.common.model.Stackpublic int rank()
public void removeAll()
Generates modelChanged action only if stack had cards.
public boolean sameColor()
Note that an empty stack returns true
as the degenerate case.
public boolean sameColor(int start, int end)
Throws an exception on an empty stack.
start
- the start of the range (zero as first card in Stack)end
- the end of the range (not included in the check, and no greater than count())
public boolean sameRank()
true
as the degenerate case.
public boolean sameRank(int start, int end)
Throws an exception on an empty stack.
start
- the start of the range (zero as first card in Stack)end
- the end of the range (not included in the check, and no greater than count())
public boolean sameSuit()
true
as the degenerate case.
Creation date: (9/30/01 10:16:30 PM)
public boolean sameSuit(int start, int end)
Throws an exception on an empty stack.
start
- the start of the range (zero as first card in Stack)end
- the end of the range (not included in the check, and no greater than count())
public boolean select()
Generates modelChanged action if Stack is non-empty.
public boolean select(int newNumSelectedCards)
Generates modelChanged action (because some games might wish to show selected cards in a different light than non-selected cards).
newNumSelectedCards
- int
public int suit()
public java.lang.String toString()
toString
in class Element
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |