package Game;
import java.util.ArrayList;
import java.util.Collections;
public class PokerGame01 {
//牌盒
//♥3 ♣3
static ArrayList<String> list = new ArrayList<>();
//静态代码块
//特点:随着类的加载而在加载的,而且只执行一次。
static {
//准备牌
// "♦", "♣", "♥", "♠"
// "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"
String[] color = {"♦", "♣", "♥", "♠"};
String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
for (String c : color) {
//c表示每一个牌的花色
for (String n : number) {
//n表示每一种牌号
list.add(c + n);
}
}
list.add("大王");
list.add("小王");
}
public PokerGame01() {
// 洗牌
Collections.shuffle(list);
// 发牌
//定义4个集合,其中3个选手的牌,还要一个是用来存储地主多出来的3张牌
ArrayList<String> dizhu = new ArrayList<>();
ArrayList<String> player1 = new ArrayList<>();
ArrayList<String> player2 = new ArrayList<>();
ArrayList<String> player3 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
//根据索引找到牌号,本质是要把前三张牌给地主
String poke = list.get(i);//得到了第i张牌
if (i <= 2) {
dizhu.add(poke);//前三张给地主
//依次轮流给3个选手发牌
} else if (i % 3 == 0) {
player1.add(poke);
} else if (i % 3 == 1) {
player2.add(poke);
} else {
player3.add(poke);
}
}
//看牌
lookpoke("张三",player1);
lookpoke("李四",player2);
lookpoke("王五",player3);
}
public void lookpoke(String name, ArrayList<String> list) {
System.out.print(name + ": ");
for (String poke : list) {
//循环遍历得到每一张牌
System.out.print(poke + " ");
}
System.out.println();
}
}
排序的
package Game;
import java.util.*;
public class PokerGame02 {
//牌盒
//♥3 ♣3
//键是序号,牌是值
//此时只要把牌跟序号产生对应关系即可,不需要按照序号进行排序,所以只需HashMap即可
static HashMap<Integer, String> hm = new HashMap<>();
//用来存储序号的单列集合
static ArrayList<Integer> list = new ArrayList<>();
//静态代码块
//特点:随着类的加载而在加载的,而且只执行一次。
static {
//准备牌
// "♦", "♣", "♥", "♠"
// "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"
String[] color = {"♦", "♣", "♥", "♠"};
String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
/*
{1=♦3, 2=♦4, 3=♦5, 4=♦6, 5=♦7, 6=♦8, 7=♦9, 8=♦10, 9=♦J, 10=♦Q, 11=♦K, 12=♦A, 13=♦2, 14=♣3,
15=♣4, 16=♣5, 17=♣6, 18=♣7, 19=♣8, 20=♣9, 21=♣10, 22=♣J, 23=♣Q, 24=♣K, 25=♣A, 26=♣2, 27=♥3,
28=♥4, 29=♥5, 30=♥6, 31=♥7, 32=♥8, 33=♥9, 34=♥10, 35=♥J, 36=♥Q, 37=♥K, 38=♥A, 39=♥2, 40=♠3,
41=♠4, 42=♠5, 43=♠6, 44=♠7, 45=♠8, 46=♠9, 47=♠10, 48=♠J, 49=♠Q, 50=♠K, 51=♠A, 52=♠2}
很明显可以看出没有按照顺序起好牌 应该先把数字一样的放在一起,而不是把花色一样的放在一起
解决: 交换color和number即可
*/
//定义一个序号
int num = 1;
for (String n : number) {
//n表示每一种牌号
for (String c : color) {
//c表示每一个牌的花色
hm.put(num, c + n);
list.add(num);
num++;
}
}
hm.put(num, "小王");
list.add(num);
num++;
hm.put(num, "大王");
list.add(num);
}
public PokerGame02() {
// 洗牌
Collections.shuffle(list);
// 发牌
/*
[39, 35, 7]
[28, 31, 15, 54, 14, 24, 3, 46, 43, 22, 37, 41, 29, 45, 16, 30, 48]
[33, 49, 11, 13, 38, 27, 25, 50, 20, 9, 1, 32, 19, 10, 44, 21, 40]
[53, 8, 34, 36, 17, 42, 5, 18, 2, 23, 12, 26, 4, 47, 6, 51, 52]
用ArrayList集合存储的索引是乱序的,我们本质是要根据序号也就是索引去到map集合里面找到牌,所以序号要有序
*/
/*
[5, 10, 34]
[2, 4, 7, 8, 13, 17, 18, 19, 20, 22, 27, 35, 37, 38, 39, 40, 42]
[1, 9, 14, 16, 23, 24, 28, 29, 30, 31, 32, 36, 41, 43, 46, 51, 52]
[3, 6, 11, 12, 15, 21, 25, 26, 33, 44, 45, 47, 48, 49, 50, 53, 54]
*/
//定义4个集合,其中3个选手的牌,还要一个是用来存储地主多出来的3张牌
TreeSet<Integer> dizhu = new TreeSet<>();
TreeSet<Integer> player1 = new TreeSet<>();
TreeSet<Integer> player2 = new TreeSet<>();
TreeSet<Integer> player3 = new TreeSet<>();
//获取所有的键,把这些键放入单列集合当中
// Set<Integer> integers1 = hm.keySet();
for (int i = 0; i < list.size(); i++) {
//根据索引找到牌的序号,本质是要把前三张牌的序号给地主
int number = list.get(i); //得到了第i张牌的序号
if (i <= 2) {
dizhu.add(number); //前三张牌的序号给地主
// integers1.add(poke);
//依次轮流给3个选手发牌
} else if (i % 3 == 0) {
player1.add(number);
} else if (i % 3 == 1) {
player2.add(number);
} else {
player3.add(number);
}
}
//看牌
lookpoke("底牌",dizhu);
lookpoke("张三",player1);
lookpoke("李四",player2);
lookpoke("王五",player3);
// look("赵六",integers1);
}
/*
* 参数一:玩家的名字
* 参数二:牌的序号
* */
public void lookpoke(String name, TreeSet<Integer> ts) {
System.out.print(name + ": ");
for (int num : ts) {
//num就是代表的是序号,遍历方法1 通过键找值的方式遍历map集合
//遍历TreeSet集合得到每一个序号,再拿着序号到Map集合中去找真正的牌
String poke = hm.get(num);//通过键(序号)拿到了每一张牌(值)
System.out.print(poke + " ");
}
System.out.println();
}
//遍历单列集合,得到每一个键;
/* public void look(String name, Set<Integer> key) {
System.out.print(name+": ");
//遍历单列集合,得到每一个键
for (int num : key) {
//num代表就是序号
//通过序号(键)去找到map集合里面的值
String value = hm.get(num);
System.out.print(value+" ");
}
System.out.println();
}*/
}
package Game;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
public class PokerGame03 {
//牌盒
//♥3 ♣3
static ArrayList<String> list = new ArrayList<>();
//创建一个集合,用来添加牌的价值,牌号是键,价值是值
static HashMap<String, Integer> hm = new HashMap<>();
//静态代码块
//特点:随着类的加载而在加载的,而且只执行一次。
static {
//准备牌
// "♦", "♣", "♥", "♠"
// "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"
String[] color = {"♦", "♣", "♥", "♠"};
String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
for (String c : color) {
//c表示每一个牌的花色
for (String n : number) {
//n表示每一种牌号
list.add(c + n);
}
}
list.add(" 大王");
list.add(" 小王");
//指定牌的价值
//牌上的数字到Map集合中判断是否存在
//存在,获取价值
//不存在,本身的数字就是价值
hm.put("J", 11);
hm.put("Q", 12);
hm.put("K", 13);
hm.put("A", 14);
hm.put("2", 15);
hm.put("小王", 50);
hm.put("大王", 100);
}
public PokerGame03() {
// 洗牌
Collections.shuffle(list);
// 发牌
//定义4个集合,其中3个选手的牌,还要一个是用来存储地主多出来的3张牌
ArrayList<String> dizhu = new ArrayList<>();
ArrayList<String> player1 = new ArrayList<>();
ArrayList<String> player2 = new ArrayList<>();
ArrayList<String> player3 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
//根据索引找到牌号,本质是要把前三张牌给地主
String poke = list.get(i);//得到了第i张牌
if (i <= 2) {
dizhu.add(poke);//前三张给地主
//依次轮流给3个选手发牌
} else if (i % 3 == 0) {
player1.add(poke);
} else if (i % 3 == 1) {
player2.add(poke);
} else {
player3.add(poke);
}
}
//排序
order(dizhu);
order(player1);
order(player2);
order(player3);
//看牌
lookpoke("张三",player1);
lookpoke("李四",player2);
lookpoke("王五",player3);
lookpoke("底牌",dizhu);
}
//利用牌的价值进行排序
//参数:集合
//♥5 ♥3 ♥6 ♥7 ♥9
private void order(ArrayList<String> list) {
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
//o1:表示当前要插入到有序序列中的牌
//o2:表示已经在有序序列中存在的牌
//负数:o1小 插入到前面
//正数:o1大 插入到后面
//0:o1的数字跟o2的数字是一样的,需要按照花色再次排序
//1.计算o1的花色和价值
String color1 = o1.substring(0, 1);;//♥3中的♥
//价值
int value1 = getValue(o1);
//2.计算o2的花色和价值
String color2= o1.substring(0, 1);//♥3中的♥
//价值
int value2 = getValue(o2);
//3.比较o1和o2的价值 ♥3 ♠3
int i = value1 - value2;
return i == 0 ? color1.compareTo(color2) : i;
}
});
}
//计算牌的价值
//参数:牌
//返回值:价值
public int getValue(String poker) { //♥3
//获取牌上的数字
String number = poker.substring(1);//比如♥3中的3
//把这里截取出来的结果,让这个结果再Map集合中存在
/*
hm.put("J", 11);
hm.put("Q", 12);
hm.put("K", 13);
hm.put("A", 14);
hm.put("2", 15);
hm.put("小王", 50);
hm.put("大王", 100);
*/
//如果不存在类型转换 如"3"的价值就值3
//如果存在在map集合里面拿出对应的价值即可
//拿着数字到map集合中判断是否存在
if (hm.containsKey(number)) {
//存在,获取价值
return hm.get(number);
} else {
//不存在,类型转换
return Integer.parseInt(number);
}
}
public void lookpoke(String name, ArrayList<String> list) {
System.out.print(name + ": ");
for (String poke : list) {
//循环遍历得到每一张牌
System.out.print(poke + " ");
}
System.out.println();
}
}