First, set a number for each card in the card, the following algorithm to achieve the number of rules are as follows:
U hearts in descending order from small to large: 1-13
U square in order from small to large: 14-26
U spades in descending order from small to large: 27-39
U plum in order from small to large: 40-52
U Xiao Wang is 53, King is 54
The algorithm is implemented as follows:
You first initialize an array of 108 digits according to the number sequence above
Every time you randomly extract a number from the array, assign it to the array that holds the player's data.
The code that implements this feature is as follows:
Copy Code code as follows:
Import java.util.*;
/**
* Implementation of the licensing algorithm
* Requirements: 2 deck cards, that is 108, to 4 people, leaving 6 cards
*/
public class exec{
public static void Main (string[] args) {
An array of 108 cards stored
int[] Total = new int[108];
Store four players ' cards
int[][] player = new INT[4][25];
Stores the number of current remaining cards
int leftnum = 108;
Random numbers
int rannumber;
Random objects
Random Random = new Random ();
Initializing an array
for (int i = 0;i < total.length;i++) {
Total[i] = (i + 1)% 54;
Handling Size King number
if (total[i] = = 0) {
Total[i] = 54;
}
}
Circular Licensing
for (int i = 0;i < 25;i++) {
A license for everyone.
for (int j = 0;j < player.length;j++) {
Generate random subscript
Rannumber = Random.nextint (leftnum);
Licensing
Player[j][i] = Total[rannumber];
Move the cards that have been sent
Total[rannumber] = total[leftnum-1];
1 reduction in the number of licences to be licensed
leftnum--;
}
}
Loop out the cards in the player's hands
for (int i = 0;i < player.length;i++) {
for (int j = 0;j < player[i].length;j++) {
System.out.print ("" + player[i][j]);
}
System.out.println ();
}
Cards
for (int i = 0;i < 8;i++) {
System.out.print ("" + total[i]);
}
System.out.println ();
}
}