package Demo02;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
/*
斗地主综合案例:有序版本
按照斗地主的规则,完成洗牌发牌的动作。 具体规则:
使用54张牌打乱顺序,三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
*/
public class Demo02Main {
public static void main(String[] args) {
// 存储牌的Map集合
HashMap<Integer, String> poker = new HashMap<>();
ArrayList<Integer> pokerIndex = new ArrayList<>();
List<String> colors = List.of("♠", "♥", "♣", "♦");
List<String> numbers = List.of("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++;
}
}
//System.out.println(poker);
//System.out.println(pokerIndex);
/*
2.洗牌
*/
Collections.shuffle(pokerIndex);
//System.out.println(pokerIndex);
/*
3.发牌
*/
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 < 54; i++) {
if (i > 50){
dipai.add(pokerIndex.get(i));
}else if (i%3 == 0){
player1.add(pokerIndex.get(i));
}else if (i%3 == 1){
player2.add(pokerIndex.get(i));
}else if (i%3 == 2){
player3.add(pokerIndex.get(i));
}
}
/*
4.给牌排序
*/
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(dipai);
/*
5.看牌
*/
cardReading("刘德华", player1, poker);
cardReading("周润发", player2, poker);
cardReading("卢本伟", player3, poker);
System.out.print("\n底牌:");
for (Integer integer : dipai) {
System.out.print(poker.get(integer) + " ");
}
}
public static void cardReading(String name, ArrayList<Integer> player, HashMap<Integer, String> poker){
System.out.print("\n玩家-->" + name + "<--的手牌:");
for (Integer integer : player) {
System.out.print(poker.get(integer)+" ");
}
}
}
简单Java斗地主案例:有序版
最新推荐文章于 2021-11-30 19:42:56 发布