具体规则:
1. 组装54张扑克牌
2. 将54张牌顺序打乱
3. 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
4. 查看三人各自手中的牌(按照牌的大小排序)、底牌
编码:
注意:要修改java文件编码,由GBK修改为UTF-8,因为默认的字符编码GBK没有我们要的梅花、方片、黑桃、红桃(♠♥♦♣)等特殊字符。
public class Demo01 { public static void main(String[] args) { // 封装Map Map<Integer, String> pooker = new HashMap<Integer, String>(); ArrayList<Integer> pookernumber = new ArrayList<Integer>(); // 准备花色 String[] color = { "♥", "♠", "♣", "♦" }; // 准备牌号 String[] number = { "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3" }; // 准备下标 int index = 2; for (String n : number) { for (String c : color) { pooker.put(index, c + n); pookernumber.add(index); index++; } } // 封装大小王 pooker.put(0, "大王"); pooker.put(1, "小王"); pookernumber.add(0); pookernumber.add(1); // 测试 System.out.println(pooker); // 洗牌 Collections.shuffle(pookernumber); // 发牌 ArrayList<Integer> player1 = new ArrayList<Integer>(); ArrayList<Integer> player2 = new ArrayList<Integer>(); ArrayList<Integer> player3 = new ArrayList<Integer>(); ArrayList<Integer> bottom = new ArrayList<Integer>(); for (int i = 0; i < pookernumber.size(); i++) { if (i < 3) { bottom.add(pookernumber.get(i)); } else if (i % 3 == 0) { player1.add(pookernumber.get(i)); } else if (i % 3 == 1) { player2.add(pookernumber.get(i)); } else if (i % 3 == 2) { player3.add(pookernumber.get(i)); } } // 排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); Collections.sort(bottom); //看牌 lookpooker("农民",player1,pooker); lookpooker("农民",player2,pooker); lookpooker("地主",player3,pooker); lookpooker("底牌",bottom,pooker); } // 看牌 public static void lookpooker(String playerName, ArrayList<Integer> player, Map<Integer, String> pooker) { System.out.print(playerName + ":"+" "); // 遍历 for (int pook : player) { System.out.print(pooker.get(pook) + " "); } System.out.println(); } }
最终效果: