Landlord 1.0

Case introduction

According to the rules of Doudizhu, complete the action of shuffling the cards.
Specific rules:

  • Using 54 cards to shuffle the order, three players participate in the game, three of them draw cards alternately, each of which has 17 cards, and the last three are reserved as hole cards.

Case analysis

Prepare cards:

  • The cards can be designed as an ArrayList, Each string is a card. Each card is composed of two parts of suit and number. We can use suit set and number set to iterate to complete the assembly of each card. The cards are sorted randomly by the shuffle method of the Collections class.

Deal cards

  • Design everyone and the hole cards as an ArrayList, store the last 3 cards directly on the hole cards, and the remaining cards will pass through the 3 The cards are dealt in sequence by taking the modulus.

Watch cards

  • Print each set directly.

Case implementation

package demo04;



import java.util.ArrayList;
import java.util.Collections;

/*
Comprehensive case of Doudizhu:
1. Prepare cards
2. Shuffle
3. Licensing
4. Look at the cards
*/
public class DouDiZhu {
public static void main(String[] args) {
//1. Prepare cards
// defines an ArrayList collection of 54 cards, generic Use String
ArrayList poker = new ArrayList<>();
//Define two arrays, one array stores the suits of cards, An array stores the serial numbers of the cards
String[] colors = {"♠","♥","♣","♦"};
String[] numbers
= {"2","A","K","Q","J","10","9","8","7", "6","5","4","3"};
//First save the king and the king into the poker collection< /span>
poker.add("大王");
poker.add(
"小王");
// Loop nesting to traverse two arrays to assemble 52 cards
for(String number: numbers){
for (String color: colors) {
//System.out.println(color+number);
//Store the assembled cards in the poker collection span>
poker.add(color+number);
}
}
//System.out.println(poker);

/*
2. Shuffle
Methods in Collections, a tool class that uses collections
static void shuffle(List list) Use the default random source to replace the specified list.
*/
Collections.shuffle(poker);
//System.out.println(poker);

/*
3. Licensing
*/
//Define 4 sets to store the player’s cards and hole cards
ArrayList player01 = new ArrayList<>();
ArrayList
player02 = new ArrayList<>();
ArrayList
player03 = new ArrayList<>();
ArrayList
diPai = new ArrayList<>();

/*
Traverse the poker collection and get each card
Use the index %3 of the poker set to deal with 3 players in turn
The remaining 3 cards to the hole cards
Note:
First judge the hole card (i>=51), otherwise the card will be dealt out
*/
for (int i = 0; i ) {
//Get every card
String p = poker.get(i);
//Licensing in turn
if(i>=51){
//Hand out cards
diPai.add(p);
}
else if(i%3==0 ){
//Deal card to player 1
player01.add(p);
}
else if(i%3==1 ){
//Deal cards to player 2
player02.add(p);
}
else if(i%3==2 ){
// Deal player 3 cards
player03.add(p);
}
}

//4. Watch the cards
System.out.println("Andy Lau:"+player01);
System.out.println(
"Zhou Yunfa:"+player02);
System.out.println(
"Xingchi Zhou:"+player03);
System.out.println(
"The hole card:"+diPai);
}
}

Execution result

Share picture

package< /span> demo04;



import java.util.ArrayList;
import java.util.Collections;

/*
Comprehensive case of Doudizhu:
1. Prepare cards
2. Shuffle
3. Licensing
4. Look at the cards
*/
public class DouDiZhu {
public static void main(String[] args) {
//1. Prepare cards
// defines an ArrayList collection of 54 cards, generic Use String
ArrayList poker = new ArrayList<>();
//Define two arrays, one array stores the suits of cards, An array stores the serial numbers of the cards
String[] colors = {"♠","♥","♣","♦"};
String[] numbers
= {"2","A","K","Q","J","10","9","8","7", "6","5","4","3"};
//First save the king and the king into the poker collection< /span>
poker.add("大王");
poker.add(
"小王");
// Loop nesting to traverse two arrays to assemble 52 cards
for(String number: numbers){
for (String color: colors) {
//System.out.println(color+number);
//Store the assembled cards in the poker collection span>
poker.add(color+number);
}
}
//System.out.println(poker);

/*
2. Shuffle
Methods in Collections, a tool class that uses collections
static void shuffle(List list) Use the default random source to replace the specified list.
*/
Collections.shuffle(poker);
//System.out.println(poker);

/*
3. Licensing
*/
//Define 4 sets to store the player’s cards and hole cards
ArrayList player01 = new ArrayList<>();
ArrayList
player02 = new ArrayList<>();
ArrayList
player03 = new ArrayList<>();
ArrayList
diPai = new ArrayList<>();

/*
Traverse the poker collection and get each card
Use the index %3 of the poker set to deal with 3 players in turn
The remaining 3 cards to the hole cards
Note:
First judge the hole card (i>=51), otherwise the card will be dealt out
*/
for (int i = 0; i ) {
//Get every card
String p = poker.get(i);
//Licensing in turn
if(i>=51){
//Hand out cards
diPai.add(p);
}
else if(i%3==0 ){
//Deal card to player 1
player01.add(p);
}
else if(i%3==1 ){
//Deal cards to player 2
player02.add(p);
}
else if(i%3==2 ){
// Deal player 3 cards
player03.add(p);
}
}

//4. Watch the cards
System.out.println("Andy Lau:"+player01);
System.out.println(
"Zhou Yunfa:"+player02);
System.out.println(
"Xingchi Zhou:"+player03);
System.out.println(
"The hole card:"+diPai);
}
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1761 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.