示例
源码
Poker.java
package com.immoc.poker;
public class Poker implements Comparable<Poker> {
private int no;
private String name;
public Poker(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public int compareTo(Poker o) {
return Integer.compare(no, o.no);
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Poker{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}
PokerGenerator.java
package com.immoc.poker;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class PokerGenerator {
public static final String[] SUITS = {"方片", "梅花", "红桃", "黑桃"};
public static final String[] KINDS = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
public static List<Poker> ascOrder() {
List<Poker> pokers = new LinkedList<>();
for (int i = 0; i < SUITS.length; i++) {
for (int j = 0; j < KINDS.length; j++) {
pokers.add(new Poker(i + j * SUITS.length, SUITS[i] + KINDS[j]));
}
}
return pokers;
}
public static List<Poker> randomOrder() {
List<Poker> pokers = ascOrder();
Collections.shuffle(pokers);
return pokers;
}
}
Player.java
package com.immoc.poker;
import java.util.ArrayList;
import java.util.List;
public class Player {
private int id;
private String name;
private List<Poker> pokers = new ArrayList<>();
public Player(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Poker> getPokers() {
return pokers;
}
@Override
public String toString() {
return "Player{" +
"id=" + id +
", name='" + name + '\'' +
", pokers=" + pokers +
'}';
}
}
Main.java
package com.immoc.poker;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("开始洗牌……");
List<Poker> pokers = PokerGenerator.randomOrder();
int index = 0;
System.out.println("洗牌结束!");
System.out.println("请输入第1位玩家的ID和姓名");
Player player1 = new Player(getPlayerNo(input), getPlayerName(input));
System.out.println("请输入第2位玩家的ID和姓名");
Player player2 = new Player(getPlayerNo(input), getPlayerName(input));
System.out.println("开始发牌……");
for (int i = 0; i < 2; i++) {
player1.getPokers().add(pokers.get(index++));
player2.getPokers().add(pokers.get(index++));
}
System.out.println("发牌结束!");
System.out.println("开始游戏……");
Collections.sort(player1.getPokers());
Poker player1MaxPoker = player1.getPokers().get(player1.getPokers().size() - 1);
System.out.printf("玩家【%s】最大的手牌为:%s%n", player1.getName(), player1MaxPoker.getName());
Collections.sort(player2.getPokers());
Poker player2MaxPoker = player2.getPokers().get(player2.getPokers().size() - 1);
System.out.printf("玩家【%s】最大的手牌为:%s%n", player2.getName(), player2MaxPoker.getName());
Player winner = player1MaxPoker.getNo() > player2MaxPoker.getNo() ? player1 : player2;
System.out.printf("游戏结束!玩家【%s】获胜!%n%n", winner.getName());
System.out.println("手牌公布……");
System.out.printf("玩家【%s】:%s%n", player1.getName(), player1.getPokers());
System.out.printf("玩家【%s】:%s%n", player2.getName(), player2.getPokers());
System.out.println("牌堆公布……");
System.out.println(pokers);
input.close();
}
private static int getPlayerNo(Scanner input) {
int playerNo;
while (true) {
System.out.print("ID:");
try {
playerNo = input.nextInt();
break;
} catch (InputMismatchException e) {
System.out.println("请输入整数类型的ID!");
}
}
return playerNo;
}
private static String getPlayerName(Scanner input) {
System.out.print("姓名:");
return input.next();
}
}
点击查看更多内容
10人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦