This is a mock bucket landlord's mini-game, the last to see the card type is sorted, the specific code and explanation are as follows:
Package cn.poker_test;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.HashMap;
Import Java.util.TreeSet;
/*
* Simulated bucket Landlord games
*
Ideas
* A: Create a HashMap collection
* B: Create a ArrayList collection
* C: Create a suit array and an array of points
* D: Store numbers in HashMap and store the corresponding cards from 0 onwards
* also store the number in ArrayList
* E: Shuffle (Wash is numbered)
* F: The card is issued (also the number of the hair, in order to ensure that the number is sorted, create TreeSet collection receive)
* G: Look at the cards (Traverse TreeSet collection, get the numbers, find the corresponding cards to the HashMap collection)
*/
public class Pokergamedemo {
public static void Main (string[] args) {
Create a HashMap collection
Hashmap<integer, string> HM = new Hashmap<integer, string> ();
Create a ArrayList collection
arraylist<integer> array = new arraylist<integer> ();
Creating suit arrays and array of points
string[] Colors = {"", "" "," "," "};
Define an array of points
string[] numbers = {"3", "4", "5", "6", "7", "8", "9", "Ten", "J", "Q", "K", "A", "2"};
Starting from 0, store the number in the HashMap and store the corresponding card, and store the number in the ArrayList.
int index = 0;
for (String number:numbers) {
for (String color:colors) {
String Poker = color.concat (number);
Hm.put (index, poker);
Array.add (index);
index++;
}
}
Hm.put (Index, "Xiao Wang");
Array.add (index);
index++;
Hm.put (Index, "King");
Array.add (index);
Shuffle (Wash is numbered)
Collections.shuffle (array);
The card is issued (the number is also numbered, in order to ensure that the number is sorted, create TreeSet collection receive
treeset<integer> Zhangxiaoqi = new treeset<integer> ();
treeset<integer> Meichangsu = new treeset<integer> ();
treeset<integer> Linqingxia = new treeset<integer> ();
treeset<integer> Dipai = new treeset<integer> ();
for (int x=0;x<array.size (); x + +) {
if (X>array.size ()-4) {
Dipai.add (Array.get (x));
} else if (x%3==0) {
Zhangbingwei.add (Array.get (x));
} else if (x%3==1) {
Meichangsu.add (Array.get (x));
} else if (x%3==2) {
Linqingxia.add (Array.get (x));
}
}
Look at the cards (traverse the TreeSet collection, get the numbers, find the corresponding cards to the HashMap collection)
Lookpoker ("Zhang Xiao Qi", zhangbingwei,hm);
Lookpoker ("lists", MEICHANGSU,HM);
Lookpoker ("Brigitte", LINQINGXIA,HM);
Lookpoker ("Hole cards", DIPAI,HM);
}
public static void Lookpoker (String name, treeset<integer> ts, Hashmap<integer, string> HM) {
System.out.print (name+ "The card is:");
for (int key:ts) {
String value = Hm.get (key);
System.out.print (value+ "");
}
System.out.println ();
}
}
Operation Result:
Zhangxiao's cards are: 3 4 6 6 8 9 J Q K K A 2 2 2 xiao Wang King
Lists's cards are: 3 4 5 5 5 6 8 9 9 J q q K a 2
Brigitte's cards are: 3 3 4 4 5 6 7 7 8 9 Ten J J Q K A
Card cards are: 7 7 8
This article is from the "Love Network-Love life" blog, please be sure to keep this source http://18071480160.blog.51cto.com/5230907/1718758
Java collection exercises: Bucket landlord games