Collection framework – Landlords game

The code is as follows:

import java. util. ArrayList; import java. util. Collections; import java. util. HashMap; /* * Realize the function of simulating landlords * 1 Combination card * 2 Shuffle * 3 Licensing * 4 Watch cards * / public class doudizhu {public static void main (string [] args) {/ / 1. Combine cards. Create a map set. The key is the number and the value is the card. HashMap < integer, string > hooker = new HashMap < integer, string > (); / / create a list set and store the number ArrayList < integer > hookernumber = new ArrayList < integer > (); / / define an array of 13 points. String [] numbers = {"2", "a", "K" ,"Q","J","10","9","8","7","6","5","4","3"}; // Define 4 Decor arrays string [] colors={“ ♠ "," ♥ "," ♣ "," ◆ "}; / / define an integer variable that appears as a key. Int index = 2; / / traverse the array and store the combination of decor + points in the map set for (string number: numbers) {for (string color: colors) {hooker.put (index, color + number); hookernumber.add (index); index + +;}}// Storage king, and Wang hooker Put (0, "King"); pookerNumber. add(0); pooker. Put (1, "Xiao Wang"); pookerNumber. add(1); // Shuffle, shuffle the number of cards shuffle(pookerNumber); System. out. println(pookerNumber); // Licensing function: number the cards and send them to the player set. The card set ArrayList < integer > player1 = new ArrayList < integer > (); ArrayList
player2=new ArrayList

(); ArrayList

player3=new ArrayList

(); ArrayList

bottompooker=new ArrayList

(); // The set index% 3 for int (I = 0; I < poolnumber. Size()) is used for licensing; I + +) {/ / finish the cards first. If (I < 3) {/ / save the cards to the bottom. Add (hookernumber. Get (I));} else if(i%3==0){ player1.add(pookerNumber.get(i)); } Else if (I% 3 = = 1) {/ / send the number on the index to player 2 player2. Add (hookernumber. Get (I));} else if (i%3==2) { player3.add(pookerNumber.get(i)); } } // Sort the numbers in the player's hands sort(player1); Collections. sort(player2); Collections. sort(player3); // Look at the cards, find the number in the player's hand in the map set, and find the value according to the key. Define methods to implement system out. Print ("player 1:"); look(player1,pooker); System. out. Print ("\ nplayer 2:"); look(player2,pooker); System. out. Print ("\ nplayer 3:"); look(player3,pooker); System. out. Print ("\ N cards:"); look(bottompooker,pooker); } Public static void look (ArrayList < integer > player, HashMap < integer, string > poker) {/ / traverse the ArrayList collection, obtain elements as keys, and find values in the set map for (integer key: player) {string value = poker.get (key); system.out.print (value + "");}}}





The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>