java uses HashMap to realize landlord fighting (ordered version)

  • 2021-09-05 00:11:48
  • OfStack

In this paper, we share the specific code of java using HashMap 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

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.*;

/**
 * @author layman
 */
public class Poker2 {
 //  Cards stack 
 private static Map<Integer, String> pokerMap = new HashMap<>();
 //  Design and color 
 private static ArrayList<String> colors = new ArrayList<>();
 //  Figures 
 private static ArrayList<String> numbers = new ArrayList<>();
 //  Number set of playing cards 
 private static ArrayList<Integer> numberList = new ArrayList<>();
 //  Player number set 
 private static ArrayList<Integer> noP1 = new ArrayList<>();
 private static ArrayList<Integer> noP2 = new ArrayList<>();
 private static ArrayList<Integer> noP3 = new ArrayList<>();
 //  Card number set 
 private static ArrayList<Integer> diPaiNo = new ArrayList<>();

 // 3 Players 
 private static ArrayList<String> playerOne = new ArrayList<String>();
 private static ArrayList<String> playerTwo = new ArrayList<String>();
 private static ArrayList<String> playerThree = new ArrayList<String>();
 //  Card 
 private static ArrayList<String> diPai = new ArrayList<String>();
 /**
  *  Create playing cards and shuffle them 
  */
 public static void createPoker(){
  
  Collections.addAll(colors, "♦", "♣", "♥", "♠");
  Collections.addAll(numbers, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
  //  Set the store number 
  int count = 1;
  pokerMap.put(count++, " King ");
  pokerMap.put(count++, " Xiao Wang ");
  //  Create playing cards 
  for (String number : numbers) {
   for (String color : colors) {
    String card = color + number;
    pokerMap.put(count++, card);
   }
  }
  
  //  Take the number first 
  Set<Integer> numberSet = pokerMap.keySet();
  numberList.addAll(numberSet);

  //  Then shuffle the cards randomly 
  Collections.shuffle(numberList);
 }

 /**
  *  Licensing 
  */
 public static void faPai(){
  for (int i = 0; i < numberList.size(); i++) {
   Integer no = numberList.get(i);
   //  Set aside a card 
   if (i >= 51) {
    diPaiNo.add(no);
   } else {
    if (i % 3 == 0) {
     noP1.add(no);
    } else if (i % 3 == 1) {
     noP2.add(no);
    } else {
     noP3.add(no);
    }
   }
  }
 }
 /**
  *  Deal and sort 
  */
 public static void sortCards(){
  //  Sort the numbers 
  Collections.sort(noP1);
  Collections.sort(noP2);
  Collections.sort(noP3);
  Collections.sort(diPaiNo);
  //  Conversion of card face 
  for (Integer i : noP1) {
   //  Get the card face according to the number and send it to the corresponding player 
   String card = pokerMap.get(i);
   playerOne.add(card);
  }
  for (Integer i : noP2) {
   String card = pokerMap.get(i);
   playerTwo.add(card);
  }
  for (Integer i : noP3) {
   String card = pokerMap.get(i);
   playerThree.add(card);
  }
  for (Integer i : diPaiNo) {
   String card = pokerMap.get(i);
   diPai.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);
 }
 public static void main(String[] args) {
  createPoker();
  faPai();
  sortCards();
  showCards();
 }
}

Additional:

Landlord Fighting Case Using ArrayList (Unordered Version): Portal


Related articles: