Java-集合实操案例(SendPoker)
PokerGame Class
package cn.collectionTest.output;
import java.util.*;
public class PokerGame {
public Map<Integer, String> pokers = new HashMap<>();
public List<Integer> randomList = new ArrayList<>();
public List<Integer> user1 = new ArrayList<>();
public List<Integer> user2 = new ArrayList<>();
public List<Integer> user3 = new ArrayList<>();
public List<Integer> last = new ArrayList<>();
public String[] attributes = {"♠️","♥️","♦️","♣️"};
public String[] cards = {"3","4","5","6","7","8","9","10","J","Q","K","A","2",};
public int count = 0;
public void buyPoker() {
for (String card : cards) {
for (String attr : attributes) {
pokers.put(count,card.concat(attr));
randomList.add(count);
count++;
}
}
pokers.put(count,"小王🃏");
randomList.add(count++);
pokers.put(count,"大王🃏");
randomList.add(count);
}
public void printPokerSys(){
Collections.shuffle(randomList);
for (int i = 0; i < randomList.size(); i++) {
if (i >= randomList.size() -3){
last.add(randomList.get(i));
}else if (i % 3 == 0){
user1.add(randomList.get(i));
}else if (i % 3 == 1) {
user2.add(randomList.get(i));
}else if (i % 3 == 2) {
user3.add(randomList.get(i));
}
}
System.out.println("张三的牌: "+showPokers(user1, pokers));
System.out.println(" ");
System.out.println("李四的牌: "+showPokers(user2, pokers));
System.out.println(" ");
System.out.println("老子的牌: "+showPokers(user3, pokers));
System.out.println(" ");
System.out.println("底牌: "+showPokers(last,pokers));
}
public String showPokers(List<Integer> nums , Map<Integer,String> pokers ) {
Collections.sort(nums);
Collections.reverse(nums);
StringBuilder sb = new StringBuilder();
for (Integer num : nums) {
String card = pokers.get(num);
sb.append(card+" ");
}
String str = sb.toString();
return str.trim();
}
public void test(){
int nums = 13;
List<Integer> dipai = new ArrayList<Integer>();
List<Integer> user1 = new ArrayList<Integer>();
List<Integer> user2 = new ArrayList<Integer>();
List<Integer> user3 = new ArrayList<Integer>();
for (int i = 0; i < nums; i++) {
System.out.println(i);
if (i >= nums -3){
dipai.add(i);
}else if (i % 3 == 0){
user1.add(i);
}
}
System.out.println(dipai);
}
public static void controlMain(){
PokerGame start = new PokerGame();
start.buyPoker();
start.printPokerSys();
}
public static void main(String[] args) {
controlMain();
}
}