斗地主案例
package Demo04;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class DouDiZhu {
public static void main(String[] args) {
//1.准备牌
//定义一个存储54张牌的集合
ArrayList<String> poker=new ArrayList<>();
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) {
//System.out.println(color+number);
//把组装好的牌放在扑克中
poker.add(color+number);
}
//System.out.println(poker);
}
//洗牌使用Collections中的方法
Collections.shuffle(poker);
System.out.println(poker);
//3.发牌 定义四个集合储存玩家的牌和底牌
ArrayList<String> player01 =new ArrayList<>();
ArrayList<String> player02 =new ArrayList<>();
ArrayList<String> player03 =new ArrayList<>();
ArrayList<String> dipai =new ArrayList<>();
/*
遍历poker集合,获取每一张牌
使用poker集合的索引%3给3个玩家轮流发牌
剩余3张牌给底牌
先判断底牌(>=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){
player01.add(p);
}
else if(i%3==1){
player02.add(p);
}
else if(i%3==2){
player03.add(p);
}
}
//4.看牌
System.out.println("老村长 :"+player01);
System.out.println("刘二狗 :"+player02);
System.out.println("廖大阳 :"+player03);
System.out.println("底牌 :"+dipai);
}
}
老村长 :[♦9, ♥2, 大王, ♣A, ♣Q, ♣2, ♠8, ♣K, ♦3, 小王, ♠7, ♦5, ♥3, ♣J, ♠4, ♥K, ♠5]
刘二狗 :[♥7, ♥J, ♠9, ♥6, ♥4, ♦A, ♦4, ♦7, ♥10, ♠10, ♦8, ♦Q, ♥5, ♥A, ♦10, ♠A, ♥9]
廖大阳 :[♣5, ♠K, ♣10, ♦2, ♦J, ♣7, ♠Q, ♠3, ♠2, ♦K, ♠J, ♣9, ♣8, ♥Q, ♣4, ♦6, ♠6]
底牌 :[♣3, ♥8, ♣6]