package Demo31;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
/*
斗地主综合案例:
1、准备牌
2、洗牌
3、发牌
4、看牌
*/
public class DouDiZhu {
public static void main(String[] args) {
//1、准备牌
//定义一个存储54张牌的ArrayList集合,泛型使用String
ArrayList<String> poker = new ArrayList<>();
//定义俩个数组,一个数组存储花色,一个数组存储牌的序号
String[] colors = {"♥","♦","♣","♠"};
String[] numbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
//先把大王和小王存储到piker集合中
poker.add("大王");
poker.add("小王");
//循环嵌套遍历两个数组,组装52张牌
for (String number : numbers) {
for (String color : colors) {
//System.out.println(color+number);
poker.add(color+number);
}
}
// System.out.println(poker);
//2、洗牌
//使用集合的工具类Collections中的方法
//static void shuffle(List<?> list) 使用默认随机源对指定列表进行置换
Collections.shuffle(poker);
System.out.println(poker);
//3、发牌
//定义4个集合,存储玩家的牌和底牌
ArrayList<Object> play01 = new ArrayList<>();
ArrayList<Object> play02 = new ArrayList<>();
ArrayList<Object> play03 = new ArrayList<>();
ArrayList<Object> DiPai = new ArrayList<>();
/*
遍历poker集合,获取每一张牌
使用poker集合的索引%3给玩家发牌
注意:判断底牌(i>=51)
*/
for (int i = 0; i < poker.size(); i++) {
//获取每一张牌
String p = poker.get(i);
//轮流发牌
if(i >= 51){
//给底牌发牌
DiPai.add(p);
}else if(i%3==0){
//给玩家1发牌
play01.add(p);
}else if(i%3==1){
//给玩家2发牌
play02.add(p);
}else if (i%3==2){
//给玩家3发牌
play03.add(p);
}
}
//4、看牌
System.out.println("刘德华"+play01);
System.out.println("周润发"+play02);
System.out.println("周星驰"+play03);
System.out.println("底牌"+DiPai);
}
}
输出
[♥8, ♣7, ♥K, ♦6, ♣5, ♠2, ♠6, ♥4, ♦9, ♠9, ♠7, ♦A, ♣Q, ♠3, 小王, ♥9, ♠10, ♣K, ♦5, ♦8, ♦2, ♦4, ♥A, ♣4, ♦Q, ♥J, ♠5, ♣3, ♣6, ♠J, ♥5, ♥2, 大王, ♥10, ♣8, ♠Q, ♥Q, ♣J, ♠K, ♦J, ♦K, ♦10, ♦3, ♣2, ♠8, ♦7, ♣A, ♥6, ♣9, ♣10, ♠A, ♥7, ♠4, ♥3]
刘德华[♥8, ♦6, ♠6, ♠9, ♣Q, ♥9, ♦5, ♦4, ♦Q, ♣3, ♥5, ♥10, ♥Q, ♦J, ♦3, ♦7, ♣9]
周润发[♣7, ♣5, ♥4, ♠7, ♠3, ♠10, ♦8, ♥A, ♥J, ♣6, ♥2, ♣8, ♣J, ♦K, ♣2, ♣A, ♣10]
周星驰[♥K, ♠2, ♦9, ♦A, 小王, ♣K, ♦2, ♣4, ♠5, ♠J, 大王, ♠Q, ♠K, ♦10, ♠8, ♥6, ♠A]
底牌[♥7, ♠4, ♥3]
(java总结)斗地主案例
最新推荐文章于 2022-01-21 12:04:55 发布