VV游戏

 找回密码
 立即注册
查看: 181|回复: 0

斗地主游戏代码

[复制链接]

2

主题

5

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2023-3-25 17:20:41 | 显示全部楼层 |阅读模式
由于斗地主游戏的复杂性,需要大量代码才能实现完整的游戏。以下是一个简化版的斗地主游戏代码。

```
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>

using namespace std;

enum class Suit { SPADES, HEARTS, CLUBS, DIAMONDS };
enum class Rank { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };

struct Card {
    Suit suit;
    Rank rank;
};

class Deck {
private:
    vector<Card> cards;
public:
    Deck() {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                cards.push_back(Card{ static_cast<Suit>(i), static_cast<Rank>(j) });
            }
        }
    }
    void shuffle() {
        random_device rd;
        mt19937 g(rd());
        std::shuffle(cards.begin(), cards.end(), g);
    }
    vector<Card> deal(int numCards) {
        vector<Card> hand;
        for (int i = 0; i < numCards; i++) {
            hand.push_back(cards.back());
            cards.pop_back();
        }
        return hand;
    }
};

class Player {
private:
    vector<Card> hand;
public:
    void addCards(vector<Card> newCards) {
        hand.insert(hand.end(), newCards.begin(), newCards.end());
    }
    vector<Card> getHand() const {
        return hand;
    }
    void playCards(vector<int> cardIndices) {
        vector<Card> playedCards;
        for (auto i : cardIndices) {
            playedCards.push_back(hand);
        }
        hand.erase(remove_if(hand.begin(), hand.end(), [&](Card c) {
            return find(playedCards.begin(), playedCards.end(), c) != playedCards.end();
        }), hand.end());
    }
};

class Game {
private:
    Deck deck;
    vector<Player> players;
    vector<Card> tableCards;
    int currentPlayer;
public:
    Game(int numPlayers) : deck(), players(numPlayers), tableCards(), currentPlayer(0) {
        deck.shuffle();
        // Deal cards to each player
        for (int i = 0; i < 17; i++) {
            for (int j = 0; j < numPlayers; j++) {
                players[j].addCards(deck.deal(1));
            }
        }
        // Deal the remaining 3 cards to the table
        tableCards = deck.deal(3);
    }
    void printGameState() const {
        cout << "Player " << currentPlayer << "'s turn\n";
        cout << "Table cards:\n";
        for (auto c : tableCards) {
            cout << static_cast<int>(c.rank) + 2 << " of ";
            switch (c.suit) {
                case Suit::SPADES:
                    cout << "spades";
                    break;
                case Suit::HEARTS:
                    cout << "hearts";
                    break;
                case Suit::CLUBS:
                    cout << "clubs";
                    break;
                case Suit::DIAMONDS:
                    cout << "diamonds";
                    break;
            }
            cout << "\n";
        }
        cout << "Your hand:\n";
        auto hand = players[currentPlayer].getHand();
        for (int i = 0; i < hand.size(); i++) {
            cout << i << ") " << static_cast<int>(hand.rank) + 2 << " of ";
            switch (hand.suit) {
                case Suit::SPADES:
                    cout << "spades";
                    break;
                case Suit::HEARTS:
                    cout << "hearts";
                    break;
                case Suit::CLUBS:
                    cout << "clubs";
                    break;
                case Suit::DIAMONDS:
                    cout << "diamonds";
                    break;
            }
            cout << "\n";
        }
    }
    bool isGameOver() const {
        for (auto p : players) {
            if (p.getHand().empty()) {
                return true;
            }
        }
        return false;
    }
    void takeTurn() {
        printGameState();
        while (true) {
            cout << "Enter the indices of the cards you want to play, separated by spaces: ";
            vector<int> cardIndices;
            int index = 0;
            while (cin >> index) {
                cardIndices.push_back(index);
            }
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            vector<Card> hand = players[currentPlayer].getHand();
            vector<Card> playedCards;
            for (auto i : cardIndices) {
                if (i >= hand.size()) {
                    cout << "Index out of range.\n";
                    break;
                }
                playedCards.push_back(hand);
            }
            if (!playedCards.empty()) {
                players[currentPlayer].playCards(cardIndices);
                tableCards.insert(tableCards.end(), playedCards.begin(), playedCards.end());
                break;
            } else {
                cout << "Invalid input. Try again.\n";
            }
        }
        currentPlayer = (currentPlayer + 1) % players.size();
    }
};

int main() {
    int numPlayers = 2;
    Game game(numPlayers);
    while (!game.isGameOver()) {
        game.takeTurn();
    }
    cout << "Game over!\n";
    return 0;
}
```
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|VV游戏

GMT+8, 2025-4-16 00:44 , Processed in 0.114530 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表