Question
having trouble figuring out what is wrong with my SHUFFLE DECK method in java: for each card in the deck exchange it with the card
having trouble figuring out what is wrong with my SHUFFLE DECK method in java:
"for each card in the deck exchange it with the card at a randomly-selected index"
public class Card { private int rank; private int suit; public Card (int rank, int suit) { this.rank = rank; this.suit = suit; } public int getRank() { return rank; } public int getSuit() { return suit; } public String toString() { String properRank = ""; String properSuit = ""; if (2 <= this.rank || this.rank <= 10) { properRank = String.valueOf(rank); } if (this.rank == 11) { properRank = "J"; } if (this.rank == 12) { properRank = "Q"; } if (this.rank == 13) { properRank = "K"; } if (this.rank == 14) { properRank = "A"; } if (this.suit == 1) { properSuit = "\u2660"; //Spade } if (this.suit == 2) { properSuit = "\u2665"; //Heart } if (this.suit == 3) { properSuit = "\u2663"; //Club } if (this.suit == 4) { properSuit = "\u2666"; //Diamond } return properRank + properSuit; } } -----------------------------------------------
import java.util.ArrayList; import java.util.Random;
public class Deck { private ArrayList
public Deck() { ArrayListOfCard = new ArrayList<>(); for(int a = 1; a <= 4; a++) { for(int b = 2; b <= 14; b++) { ArrayListOfCard.add(new Card(b,a)); } } } public Card topCard() { Card topCard = ArrayListOfCard.get(0); return topCard; } public void Shuffle() { Random generator = new Random(); int shuffNum = generator.nextInt(100); for (int x = 0; x < shuffNum; x++) { for (int y = 0; y < ArrayListOfCard.size(); y++) { int n = 51; int randIndex = generator.nextInt(n); Card remCard = ArrayListOfCard.get(randIndex); int tempIndex = ArrayListOfCard.indexOf(remCard); Card yCard = ArrayListOfCard.get(y); Card tempCard = ArrayListOfCard.remove(tempIndex); if (y <= ArrayListOfCard.size()-1) { ArrayListOfCard.add(y,tempCard); ArrayListOfCard.add(tempIndex,yCard); } else { ArrayListOfCard.add(remCard); } } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started