import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class CardGame {

    public static void main(String[] args) {

        // 创建扑克牌
        ArrayList<Card> cards = new ArrayList<>();
        for (int i = 2; i <= 10; i++) {
            cards.add(new Card('黑桃', i));
            cards.add(new Card('红桃', i));
            cards.add(new Card('梅花', i));
            cards.add(new Card('方片', i));
        }
        cards.add(new Card('黑桃', 'J'));
        cards.add(new Card('红桃', 'J'));
        cards.add(new Card('梅花', 'J'));
        cards.add(new Card('方片', 'J'));
        cards.add(new Card('黑桃', 'Q'));
        cards.add(new Card('红桃', 'Q'));
        cards.add(new Card('梅花', 'Q'));
        cards.add(new Card('方片', 'Q'));
        cards.add(new Card('黑桃', 'K'));
        cards.add(new Card('红桃', 'K'));
        cards.add(new Card('梅花', 'K'));
        cards.add(new Card('方片', 'K'));
        cards.add(new Card('小王'));
        cards.add(new Card('大王'));

        // 随机洗牌
        Collections.shuffle(cards);

        // 创建玩家
        Scanner scanner = new Scanner(System.in);
        System.out.print('请输入玩家1的ID:');
        String id1 = scanner.nextLine();
        System.out.print('请输入玩家1的姓名:');
        String name1 = scanner.nextLine();
        Player player1 = new Player(id1, name1);
        System.out.print('请输入玩家2的ID:');
        String id2 = scanner.nextLine();
        System.out.print('请输入玩家2的姓名:');
        String name2 = scanner.nextLine();
        Player player2 = new Player(id2, name2);

        // 发牌
        for (int i = 0; i < 3; i++) {
            player1.addCard(cards.remove(0));
            player2.addCard(cards.remove(0));
        }

        // 比较手牌
        Card maxCard1 = player1.getMaxCard();
        Card maxCard2 = player2.getMaxCard();
        int compareResult = maxCard1.compareTo(maxCard2);
        if (compareResult > 0) {
            System.out.println(player1.getName() + '获胜!');
        } else if (compareResult < 0) {
            System.out.println(player2.getName() + '获胜!');
        } else {
            if (maxCard1.getSuitValue() > maxCard2.getSuitValue()) {
                System.out.println(player1.getName() + '获胜!');
            } else if (maxCard1.getSuitValue() < maxCard2.getSuitValue()) {
                System.out.println(player2.getName() + '获胜!');
            } else {
                System.out.println('平局!');
            }
        }

    }

}

class Card implements Comparable<Card> {

    private String suit;  // 花色
    private String rank;  // 点数
    private int suitValue;  // 花色大小,用于比较

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = String.valueOf(rank);
        switch (suit) {
            case '黑桃':
                this.suitValue = 4;
                break;
            case '红桃':
                this.suitValue = 3;
                break;
            case '梅花':
                this.suitValue = 2;
                break;
            case '方片':
                this.suitValue = 1;
                break;
        }
    }

    public Card(String rank) {
        this.rank = rank;
        if (rank.equals('小王')) {
            this.suit = '小王';
            this.suitValue = 5;
        } else {
            this.suit = '大王';
            this.suitValue = 6;
        }
    }

    public String getSuit() {
        return suit;
    }

    public String getRank() {
        return rank;
    }

    public int getSuitValue() {
        return suitValue;
    }

    @Override
    public int compareTo(Card o) {
        int rankValue1 = getRankValue(this.rank);
        int rankValue2 = getRankValue(o.rank);
        if (rankValue1 > rankValue2) {
            return 1;
        } else if (rankValue1 < rankValue2) {
            return -1;
        } else {
            return 0;
        }
    }

    private int getRankValue(String rank) {
        switch (rank) {
            case '2':
                return 2;
            case '3':
                return 3;
            case '4':
                return 4;
            case '5':
                return 5;
            case '6':
                return 6;
            case '7':
                return 7;
            case '8':
                return 8;
            case '9':
                return 9;
            case '10':
                return 10;
            case 'J':
                return 11;
            case 'Q':
                return 12;
            case 'K':
                return 13;
            case 'A':
                return 14;
            default:
                return 0;
        }
    }

    @Override
    public String toString() {
        return suit + rank;
    }

}

class Player {

    private String id;  // ID
    private String name;  // 姓名
    private ArrayList<Card> cards = new ArrayList<>();  // 手牌

    public Player(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void addCard(Card card) {
        cards.add(card);
    }

    public Card getMaxCard() {
        Card maxCard = cards.get(0);
        for (int i = 1; i < cards.size(); i++) {
            if (cards.get(i).compareTo(maxCard) > 0) {
                maxCard = cards.get(i);
            }
        }
        return maxCard;
    }

}

原文地址: https://www.cveoy.top/t/topic/oQWJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录