Home > Java > javaTutorial > How to implement the card shuffling and dealing system of Doudizhu game in Java

How to implement the card shuffling and dealing system of Doudizhu game in Java

PHPz
Release: 2023-04-24 15:55:07
forward
1074 people have browsed it

    1. Landlord Fight

    1. Foreword

    I believe that every student who has watched "God of Gamblers" knows that there is a A magical power similar to magic.

    Yes! The magical thing we are going to talk about today is: special function (not????)

    is: the card shuffling and dealing system of Landlord! ! !

    2. Introduction

    I believe many people can play the classic poker game "Dudi Zhu". This time, I will use Java language to write a card shuffling and dealing program for Dou Landlord. The requirements are to follow the rules of Doudizhu. The landlord's rules complete the process of shuffling and dealing the cards.

    There are a total of 54 cards in a deck of playing cards. The cards are composed of suits and numbers or letters. There are four suits: ♠, ♣, ♦, and ♥, which represent spades, clubs, diamonds, and hearts respectively. ♛ means big king, ♝ means little king.

    There are three players participating in the Landlord game. First, the order of the 54 cards is shuffled. Then each person takes turns to draw a card. The remaining three cards are reserved as hole cards. Finally, the three players are printed on the console. cards and three hole cards.

    3. Illustration

    How to implement the card shuffling and dealing system of Doudizhu game in Java

    2. Prepare cards

    The following codes are all written in the main method! ! !

    1. Code example

    //准备牌
            HashMap<Integer,String> map=new HashMap<>();
            ArrayList<Integer> list=new ArrayList<>();
            map.put(1,"大王");
            map.put(2,"小王");
            list.add(1);
            list.add(2);
    
            String[] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
            String[] colors={"♠","♥","♦","♣"};
    
            int index=3;
            for (String number : numbers) {
                for (String color : colors) {
                    map.put(index,number + color);
                    list.add(index);
                    index++;
                }
            }
    Copy after login

    Note: The code here needs to import two packages:

    import java.util.HashMap;

    import java.util.ArrayList;

    2. Data display

    Use System.out.println(map);and System.out.println(list);The statement displays the data as follows:

    {1=大王, 2=小王, 3=2♠, 4=2♥, 5 =2♦, 6=2♣, 7=A♠, 8=A♥, 9=A♦, 10=A♣, 11=K♠, 12=K♥, 13=K♦, 14=K♣, 15 =Q♠, 16=Q♥, 17=Q♦, 18=Q♣, 19=J♠, 20=J♥, 21=J♦, 22=J♣, 23=10♠, 24=10♥, 25 =10♦, 26=10♣, 27=9♠, 28=9♥, 29=9♦, 30=9♣, 31=8♠, 32=8♥, 33=8♦, 34=8♣, 35 =7♠, 36=7♥, 37=7♦, 38=7♣, 39=6♠, 40=6♥, 41=6♦, 42=6♣, 43=5♠, 44=5♥, 45 =5♦, 46=5♣, 47=4♠, 48=4♥, 49=4♦, 50=4♣, 51=3♠, 52=3♥, 53=3♦, 54=3♣}
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 , 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49 , 50, 51, 52, 53, 54]

    3. Shampooing cards and sorting

    Code example

    	//洗牌
    	Collections.shuffle(list);
    	
    	//发牌
    	ArrayList<Integer> diPai=new ArrayList<>();
    	ArrayList<Integer> player1=new ArrayList<>();
    	ArrayList<Integer> player2=new ArrayList<>();
    	ArrayList<Integer> player3=new ArrayList<>();
    	
    	for (int i = 0; i < list.size(); i++) {
    	    if(i>=51){
    	        diPai.add(list.get(i));
    	    }else if(i%3==0){
    	        player1.add(list.get(i));
    	    }else if(i%3== 1){
    	        player2.add(list.get(i));
    	    }else {
    	        player3.add(list.get(i));
    	    }
    	}
    	//排序
        Collections.sort(diPai);
        Collections.sort(player1);
        Collections.sort(player2);
        Collections.sort(player3);
    Copy after login

    Note: The code written here needs to be imported A package:

    import java.util.Collections;

    4. Check the cards (print to the console)

    1. Code example

    //看牌
        look("周润发",player1,map);
        look("刘德华",player1,map);
        look("周星驰",player1,map);
        look("底牌",diPai,map);
    Copy after login

    Among them, to create a look method:

      private static void look(String name, ArrayList<Integer> list, HashMap<Integer, String> map) {
            System.out.println(name+":");
            for (Integer number : list) {
                System.out.print(map.get(number)+" ");
            }
            System.out.println();
        }
    Copy after login

    2. Show

    Chow Yun-fat:
    A♣ K♣ Q♥ Q ♣ J♠ J♦ J♣ 10♥ 10♦ 10♣ 9♠ 6♠ 6♦ 5♦ 4♦ 3♠ 3♦
    Andy Lau:
    A♣ K♣ Q♥ Q♣ J♠ J♦ J♣ 10 ♥ 10♦ 10♣ 9♠ 6♠ 6♦ 5♦ 4♦ 3♠ 3♦
    Stephen Chow:
    A♣ K♣ Q♥ Q♣ J♠ J♦ J♣ 10♥ 10♦ 10♣ 9♠ 6 ♠ 6♦ 5♦ 4♦ 3♠ 3♦
    House card:
    2♥ A♠ 7♠

    The above is the detailed content of How to implement the card shuffling and dealing system of Doudizhu game in Java. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template