1: Implementation with ArrayList Collection
Case analysis:
What we need to do now is to simulate the landlord game to shuffle and issue cards.
Specific steps:
A: First, create an ArrayList set to store cards. Store the card's colors and points in two string arrays. Then use the for loop to strengthen the traversal and store the card's colors and points as well as Kings and Xiaowangs in the set.
B: Shuffle the cards and call the shuffle method of collectionas.
C: Carry out licensing and create four ArrayList collections, which are used to store the cards that player 1, player 2, player 3 and the bottom cards are issued to hand respectively. Then use the for loop to traverse the card and use the If sentence to judge, and then deal the card.
D: Create a method of looking at cards and call it.
Specific code:
package com.study_01;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
* Simulated landlord shuffling and licensing
* Analysis steps:
* 1: Create a collection to store cards
* 2: Shuffle the cards
* 3: Licensing
* 4: Look at cards
*
*/
public class Poker1 {
public static void main(String[] args) {
//Create a collection to store cards
ArrayList <String> Pokers=new ArrayList<>();
String[] colors={"♠","♥","♦","♣"};
String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
for(String color:colors){
for(String number:numbers){
Pokers.add(color+number);
}
Pokers.add("king");
Pokers.add("Xiao Wang");
}//Shuffle the cards
Collections.shuffle(Pokers);
//Licensing
/*Create four collections to store
* 1: Zhang San
* 2: Li Si
* 3:Wang Ma
* 4: A hand
*/
ArrayList<String> Zhangsan=new ArrayList<>();
ArrayList<String> Lisi=new ArrayList<>();
ArrayList<String> Wangma=new ArrayList<>();
ArrayList<String> Dipai=new ArrayList<>();
for(int i=0;i<Pokers.size();i++){
if(i>=Pokers.size()-3){
Dipai.add(Pokers.get(i));
}else if(i%3==0){
Zhangsan.add(Pokers.get(i));
}else if(i%3==1){
Lisi.add(Pokers.get(i));
}else if(i%3==2){
Wangma.add(Pokers.get(i));
}
}//Look at cards
lookPokers("Zhang San",Zhangsan);
lookPokers("Li Si",Lisi);
lookPokers("Wang Ma",Wangma);
lookPokers("A hand",Dipai);
}
private static void lookPokers(String name, ArrayList<String> Pokers){
System.out.println(name+"The cards are:");
for(String poker:Pokers){
System.out.print(poker+" ");
}System.out.println();
}
}
Operation results:
2: Implement with TreeMap Collection
Specific steps:
A: Create the TreeMap Collection Storage Card, and the big key corresponds to the big card, and the small key corresponds to the small card. Similarly, create two arrays, one for storing patterns, one for storing points, and another for creating an ArrayList collection to store the index without any corresponding card.
B:. shuffle, shuffle is the index (the key corresponding to each card in the map set), we use ArrayList here to store.
C: The licensing, issuing index, and sorting the index, which takes advantage of the orderliness of TreeSet.
D: Look at the cards and find the corresponding values in the map set according to the index issued.
Specific code:
package com.study_01;
import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* 1.Create a Map Collection Storage Card, and the big key corresponds to the big card, the small key corresponds to the small card.
* 2.Shuffle, shuffle is the index (the key corresponding to each card in the map set)
* 3.Licensing, index of issuance, and index of age for sorting
* 4.Look at the cards and find the corresponding values in the map set according to the keys sent.
*/
public class Poker2 {
public static void main(String[] args) {
//Create a TreeMap collection
TreeMap<Integer,String> Pokers=new TreeMap<>();
String[] colors={"♥","♠","♦","♣"};
String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2",};
//Create an ArrayList collection to store an index without a card
ArrayList<Integer> indexs=new ArrayList<>();
int index = 0;
for(String color:colors){
for(String number:numbers){
Pokers.put(index, color+number);
indexs.add(index);
index++;
}//King Cun, King Xiao
Pokers.put(index, "king");
indexs.add(index);
index++;
Pokers.put(index, "Xiao Wang");
indexs.add(index);
index++;
}//Shuffle, shuffle is the index (the key corresponding to each card in the map set)
Collections.shuffle(indexs);
//Licensing, issuing the index to find its corresponding value according to the index
TreeSet<Integer> zhangsan=new TreeSet<>();
TreeSet<Integer> lisi=new TreeSet<>();
TreeSet<Integer> wangma=new TreeSet<>();
TreeSet<Integer> dipai=new TreeSet<>();
for(int i=0;i<Pokers.size();i++){
if(i>=Pokers.size()-3){
dipai.add(indexs.get(i));
}else if(i%3==0){
zhangsan.add(indexs.get(i));
}else if(i%3==1){
lisi.add(indexs.get(i));
}else if(i%3==2){
wangma.add(indexs.get(i));
}
}//Look at cards
lookPoker("Zhang San",zhangsan,Pokers);
lookPoker("Li Si",lisi,Pokers);
lookPoker("Wang Ma",wangma,Pokers);
lookPoker("A hand",dipai,Pokers);
}
private static void lookPoker(String name, TreeSet<Integer> indexs,
TreeMap<Integer, String> Pokers) {
System.out.println(name+"The cards are:");
for(Integer index:indexs){
System.out.print(Pokers.get(index));
}System.out.println();
}
}
Operation results: