package com.joker.test; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Map; import java.util.Scanner; import java.util.Set; import com.joker.entity.Player; import com.joker.entity.Poker; /** * 主体类 * @author MeRos * */ public class PokerGame { Map<Integer, Player> playerMap; List<Poker> pokerList; Player player; String[] type = { "黑桃", "红桃", "梅花", "方块" }; String[] pointer = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" }; public PokerGame() { pokerList = new ArrayList<Poker>(); playerMap = new HashMap<Integer, Player>(); } /** * 创建扑克牌 * * @param args */ public void pokerAdd() { System.out.println("-------创建扑克牌-------"); for (int i = 0; i < type.length; i++) { for (int j = 0; j < pointer.length; j++) { pokerList.add(new Poker(type[i], pointer[j])); } } System.out.println("-------扑克牌创建成功-------"); } /** * 遍历显示所有扑克牌 * * @param args */ public void pokerGet() { for (Iterator<Poker> it = pokerList.iterator(); it.hasNext();) { for (int i = 0; i < type.length; i++) { for (int j = 0; j < pointer.length; j++) { Poker poker = it.next(); System.out.print(poker.toString() + " "); } System.out.println(); } } } /** * 洗牌 */ public void shufflePoker() { System.out.println("-------开始洗牌-------"); Collections.shuffle(pokerList); } /** * 创建玩家 * * @param args */ public void playerAdd() { System.out.println("-------创建玩家-------"); int i = 0; while (i < 2) { System.out.println("请输入玩家的ID:"); try { Scanner input = new Scanner(System.in); int id = input.nextInt(); Player playerID = playerMap.get(id); if (playerID == null) { System.out.println("请输入第" + (i + 1) + "位玩家的姓名:"); String name = input.next(); Player player = new Player(id, name); playerMap.put(id, player); System.out.println("成功创建玩家:" + playerMap.get(id).getName()); System.out.println("--------------------"); i++; } else { System.out.println("该ID已被占用~~"); continue; } } catch (Exception e) { System.out.println("请输入整数类型ID!!"); // e.printStackTrace(); continue; } } // 遍历输出玩家信息 Set<Entry<Integer, Player>> entrySet = playerMap.entrySet(); for (Entry<Integer, Player> entry : entrySet) { System.out.println("-----欢迎玩家:" + entry.getValue().getName()); } } /** * 开始发牌 * * @param args */ public void pokerSend() { System.out.println("-------开始发牌-------"); Set<Integer> keySet = playerMap.keySet(); int num = 0; for (int i = 0; i < 2; i++) { for (Integer id : keySet) { Player player = playerMap.get(id); if (player != null) { System.out.println("---玩家:" + player.getName() + "拿牌~~~"); player.handPoker.add(pokerList.get(num)); num++; } } } System.out.println("-------发牌结束-------"); } /** * 开始游戏,每个玩家取得手牌中的最大牌 * * @param args */ public void pokerGame() { System.out.println("-------游戏开始-------"); List<Poker> pokerMax = new ArrayList<Poker>(); Set<Entry<Integer, Player>> entrySet = playerMap.entrySet(); // 取得当前玩家的最大手牌 for (Entry<Integer, Player> entry : entrySet) { // Set集合转换为List集合 List<Poker> poker = new ArrayList<Poker>(entry.getValue().handPoker); // 手牌sort排序后最大的在最后,所以用到reverse进行翻转,将最大值放在最前面 Collections.sort(poker); Collections.reverse(poker); // 玩家手牌当中的最大牌放入pokerMax集合 pokerMax.add(poker.get(0)); System.out.println("玩家:" + entry.getValue().getName() + "最大的手牌是:" + poker.get(0).toString()); } // 取得pokerMax集合中的最大牌判断胜负 Collections.sort(pokerMax); Collections.reverse(pokerMax); for (Entry<Integer, Player> entry : entrySet) { if (entry.getValue().handPoker.contains(pokerMax.get(0))) { System.out.println("-------玩家:" + entry.getValue().getName() + "获胜!!"); } } // 输出玩家手中的所有牌 System.out.println("玩家各自的手牌为:"); for (Entry<Integer, Player> entry : entrySet) { System.out.print(entry.getValue().getName() + ":"); for (Poker poker : entry.getValue().handPoker) { System.out.print(poker.toString()+" "); } System.out.println(); } } public static void main(String[] args) { PokerGame pg = new PokerGame(); pg.pokerAdd(); pg.pokerGet(); pg.shufflePoker(); pg.playerAdd(); pg.pokerSend(); pg.pokerGame(); } }
package com.joker.entity; /** * 扑克牌类 四种花色:黑桃、红桃、梅花、方片 十三种点数:2-10、J、Q、K、A,不考虑大小王 * * @author MeRos * */ public class Poker implements Comparable<Poker> { private String type; private String pointer; public Poker(String type, String pointer) { this.setPointer(pointer); this.setType(type); } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getPointer() { return pointer; } public void setPointer(String pointer) { this.pointer = pointer; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((pointer == null) ? 0 : pointer.hashCode()); result = prime * result + ((type == null) ? 0 : type.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Poker)) return false; Poker other = (Poker) obj; if (pointer == null) { if (other.pointer != null) return false; } else if (!pointer.equals(other.pointer)) return false; if (type == null) { if (other.type != null) return false; } else if (!type.equals(other.type)) return false; return true; } @Override public int compareTo(Poker o) { String pokerPointer = "2,3,4,5,6,7,8,9,10,J,Q,K,A"; String pokerType = "方块,梅花,红桃,黑桃"; int result; result = pokerPointer.indexOf(this.getPointer()) - pokerPointer.indexOf(o.getPointer()); if (result == 0) { result = pokerType.indexOf(this.getType()) - pokerType.indexOf(o.getPointer()); } return result; } @Override public String toString() { return type + pointer; } }
package com.joker.entity; import java.util.HashSet; import java.util.Set; /** * 玩家类 * 属性:ID、姓名、手牌 * 手牌为扑克牌集合 * @author MeRos * */ public class Player { private int id; private String name; public Set<Poker> handPoker; public Player(int id, String name) { this.setId(id); this.setName(name); handPoker = new HashSet<Poker>(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
这里三个集合都有用到、玩家类中的手牌使用Set集合是因为只有一副牌,每个玩家拿到的手牌都是不同的~~
你的比较大小的方法,写的太棒啦!!!膜拜膜拜。。。。。。
Set<Entry<Integer, Player>> entrySet = playerMap.entrySet();
entry是在哪一节的??有点印象,忘记了
我不太明白,为什么Set,Map,List之间来回转换,都用统一的集合类型不行吗?
比如:
List<Poker> pokerList;
Set<Poker> handPoker;
handPoker = new HashSet<Poker>();这些都是存放扑克的,统一用Set或者List不行吗
还有
List<Poker> pokerMax = new ArrayList<Poker>();
Set<Entry<Integer, Player>> entrySet = playerMap.entrySet(); 这些转换一时真的很费解!!
求大神解读!!!!!!
比较大小用index那个方法真的太棒了!!!