演示效果:
Card
/** * 扑克牌类 */ public class Card implements Comparable<Card> { private int pointsScore; //手牌的点数 private int colourScore; //花色的点数 private String points; //牌的名称 private String color; //牌的花色 public Card(int pointsScore, int colourScore, String points, String color) { this.pointsScore = pointsScore; this.colourScore = colourScore; this.points = points; this.color = color; } //重写Comparable类中compareTo()方法 @Override public int compareTo(Card c) { //如果点数一样,则比较花色 if (this.pointsScore == c.pointsScore) { //这样定是降序排序,反着写就是升序排序 return c.colourScore - this.colourScore; } //比较花色,降序排序,也就是说容器中最大的下标为0 return c.pointsScore - this.pointsScore; } @Override public String toString() { return this.color + this.points; } }
Poker
import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * 整副扑克牌 */ public class Poker { private List<Card> cards = new ArrayList<>(); public Poker() { createPoker(); randomPoker(); } /** * 创建扑克牌 */ private void createPoker() { System.out.println("-------------创建扑克牌...--------------"); String[] names = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; //定义一个字符串数组 String[] colors = {"方片", "梅花", "红桃", "黑桃"}; for (int i = 0; i < names.length; i++) { for (int j = 0; j < colors.length; j++) { cards.add(new Card(i, j, names[i], colors[j])); } } System.out.println("-------------扑克牌创建完毕!--------------"); System.out.println(cards); //输出 } /** * 洗牌 */ public void randomPoker() { System.out.println("-------------开始洗牌--------------"); Collections.shuffle(cards); //调用Collections类中shuffle()方法就好了 System.out.println("-------------洗牌结束!--------------"); } /** * 根据索引获取扑克牌 * @param index * @return */ public Card get(int index) { return this.cards.get(index); } /** * 获取扑克牌的数量 * @return */ public int size() { return this.cards.size(); } }
Player
import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * 玩家类 */ public class Player implements Comparable<Player> { private int id; //玩家ID private String name; //玩家姓名 private Card maxCard; // 玩家手中最大的牌 private List<Card> cards = new ArrayList<>(); //存放玩家手中的牌的容器 public Player(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public String getName() { return name; } public List<Card> getCards() { return cards; } //获取玩家手中最大的牌的方法 public Card getMaxCard() { if (this.maxCard == null) { //先排序,由于重写了Poker类中compareTo()方法,下标为0的为最大值 Collections.sort(cards); this.maxCard = cards.get(0); } return this.maxCard; } @Override public int compareTo(Player o) { // 按照玩家最大的牌进行排序,获取玩家排在最前面 return this.getMaxCard().compareTo(o.getMaxCard()); } public void add(Card card) { System.out.println("--玩家[" + this.getName() + "]拿牌:" + card); this.cards.add(card); } /** * 输出玩家最大的手牌 */ public void showMaxCard() { System.out.println("玩家[" + this.getName() + "]最大的手牌为:" + this.getMaxCard()); } /** * 输出玩家各自手牌 */ public void showCards() { System.out.println(this.getName() + ":" + this.getCards()); } }
PokerGame
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.function.Function; /** * 扑克游戏 */ public class PokerGame { private List<Player> players = new ArrayList<>(); //用来存放玩家 private Poker poker = new Poker(); // 整副扑克牌 private Scanner scanner = new Scanner(System.in); // 接收用户在控制台输入的信息 /** * 启动方法 */ public void startGame() { creatPlayer(); //创建玩家 dealPoker(); // 发牌 comparePoker(); //统计 } /** * 创建玩家 */ public void creatPlayer() { System.out.println("-------------创建玩家...--------------"); int playerNum = getInput("请输入玩家数量:", playerCount -> { if (playerCount < 2) { System.out.println("玩家数量最少有2位!请重新输入!"); return false; } if (playerCount > poker.size()) { System.out.println("输入的玩家数量不能大于扑克牌的数量(" + poker.size() + ")!请重新输入!"); return false; } return true; }); for (int i = 1; i <= playerNum; i++) { //这所以写个循环,是为了方便修改玩家的人数,只需改一下循环的判断条件就好了 System.out.print("请输入第" + i + "位玩家的姓名:"); players.add(new Player(i, scanner.next())); } // 输出各位玩家信息 for (Player player : players) { System.out.println("----欢迎玩家:" + player.getName()); } } /** * 发牌 */ public void dealPoker() { System.out.println("-------------开始发牌...--------------"); int cardNum = getInput("请设置给每个玩家发多少张牌:", cardCount -> { if (cardCount <= 0) { System.out.println("发牌数不能必须大于0,请重新输入!"); return false; } if (cardCount * players.size() > poker.size()) { System.out.println("非法的发牌数,总发牌数量不能超过扑克牌的数量(" + poker.size() + ")!请重新输入!"); return false; } return true; }); int limit = players.size() * cardNum; for (int i = 0; i < limit; i++) { players.get(i % players.size()).add(poker.get(i)); } System.out.println("-------------发牌结束...--------------"); } /** * 统计 */ public void comparePoker() { System.out.println("-------------开始统计...--------------"); players.forEach(Player::showMaxCard); Player winner = getWinner(); System.out.println("-------------玩家[" + winner.getName() + "]获胜!--------------"); System.out.println("玩家各自的手牌为:"); players.forEach(Player::showCards); } private Player getWinner() { // 对所有玩家进行排序,排序规则参考Player.compareTo()方法 Collections.sort(players); // 得到获胜玩家 return players.get(0); } /** * 从控制台接受用户输入的一个正整数 * * @param hint 提示信息 * @param function 用户判断用户输入的信息是否合法,如果不合法,需要重新输入 * @return */ private int getInput(String hint, Function<Integer, Boolean> function) { while (true) { System.out.print(hint); try { int input = scanner.nextInt(); if (!function.apply(input)) { continue; } return input; } catch (Exception e) { System.out.println("输入不合法!请重新输入!"); // 如果出现异常,则跳过本次输入的信息 scanner = scanner.skip(".*"); } } } }
Test
/** * 测试类 */ public class Test { public static void main(String[] args) { new PokerGame().startGame(); } }
前辈,写得好精简啊!真厉害,感谢分享,我写得好烂啊,不知道你之前是怎么提升的呢?