集合模拟斗地主
public class Demo04Main {
public static void main(String[] args) {
//1、准备牌
ArrayList<String> poker = new ArrayList<>();
String[] colors = {"♥","♠","♦","♣"};
String[] nums = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
poker.add("大王");
poker.add("小王");
for (String color : colors) {
for (String num : nums) {
poker.add(color + num);
}
}
System.out.println(poker);
/*Iterator<String> it = poker.iterator();
while (it.hasNext()){
System.out.print(it.next());
}*/
//2、洗牌
Collections.shuffle(poker);
//3、发牌
ArrayList<String> play1 = new ArrayList<>();
ArrayList<String> play2 = new ArrayList<>();
ArrayList<String> play3 = new ArrayList<>();
ArrayList<String> dipai = new ArrayList<>();
//3张底牌
for (int i = 0; i < poker.size(); i++) {
String s = poker.get(i);
if (i>=51){
dipai.add(s);
}else if (i % 3 == 0){
play1.add(s);
}else if (i % 3 == 1){
play2.add(s);
}else if (i % 3 == 2){
play3.add(s);
}
}
//4看牌
System.out.println(play1);
System.out.println(play2);
System.out.println(play3);
System.out.println(dipai);
}
}
运行结果:
[♦4, ♦Q, ♠9, ♣8, ♦A, ♣K, ♥5, ♠8, ♥J, ♠4, ♣4, ♦10, ♦6, ♥7, ♠2, ♣3, ♠10]
[♠7, ♥6, ♠Q, ♠J, ♦J, ♠K, ♣5, ♥K, 小王, ♣2, ♥8, ♠A, ♣9, ♣7, 大王, ♦8, ♦K]
[♠5, ♥A, ♠6, ♥Q, ♣Q, ♦5, ♥4, ♦7, ♦3, ♠3, ♥9, ♦9, ♣10, ♥2, ♥3, ♣J, ♣6]
[♣A, ♥10, ♦2]