个人小结,写的过程中主要卡在三个地方,:
1.洗牌打乱顺序 不知道用Collections的shuffle()方法 自己用ArrayList遍历放到HashSet里再放回去试了一通,完全不能用(顺序确实乱了,但每次都乱的一毛一样)
2.输入ID不正时,如何重复输入。 琢磨出while死循环+try-catch方式 一直有个疑问:编译器总提示Scanner对象从未close,加了close又会出现死循环,没等用户输入就向下继续执行了
3.比较牌面时,Comparable和Comparator重写的方法的实现方式 打扑克2最大,A其次,然后K Q J 只知道课程里讲到的compareTo和compare方法,于是投机取巧利用HashMap硬将无规律字符串与整数值映射,再compare. 疑问:参考了其它同学的内容,一种做法是把代表牌面大小(点数和花色)变一个属性会更简便些,或者其它做法...?
代码如下:
GameTest.java
package test_pukepa_game; import java.util.ArrayList; import java.util.Collections; import java.util.InputMismatchException; import java.util.List; import java.util.Scanner; public class GameTest { private List<PlayingCard> cardsList; private List<Player> playerList; public GameTest() { this.cardsList = new ArrayList<>(); this.playerList = new ArrayList<Player>(); } // 创建扑克牌 public void makeCards() { String[] types = {"黑桃","红桃","梅花","方片"}; String[] points = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; List<String> cards = new ArrayList<String>(); for (int i = 0; i < types.length; i++) { for (int j = 0; j < points.length; j++) { cardsList.add(new PlayingCard(points[j], types[i])); cards.add(types[i] + points[j]) ; } } System.out.println("----------扑克牌创建成功!------------"); System.out.println("为:" + cards.toString()); } // 洗牌 public void resetCards() { // 重复玩时, 清空玩家的手牌 if(playerList.size() > 0) { for (Player player : playerList) { player.getCards().clear(); } } System.out.println("----------洗牌前:"+cardsList.toString()); //*******Collections类的shuffle()方法的作用是将List中的内容随机打乱顺序*****// Collections.shuffle(cardsList); //**********************************// System.out.println("----------洗牌后:"+cardsList.toString()); System.out.println("----------洗牌结束!------------------"); } // 创建玩家 public void makePlayer() { for (int i = 1; i < 3; i++) { System.out.println("请输入第" + i + "位玩家的ID和姓名:"); while (true) { try { Scanner console = new Scanner(System.in); System.out.println("输入ID:"); Integer id = console.nextInt(); System.out.println("输入姓名:"); String name = console.next(); playerList.add(new Player(id, name)); break; } catch (InputMismatchException e) { // TODO Auto-generated catch block System.out.println("请输入整数类型的ID!"); continue; } } } for (Player player : playerList) { System.out.println("---欢迎玩家:" + player.getName()); } } // 发牌 一人发两张牌 public void takeCards() { for (int i = 0; i < 2; i++) { for (int j = 0; j < playerList.size(); j++) { Player player = playerList.get(j); player.getCards().add(new PlayingCard(cardsList.get(i*2 +j).getNumber(), cardsList.get(i*2+j).getType())); System.out.println("---玩家:"+ player.getName() + "-拿牌"); } } System.out.println("----------发牌结束!------------------"); } // 游戏开始比较 public void startGame() { ArrayList<PlayingCard> cards = new ArrayList<PlayingCard>(); //取出手中点数最大的牌 for (Player player : playerList) { Collections.sort(player.getCards()); PlayingCard pc = player.getCards().get(1); cards.add(pc); System.out.println("玩家:"+player.getName() + "最大的手牌为:"+pc.getType()+pc.getNumber()); } // 最大的牌进行比较 if(cards.get(0).getNumber().equals(cards.get(1).getNumber())) { Collections.sort(cards, new PlayingCardComparator()); } else { Collections.sort(cards); } // 遍历找出最大牌的拥有者为胜者 hashCode 和 equals方法重写 for (Player player : playerList) { if(player.getCards().contains(cards.get(1))){ System.out.println("---------玩家:"+player.getName()+"获胜!--------------------"); break; } } System.out.println("玩家各自的手牌为:"); for (Player player : playerList) { System.out.println(player.getName()+":"+player.getCards().toString()); } } public static void main(String[] args) { GameTest gt = new GameTest(); System.out.println("----------创建扑克牌------------------"); gt.makeCards(); gt.makePlayer(); while(true) { System.out.println("----------开始洗牌...--------------------"); gt.resetCards(); System.out.println("----------开始发牌...--------------------"); gt.takeCards(); System.out.println("----------开始游戏...--------------------"); gt.startGame(); System.out.println("----------还要继续吗?1--是 0--否------"); Scanner console = new Scanner(System.in); try { if(0 == console.nextInt()) { System.out.println("----------游戏结束------"); break; } } catch(InputMismatchException e) { System.out.println("----------游戏结束------"); break; } } } }
PlayingCard.java //扑克牌
package test_pukepa_game; import java.util.HashMap; public class PlayingCard implements Comparable<PlayingCard>{ private String number; private String type; public PlayingCard(String number, String type) { this.number = number; this.type = type; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getType() { return type; } public void setType(String type) { this.type = type; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((number == null) ? 0 : number.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 PlayingCard)) return false; PlayingCard other = (PlayingCard) obj; if (number == null) { if (other.number != null) return false; } else if (!number.equals(other.number)) 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(PlayingCard o) { // TODO Auto-generated method stub HashMap<String, Integer> hashmap = new HashMap<String, Integer>(); for(int i = 3; i <=10; i++) { hashmap.put(String.valueOf(i), i); } hashmap.put("J", 11); hashmap.put("Q", 12); hashmap.put("K", 13); hashmap.put("A", 14); hashmap.put("2", 15); // 按照点数比较大小 return hashmap.get(this.number).compareTo(hashmap.get(o.number)); } @Override public String toString() { return type+ number ; } }
PlayingCardComparator.java //按花色比较的Comparator接口实现类
package test_pukepa_game; import java.util.Comparator; import java.util.HashMap; public class PlayingCardComparator implements Comparator<PlayingCard> { @Override public int compare(PlayingCard o1, PlayingCard o2) { // TODO Auto-generated method stub HashMap<String, Integer> hashmap = new HashMap<String, Integer>(); hashmap.put("黑桃", 4); hashmap.put("红桃", 3); hashmap.put("梅花", 2); hashmap.put("方片", 1); return hashmap.get(o1.getType()).compareTo(hashmap.get(o2.getType())); } }
Player.java // 玩家类
package test_pukepa_game; import java.util.ArrayList; import java.util.List; // 玩家类 public class Player { private Integer id; private String name; private List<PlayingCard> cards; public Player(Integer id, String name) { this.id = id; this.name = name; this.cards = new ArrayList<PlayingCard>(); } public Integer getId() { return id; } public String getName() { return name; } public List<PlayingCard> getCards() { return cards; } }
shuffle()方法 有这么一个功能:
Random rand = new Random(47); shuffle(cardsList,rand);
就可以实现随机打乱
网址:https://blog.csdn.net/u011514810/article/details/51218784
楼主我也卡在try语句块这里了,我查了一些资料,也找到了你这种方法,但是据说在while语句块中使用try效率很低,不知道楼主找到替代方法了没?