0

I have a simple stack of strings to represent a card deck already and I need to randomly move the cards into a new deck. I initially was going to just use Collections.shuffle and be done with it, but one of the requirements is to use the random method and this is where I'm getting tripped up, I'm sure it's something super simple but the whole going from string to int thing still confuses me.

Here's my stack of cards.

    import java.util.Stack;

public class Deck {
   public static void main(String[] args) {

      Stack<String> stackOfCards = new Stack<>();

         stackOfCards.push("2Spade");
         stackOfCards.push("3Spade");
         stackOfCards.push("4Spade");
         stackOfCards.push("5Spade");
         stackOfCards.push("6Spade");
         stackOfCards.push("7Spade");
         stackOfCards.push("8Spade");
         stackOfCards.push("9Spade");
         stackOfCards.push("10Spade");
         stackOfCards.push("JSpade");
         stackOfCards.push("QSpade");
         stackOfCards.push("KSpade");
         stackOfCards.push("ASpade");
         stackOfCards.push("2Diamond");
         stackOfCards.push("3Diamond");
         stackOfCards.push("4Diamond");
         stackOfCards.push("5Diamond");
         stackOfCards.push("6Diamond");
         stackOfCards.push("7Diamond");
         stackOfCards.push("8Diamond");
         stackOfCards.push("9Diamond");
         stackOfCards.push("10Diamond");
         stackOfCards.push("JDiamond");
         stackOfCards.push("QDiamond");
         stackOfCards.push("KDiamond");
         stackOfCards.push("ADiamond");
         stackOfCards.push("2Club");
         stackOfCards.push("3Club");
         stackOfCards.push("4Club");
         stackOfCards.push("5Club");
         stackOfCards.push("6Club");
         stackOfCards.push("7Club");
         stackOfCards.push("8Club");
         stackOfCards.push("9Club");
         stackOfCards.push("10Club");
         stackOfCards.push("JClub");
         stackOfCards.push("QClub");
         stackOfCards.push("KClub");
         stackOfCards.push("AClub");
         stackOfCards.push("2Heart");
         stackOfCards.push("3Heart");
         stackOfCards.push("4Heart");
         stackOfCards.push("5Heart");
         stackOfCards.push("6Heart");
         stackOfCards.push("7Heart");
         stackOfCards.push("8Heart");
         stackOfCards.push("9Heart");
         stackOfCards.push("10Heart");
         stackOfCards.push("JHeart");
         stackOfCards.push("QHeart");
         stackOfCards.push("KHeart");
         stackOfCards.push("AHeart");       

    System.out.println("Deck: " + stackOfCards);
    }
}
H. Alex
  • 7
  • 2

1 Answers1

0

Note that a Stack is a List, and elements can therefore be accessed by index: it has methods get(x), set(x). Thanks to @andreas for pointing that out to me.

So, here's a very simple approach: (there are better, but I am going for simple).

Get a couple of random indexes in the range of 0 to one less than the size of the array, say k and j, and switch the element at index k with that at index j. Do that a sufficiently large number of times.

  • 1
    Since a `Stack` *is* a `List`, you can apply that [Fisher–Yates shuffle](http://en.wikipedia.org/wiki/Fisher-Yates_shuffle) algorithm directly to the stack. – Andreas Feb 21 '19 at 00:45
  • Hmm, that is indeed true. I had supposed we were limited to the actual methods (push/pop, etc) declared in the Stack interface. I'll edit my answer. Thank you. –  Feb 21 '19 at 01:07
  • It has to be in a stack for the assignment which is causing the problem, I've found tons of posts for basic card shuffling like this but none of them are stacks. I need to move the cards from one stack to another stack using random. – H. Alex Feb 21 '19 at 01:09
  • @H.Alex So pick a random card from the current "stack" (aka `List`) and move it to the new `Stack`. Repeat until all cards moved. – Andreas Feb 21 '19 at 01:11
  • I'm so sorry I'm still not getting it, I can get the fisher-yates shuffle working fine with a array of integers, but I'm cant figure it out with the stack or arraylist. – H. Alex Feb 21 '19 at 02:51
  • Using fisher-yates shuffle on a stack is a hassle.That is why they are suggesting to make it a ```List``` instead of a ```Stack``` – Mark Melgo Feb 21 '19 at 03:54
  • Never mind I changed my major I'm done with computer science – H. Alex Feb 23 '19 at 00:50