模拟斗地主发牌(一)
- 准备牌
运用for循环准备好54张扑克牌,存入poker集合
ArrayList<String> poker=new ArrayList<>();
String[] color={"♠","♥","♣","♦"};
String[] number={"A","J","Q","K","2","3","4","5","6","7","8","9","10"};
poker.add("大王");
poker.add("小王");
for (String c : color) {
for (String n : number) {
poker.add(c+n);
}
}
输出poker:可以看出扑克牌是按照花色排序,若将number放在for的外循环,扑克牌则按照数字大小排序
[大王, 小王, ♠A, ♠J, ♠Q, ♠K, ♠2, ♠3, ♠4, ♠5, ♠6, ♠7, ♠8, ♠9, ♠10, ♥A, ♥J, ♥Q, ♥K, ♥2, ♥3, ♥4, ♥5, ♥6, ♥7, ♥8, ♥9, ♥10, ♣A, ♣J, ♣Q, ♣K, ♣2, ♣3, ♣4, ♣5, ♣6, ♣7, ♣8, ♣9, ♣10, ♦A, ♦J, ♦Q, ♦K, ♦2, ♦3, ♦4, ♦5, ♦6, ♦7, ♦8, ♦9, ♦10]
- 洗牌
poker被打乱
/*
* static void shuffle(List<?> list)
*使用默认随机源对指定列表进行置换。
*/
Collections.shuffle(poker);
- 发牌
ArrayList<String> player1=new ArrayList<>();
ArrayList<String> player2=new ArrayList<>();
ArrayList<String> player3=new ArrayList<>();
ArrayList<String> dipai=new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
if (i >= 51) {
dipai.add(poker.get(i));
} else if (i % 3 == 0) {
player1.add(poker.get(i));
} else if (i % 3 == 1) {
player2.add(poker.get(i));
} else if (i % 3 == 2) {
player3.add(poker.get(i));
}
}
- 看牌
System.out.println(dipai);
System.out.println(player1);
System.out.println(player2);
System.out.println(player3);
- 结果
[♣3, 大王, ♥10]
[♥J, ♦6, ♥8, ♥6, ♣5, ♣A, ♠6, ♦A, ♣9, ♠5, ♣Q, ♣10, ♦5, ♠A, ♥3, ♠Q, ♣4]
[小王, ♦8, ♦Q, ♠K, ♥9, ♥2, ♥5, ♠8, ♥K, ♠9, ♣J, ♠J, ♥4, ♦10, ♥A, ♦9, ♦K]
[♠3, ♠7, ♠10, ♣K, ♦3, ♠2, ♣8, ♦J, ♣6, ♥7, ♠4, ♦4, ♥Q, ♦7, ♦2, ♣2, ♣7]
模拟斗地主发牌(二)
- 准备牌
(一)中是将扑克牌组合好后存入list集合
(二)将扑克牌存入map<K,V>集合的V,此时扑克牌按照大小排列。并给每张牌按照0-53编号存入map<K,V>集合的键值K。这样每张牌都有一个自己唯一的编号。
Map<Integer,String> poker=new HashMap<>();
List<String> colors=List.of("♠","♥","♣","♦");
List<String> numbers=List.of("A","J","Q","K","2","3","4","5","6","7","8","9","10");
int index=0;
poker.put(index,"大王");
index++;
poker.put(index,"小王");
index++;
for (String number:numbers) {
for (String color : colors) {
poker.put(index,color+number);
index++;
}
}
输出poker看一下
{0=大王, 1=小王, 2=♠A, 3=♥A, 4=♣A, 5=♦A, 6=♠J, 7=♥J, 8=♣J, 9=♦J, 10=♠Q, 11=♥Q, 12=♣Q, 13=♦Q, 14=♠K, 15=♥K, 16=♣K, 17=♦K, 18=♠2, 19=♥2, 20=♣2, 21=♦2, 22=♠3, 23=♥3, 24=♣3, 25=♦3, 26=♠4, 27=♥4, 28=♣4, 29=♦4, 30=♠5, 31=♥5, 32=♣5, 33=♦5, 34=♠6, 35=♥6, 36=♣6, 37=♦6, 38=♠7, 39=♥7, 40=♣7, 41=♦7, 42=♠8, 43=♥8, 44=♣8, 45=♦8, 46=♠9, 47=♥9, 48=♣9, 49=♦9, 50=♠10, 51=♥10, 52=♣10, 53=♦10}
大王 ♠4 ♠8
- 洗牌
因为每张扑克牌都有自己唯一的编号,所以洗牌时打乱牌的编号即可。
Set<Integer> pIndex=poker.keySet();//获取poker的键集
List<Integer> pokerIndex=new ArrayList<>();//因为要打乱牌的编号,所以打乱之前先存起来
pokerIndex.addAll(pIndex);
Collections.shuffle(pokerIndex);//打乱牌的编号
被打乱的牌的编号
[4, 15, 29, 26, 28, 8, 13, 46, 36, 48, 11, 20, 32, 17, 9, 12, 25, 33, 47, 22, 23, 18, 1, 27, 35, 19, 3, 51, 10, 43, 16, 52, 41, 34, 49, 2, 30, 38, 5, 21, 44, 14, 40, 7, 45, 31, 42, 0, 53, 39, 24, 37, 50, 6]
- 发牌
ArrayList<Integer> nodipai=new ArrayList<>();
ArrayList<Integer> no01=new ArrayList<>();
ArrayList<Integer> no02=new ArrayList<>();
ArrayList<Integer> no03=new ArrayList<>();
//将牌的编号发给玩家
for (Integer in : pokerIndex) {
if(in>=51){
nodipai.add( pokerIndex.get(in));
}else if(in%3==0){
no01.add( pokerIndex.get(in));
}else if(in%3==1){
no02.add( pokerIndex.get(in));
}else if(in%3==2){
no03.add( pokerIndex.get(in));
}
}
//对编号排序
Collections.sort(nodipai);
Collections.sort(no01);
Collections.sort(no02);
Collections.sort(no03);
- 看牌
通过每张牌的编号找到对应的扑克牌,发给玩家。因为刚开始我们对扑克牌编号时是按照大小顺序编号的,并且发完牌之后对编号进行了排序,所以每个玩家的扑克牌是按照大小排列的。
//调用方法
lookpoker(nodipai,poker);
lookpoker(no01,poker);
lookpoker(no02,poker);
lookpoker(no03,poker);
//Lookpoker方法
private static void lookpoker(List<Integer> playernum,Map poker) {
for (Integer i : playernum) {
System.out.print(poker.get(i)+" ");
}
System.out.println();
}
玩家的牌和底牌
♠J ♦6 ♠10
♣A ♣Q ♦Q ♣K ♠2 ♦2 ♠4 ♠5 ♥5 ♣5 ♠6 ♥6 ♣7 ♥9 ♣9 ♥10 ♦10
小王 ♥J ♠Q ♥Q ♥K ♦K ♥2 ♠3 ♦3 ♣4 ♠7 ♥7 ♠8 ♣8 ♠9 ♦9 ♣10
大王 ♠A ♥A ♦A ♣J ♦J ♠K ♣2 ♥3 ♣3 ♥4 ♦4 ♦5 ♣6 ♦7 ♥8 ♦8