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.
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.
①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.
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); } }
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.
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.
①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.
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!