思路
1、组装一副牌
2、在遍历组装好的扑克牌前先把大小王存储起来
3、打乱扑克牌的顺序
4、遍历扑克牌
5、首先把三张底牌抽取出来存储
6、依次给三个玩家发牌
7、看牌
组装前的准备工作
//存储牌和索引
Map poker = new HashMap<>();
//存储纸牌索引
List pokerIndex = new ArrayList<>();
//花色和数字
List colors = List.of("♠","♥","♣","♦");
List nums = List.of("A","2","3","4","5","6","7","8","9","10","J","Q","K");
存储大小王
//索引
int index = 0;
//先把大王和小王存储
poker.put(index,"大王");
pokerIndex.add(index);
index += 1;
poker.put(index,"小王");
pokerIndex.add(index);
index += 1;
开始组装
for (String num : nums) {
for (String color : colors) {
poker.put(index,num+color);
pokerIndex.add(index);
index += 1;
}
}
打乱顺序
Collections.shuffle(pokerIndex);
发牌给玩家(先存储三张底牌)
Listplayer01 = new ArrayList<>(); List player02 = new ArrayList<>(); List player03 = new ArrayList<>(); List downPoker = new ArrayList<>(); for (int i = 0; i < pokerIndex.size(); i++) { Integer integer = pokerIndex.get(i); if (i >= 51){ downPoker.add(integer); }else if (i % 3 == 0){ player01.add(integer); }else if (i % 3 == 1){ player02.add(integer); }else if (i % 3 == 2){ player03.add(integer); } }
看牌的方法
public static void seePoker(String name,Mappoker,List pokerIndex){ System.out.print(name+":"); for (Integer key : pokerIndex) { String value = poker.get(key); System.out.print(value+" "); } System.out.println(); }
效果
源代码
public class Poker {
public static void main(String[] args) {
//存储牌和索引
Map poker = new HashMap<>();
//存储纸牌索引
List pokerIndex = new ArrayList<>();
//花色和数字
List colors = List.of("♠","♥","♣","♦");
List nums = List.of("A","2","3","4","5","6","7","8","9","10","J","Q","K");
//索引
int index = 0;
//先把大王和小王存储
poker.put(index,"大王");
pokerIndex.add(index);
index += 1;
poker.put(index,"小王");
pokerIndex.add(index);
index += 1;
for (String num : nums) {
for (String color : colors) {
poker.put(index,num+color);
pokerIndex.add(index);
index += 1;
}
}
List player01 = new ArrayList<>();
List player02 = new ArrayList<>();
List player03 = new ArrayList<>();
List downPoker = new ArrayList<>();
Collections.shuffle(pokerIndex);
for (int i = 0; i < pokerIndex.size(); i++) {
Integer integer = pokerIndex.get(i);
if (i >= 51){
downPoker.add(integer);
}else if (i % 3 == 0){
player01.add(integer);
}else if (i % 3 == 1){
player02.add(integer);
}else if (i % 3 == 2){
player03.add(integer);
}
}
Collections.sort(player01);
Collections.sort(player02);
Collections.sort(player03);
Collections.sort(downPoker);
seePoker("玩家A",poker,player01);
seePoker("玩家B",poker,player02);
seePoker("玩家C",poker,player03);
seePoker("底 牌",poker,downPoker);
}
public static void seePoker(String name,Map poker,List pokerIndex){
System.out.print(name+":");
for (Integer key : pokerIndex) {
String value = poker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}



