场景
1.按照斗地主的规则,完成洗牌发牌动作
具体规则:
使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌
package dome03;
import java.util.ArrayList;
import java.util.Collections;
public class DouDiZhu {
public static void main(String[] args) {
// 1.准备牌
// 定义一个存储54张牌的ArrayList集合,泛型是String
ArrayList<String> poKe = new ArrayList<>();
// 定义两个数组,一个数组存储牌的花色,另一个存储牌的序号
String[] colors = {"红桃","黑桃","方片","梅花"};
String[] numbers ={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
// 先把大小王添加到poke集合中
poKe.add("大王");
poKe.add("小王");
// 循环嵌套遍历两个数组,组装52张牌
for (String color:colors){
for (String number:numbers){
// 把组合好的牌放到集合中
poKe.add(color+number);
}
}
// 2.洗牌
// 使用集合的工具类Collections中的方法
Collections.shuffle(poKe);
// 3.发牌
// 定义4个集合来存储玩家的牌和底牌
ArrayList<String> player1 = new ArrayList<>();
ArrayList<String> player2 = new ArrayList<>();
ArrayList<String> player3 = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
// 遍历poKe集合,获取每一张牌
// 使用poKe集合的索引,%3给3个玩家轮流发牌
// 注意:先判断底牌是否大于等于51
for (int i = 0 ; i <poKe.size();i++){
// 获取每一张牌
String p = poKe.get(i);
// 轮流发牌
if (i >= 51){
dipai.add(p);
}else if (i%3 == 0){
player1.add(p);
}else if (i%3 == 1){
player2.add(p);
}else if (i%3 == 2){
player3.add(p);
}
}
// 4.看牌
System.out.println("刘德华"+player1);
System.out.println("周润发"+player2);
System.out.println("周星驰"+player3);
System.out.println("底牌"+dipai);
}
}