package cn.itcast;
import java.util.ArrayList;
import java.util.Collections;
/*
* 模拟斗地主的洗牌和发牌
* 斗地主思路:
* 准备牌:
* 数字:
* ArrayList<String> 3,4,5,6,7,8,9,10,J,Q,K,A,2
* 花色:
* ArrayList<String> ♠♥♦♣
* 牌:
* ArrayList<String> 花色+数字
* 大小王:
* 小☺ 大☻
* 洗牌
* 打乱牌的顺序:Collecions.shuffle(牌)
* 发牌
* 将牌分配给4个arraylist
* ArrayList<String> 玩家1
* ArrayList<String> 玩家2
* ArrayList<String> 玩家3
* ArrayList<String> 底牌
*/
public class Test8 {
public static void main(String[] args) {
//准备牌
//花色
ArrayList<String> color = new ArrayList<String>();
color.add("♠");
color.add("♥");
color.add("♦");
color.add("♣");
//数字
ArrayList<String> number = new ArrayList<String>();
Collections.addAll(number, "3","4","5","6","7","8","9","10","J","Q","K","A","2");
//牌
ArrayList<String> cards = new ArrayList<String>();
for (String thisNumber : number) {
for (String thisColor : color) {
cards.add(thisColor+thisNumber);
}
}
//大小王
cards.add("小☺");
cards.add("大☻");
//洗牌
Collections.shuffle(cards);
//发牌
ArrayList<String> player = new ArrayList<String>();
ArrayList<String> player2 = new ArrayList<String>();
ArrayList<String> player3 = new ArrayList<String>();
ArrayList<String> dipai = new ArrayList<String>();
for (int i = 0; i < cards.size(); i++) {
//最后三张给底牌
if(i>=51) {
dipai.add(cards.get(i));
}else {
//模与3 余1给第一个人发,余2给第二个人发,剩下的给第三个人发
if(i%3==1) {
player.add(cards.get(i));
}else if(i%3==2){
player2.add(cards.get(i));
}else {
player3.add(cards.get(i));
}
}
}
//看牌
System.out.println(player);
System.out.println(player2);
System.out.println(player3);
System.out.println(dipai);
}
}
|
|