案例_模拟斗地主发牌
按照斗地主的规则,完成洗牌发牌的动作。
具体规则:
1.组装54张扑克牌
2.将54张牌顺序打乱
3.三个玩家参与游戏,三个人交替摸牌,每个人17张牌,最后三张留作底牌。
4.查看三人各自手中的牌(按照牌的大小排序)、底牌
手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
代码实现:
public class Test{
public static void main(String[] args) {
HashMap<Integer, String> poker = new HashMap<>();
ArrayList<Integer> pokerIndex = new ArrayList<>();
ArrayList<String> colors = new ArrayList<>();
Collections.addAll(colors,"♠","♥","♣","♦");
ArrayList<String> numbers = new ArrayList<>();
Collections.addAll(numbers,"2","A","K","Q","J","10","9","8","7","6","5","4","3");
int index = 0;
poker.put(index,"大王");
pokerIndex.add(index);
index++;
poker.put(index,"小王");
pokerIndex.add(index);
index++;
for (String number : numbers) {
for (String color : colors) {
poker.put(index,color+number);
pokerIndex.add(index);
index++;
}
}
Collections.shuffle(pokerIndex);
ArrayList<Integer> player1 = new ArrayList<>();
ArrayList<Integer> player2 = new ArrayList<>();
ArrayList<Integer> player3 = new ArrayList<>();
ArrayList<Integer> diPai = new ArrayList<>();
for (int i = 0; i < pokerIndex.size(); i++) {
Integer in = pokerIndex.get(i);
if (i > 50) {
diPai.add(in);
} else if (i % 3 == 0) {
player1.add(in);
} else if (i % 3 == 1) {
player2.add(in);
} else if (i % 3 == 2) {
player3.add(in);
}
}
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(diPai);
System.out.print("玩家1:");
for (Integer integer : player1) {
String s = poker.get(integer);
System.out.print(s+" ");
}
System.out.println();
System.out.print("玩家2:");
for (Integer integer : player2) {
String s = poker.get(integer);
System.out.print(s+" ");
}
System.out.println();
System.out.print("玩家3:");
for (Integer integer : player3) {
String s = poker.get(integer);
System.out.print(s+" ");
}
System.out.println();
System.out.print("底牌:");
for (Integer integer : diPai) {
String s = poker.get(integer);
System.out.print(s+" ");
}
}
}