需求分析
准备一副牌,留出底牌后发给三个玩家,且玩家拿到牌后自己排好大小。
设计
1.准备一副牌
1.1:使用HashMap集合存放牌的索引和内容。
1.2:使用一个ArrayList集合存放四个花色。
1.3:使用一个ArrayList集合存放A–K数值。
1.4:使用一个ArrayList集合存放牌的索引,后期发牌用。
2.洗牌,将1.4集合的索引打乱。
3.发牌,准备四个ArrayList集合存放玩家1.2.3发到牌的索引。
4.看牌,将每个玩家的牌的索引拿到后按大小排序,后根据这个索引到1.1的HashMap中获取自己的牌。
代码实现
public class DouDiZhu {
public static void main(String[] args) {
//1.准备牌
HashMap<Integer, String> poker = new HashMap<>();
ArrayList<Integer> pokerIndex = new ArrayList<>();
List<String> colors = new ArrayList<>();
colors.add("♠");
colors.add("♥");
colors.add("♦");
colors.add("♣");
List<String> indexs = new ArrayList<>();
indexs.add("2");
indexs.add("A");
indexs.add("K");
indexs.add("Q");
indexs.add("J");
indexs.add("10");
indexs.add("9");
indexs.add("8");
indexs.add("7");
indexs.add("6");
indexs.add("5");
indexs.add("4");
indexs.add("3");
poker.put(0,"大王");
pokerIndex.add(0);
poker.put(1,"小王");
pokerIndex.add(1);
int index=2;
for(String num :indexs){
for(String color:colors){
poker.put(index,color+num);
pokerIndex.add(index);
index++;
}
}
System.out.println(poker);//查看准备好的牌
System.out.println(pokerIndex);
//2.洗牌
Collections.shuffle(pokerIndex);
System.out.println(pokerIndex);//看看洗好牌的顺序
//3.发牌
ArrayList<Integer> player01 = new ArrayList<>();
ArrayList<Integer> player02 = new ArrayList<>();
ArrayList<Integer> player03 = new ArrayList<>();
ArrayList<Integer> diPai = new ArrayList<>();
for (int i = 0; i < pokerIndex.size(); i++) {
if(i>=51){
diPai.add(pokerIndex.get(i));
}else if(i%3==0){
player01.add(pokerIndex.get(i));
}else if(i%3==1){
player02.add(pokerIndex.get(i));
}else if(i%3==2){
player03.add(pokerIndex.get(i));
}
}
System.out.println(player01);
Collections.sort(player01);
Collections.sort(player02);
Collections.sort(player03);
//4.看牌
look("玩家一",player01,poker);
look("玩家二",player02,poker);
look("玩家三",player03,poker);
look("底牌",diPai,poker);
}
private static void look(String name,ArrayList<Integer> player,HashMap<Integer, String> poker) {
System.out.print(name+":");
for (int i = 0; i < player.size(); i++) {
Integer index = player.get(i);
System.out.print(poker.get(index)+" ");
}
System.out.println();
}
}
来源:CSDN
作者:Stecolin
链接:https://blog.csdn.net/weixin_42656797/article/details/104184537