package collectiontest.games; import Java.util.ArrayList; import java.util.List; public class Player { //玩家的ID private String id ; //玩家姓名 private String name ; //玩家所持牌 private List<Card> pokerType ; public Player() { } public Player(String id, String name, List<Card> pokerType) { this.id = id; this.name = name; this.pokerType = new ArrayList<>(); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Card> getPokerType() { return pokerType; } public void setPokerType(List<Card> pokerType) { this.pokerType = pokerType; } }
扑克牌游戏主类:包含1)扑克牌创建 2)玩家创建 3)洗牌 4)发牌 5)比较胜负
package collectiontest.games; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class GamsBegin { // 创建扑克牌 public Set<Poker> cPoker() { System.out.println("**********开始创建扑克牌**********"); // 创建一副poker // 四个类型:黑--4、红--3、梅--2、方--1 Set<Poker> pokers = new HashSet<>(); Poker[] poker = { new Poker(1), new Poker(2), new Poker(3), new Poker(4) }; /* * Collections工具类的使用 * Collections.addAll(pokers, new Poker(1), new Poker(2), new Poker(3),new Poker(4)); * * */ pokers.addAll(Arrays.asList(poker)); System.out.println("**********扑克牌创建成功**********"); return pokers; } // 创建两个玩家 public Map<String, Player> cPlayer() { System.out.println("**********开始创建玩家**********"); Map<String, Player> map = new HashMap<String, Player>(); // 控制数量 Integer control = 0; System.out.println("创建两名玩家,根据提示创建"); Scanner console = new Scanner(System.in); while (true) { System.out.println("请输入第 "+(control+1)+" 个玩家ID:"); String courseId = console.next(); if (isNumeric(courseId)) { System.out.println("请输入第 "+(control+1)+" 个玩家姓名:"); String courseName = console.next(); Player players = new Player(courseId, courseName, null); //保存数据 map.put(courseId, players); System.out.println("添加第 " + (control + 1) + " 个玩家 " + courseName + " 成功"); //数量自加 control++; } else { System.out.println("*****请输入数字ID*****"); continue; } if (control == 2) { break; } } System.out.println("**********玩家创建成功**********"); return map; } // 判断输入是否为数字, Character.isDigit()为java方法 public boolean isNumeric(String str) { for (int i = 0; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } /** * 洗牌 :也可以产生52个不同随机数,实现洗牌 * **/ public List<Card> wPoker(Set<Poker> pokers) { System.out.println("**********开始洗牌**********"); //利用List的有序排序,洗牌之后保存顺序不变 List<Card> listCard = new ArrayList<>(); // 利用Set集合的无序排序,实现洗牌 Set<Card> listSet = new HashSet<>(); //保存到Set集合,无序 for (Poker pk : pokers) { listSet.add(pk.getId2()); listSet.add(pk.getId3()); listSet.add(pk.getId4()); listSet.add(pk.getId5()); listSet.add(pk.getId6()); listSet.add(pk.getId7()); listSet.add(pk.getId8()); listSet.add(pk.getId9()); listSet.add(pk.getId10()); listSet.add(pk.getJ()); listSet.add(pk.getQ()); listSet.add(pk.getK()); listSet.add(pk.getA()); } //保存在List集合,有序 for (Card cd : listSet) { listCard.add(cd); System.out.println(cd); } System.out.println("**********洗牌成功**********"); return listCard; } // 发牌 public Map<String, Player> pushPoker(List<Card> listCard, Map<String, Player> pMap) { System.out.println("**********发牌开始**********"); // 控制每人发两张牌后结束 int control = 0; for (Map.Entry<String, Player> entry : pMap.entrySet()) { if (control == 0) { for (int i = 0; i < 3; i = i + 2) { // 发牌 entry.getValue().getPokerType().add(listCard.get(i)); } // 更新map对象 pMap.put(entry.getKey(), entry.getValue()); control++; } else if (control == 1) { for (int i = 1; i < 4; i = i + 2) { // 发牌 entry.getValue().getPokerType().add(listCard.get(i)); } // 更新map对象 pMap.put(entry.getKey(), entry.getValue()); control++; } else { break; } } System.out.println("**********发牌成功**********"); return pMap; } public void compareMatch(Map<String, Player> newMap) { /*比较胜负 * 1.首先取得每个玩家手中最大牌的ID和花色ID。 * 2.比较俩玩家手中最大牌的ID大小,牌大者获胜。 * 3.如果两张牌的ID相等,在比较两张牌的花色ID,花色ID更大着获胜。 * * */ List<Player> players = new ArrayList<>(); // 获得两个玩家 for (Map.Entry<String, Player> entry : newMap.entrySet()) { players.add(entry.getValue()); } // 玩家一信息和所持牌 List<Card> playerOne = players.get(0).getPokerType(); //获得最大牌的ID和花色 Integer oneMaxId = Math.max(playerOne.get(0).getId(), playerOne.get(1) .getId()); Integer oneMaxType = (oneMaxId!=playerOne.get(0).getId()) ? playerOne.get(1).getType() : playerOne.get(0).getType() ; // 玩家二信息和所持牌 List<Card> playerTwo = players.get(1).getPokerType(); //获得最大牌的ID和花色 Integer twoMaxId = Math.max(playerTwo.get(0).getId(), playerTwo.get(1) .getId()); Integer twoMaxType = (twoMaxId!=playerTwo.get(0).getId()) ? playerTwo.get(1).getType() : playerTwo.get(0).getType() ; if (oneMaxId > twoMaxId) { System.out.println("玩家 : " + players.get(0).getName() + " 获胜!!"); } else if (oneMaxId == twoMaxId) { if (oneMaxType > twoMaxType) { System.out .println("玩家 : " + players.get(0).getName() + " 获胜!!"); } else { System.out .println("玩家 : " + players.get(1).getName() + " 获胜!!"); } } else { System.out.println("玩家 : " + players.get(1).getName() + " 获胜!!"); } System.out.println("**********************************************"); System.out.println("玩家 : " + players.get(0).getName() + "的牌是:" + showName(playerOne.get(0).getType(), 0) + "--" + showName(playerOne.get(0).getId(), 1) + " " + showName(playerOne.get(1).getType(), 0) + "--" + showName(playerOne.get(1).getId(), 1)); System.out.println("玩家 : " + players.get(1).getName() + "的牌是:" + showName(playerTwo.get(0).getType(), 0) + "--" + showName(playerTwo.get(0).getId(), 1) + " " + showName(playerTwo.get(1).getType(), 0) + "--" + showName(playerTwo.get(1).getId(), 1)); } // 显示牌的名称 private String showName(Integer i, Integer type) { String str = ""; // 显示花色 if (type == 0) { switch (i) { case 1: { str = "方块"; break; } case 2: { str = "梅花"; break; } case 3: { str = "红桃"; break; } case 4: { str = "黑桃"; break; } default: { break; } } } // 显示数字 if (type == 1) { if (i < 11) { return i.toString(); } else { switch (i) { case 11: { str = "J"; break; } case 12: { str = "Q"; break; } case 13: { str = "K"; break; } case 14: { str = "A"; break; } default: { break; } } } } return str; } public static void main(String[] args) { GamsBegin gb = new GamsBegin(); // 1、创建扑克牌 Set<Poker> pokers = gb.cPoker(); // 2、创建两个玩家 Map<String, Player> pMap = gb.cPlayer(); // 3、洗牌 List<Card> listCard = gb.wPoker(pokers); // 4、发牌 Map<String, Player> newMap = gb.pushPoker(listCard, pMap); // 4、比较胜负 gb.compareMatch(newMap); } }
运行结果: