中级农民
- 积分
- 120
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-7-13
- 最后登录
- 1970-1-1
|
我大概写了一下,楼上看看是不是差不多这个意思?- public class Main {
- public static void main(String[] args) {
- Game game = new Game("Scott", "Sam");
- game.start();
- }
- }
- enum Suit {
- CLUB,
- DIAMOND,
- HEART,
- SPADE
- }
- class Game {
- private final int MAX_ROUND = 5;
- Deck deck;
- Player p1;
- Player p2;
- int p1Score;
- int p2Score;
- public Game(String player1, String player2) {
- deck = new Deck();
- p1 = new Player(player1);
- p2 = new Player(player2);
- p1Score = 0;
- p2Score = 0;
- }
- public void start() {
- for (int i = 0; i < MAX_ROUND; i++) {
- p1.draw(deck.getCardList());
- p2.draw(deck.getCardList());
- System.out.print(p1.showCard() + "--");
- System.out.println(p2.showCard());
- if (p1.show() > p2.show())
- p1Score++;
- else if (p1.show() < p2.show())
- p2Score++;
- }
- System.out.println(p1Score);
- System.out.println(p2Score);
- }
- }
- class Card {
- private int number;
- private Suit suit;
- public Card(int number, Suit suit) {
- this.number = number;
- this.suit = suit;
- }
- public int getNumber() { return number; }
- public String getSuit() { return suit.toString(); };
- }
- class Player {
- private final int DRAW_NUM = 5;
- PriorityQueue<Card> pq;
- String name;
- public Player(String name) {
- this.name = name;
- this.pq = new PriorityQueue<>((a,b) -> (b.getNumber() - a.getNumber()));
- }
- public void draw(List<Card> cardList) {
- this.pq = new PriorityQueue<>((a,b) -> (b.getNumber() - a.getNumber()));
- for (int i = 0; i < DRAW_NUM; i++) {
- if (!cardList.isEmpty()) {
- pq.offer(cardList.remove(0));
- } else {
- System.out.println("Cards run out!");
- break;
- }
- }
- }
- public String showCard() {
- if (!pq.isEmpty()) {
- return pq.peek().getSuit() + " " + pq.peek().getNumber();
- }
- return "";
- }
- public int show() {
- if (!pq.isEmpty()) {
- return pq.peek().getNumber();
- }
- return -1;
- }
- }
- class Deck {
- private final int MAX_CARD = 13;
- List<Card> cardList;
- public Deck() {
- cardList = new ArrayList<>();
- for (int i = 1; i <= MAX_CARD; i++) {
- cardList.add(new Card(i, Suit.CLUB));
- cardList.add(new Card(i, Suit.DIAMOND));
- cardList.add(new Card(i, Suit.HEART));
- cardList.add(new Card(i, Suit.SPADE));
- }
- //printDeck();
- //System.out.println("---------");
- Collections.shuffle(cardList);
- //printDeck();
- }
- public void shuffle() {
- Collections.shuffle(cardList);
- }
- private void printDeck() {
- for (int i = 0; i < 52; i++) {
- System.out.print(cardList.get(i).getSuit());
- System.out.println(cardList.get(i).getNumber());
- }
- }
- public List<Card> getCardList() { return cardList; }
- }
复制代码 |
|