-
2222222222
查看全部 -
1111111111111111111111111
查看全部 -
Liset接口说明
查看全部 -
Liset接口说明
查看全部 -
Collection接口主要增删改查
查看全部 -
集合框架collection和Map
查看全部 -
集合框架collection和ma
查看全部 -
集合框架collection和map
查看全部 -
random()方法返回double类型,强制类型转换为int,不然编译报错。
查看全部 -
为了让基本数据类型也具备对象的特性, Java 为每个基本数据类型都提供了一个包装类,这样我们就可以像操作对象那样来操作基本数据类型。
查看全部 -
用多个不同的catch块应对不同的异常,此处抛出的两异常为输入不匹配异常和算术异常
查看全部 -
捕获异常进行处理
查看全部 -
四种非检查异常
查看全部 -
package com.Game; import java.util.Objects; /** * 扑克 */ public class Card implements Comparable<Card> { public String number; public CardColor color; public Card(String number, CardColor color) { this.number = number; this.color = color; } public Card(int number, CardColor color) { this.number = setNum(number); this.color = color; } /** * 数值设置点数 * @param number * @return */ public String setNum(int number) { if (number == 11) { return "J"; } else if (number == 12) { return "Q"; } else if (number == 13) { return "K"; } else if (number == 14) { return "A"; } else { return String.valueOf(Math.min(10,Math.max(2,number))); } } /** * 获取权重,用于比较大小 * @return */ public int getNum() { if (number == "A") { return 14; } else if (number == "J") { return 11; } else if (number == "Q") { return 12; } else if (number == "K") { return 13; } else if (number == "小王") { return 15; } else if (number == "大王") { return 16; } else { return Integer.valueOf(number); } } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Card card = (Card) o; return Objects.equals(number, card.number) && Objects.equals(color, card.color); } @Override public int hashCode() { return Objects.hash(number, color); } @Override public int compareTo(Card o) { int number1 = this.getNum(); int number2 = o.getNum(); //先比较数字大小,数字大直接大 if (number1 != number2) { return number1 - number2; } return color.compareTo(o.color); } @Override public String toString() { if (getNum() == 15) { return "小王"; } else if (getNum() == 16) { return "大王"; } return color.color + this.number; } }
package com.Game; /** * 花色 */ public class CardColor implements Comparable<CardColor> { public String color; //用于比较大小 public int id; public CardColor(int id,String color) { this.color = color; } public static CardColor BLACK() { return new CardColor(0,"黑桃"); } public static CardColor RED() { return new CardColor(1,"红桃"); } public static CardColor PLUM() { return new CardColor(2,"梅花"); } public static CardColor BLOCK() { return new CardColor(3,"方块"); } @Override public int compareTo(CardColor o) { return this.id - o.id; } }
package com.Game; import com.com.rentCar.Car; import java.util.ArrayList; import java.util.List; /** * 玩家 */ public class Player implements Comparable<Player> { /** * 手牌 */ public List<Card> cards; /** * 玩家ID */ public String id; /** * 玩家姓名 */ public String name; public Player(String id,String name) { this.id = id; this.name = name; this.cards = new ArrayList<Card>(); } /** * 发牌 * @param card */ public void addCard(Card card) { this.cards.add(card); } /** * 清空 */ public void resetCard() { this.cards.clear(); } /** * 取出玩家手中的最大牌 * @return * @throws Exception */ public Card maxCard() throws Exception { if (cards.size() == 0) { throw new Exception("没有手牌"); } else if (cards.size() == 1) { return cards.get(0); } Card card = cards.get(0); for (int i = 1;i < cards.size(); i ++) { Card temp = cards.get(i); card = card.compareTo(temp) > 0 ? card : temp; } return card; } @Override public int compareTo(Player o) { try { return this.maxCard().compareTo(o.maxCard()); } catch (Exception e) { e.printStackTrace(); return 0; } } }
package com.Game; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PlayManager { //玩家 public List<Player> playerList; //创建的扑克牌 public List<Card> cardList; //洗牌后的扑克牌,每次洗牌清空,用于发牌 public List<Card> gameCards; /*用于输入*/ public Scanner scanner; /*构造方法*/ public PlayManager() { scanner = new Scanner(System.in); playerList = new ArrayList<Player>(); cardList = new ArrayList<Card>(); gameCards = new ArrayList<Card>(); } /** * 创建 */ private void createCards() { System.out.println("************开始创建扑克牌************"); CardColor[] colors = {CardColor.BLACK(),CardColor.RED(),CardColor.PLUM(),CardColor.BLOCK()}; for (CardColor color:colors) { for (int i = 2; i <= 14;i ++) { Card card = new Card(i,color); cardList.add(card); } } cardList.add(new Card("小王",null)); cardList.add(new Card("大王",null)); System.out.println("************扑克牌创建成功************"); System.out.println("总共多少张牌:" + cardList.size()); System.out.println(cardList.toString()); } /** * 洗牌 */ private void shuffleCards() { System.out.println("************开始洗牌************"); //有的话清空 if (!gameCards.isEmpty()) { gameCards.clear();; } while (cardList.size() > 0) { //随机产生一个下标 int index = (int)(Math.random() * cardList.size()); Card card = cardList.get(index); gameCards.add(card); cardList.remove(index); } System.out.println("************洗牌结束************"); // System.out.println("总共多少张牌:" + gameCards.size()); // System.out.println(gameCards.toString()); } /** * 创建玩家 */ private void createPlayer() { System.out.println("************创建玩家************"); //玩家 int playerNum = 0; while (true) { scanner = new Scanner(System.in); System.out.println("请输入创建几名玩家:"); try { playerNum = scanner.nextInt(); break; } catch (Exception e) { System.out.println("请输入整数类型!"); } } //最大10人,最小2人 playerNum = Math.min(10,Math.max(playerNum,2)); int playerID; String playerName; for (int i = 0; i < playerNum; i ++) { System.out.println("请输入第" + (i + 1) + "位玩家的ID和姓名"); //输入玩家ID while (true) { //这里需要重新创建,猜测可能是抛出异常,需要重置吧 scanner = new Scanner(System.in); System.out.println("输入ID:"); try { playerID = scanner.nextInt(); break; } catch (Exception e) { System.out.println("请输入整数类型的ID"); } } System.out.println("输入姓名:"); playerName = scanner.next(); //构造玩家 Player player = new Player(String.valueOf(playerID),playerName); playerList.add(player); } for (Player player:playerList) { System.out.println("***欢迎玩家:"+player.name); } } /** * 发牌 */ private void dealCard() { System.out.println("************开始发牌************"); //发牌前需要先把玩家手中的牌清空 for (Player player:playerList) { player.resetCard(); } //玩家 //发牌两轮,每人两张 int dealNum = 0; while (true) { scanner = new Scanner(System.in); System.out.println("请输入每人发几张牌:"); try { dealNum = scanner.nextInt(); break; } catch (Exception e) { System.out.println("请输入整数类型!"); } } //最小1张 dealNum = Math.max(dealNum,1); for (int i = 0; i < dealNum && gameCards.size() > 0; i ++) { for (int j = 0; j < playerList.size() && gameCards.size() > 0; j ++) { Card card = gameCards.get(0); Player player = playerList.get(j); System.out.println("**玩家:" + player.name + "-拿牌"); player.addCard(card); gameCards.remove(0); } } System.out.println("************发牌结束************"); } /** * 游戏开始 */ private void gameStart() { System.out.println("************开始游戏************"); if (playerList.size() == 0) { System.out.println("没有玩家"); return; } Player winPlayer = playerList.get(0); try { System.out.println("玩家:" + winPlayer.name + "最大的手牌为:" + winPlayer.maxCard().toString()); for (int i = 1; i < playerList.size(); i ++) { Player player = playerList.get(i); System.out.println("玩家:" + player.name + "最大的手牌为:" + player.maxCard().toString()); if (player.compareTo(winPlayer) > 0) { winPlayer = player; } } } catch (Exception e) { System.out.println("游戏出现错误:" + e.getMessage()); return; } System.out.println("************玩家:" + winPlayer.name + "获胜!************"); System.out.println("玩家各自手牌为:"); for (Player player: playerList) { System.out.println(player.name + player.cards.toString()); } } public static void main(String[] args) { PlayManager play = new PlayManager(); play.createCards();//创建牌 play.shuffleCards();//洗牌 play.createPlayer();//玩家 play.dealCard();//发牌 play.gameStart();//开始比大小 } }
查看全部 -
截图......
查看全部
举报
0/150
提交
取消