olaolaola
2020-02-29 02:37
我自己转了下可以用,但是很奇怪Poker.java文件的ArrayList我存的时候是Poker,但是取出来却是Object需要转换。
Poker.java
package kasei; import java.util.Map; import java.util.HashMap; import java.util.List; public class Poker implements Comparable<Poker> { public int suit; public int number; public static Map<Integer, String> suits = new HashMap<Integer, String>() { { put(1, "方片"); put(2, "梅花"); put(3, "紅心"); put(4, "黑桃"); } }; public static Map<Integer, String> numbers = new HashMap<Integer, String>() { { put(2, "2"); put(3, "3"); put(4, "4"); put(5, "5"); put(6, "6"); put(7, "7"); put(8, "8"); put(9, "9"); put(10, "10"); put(11, "J"); put(12, "Q"); put(13, "K"); put(14, "A"); } }; public Poker() { } public Poker(int suit, int number) { this.suit = suit; this.number = number; } public static String getPokersList(List pokers){ String print = "["; for (Object obj : pokers) { Poker poker = (Poker) obj; print += poker.getName() + ", "; } // 删除最后一项逗号和空格,替换为] print = print.replaceAll(", $", "]"); return print; } public String getName() { return suits.get(suit) + numbers.get(number); } public int getSize() { return this.number * 10 + this.suit; } /** * 降序需要-1 t>o */ public int compareTo(Poker o) { if (this.getSize() > o.getSize()) { return -1; } else { return 1; } } }
Initail.java
package kasei; /** * 扑克牌游戏 * 1. 创建扑克牌 * 1.1 四个花色黑桃、红桃、梅花、方片 * 1.2 十三种点数2-10, J, Q, K, A * 2. 两名玩家 * 2.1 要有ID、姓名、手牌(扑克牌集合) * 2.2 手牌2张 * 3. 洗牌 * 3.1 打乱之前扑克牌顺序 * 4. 发牌 * 4.1 从第一张开始,一人一张发给每个玩家 * 4.2 每人发两张 * 5. 比大小 * 5.1 取玩家手牌中最大一张进行比较 * 5.2 先比点数,点数相同则比花色 */ /** * UML * 1. Game * pokers * players * playerNum * suits * initPokers() * randomPokers() * putPoker() * startGame() * 2. Poker * suit * number * compareTo() * 3. Player * id * name * hand */ public class Initail { public static void main(String[] args) { Game g = new Game(); g.startGame(); } }
Game.java
package kasei; import java.util.Scanner; import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.InputMismatchException; public class Game { public Scanner console = new Scanner(System.in); public List<Poker> pokers; public List<Player> players; public int playerNum = 2; public int playerHandNum = 3; public Game() { this.pokers = new ArrayList<Poker>(); this.players = new ArrayList<Player>(); } /** * 初始化扑克牌 */ public void initPokers() { for (Integer suit : Poker.suits.keySet()) { for (Integer number : Poker.numbers.keySet()) { Poker poker = new Poker(suit, number); pokers.add(poker); } } } public static String getType(Object o) { //获取变量类型方法 return o.getClass().toString(); //使用int类型的getClass()方法 } /** * 打乱扑克牌 */ public void randomPokers() { Collections.shuffle(this.pokers); } /** * 将牌库第一张牌 派发给指定玩家 */ public void putPoker(int index) { Poker poker = pokers.get(0); pokers.remove(0); players.get(index).hand.add(poker); } /** * 开始游戏 */ public void startGame() { System.out.println("——创建扑克牌——"); this.initPokers(); System.out.println("——创建扑克牌成功!——"); System.out.println("为:" + Poker.getPokersList(this.pokers)); System.out.println("——开始洗牌——"); this.randomPokers(); System.out.println("——洗牌结束!——"); System.out.println("——创建玩家...——"); for (int i = 0; i < playerNum; i++) { System.out.println("——请输入第" + (i + 1) + "位玩家的ID和姓名——"); int id = -1; String name = ""; boolean flag = false; // 创建id do { try { System.out.println("输入ID:"); id = console.nextInt(); flag = true; } catch (InputMismatchException e) { System.out.println("请输入整数类型的ID!"); } catch (Exception e) { System.out.println("未知异常"); e.printStackTrace(); } console.nextLine(); // 跳过异常流的输入 } while (flag == false); // 创建name flag = false; do { try { System.out.println("输入姓名:"); name = console.nextLine(); if (!"".equals(name)) { flag = true; } else { throw new InputMismatchException(); } } catch (InputMismatchException e) { System.out.println("请输入不为空的字符串!"); } catch (Exception e) { System.out.println("未知异常"); e.printStackTrace(); } } while (flag == false); Player p = new Player(id, name); players.add(p); } for (Player player : players) { System.out.println("——欢迎玩家" + player.name); } System.out.println("——开始发牌——"); for (int i = 0; i < playerNum * playerHandNum; i++) { int index = i % playerNum; putPoker(index); System.out.println("——玩家:" + players.get(index).name + "-拿牌"); } System.out.println("——发牌结束!——"); System.out.println("——开始游戏...——"); for (Player player : players) { Poker poker = player.getItibannPoker(); System.out.println("玩家:" + player.name + "最大的手牌为:" + poker.getName()); } List<Player> pMirror = new ArrayList<Player>(this.players); Collections.sort(pMirror, new PlayerComparator()); System.out.println("——玩家:" + pMirror.get(0).name + "获胜!——"); System.out.println("——玩家各自的手牌为:——"); for (Player player : players) { System.out.println(player.name + ":" + Poker.getPokersList(player.hand)); } } }
Player.java
package kasei; import java.util.Collections; import java.util.List; import java.util.ArrayList; public class Player { public int id; public String name; public List<Poker> hand; public Player(int id, String name) { this.id = id; this.name = name; this.hand = new ArrayList<Poker>(); } public Poker getItibannPoker() { List<Poker> pokers = new ArrayList<Poker>(this.hand); Collections.sort(pokers); if (pokers.size() > 0) { return pokers.get(0); } return new Poker(); } }
PlayerComparator.java
package kasei; import java.util.Comparator; public class PlayerComparator implements Comparator<Player> { public int compare(Player p1, Player p2) { if (p1.getItibannPoker().getSize() > p2.getItibannPoker().getSize()) { return -1; } else { return 1; } } }
如果把对象放入集合中,会忽略他的类型,把他当作Object来处理,因此在取集合中元素的同时,需要强制类型转换。
Java入门第三季
409792 学习 · 4340 问题
相似问题