Natan Azizov
Natan Azizov

Reputation: 11

Java stacks simple solitaire

So for my assignment I need to create simple solitaire (no GUI) using stacks. We are allowed to implement the Stack API from Java. We are also supposed to have 3 classes. Card, CardStack, Stackitaire

So my Card class is fairly simple. It creates a Card object with the parameters suit,value, and isFaceUp. All is good here.

Then I create the CardStack class which represents a stack of class.There are 4 types: 's' - stock, 'w' - waste, 'f' - foundations, and 't' - tableau. It is supposed to have the following. I also included a private field "type" in the object and a getter and setter for it.

public class CardStack extends Stack {
private char type;

public char getType() {
    return type;
}

public void setType(char type) {
    this.type = type;
}

public CardStack(char type) {
    this.setType(type);
    Stack<Card> Stack = new Stack<Card> ();
}

public void pushes(Card newCard) {
    this.push(newCard);
}

public Card pops() {
    this.pop();
    return null;
}

public boolean isEmpty() {
    return this.empty();
}

public int size() {
    return capacityIncrement;
}

However I feel like this is too easy and wrong. Does that make sense?

Now here is what we are supposed to do for the actual meat of the game.

1."you should have an array of stacks that represent the tableau stacks, an array of stacks that represent the foundation stacks, and 2 individual stacks that represent the stock and waste stacks respectively"

Why do we need an array of stacks? Why isn't one stack enough? Is this what my professor wants?

static CardStack[] tableau;
static CardStack[] foundation;
static CardStack waste = new CardStack('w');
static CardStack stock = new CardStack('s');

Then we create a main stack of the 52 cards which I did like this

static Card[] mainStack = new Card[53];

for (int i = 1; i < 13; i++) {
        for (int j = 1; j < 4; j++) {
            mainStack[i * j] = new Card(i, j, true);
        }
    }

I think that part is right.

Ok then "move all 52 cards from the game stacks into the deck stack. Then, shuffle the deck by using the static method shuffle() in the Collections class "

Here is where I get caught up. The mainstack is an Array of Card that has 52 cards in it. The deck stack is a stack which is not an array. How do I put the array into the stack and then how do I use the method shuffle on a stack. Isn't the shuffle method only for arrays?

There is a bunch more but I guess that's enough for now

Upvotes: 0

Views: 851

Answers (1)

ChiefTwoPencils
ChiefTwoPencils

Reputation: 13930

Why do we need an array of stacks? Why isn't one stack enough?

Think about the way the game is played. You have a foundation which is 7, IIRC, stacks of foundation cards. You also have 4 stacks for the table that you're working your cards to. Then of course you have single stacks of cards for the waste and stock.

Is this what my professor wants?

They may be the only one to truly answer that question, but it looks good to me.

How do I put the array into the stack and then how do I use the method shuffle on a stack. Isn't the shuffle method only for arrays?

You can push elements onto the stack, but I wouldn't shuffle the stack. It's not in the wheel-house of functionality for a stack. If you put them in and then shuffled them, your expected order no longer exists. Shuffle the array, then distribute the cards in the appropriate stacks in the same manner in which one would deal the game.

Upvotes: 2

Related Questions