Java Study Notes 34 (Collection framework 8: comprehensive case: Simulated landlords shuffling and licensing), learning notes shuffling
Rules:
1.54 cards with color
2. The order is disordered. One person is licensed in turn, 17 are licensed, and three are reserved as cards.
3. Card Viewing: print the card in the order of the size of King 2A... 43
Example:
Package demo; import java. util. arrayList; import java. util. collections; import java. util. hashMap; public class DouDiZhu {public static void main (String [] args) {// create a Map set. The key is the number and the value is the HashMap card <Integer, string> pooker = new HashMap <Integer, String> (); // storage ID of the List set // reasons for using the List set: you can call the sorting method ArrayList <Integer> pookerNumber = new ArrayList <Integer> (); // The array String [] numbers = {"2 ", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5 ", "4", "3"}; // constant color, defined array String [] colors = {"","","",""}; // define the integer variable as the key. values 0 and 1 are left to the int index of the big-sized Wang account = 2; // traverses the array and stores the Map set for (String number: numbers) {for (String color: colors) {pooker. put (index, color + number); pookerNumber. add (index); index ++ ;}/// separate storage size Wang pooker. put (0, ""); pookerNumber. add (0); pooker. put (1, "John"); pookerNumber. add (1); // shuffles the card number to disrupt Collections. shuffle (pookerNumber); // licensing // three players and the base card ArrayList <Integer> player1 = new ArrayList <Integer> (); arrayList <Integer> player2 = new ArrayList <Integer> (); ArrayList <Integer> player3 = new ArrayList <Integer> (); arrayList <Integer> dipai = new ArrayList <Integer> (); // each part is sent to three players for (int I = 0; I <pookerNumber. size (); I ++) {// set the card first if (I <3) {dipai. add (pookerNumber. get (I);} // assign each player an else if (I % 3 = 0) {player1.add (pookerNumber. get (I);} else if (I % 3 = 1) {player2.add (pookerNumber. get (I);} else if (I % 3 = 2) {player3.add (pookerNumber. get (I) ;}}// sort the cards in the player's hands by Collections. sort (player1); Collections. sort (player2); Collections. sort (player3); // card watching, based on the key value look ("Player 1", player1, pooker); look ("Player 2", player2, pooker ); look ("player 3", player3, pooker); look ("card", dipai, pooker);} public static void look (String name, ArrayList <Integer> player, hashMap <Integer, String> pooker) {System. out. print (name + ":"); for (Integer key: player) {String value = pooker. get (key); System. out. print ("" + value);} System. out. println ();}}
Effect:
The results of each operation are inconsistent:
If you are interested, you can continue to develop your game based on the rules of landlords.