Dou DiZhu Mini Game: Shuffling and Dealing Cards
Task Description
Write a program for dealing and shuffling cards in Dou DiZhu, which is required to complete the process of shuffling and dealing cards in accordance with the rules of Dou DiZhu , the card surface is composed of suits, colors and numbers (including the letters J, Q, K, A), and the suits are composed of hearts, spades, diamonds and clubs. There are also big kings and little kings. The order of these 54 cards is disrupted, and three players participate in the game. Each player takes turns to draw one card at a time, and the remaining three are used as hole cards. The program ends and the cards and hole cards in each player's hand are printed.
Run results
Task objectives
Learn to analyze "The Landlord Mini Game: Shuffling and Dealing Cards" Program implementation ideas.
Be able to independently complete the code writing, compilation and operation of the "Dou Di Zhu Mini Game Shuffling and Dealing Cards" program based on ideas.
Master the characteristics of ArrayList and HashMap collections and enhance the use of for loops.
Implementation ideas
①To implement the card program, you must first add 54 cards to the program. These cards include 13 hearts, spades, clubs, and diamonds. Zhang, plus Da Wang and Xiao Wang.
② Perform a nested loop on the two sets of the suit set and the number set, combine the suits and numbers to form 52 cards, and assign numbers to them. Put the combined cards into the HashMap set. The key value is the number, and the value value is the assembled cards, as well as the big king and the small king. Since the assembly rules are inconsistent, you need to use the add() method separately to add these two cards to the HashMap collection.
③Create a number set and use this number set to replace the cards to complete the operations of shuffling and dealing cards. Since there are 54 cards in total, the range of the created set is 0-53.
④ You can use the shuffle() method of the Collections class to complete the operation of shuffling the number collection to achieve the effect of shuffling. Since there are only three people, you can use a for loop to distribute the numbers representing different cards to different people and hole cards by taking the remainder of the number and 3 to achieve the effect of dealing cards.
⑤After the shuffling and dealing of cards is completed, the sorting can be completed through the sort() method of the Collections class, and then through the enhanced for loop HashMap collection, the corresponding card strings are searched according to the numbers, and the newly created characters are stored. In the string collection, the string collection is displayed last.
Implementation code
package Swing; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; public class DoudiZhu { public static void main(String[]args) { //准备花色 ArrayList<String>color=new ArrayList<String >(); color.add("黑桃"); color.add("红桃"); color.add("方块"); color.add("梅花"); //准备数字,用列表将纸牌从大到小排列 ArrayList<String >number=new ArrayList<String >(); for(int i=3;i<=10;i++){ number.add(i+""); } number.add("J"); number.add("Q"); number.add("K"); number.add("A"); number.add("2"); //定义一个map集合,用来将数字与每一张纸牌进行对应 HashMap<Integer,String>map=new HashMap<Integer,String>(); //纸牌编号 int index=0; //循环纸牌数字 for(String thisNumber:number){ //循环纸牌花色 for(String thisColor:color){ //将花色与数字组合,形成52张牌,并赋予编号 map.put(index++,thisColor+thisNumber); } } map.put(index++,"小王"); map.put(index++,"大王"); //创建0-53的数字集合代表54张牌 ArrayList<Integer>cards=new ArrayList<Integer>(); for(int i=0;i<=53;i++){ cards.add(i); } //洗牌,使用Collections工具类的shuffle()方法 Collections.shuffle(cards); //创建三个玩家和底牌 ArrayList<Integer>iPlayer=new ArrayList<Integer>(); ArrayList<Integer>iPlayer2=new ArrayList<Integer>(); ArrayList<Integer>iPlayer3=new ArrayList<Integer>(); ArrayList<Integer>iSecretCards=new ArrayList<Integer>(); //遍历这副洗好的牌,遍历的过程中,将牌发到三个玩家和底牌中 for(int i=0;i<cards.size();i++){ if(i>=51){ //留取三个底牌 iSecretCards.add(cards.get(i)); }else{ if(i%3==0){//与3取余为0的发给玩家1 iPlayer.add(cards.get(i)); }else if(i%3==1){//与3取余为1的发给玩家2 iPlayer2.add(cards.get(i)); }else {//剩余的牌发给玩家3 iPlayer3.add(cards.get(i)); } } } //对每个人手中的牌进行排序,使用的使Collections工具类中的sort()方法 Collections.sort(iPlayer); Collections.sort(iPlayer2); Collections.sort(iPlayer3); ArrayList<String>sPlayer=new ArrayList<String>(); ArrayList<String>sPlayer2=new ArrayList<String>(); ArrayList<String>sPlayer3=new ArrayList<String>(); ArrayList<String>sSectCards=new ArrayList<String>(); //循环主键,从map中获取纸牌 for (Integer key:iPlayer){ sPlayer.add(map.get(key)); } for (Integer key:iPlayer2){ sPlayer2.add(map.get(key)); } for (Integer key:iPlayer3){ sPlayer3.add(map.get(key)); } for (Integer key:iSecretCards){ sSectCards.add(map.get(key)); } //将分发的牌显示出来 System.out.println("玩家1:"+sPlayer); System.out.println("玩家2:"+sPlayer2); System.out.println("玩家3:"+sPlayer3); System.out.println("底牌:"+sSectCards); } }
Guess the number game
Task description
Write a guessing game. This game is "You give me a number and I will guess it." ", the program pre-generates a random number from 0 to 9 in the background, and the user enters a guessed number on the keyboard. If the entered number is the same as the number pre-generated in the background, it means the guess is correct. At this time, the program will print out "Congratulations. , correct answer", if they are not the same, compare the size of the entered number with the number pre-generated in the background. If it is larger, print "sorry, you guessed it is bigger!"; if it is smaller, it will print "sorry, you guessed it is smaller" "; If you guess wrong all the time, the game will continue until the number is guessed correctly.
Running results
Task goal
Learn to analyze the implementation ideas of the "guessing number game" program.
Independently complete the source code writing, compilation and operation of the "guessing number game" based on the idea.
Master the use of if selection structure and while loop structure statements to perform operations in the program.
Implementation ideas
①To realize this function, first the program must pre-generate a random number from 0 to 9 in the background. To generate a random number, you can use the Random class nextInt(int n) method, its specific definition is as follows:
public int nextInt(int n)
② To use the keyboard to enter the guessed number, you can use the Scanner class, which allows the user to enter numbers from the keyboard.
Scanner sc=new Scanner(System.in); int i=sc.nextInt();
③After entering the number, you need to compare the number entered by the keyboard and the number pre-generated in the background. Since guessing the number does not necessarily succeed once, it is likely to be done multiple times, so the program can be used multiple times through a while loop. Input from the keyboard, and guess whether the number is right or wrong every time you enter. If you guess correctly, jump out of the loop and output "Congratulations, you got the answer right!" and the game ends.
④If you guess wrong, use the if...else statement to judge and divide the error into two results: guessing too high and guessing too low. If the guess is too high, print "sorry, your guess is too big!" and continue the next cycle; if the guess is too small, print "sorry, your guess is too small!" and continue the next cycle. Based on the result, a hint is given, and then the game continues by guessing the number.
Implementation code
package math; import java.util.Random; import java.util.Scanner; public class CaishuZi { public static void main(String[] args) { int randomNumber = new Random().nextInt(10); System.out.println("随机数已经生成"); System.out.println("请输入你所猜的数字"); Scanner sc =new Scanner(System.in); int enterNumber =sc.nextInt(); //通过while循环,进行猜数字对错判断 //猜对,跳出循环,游戏结束 while(enterNumber!=randomNumber){ //猜错了,根据结果,给出提示,接着猜数字,游戏继续 if(enterNumber>randomNumber){ //猜大了给出的提示 System.out.println("sorry,你猜大了"); }else{ //猜小了,给出的提示 System.out.println("sorry,你猜小了"); } //输入猜的数字 System.out.println("请输入你猜的数字"); enterNumber = sc.nextInt(); } System.out.println("恭喜你,答对了!"); } }
The above is the detailed content of How to implement landlord fighting and number guessing games in Java?. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment