斗地主案例
作者:互联网
案例介绍
按照斗地主的规则,完成洗牌的动作
具体规则:
使用54张牌打乱顺序,三个玩家参与游戏,三个人交替摸牌,每个人17张牌后,最后三张留作底牌
案例分析
准备牌:
牌可以设计一个ArrayList
每张牌由花色数字两部分组成,我们可以使用花色集合与数字集合嵌套迭代完成每张牌的组装
牌由Collections类的shuffle方法进行随机排序
发牌:
将每个人以及底牌设计为ArrayList
看牌:
直接打印每个集合
代码实现
public class Test {
public static void main(String[] args) {
//准备牌
//定义一个存储54张牌的ArrayList集合,泛型使用String
ArrayList<String> poker = new ArrayList<>();
//定义两个数组一个来存放花色,一个来存放牌的序号
String[] color = {"♠","♥","♣","♦"};
String[] number ={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//先把大王小王存到poker
poker.add("大王");
poker.add("小王");
//循环嵌套两个数组,组装52张牌
for (String s : number) {
for (String s1 : color) {
poker.add(s+s1);
}
}
//打乱牌序
Collections.shuffle(poker);
ArrayList<String> player01 = new ArrayList<>();
ArrayList<String> player02 = new ArrayList<>();
ArrayList<String> player03 = 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) {
player01.add(poker.get(i));
} else if (i % 3 == 1) {
player02.add(poker.get(i));
} else if (i % 3 == 2) {
player03.add(poker.get(i));
}
}
System.out.println("玩家1"+player01);
System.out.println("玩家2"+player02);
System.out.println("玩家3"+player03);
System.out.println("底牌"+diPai);
}
}
标签:poker,String,斗地主,ArrayList,get,案例,add,new 来源: https://www.cnblogs.com/ailhy/p/16445853.html