catalogue
1, Create and initialize playing cards
1. Shuffle cards from front to back
2. Shuffle cards from back to front
1. 17 sheets for each person, 3 persons in total
2. 17 licensing rounds, 3 times per round
preface
The method of realizing the function is very important. The logic expressed by yourself is not necessarily the best. Therefore, you need to refer to other people's methods to understand and use them.
What matters is the logic of shuffling, which is the standard poker shuffling rules.
Tip: the following is the main content of this article. The following cases can be used for reference
1, Create and initialize playing cards
1. Create playing cards -A total of 54 playing cards; -The properties of each playing card are the same, so array is selected 2. Define playing cards -First, use the initialization definition (preferably the enumeration type) to compare with the enumeration definition -It is not rigorous to define the assignment with a double-layer for loop. Do not use it. (you can't write code based on results, logic is very important)
The code is as follows (example):
String[] playingCards = {"heart A", "Hearts 2", "Heart 3", "Hearts 4", "Heart 5", "Heart 6", "Heart 7", "Hearts 8", "Heart 9", "Hearts 10", "heart J", "heart Q", "heart K", "spade A", "Spade 2", "Spades 3", "Spades 4", "Spade 5", "Spade 6", "Spade 7", "Spade 8", "Spade 9", "Spades 10", "spade J", "spade Q", "spade K", "Square slice A", "Square slice 2", "Square piece 3", "Square slice 4", "Square piece 5", "Square slice 6", "Square slice 7", "Square piece 8", "Square slice 9", "Square piece 10", "Square slice J", "Square slice Q", "Square slice K", "Plum blossom A", "Plum blossom 2", "Plum blossom 3","Plum blossom 4", "Plum blossom 5", "Plum blossom 6", "Plum blossom 7", "Plum blossom 8", "Plum blossom 9", "Plum blossom 10", "Plum blossom J", "Plum blossom Q", "Plum blossom K", "king", "Xiao Wang"};
2, Shuffle rules
Random number, two data exchanges, for loop
1. Shuffle cards from front to back
-Draw the cards in the front n position from front to back -Draw cards in the range of n ~ 54 with random numbers (random. Nextint (max min + 1) - min) -Repeat the above process 53 times
The code is as follows (example):
//Shuffle from front to back, and the minimum value will gradually increase by 1 int max = 53; int min = 1; Random random = new Random(); int index = random.nextInt(max-min+1)-min);
2. Shuffle cards from back to front
-Draw the cards in the back n position from the back to the front -Random number extracts the integer at position 0 ~ n for the subscript of playing cards, which is equivalent to randomly extracting the front playing cards -Swap two cards (two data exchange positions) -Repeat the above process 53 times
//The array has a subscript. Create a random number, extract the subscript, and exchange the position of the last two numbers Random random = new Random(); //Shuffle from the back to the front, select a card from the back, randomly select a card, exchange data, complete the shuffle, and repeat the process 53 times for (int i=playingCards.length-1;i>0;i--) {//Greater than 0 is to omit the last exchange //Draw the last card, shuffle each time, and move the position forward one bit String card = playingCards[i]; //Randomly draw the subscripts of cards without exchange position int index = random.nextInt(i); //Swap randomly selected cards with the last card (cards to be shuffled) playingCards[i] = playingCards[index]; playingCards[index] = card; //Repeat the above process, }
3, Licensing process
-Business rules for licensing -There are three people, each with 17 cards, and the last three cards are the bottom cards -There are two licensing ideas in the licensing stage
//Initialize 3 people and 17 cards String[] p1 = new String[17]; String[] p2 = new String[17]; String[] p3 = new String[17];
1. 17 sheets for each person, 3 persons in total
int index = 0; for (int i=0;i<17;i++) { p1[i] = playingCards[index]; p2[i] = playingCards[index+1]; p3[i] = playingCards[index+2]; index+=3; }
2. 17 licensing rounds, 3 times per round
int index = 0; for (int i=0;i<=48;i+=3) { p1[index] = playingCards[i]; p2[index] = playingCards[i+1]; p3[index] = playingCards[i+2]; index++; }
//Deposit cards String[] base = {playingCards[51],playingCards[52],playingCards[53]};
4, Output to console
Enhance the for loop. Compared with for, foreach only writes fewer loops, and will be compiled into a normal for loop at compile time
System.out.print("p1: "); for (String card : p1) { System.out.print(card+ " "); } System.out.println(); System.out.print("p2: "); for (String card : p2) { System.out.print(card+ " "); } System.out.println(); System.out.print("p3: "); for (String card : p3) { System.out.print(card+ " "); } System.out.println(); System.out.print("a hand: "); for (String card : base) { System.out.print(card+ " "); }
summary
-The enumeration type is used to define fixed content. Although the for loop can also be used, it is not rigorous and is not recommended.
-You can't write results oriented code. Logic is important.
-A fool can write code that a computer can understand, while a good programmer writes code that people can understand.
-The computer only knows 0 or 1. The random number draws lessons from the algorithm designed by "Gartner" and realizes the API of random number.