--> Poker類用於存入54張撲克牌 --> Player類對玩家進行發牌和顯示處理(寫完我就後悔了,J,Q,K,A,2的排序太low了..package com.dragon.java.hwddz; --> 寫完發現邏輯是不是有問題啊... ...
package com.dragon.java.hwddz; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /*1. 寫一個可以給鬥地主三家隨機發牌的程式。 a:牌可以隨機發給三個玩家 b:在控制台列印每個玩家的牌。 c:對每個玩家手中的牌按照大寫排序。 ``` 牌的花色:"♠", "♥", "♣", "♦" *思路:創建一個容器存儲所有的牌,再創建三個容器分別表示三個用戶的牌,三個用戶輪流從第一個容器中取牌, *剩下三張為底牌。把大王和小王也算進去**/ public class Test { public static void main(String args[]) { Map<Integer, String> mapPoker = new HashMap<>(); List<Integer> listIndex = new ArrayList<>(); Poker.setPoker(mapPoker, listIndex); Poker.washPoker(listIndex);
// 定義用戶 Player[] players = new Player[3]; players[0] = new Player("躺之徳"); players[1] = new Player("小剪"); players[2] = new Player("呆立叫");
// 用戶容器 List<Integer> tzd = new ArrayList<>(); List<Integer> xj = new ArrayList<>(); List<Integer> dlj = new ArrayList<>(); List<Integer> dp = new ArrayList<>(); Player.getPoker(tzd, xj, dlj, dp, listIndex); // int index = Player.getMaxNumber(players); // System.out.println(players[index].getName() + "搶到了地主!"); players[0].showPoker(tzd, mapPoker); players[1].showPoker(xj, mapPoker); players[2].showPoker(dlj, mapPoker); // players[index].showDp(dp, mapPoker); System.out.println("\n" + "底牌:"); for (Integer integer : dp) { System.out.print(mapPoker.get(integer) + " "); } } }
--> Poker類用於存入54張撲克牌
package com.dragon.java.hwddz; import java.util.Collections; import java.util.List; import java.util.Map; public class Poker { private static String[] pokerColor = { "♠", "♥", "♣", "♦" }; private static String[] pokerNum = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
// 存入54張不同的牌,並給每張牌一個標號 public static void setPoker(Map<Integer, String> mapPoker, List<Integer> listIndex) { int index = 0; for (String nums : pokerNum) { for (String colors : pokerColor) { String poker = colors.concat(nums); mapPoker.put(index, poker); listIndex.add(index); index++; } } mapPoker.put(index, "大☝王"); listIndex.add(index); index++; mapPoker.put(index, "小☟王"); listIndex.add(index); }
// 洗牌 --> 即洗標號 public static void washPoker(List<Integer> listIndex) { Collections.shuffle(listIndex); } }
--> Player類對玩家進行發牌和顯示處理(寫完我就後悔了,J,Q,K,A,2的排序太low了..package com.dragon.java.hwddz;
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Random; public class Player { private String name; Player() { super(); } Player(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }
// 發牌流程 public static void getPoker(List<Integer> player1, List<Integer> player2, List<Integer> player3, List<Integer> dp, List<Integer> list) { for (int i = 0; i < list.size(); i++) { if (i >= list.size() - 3) { dp.add(list.get(i)); } else if (i % 3 == 1) { player1.add(list.get(i)); } else if (i % 3 == 2) { player2.add(list.get(i)); } else if (i % 3 == 0) { player3.add(list.get(i)); } } }
// 顯示牌面 public void showPoker(List<Integer> list, Map<Integer, String> mapPoker) { System.out.println("\n" + name + "的手牌是:"); List<String> listPoker = new ArrayList<>(); for (Integer key : list) { listPoker.add(mapPoker.get(key)); } Collections.sort(listPoker, new MyComparator()); for (String string : listPoker) { System.out.print(string + " "); } } // public void showDp(List<Integer> dp, Map<Integer, String> mapPoker) { // List<String> listPoker = new ArrayList<>(); // for (Integer key : dp) { // listPoker.add(mapPoker.get(key)); // } // Collections.sort(listPoker, new MyComparator()); // for (String string : listPoker) { // System.out.print(string + " "); // } // } public int throwsElbows() { Random random = new Random(); int num = random.nextInt(6) + 1; System.out.println(name + "搖了" + num + "點"); return num; } // 請忽略下麵註釋... // public static int getMaxNumber(Player[] players) { // List<Integer> numbers = new ArrayList<>(); // for (int i = 0; i < players.length; i++) { // numbers.add(players[i].throwsElbows()); // } // Collections.sort(numbers); // for (int i = 0; i < numbers.size() - 1; i++) { // if (numbers.get(numbers.size() - 1) == numbers.get(i)) { // System.out.println(players[numbers.size() - 1].name + "和" // + players[i].name + "搖的點數相同"); // System.out.println("他們打了一架! --> " + players[i].name + "贏了!"); // return i; // } // } // return numbers.size() - 1; // }
// 牌面的比較排序 --> 看得就煩... public static class MyComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { String m1 = o1.substring(1, 2); String m2 = o2.substring(1, 2); if (m1.equals("1")) { m1 = "a"; } else if (m1.equals("J")) { m1 = "b"; } else if (m1.equals("Q")) { m1 = "c"; } else if (m1.equals("K")) { m1 = "d"; } else if (m1.equals("A")) { m1 = "e"; } else if (m1.equals("2")) { m1 = "f"; } if (m2.equals("1")) { m2 = "a"; } else if (m2.equals("J")) { m2 = "b"; } else if (m2.equals("Q")) { m2 = "c"; } else if (m2.equals("K")) { m2 = "d"; } else if (m2.equals("A")) { m2 = "e"; } else if (m2.equals("2")) { m2 = "f"; } return m1.compareTo(m2); } } }
--> 寫完發現邏輯是不是有問題啊...