Poker Game Programming Exam Questions

topic:

Four people, A, B, C, and D, play a deck of poker cards together. Each card is scored according to a number. Cards with the same number in different suits have the same score. Among them, the playing cards of various suits from 1 to 10 are respectively 1-10, J, Q, and K are recorded as 11, 12, and 13 respectively, and the big king and small king are recorded as 20 respectively. The four suits (diamonds, clubs, hearts, and spades) are recorded as A, B, C, and D respectively. For example, the K of Hearts is represented as 13C, with a score of 13; the 5 of Diamonds is represented as 5A, with a score of 5. The king is represented as 20K, and the score is 20. The small king is represented as 20Q, and the score is 20. 54 cards are stacked together and placed upside down on the table.
The game process is as follows:
1) Shuffling: 54 cards are combined in random order.
2) Sorting of 4 people: Randomly generate the card-taking order of 4 people, and the order of playing cards is the same as the order of taking cards. The first person to take the card is the first person to play the card.
3) After the 4 people are sorted, two teams are formed. The first and third people form a team according to the order, and the second and fourth people form a pair of 4)
Card sharing: Each person takes turns to take the cards in the order of taking the cards. The player takes 13 cards and leaves the last two cards on the table.
5) Information: Everyone does not know other people’s cards, nor the cards left on the table. The information available includes the cards they hold and the cards played by 4 people. Members of the team can communicate strategies for playing cards in each round.
6) Playing cards: Starting from the first person to get the cards (you can choose the strategy with the greatest chance of winning), 4 people play the cards in each round, and then compare the cards, and the person with the highest cards gets the 4 cards of the current round. Card. Cards that have been played cannot be played again. The size determination rule is: card score (large) > card score (small). For the same card score, the size is determined in the order of spades > hearts > clubs > diamonds. For example, 9 of spades > 9 of hearts. When comparing cards with a king or a king, the rule for size determination is king > king > spades 13 > hearts 13 > clubs 13 > diamonds 13 > spades 12...". Example: Round 1 A plays 3C, B plays 9D, C plays 6C, D plays 13B, then D wins 4 cards
(3C, 9D, 6C, 13B).
7) Determine the outcome: The total score of the cards in each person's hand is each person's score. The score of each team is the sum of the scores of two people. The team with the highest score wins. If the two teams score the same, it is a draw.

Programming requirements:

1. Use object-oriented methods for programming, and you can freely choose the programming language according to your own preferences.
2. Code implementation should take scalability into consideration. The code can be easily extended and supported when the card value and size comparison rules change.
3. Output results: the result of each person’s card play, each person’s score, and the winning player

The output example is as follows:

Play cards:

The order of taking/playing cards: A, C, D and B. Team 1: A, D, Team 2: C, B A
3C, 6B, 8B, 11D, 7C, 7D, 2C, 8D, 5B, 12B, 12C, 4B, 10C C 6C, 1B,
4D , 2A, 10B, 13A, 2D, 5A, 11B, 20Q, 11A, 4C, 8C, 13B, 4A, 5C, 1A, 6A, 10A, 11C, 7A, 12A, 7B, 3B,
13D, 3D, 9D,
10D ,3A,9C,8A,9A,6D,20K,12D,1D,9B,2B,13C

Score:

A 78
C 110
D 75
B 135


The following is implemented through Java language, the specific code is as follows:

import java.util.*;

//扑克牌类
class Card {
    private String suit;
    private int rank;

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    public String getSuit() {
        return suit;
    }

    public int getRank() {
        return rank;
    }

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

//玩家类
class Player {
    private String name;
    private List<Card> hand;
    private int score;

    public Player(String name) {
        this.name = name;
        this.hand = new ArrayList<>();
        this.score = 0;
    }

    public String getName() {
        return name;
    }

    public List<Card> getHand() {
        return hand;
    }

    public int getScore() {
        return score;
    }

    public void addToHand(Card card) {
        hand.add(card);
    }

    public void removeFromHand(Card card) {
        hand.remove(card);
    }

    public void increaseScore(int points) {
        score += points;
    }
}

//卡牌比较器
class CardComparator implements Comparator<Card> {
    private Map<String, Integer> suitRank = new HashMap<>();

    public CardComparator() {
        suitRank.put("K", 6); // 大王
        suitRank.put("Q", 5); // 小王
        suitRank.put("D", 4); // 黑桃
        suitRank.put("C", 3); // 红桃
        suitRank.put("B", 2); // 梅花
        suitRank.put("A", 1); // 方片
    }

    @Override
    public int compare(Card card1, Card card2) {
        if (card1.getRank() != card2.getRank()) {
            return card2.getRank() - card1.getRank();
        }
        return suitRank.get(card2.getSuit()) - suitRank.get(card1.getSuit());
    }
}


//扑克游戏类
public class PokerGame {
    private List<Card> deck;
    private List<Player> players;
    private CardComparator cardComparator;

    public PokerGame() {
        initializeDeck(); // 初始化牌组
        initializePlayers();// 初始化玩家
        initializePlayerHand(); //给玩家发牌

        cardComparator = new CardComparator();
    }


    private void initializeDeck() {
        //花色:A:方片、B:梅花、C:红桃、D:黑桃
        List<String> suits = Arrays.asList("A", "B", "C", "D");

        //牌面:1-13
        List<Integer> ranks = new ArrayList<>();
        for (int i = 1; i <= 13; i++) {
            ranks.add(i);
        }

        //准备一副扑克牌
        deck = new ArrayList<>();
        for (String suit : suits) {
            for (int rank : ranks) {
                deck.add(new Card(suit, rank));
            }
        }

        deck.add(new Card("K", 20)); // 大王
        deck.add(new Card("Q", 20)); // 小王

        //打散扑克,并去除两张底牌
        Collections.shuffle(deck);
        deck.remove(deck.size() - 1);
        deck.remove(deck.size() - 1);
    }

    private void initializePlayers() {
        players = new ArrayList<>();
        players.add(new Player("甲"));
        players.add(new Player("乙"));
        players.add(new Player("丙"));
        players.add(new Player("丁"));

        //打散玩家顺序
        Collections.shuffle(players);
    }

    private void initializePlayerHand() {
        for (int i = 0; i < deck.size(); i++) {
            players.get(i % 4).addToHand(deck.get(i));
        }
    }


    private void playRound(int round) {
        System.out.println("第 " + round + " 轮开始:");

        //存储当前轮次每个玩家出的牌
        Map<Player, Card> roundCards = new HashMap<>();

        //自定义的出牌策略:同组成员中出最大的牌,同组另外成员出最小的牌
        //players(0)和players(2)是一组,players(1)和players(3)是一组
        compareGroupCards(players.get(0), players.get(2), roundCards);
        compareGroupCards(players.get(1), players.get(3), roundCards);

        //确定本轮胜者
        Player roundWinnerPlayer = determineRoundWinner(roundCards);
        System.out.println("本轮胜者: " + roundWinnerPlayer.getName());

        //更新玩家分数并从各个玩家手牌中移除本轮出的牌
        updateScoresAndRemoveCards(roundWinnerPlayer, roundCards);
    }


    private void compareGroupCards(Player player1, Player player2, Map<Player, Card> roundCards) {
        //对palyer1和player2的手牌进行排序
        player1.getHand().sort(new CardComparator());
        player2.getHand().sort(new CardComparator());

        //获取palyer1和player2的手牌中最大和最小的一张牌
        Card maxCard1 = player1.getHand().get(0);
        Card maxCard2 = player2.getHand().get(0);
        Card minCard1 = player1.getHand().get(player1.getHand().size() - 1);
        Card minCard2 = player2.getHand().get(player2.getHand().size() - 1);

        //比较palyer1和player2的手牌最大的牌,决定如何出牌
        int compare = cardComparator.compare(maxCard1, maxCard2);
        if (compare < 0) {
            //palyer1的手牌最大,palyer1出最大的牌,palyer2出最小的牌
            roundCards.put(player1, maxCard1);
            roundCards.put(player2, minCard2);
        } else {
            //palyer2的手牌最大,palyer2出最大的牌,palyer1出最小的牌
            roundCards.put(player1, minCard1);
            roundCards.put(player2, maxCard2);
        }
    }

    private Player determineRoundWinner(Map<Player, Card> roundCards) {
        Player roundWinnerPlayer = null;
        Card roundMaxCard = null;

        for (Map.Entry<Player, Card> playerCardEntry : roundCards.entrySet()) {
            Card currentCard = playerCardEntry.getValue();
            //第一次循环,roundMaxCard为null,直接赋值,后续循环比较大小,找出最大的牌和对应的玩家
            if (roundWinnerPlayer == null || cardComparator.compare(currentCard, roundMaxCard) < 0) {
                roundWinnerPlayer = playerCardEntry.getKey();
                roundMaxCard = currentCard;
            }
        }

        return roundWinnerPlayer;
    }

    private void updateScoresAndRemoveCards(Player roundWinnerPlayer, Map<Player, Card> roundCards) {
        for (Map.Entry<Player, Card> playerCardEntry : roundCards.entrySet()) {
            Player currentPlayer = playerCardEntry.getKey();
            Card currentCard = playerCardEntry.getValue();
            int points = currentCard.getRank();
            //如果当前玩家是本轮胜者,则累加分数
            roundWinnerPlayer.increaseScore(points);
            //从玩家手牌中移除本轮出的牌
            currentPlayer.removeFromHand(currentCard);
        }
    }

    private void determineWinner() {
        Player team1Player1 = players.get(0);
        Player team1Player2 = players.get(2);
        Player team2Player1 = players.get(1);
        Player team2Player2 = players.get(3);

        int team1Score = team1Player1.getScore() + team1Player2.getScore();
        int team2Score = team2Player1.getScore() + team2Player2.getScore();

        System.out.println("游戏结束,各玩家得分:");
        for (Player player : players) {
            System.out.println(player.getName() + ": " + player.getScore());
        }

        if (team1Score > team2Score) {
            System.out.println(team1Player1.getName() + "+" + team1Player2.getName() + " 组获胜");
        } else if (team1Score < team2Score) {
            System.out.println(team2Player1.getName() + "+" + team2Player2.getName() + " 组获胜");
        } else {
            System.out.println("平局");
        }
    }

    public static void main(String[] args) {
        PokerGame game = new PokerGame();
        //打印初始牌组
        System.out.println("初始牌组:");
        for (Player player : game.players) {
            System.out.println(player.getName() + ": " + player.getHand());
        }

        //比赛开始,共13轮比赛
        for (int round = 0; round < 13; round++) {
            game.playRound(round + 1);
        }

        //比赛结束,确定胜者
        game.determineWinner();
    }
}

The above code makes full use of the principles of object-oriented programming and divides different functions into different classes, making the code structure clearer and easier to understand and maintain. The running results are as follows (the results are different each time you run the code):

第 1 轮开始:
本轮胜者: 丁
第 2 轮开始:
本轮胜者: 丁
第 3 轮开始:
本轮胜者: 丙
第 4 轮开始:
本轮胜者: 丙
第 5 轮开始:
本轮胜者: 甲
第 6 轮开始:
本轮胜者: 甲
第 7 轮开始:
本轮胜者: 甲
第 8 轮开始:
本轮胜者: 乙
第 9 轮开始:
本轮胜者: 甲
第 10 轮开始:
本轮胜者: 丙
第 11 轮开始:
本轮胜者: 丙
第 12 轮开始:
本轮胜者: 丁
第 13 轮开始:
本轮胜者: 丁
游戏结束,各玩家得分:
丙: 110
乙: 27
丁: 130
甲: 125
丙+丁 组获胜

Guess you like

Origin blog.csdn.net/qq_32020645/article/details/132588731