斗地主游戏需求分析:
1.准备牌:54张牌,存储到一个集合中
特殊牌:大王和小王
其他52张牌:
定义一个数组/集合 ,存储四种花色
定义一个数组/集合,存储13个序号
循环嵌套遍历俩个数组/集合,组装52张牌
2.洗牌
使用集合工具类:Collections的方法
static void shuffle(List<?> list) 使用制定的随机顺序对制定列表进行置换
3.发牌
要求:1人17张牌 0-53%3
定义四个集合:存储三个玩家和底牌
索引:>=51 给底牌发牌
4.看牌
直接打印集合/遍历存储玩家和底牌的集合
代码实现:
public class doudiz {
public static void main(String[] args) {
//1.准备牌
ArrayList<String> poker=new ArrayList<>();
//定义俩个数组来组装牌:1)一个存花色 2)一个存序号
String[] colors={"♣","♥","♠","♦"};
String [] numbers={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
poker.add("大王");
poker.add("小王");
for(String number:numbers)
{
for (String color : colors) {
//把组装好的牌存储到扑克集合中
poker.add(color+number);
}
}
// System.out.println(poker);
//2.洗牌
Collections.shuffle(poker);
// System.out.println(poker);
//3.发牌
//定义四个集合,存储玩家的牌和底牌
ArrayList<String > play01=new ArrayList<>();
ArrayList<String> play02 = new ArrayList<>();
ArrayList<String> play03 = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
//4.遍历poker集合,获取每一张牌
for (int i=0;i<poker.size();i++)
{
String p = poker.get(i);
//轮流发牌
if(i>=51)
dipai.add(p);
else if(i%3==0) play01.add(p);
else if(i%3==1) play02.add(p);
else if(i%3==2) play03.add(p);
}
//5.看牌
System.out.println("刘德华"+play01);
System.out.println("周瑞发"+play02);
System.out.println("周启池"+play03);
}
}
ln("周瑞发"+play02);
System.out.println("周启池"+play03);
}
}