斗地主发牌案例ArrayList版
- 准备牌
ArrayList<String> poker = new ArrayList<>();
- 洗牌
Collections.shuffle(poker);
- 发牌
- 看牌
import java.util.ArrayList;
import java.util.Collections;
public class DouDiZhu {
public static void main(String[] args) {
// 1、准备牌
// 定义一个存储54张牌的ArrayList集合,泛型使用String
ArrayList<String> poker = new ArrayList<>();
// 定义两个数组,分别存储花色和牌的序号
String[] colors = {"♠", "♥", "♣", "♦"};
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
poker.add("ColoredJoker");
poker.add("BlackJoker");
// 组装52张牌
for (String number : numbers) {
for (String color : colors) {
poker.add(color + number);
}
}
//System.out.println(poker);查看准备的54张牌
//-----------------------------------------//
// 2、洗牌
Collections.shuffle(poker);
//System.out.println(poker);查看打乱的54张牌
//-----------------------------------------//
// 3、发牌
// 定义4个集合
ArrayList<String> player1 = new ArrayList<>();
ArrayList<String> player2 = new ArrayList<>();
ArrayList<String> player3 = new ArrayList<>();
ArrayList<String> diPai = new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
String p = poker.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 {
player3.add(p);
}
}
//-----------------------------------------//
// 4、看牌
System.out.println("我:" + player1);
System.out.println("卢本伟:" + player2);
System.out.println("阿姨:" + player3);
System.out.println("底牌:" + diPai);
}
}
/*
我:[♥6, ColoredJoker, ♠Q, ♠7, ♦5, ♠10, ♥10, ♦J, ♣Q, ♣10, ♥5, ♠K, ♦2, ♣5, ♦10, ♥2, ♣9]
卢本伟:[♥4, ♦9, ♦Q, BlackJoker, ♠8, ♠6, ♣J, ♣2, ♦A, ♣A, ♦8, ♥A, ♦K, ♦3, ♦6, ♠3, ♥8]
阿姨:[♣4, ♠5, ♦4, ♣K, ♥J, ♣7, ♦7, ♣3, ♠A, ♠J, ♥K, ♣6, ♣8, ♥9, ♠4, ♥7, ♥Q]
底牌:[♥3, ♠2, ♠9]
*/