java uses ArrayList to realize landlord fighting (disordered version)

  • 2021-09-05 00:12:30
  • OfStack

In this paper, we share the specific code of java using ArrayList to realize landlord fighting for your reference. The specific contents are as follows

Case introduction

According to the rules of fighting landlords, complete the action of shuffling and dealing cards. Specific rules:

Use 54 cards to disrupt the order, 3 players participate in the game, 3 people alternately touch cards, each with 17 cards, and the last 3 cards are reserved as cards.

Case analysis

Step 1 Prepare cards

Each playing card consists of two parts: color and number. The assembly of playing cards can be completed by nesting the color set and the number set iteratively.

2. Licensing

After the playing cards are transferred, they are disrupted and rearranged by Collections shuffle method, and the last three cards are used as cards, and the remaining cards are dealt in turn by taking the mold of three cards.

3. Look at the cards

Print the collection.

Code demo


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

/**
 * @author layman
 */
public class Poker {
 // Cards stack 
 private static ArrayList<String> pokerBox = new ArrayList<>();
 // Set of colors 
 private static ArrayList<String> colors = new ArrayList<>();
 // Create a collection of numbers 
 private static ArrayList<String> numbers = new ArrayList<>();
 //3 Famous player 
 private static ArrayList<String> playerOne = new ArrayList<>();
 private static ArrayList<String> playerTwo = new ArrayList<>();
 private static ArrayList<String> playerThree = new ArrayList<>();
 private static ArrayList<String> diPai = new ArrayList<>();

 public static void main(String[] args) {
  createPoker();
  faPai();
  showCards();
 }
 /**
 *  Create playing cards and shuffle them 
 */
 public static void createPoker(){
  //4 Species color 
  colors.add("♥");
  colors.add("♦");
  colors.add("♠");
  colors.add("♣");
  //13 Number 
  for(int i = 2;i <= 10;i++){
   numbers.add(i+"");
  }
  numbers.add("J");
  numbers.add("Q");
  numbers.add("K");
  numbers.add("A");
  // Generate playing cards 
  for (String color : colors) {
   for(String number : numbers){
    String card = color+number;
    pokerBox.add(card);
   }
  }
  pokerBox.add(" Xiao Wang ");
  pokerBox.add(" King ");
  // Shuffle cards (randomly disturb the order of playing cards) 
  Collections.shuffle(pokerBox);
 }
 /**
  *  Licensing 
  */
 public static void faPai(){
  // Licensing 
  for(int i = 0;i < pokerBox.size();i++){
   String card = pokerBox.get(i);
   if(i >= 51){
    // Finally 3 Zhang as the card 
    diPai.add(card);
   } else {
    if(i%3 == 0){
     playerOne.add(card);
    }else if(i%3 == 1){
     playerTwo.add(card);
    }else{
     playerThree.add(card);
    }
   }
  }
 }
 /**
  *  Look at cards 
  */
 public static void showCards(){
  System.out.println(" Gambler: " + playerOne);
  System.out.println(" Gambler: " + playerTwo);
  System.out.println(" King of Gambling: " + playerThree);
  System.out.println(" Card: " + diPai);
 }
}

Related articles: