标签:rgs out hash 添加 demo user shuf vaule ipa
import java.util.ArrayList;
import java.util.Collections;
public class PokerDemo {
public static void main(String[] args) {
// 使用ArrayList添加一副牌
ArrayList<String> arrayList = new ArrayList<String>();
String[] colors = {"?", "?", "?", "?"};
String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String color : colors) {
for (String number : numbers) {
arrayList.add(color + number);
}
}
arrayList.add("小王");
arrayList.add("大王");
// 洗牌
Collections.shuffle(arrayList);
// 发牌
// 创建三个角色+底牌的集合
ArrayList<String> user1 = new ArrayList<String>();
ArrayList<String> user2 = new ArrayList<String>();
ArrayList<String> user3 = new ArrayList<String>();
ArrayList<String> dipaiArray = new ArrayList<String>();
for (int i = 0; i < arrayList.size(); i++) {
String poker = arrayList.get(i);
if (i >= (arrayList.size() - 3)) {
dipaiArray.add(poker);
} else if (i % 3 == 0) {
user1.add(poker);
} else if (i % 3 == 1) {
user2.add(poker);
} else if (i % 3 == 2) {
user3.add(poker);
}
}
// 看牌
show(user1, "user1");
show(user2, "user2");
show(user3, "user3");
show(dipaiArray, "底牌");
}
// 看牌方法,传入ArrayList和用户名
public static void show(ArrayList<String> arrayList, String name) {
System.out.print(name + "的手牌是:");
for (String poker : arrayList) {
System.out.print(poker + " ");
}
System.out.println();
}
}
可以将手牌按照顺序显示
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Poker {
public static void main(String[] args) {
// HashMap存储索引(key)和牌(vaule)
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
// ArrayList存储索引
ArrayList<Integer> arrayList = new ArrayList<Integer>();
String[] colors = {"?", "?", "?", "?"};
String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
int index = 0;
for (String number : numbers) {
for (String color : colors) {
hashMap.put(index, color + number);
arrayList.add(index);
index++;
}
}
hashMap.put(index, "小王");
arrayList.add(index);
index++;
hashMap.put(index, "大王");
arrayList.add(index);
// 洗索引
Collections.shuffle(arrayList);
// 测试输出
// System.out.println(hashMap);
// System.out.println(arrayList);
// 三个玩家一个底牌,添加索引
TreeSet<Integer> user1 = new TreeSet<Integer>();
TreeSet<Integer> user2 = new TreeSet<Integer>();
TreeSet<Integer> user3 = new TreeSet<Integer>();
TreeSet<Integer> dipaiTree = new TreeSet<Integer>();
for (int i = 0; i < arrayList.size(); i++) {
int x = arrayList.get(i);
if (i >= (arrayList.size() - 3)) {
dipaiTree.add(x);
} else if (i % 3 == 0) {
user1.add(x);
} else if (i % 3 == 1) {
user2.add(x);
} else if (i % 3 == 2) {
user3.add(x);
}
}
show("user1", user1 ,hashMap);
show("user2", user2 ,hashMap);
show("user3", user3 ,hashMap);
show("底牌", dipaiTree ,hashMap);
}
// 根据索引遍历值
public static void show(String name ,TreeSet<Integer> treeSet, HashMap<Integer, String> hashMap){
System.out.print(name + "的手牌:");
for(Integer key : treeSet){
String s = hashMap.get(key);
System.out.print(s + " ");
}
System.out.println();
}
}
Java使用ArrayList、HashMap实现三人斗地主
标签:rgs out hash 添加 demo user shuf vaule ipa
原文地址:https://www.cnblogs.com/shley/p/14827179.html