import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
class Poke {
/*
* A:案例演示
* 模拟斗地主洗牌和发牌,牌没有排序
*/
public static void main(String[] args) throws Exception {
//ArrayList<Character> pocker = new ArrayList<>();
//创建映射,映射维护键为Integer,映射值类型为String map(映射)
HashMap<Integer,String> map = new HashMap<>();
//创建扑克牌集合
ArrayList<Integer> poker = new ArrayList<>();
//定义反射键
int index = 0;
//创建扑克牌对象
Poker p = new Poker();
//接收扑克牌
String[][] newPoker = p.poker();
/*
for (String s:pai ) {
poker.add(s);
}
*/
/*
for (int i=0;i <newPoker.length ;i++ ) {
System.out.print(newPoker[i] + " ");
}
*/
//poker.add("大王"); //添加元素进入数组集合
//poker.add("小王"); //添加元素进入数组集合
//System.out.println(poker); //查看集合中的元素
for (int i = 0;i < 13 ;i++ ) {
for (int j = 0;j < 4 ;j++ ) {
map.put(index,newPoker[i][j]);
poker.add(index);
index++;
}
//String str = newPoker[i];
}
String STR2 = "";
char[] GHOST = {1,2};
String str1 = STR2 + GHOST[1];
String str3 = STR2 + GHOST[0];
map.put(index,str1);
poker.add(index);
index++;
map.put(index,str3);
poker.add(index2);
//System.out.println(poker);
//System.out.println(poker.size()); //查看集合中元素的个数
Collections.shuffle(poker); //使用默认随机源对指定列表进行置换。
//System.out.println(poker); //查看集合中的元素
//创建TreeSet集合,set根据其元素的自然顺序进行排序
TreeSet dong = new TreeSet();
TreeSet nan = new TreeSet();
TreeSet xi = new TreeSet();
TreeSet dipai = new TreeSet();
for (int i = 0;i < poker.size() ;i++ ) { //通过循环进行发牌
if (i >= poker.size()-3) { //当牌数等于小于3时就作为底牌
dipai.add(poker.get(i));
}else if (i % 3 == 0) { //当牌数模于等于0时给东家发牌
dong.add(poker.get(i));
}else if (i % 3 == 1) { //当牌数模于等于1时给南家发牌
nan.add(poker.get(i));
}else { //当牌数模于等于2时给西家发牌
xi.add(poker.get(i));
}
}
lookPoker(dong,map);
lookPoker(nan,map);
lookPoker(xi,map);
lookPoker(dipai,map);
}
public static void lookPoker(TreeSet<Integer> ts,HashMap<Integer,String> hm) {
for (Integer index : ts ) {
System.out.print(hm.get(index)+ " ");
}
System.out.println();
}
}
class Poker {
private final String[] STR1 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
private final char[] COLOR = {3,4,5,6};
private final char[] GHOST = {1,2};
private final String STR2 = "";
public String[][] poker() {
String[] poker = new String[54];
String[][] test = new String[13][4];
for (int i = 0;i < STR1.length;i++ ) {
for (int j = 0;j < COLOR.length ;j++ ) {
test[i][j] = STR1[i] + COLOR[j];
}
}
/*
for (int i = 0;i < COLOR.length;i++ ) {
for (int j = 0;j < STR1.length ;j++ ) {
if (i == 0) {
test1[j] = COLOR[i] + STR1[j];
}else if (i == 1) {
test2[j] = COLOR[i] + STR1[j];
}else if (i == 2) {
test3[j] = COLOR[i] + STR1[j];
}else {
test4[j] = COLOR[i] + STR1[j];
}
}
}
for (int k = 0;k < 52 ;k++ ) {
if (k < 13) {
poker[k] = test1[k];
}else if (k>=13 && k <26) {
poker[k] = test2[k-13];
}else if (k>=26 && k <39) {
poker[k] = test3[k-26];
}else if (k>=39 && k <52) {
poker[k] = test4[k-39];
}
}
*/
poker[52] = GHOST[0] + STR2;
poker[53] = GHOST[1] + STR2;
return test;
}
public static void testPoker() {
String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] color = {"黑","红","梅","方"};
ArrayList<String> poker = new ArrayList<>(); //创建数组集合
for (String s1 : num ) {
for (String s2 :color ) {
poker.add(s2.concat(s1)); //添加元素进入数组集合
}
}
}
public static void look() {
ArrayList<Integer> dong = new ArrayList<>();
ArrayList<Integer> xi = new ArrayList<>();
ArrayList<Integer> nan = new ArrayList<>();
ArrayList<Integer> dipan = new ArrayList<>();
for (int i = 0;i < poker.size() ;i++ ) { //通过循环进行发牌
if (i >= poker.size()-3) { //当牌数等于小于3时就作为底牌
dipan.add(poker.get(i));
}else if (i % 3 == 0) { //当牌数模于等于0时给东家发牌
dong.add(poker.get(i));
}else if (i % 3 == 1) { //当牌数模于等于1时给南家发牌
nan.add(poker.get(i));
}else { //当牌数模于等于2时给西家发牌
xi.add(poker.get(i));
}
}
System.out.println("东="+dong);
System.out.println("南="+nan);
System.out.println("西="+xi);
System.out.println("底牌="+dipan);
}
} |
|