“This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”
Related articles
Java with Notes: Java with Notes
- Recently, leetCood has become obsessed with writing a card shuffle implementation.
-
The first impression in your brain is made with random numbers:
- Random numbers range from 1 to 54
- Create a set to store randomly generated numbers
- New random numbers are discarded if they exist in the set, and added otherwise.
-
Take a look at the code:
-
Public static void ShuffleTheDeck() {system.out.println (" start time: " + new Date()); List reslutList = new ArrayList(); // start Random shuffle Random = new Random(); while (true) { int endRand = random.nextInt(54); reslutList.add(endRand+1); if (ifRepeat(reslutList) == false) { reslutList.remove(reslutList.size() - 1); } if (reslutList.size() >= 54) { break; }} system.out.println (" end time: "+ new Date()); for (int j = 0; j < reslutList.size(); j++) { System.out.println(reslutList.get(j).toString()); Public static Boolean ifRepeat(List List) {HashSet set = new public static Boolean ifRepeat(List List) {HashSet set = new HashSet<>(list); Boolean bool = set.size() == list.size() ? true : false; return bool; }Copy the code
- Execution Result:
- Barely?
-
-
But what are our actual cards?
- 1-10 J Q K King Xiao Wang
- A total of 15
- In addition to the big wang single, the others are four, and respectively, hearts, spades, diamonds, clubs.
- How do you optimize it?
-
Let’s add an entity class here to store the cards that are generated each time.
-
class poker { public poker(String color, String num) { super(); this.color = color; this.num = num; } String color; String num; public String toString() { return color+num; }}Copy the code
-
Let’s write generate first, and then shuffle.
-
The difference is defining the suit and the face.
-
Public static void buildPoker(){String[] colors = {" spades "," diamonds "," clubs "," hearts "}; String[] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; LinkedList pokers = new LinkedList(); for(int i=0; i < colors.length; i++) { for(int j=0; j<nums.length; j++) { pokers.add(new poker(colors[i],nums[j])); }} pokers.add(new poker(" xiao Wang "," black ")); Pokers. Add (new poker(" King "," red ")); }Copy the code
-
Now that the whole deck is generated, here’s how to shuffle it randomly.
-
Shuffle: or use random number to control!
-
Public static void buildPoker(){String[] colors = {" spades "," diamonds "," clubs "," hearts "}; String[] nums = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; LinkedList pokers = new LinkedList(); for(int i=0; i < colors.length; i++) { for(int j=0; j<nums.length; j++) { pokers.add(new poker(colors[i],nums[j])); }} pokers.add(new poker(" xiao Wang "," black ")); Pokers. Add (new poker(" King "," red ")); // shuffledpokers = new LinkedList(); while(shuffledpokers.size()<pokers.size()) { Random x = new Random(); poker poke = (poker) pokers.get(x.nextInt(pokers.size())); if(! shuffledpokers.contains(poke)) { shuffledpokers.add(poke); }} system.out. println(" before: "+pokers); System.out.println(" +shuffledpokers); }Copy the code
-
Execution Result:
- Can do shuffle!
- Individual think, although very get, but also calculate a train of thought.
- Please point out any mistakes.
The road ahead is long, I see no end, I will search high and low
If you think I bloggers write well! Writing is not easy, please like, follow, comment and give encouragement to the blogger ~ Hahah