/**
* 实现模拟斗地主的功能
* 1.组合牌
* 2.洗牌
* 3.发牌
* 4.看牌
*/
public class DouDiZhu {
public static void main(String[] args) {
//1.组合牌
//创建Map集合,键是编号,值是排
HashMap<Integer, String> poker = new HashMap<Integer, String>();
//创建List集合,存储编号
ArrayList<Integer> pokerNumber = new ArrayList<Integer>();
// 定义13个点数的数组
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
// 定义4个花色数组
String[] colors = {"♠", "♥", "♣", "◇"};
// 定义整数变量 作为键出现
int index = 2;
// 遍历数组 花色+点数的组合,存储到Map集合
for (String number : numbers) {
for (String color : colors) {
poker.put(index, color + number);
pokerNumber.add(index);
index++;
}
}
//存储大王 小王
poker.put(0, "大王");
pokerNumber.add(0);
poker.put(1, "小王");
pokerNumber.add(1);
// 洗牌功能 把牌的编号打乱
Collections.shuffle(pokerNumber);
// 发牌功能 将牌编号,发给玩家集合,底牌集合
ArrayList<Integer> player1 = new ArrayList<Integer>();
ArrayList<Integer> player2 = new ArrayList<Integer>();
ArrayList<Integer> player3 = new ArrayList<Integer>();
ArrayList<Integer> boottm = new ArrayList<Integer>();
// 发牌采用的是集合索引%3
for (int i = 0; i < pokerNumber.size(); i++) {
if (i < 3) {
boottm.add(pokerNumber.get(i));
} else if (i % 3 == 0) {
player1.add(pokerNumber.get(i));
} else if (i % 3 == 1) {
player2.add(pokerNumber.get(i));
} else if (i % 3 == 2) {
player3.add(pokerNumber.get(i));
}
}
//对玩家手中的牌进行排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
// 看牌
look("player1",player1,poker);
look("player2",player2,poker);
look("player3",player3,poker);
look("底牌",boottm,poker);
}
public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> poker){
//遍历ArrayList集合
for(Integer key:player){
String value = poker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}
实现模拟斗地主
最新推荐文章于 2024-03-19 08:01:47 发布