类图
演示效果:
源代码
package com.zzl.test.poker;
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;
}
@Override
public int compareTo(Card c) {
if (this.pointsScore == c.pointsScore) {
return c.colourScore - this.colourScore;
}
return c.pointsScore - this.pointsScore;
}
@Override
public String toString() {
return this.color + this.points;
}
}
package com.zzl.test.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);
System.out.println("-------------洗牌结束!--------------");
}
public Card get(int index) {
return this.cards.get(index);
}
public int size() {
return this.cards.size();
}
}
package com.zzl.test.poker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Player implements Comparable<Player> {
private int 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) {
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());
}
}
package com.zzl.test.poker;
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() {
Collections.sort(players);
return players.get(0);
}
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(".*");
}
}
}
}
public class Test {
public static void main(String[] args) {
new PokerGame().startGame();
}
}
打开App,阅读手记