21xrx.com
2024-11-05 14:59:40 Tuesday
登录
文章检索 我的文章 写文章
C++扑克牌游戏程序设计代码
2023-07-06 02:38:02 深夜i     --     --
C++ 扑克牌 游戏 程序设计 代码

C++语言是非常常用的面向对象的编程语言之一,它可以应用于多种领域,包括游戏开发。扑克牌游戏是一种很受欢迎的休闲娱乐项目,因此,本文将介绍使用C++语言开发一个简单的扑克牌游戏程序。 

为了实现这个程序,我们需要使用C++随处可见的标准库——STL。其中的vector类和algorithm库都将被用到。在这个程序中,我们将使用一些基本的C++概念,如循环,条件语句,函数和类,以及一些STL中的基本数据结构,如vector和iterator。

下面是一个简单的C++扑克牌游戏程序设计代码示例:


#include <iostream>

#include <cstdlib>

#include <ctime>

#include <algorithm>

#include <vector>

using namespace std;

class Card {

public:

  enum Suit

    diamonds;

  enum Rank

    jack;

  Card(Rank r = ace, Suit s = spades, bool face_up = true): m_rank(r), m_suit(s), m_face_up(face_up) {}

  int get_value() const {

    int value = 0;

    if (m_face_up) {

      value = m_rank;

      if (value > 10)

        value = 10;

    }

    return value;

  }

  void flip() { m_face_up = !(m_face_up); }

  friend ostream& operator<<(ostream& os, const Card& a_card);

private:

  Suit m_suit;

  Rank m_rank;

  bool m_face_up;

};

ostream& operator<<(ostream& os, const Card& a_card)

{

  const string suits[] = "Spades";

  const string ranks[] = "";

  if (a_card.m_face_up)

    os << ranks[a_card.m_rank] << " of " << suits[a_card.m_suit];

  else

    os << "XX";

  return os;

}

class Hand {

public:

  Hand() { m_cards.reserve(7); }

  virtual ~Hand() { clear(); }

  void add(Card* pCard) { m_cards.push_back(pCard); }

  void clear() {

    vector<Card*>::iterator iter = m_cards.begin();

    for (iter = m_cards.begin(); iter != m_cards.end(); ++iter) {

      delete *iter;

      *iter = nullptr;

    }

    m_cards.clear();

  }

  int get_total() const {

    int total = 0;

    bool contains_ace = false;

    vector<Card*>::const_iterator iter;

    for (iter = m_cards.begin(); iter != m_cards.end(); ++iter) {

      total += (*iter)->get_value();

      if ((*iter)->get_value() == Card::ace)

        contains_ace = true;

    }

    if (contains_ace && total <= 11)

      total += 10;

    return total;

  }

protected:

  vector<Card*> m_cards;

};

class GenericPlayer : public Hand {

  friend ostream& operator<<(ostream& os, const GenericPlayer& a_generic);

public:

  GenericPlayer(const string& name = ""): m_name(name) {}

  virtual bool is_hitting() const = 0;

  bool is_busted() const { return (get_total() > 21); }

  void bust() const { cout << m_name << " busts.\n"; }

protected:

  string m_name;

};

ostream& operator<<(ostream& os, const GenericPlayer& a_generic)

{

  os << a_generic.m_name << ":\t";

  vector<Card*>::const_iterator iter;

  if (!a_generic.m_cards.empty()) {

    for (iter = a_generic.m_cards.begin(); iter != a_generic.m_cards.end(); ++iter) {

      os << *(*iter) << "\t";

    }

    if (a_generic.get_total() != 0)

      cout << "(" << a_generic.get_total() << ")";

  }

  else {

    os << "<empty>";

  }

  return os;

}

class Player : public GenericPlayer {

public:

  Player(const string& name = ""): GenericPlayer(name) {}

  virtual ~Player() {}

  virtual bool is_hitting() const {

    cout << m_name << ", do you want a hit? (Y/N): ";

    char response;

    cin >> response;

    return (response == 'y' || response == 'Y');

  }

  void win() const { cout << m_name << " wins.\n"; }

  void lose() const { cout << m_name << " loses.\n";}

  void push() const { cout << m_name << " pushes.\n";}

};

class House : public GenericPlayer {

public:

  House(const string& name = "House"): GenericPlayer(name) {}

  virtual ~House() {}

  virtual bool is_hitting() const { return (get_total() <= 16); }

  void flip_first_card() {

    if (!m_cards.empty())

      m_cards[0]->flip();

    else

      cout << "No card to flip!\n";

  }

};

class Deck : public Hand {

public:

  Deck() {

    m_cards.reserve(52);

    populate();

  }

  void shuffle() {

    random_shuffle(m_cards.begin(), m_cards.end());

  }

  void deal(Hand& a_hand) {

    if (!m_cards.empty()) {

      a_hand.add(m_cards.back());

      m_cards.pop_back();

    }

    else {

      cout << "Out of cards. Unable to deal.";

    }

  }

  void populate() {

    clear();

    for (int s = Card::clubs; s <= Card::spades; ++s) {

      for (int r = Card::ace; r <= Card::king; ++r) {

        add(new Card(static_cast<Card::Rank>(r), static_cast<Card::Suit>(s)));

      }

    }

  }

};

int main() {

  cout << "\t\tWelcome to Blackjack!\n\n";

  int num_players = 0;

  while (num_players < 1 || num_players > 7) {

    cout << "How many players? (1 - 7): ";

    cin >> num_players;

  }

  vector<Player> players;

  string name;

  for (int i = 0; i < num_players; ++i) {

    cout << "Enter player name: ";

    cin >> name;

    players.push_back(Player(name));

  }

  House house;

  Deck deck;

  deck.populate();

  deck.shuffle();

  for (int i = 0; i < 2; ++i) {

    for (auto& player : players) {

      deck.deal(player);

    }

    deck.deal(house);

  }

  house.flip_first_card();

  for (auto& player : players) {

    cout << player << endl;

  }

  cout << house << endl;

  for (auto& player : players) {

    while (player.is_hitting()) {

      deck.deal(player);

      cout << player << endl;

      if (player.is_busted())

        player.bust();

    }

  }

  house.flip_first_card();

  cout << endl << house << endl;

  while (house.is_hitting()) {

    deck.deal(house);

    cout << house << endl;

    if (house.is_busted())

      house.bust();

  }

  if (house.is_busted()) {

    for (auto& player : players) {

      if (!player.is_busted())

        player.win();

    }

  }

  else {

    for (auto& player : players) {

      if (!player.is_busted()) {

        if (player.get_total() > house.get_total())

          player.win();

        else if (player.get_total() < house.get_total())

          player.lose();

        else

          player.push();

      }

    }

  }

  for (auto& player : players) {

    player.clear();

  }

  house.clear();

  return 0;

}

在这个程序中,我们首先定义了三个类:Card、Hand和GenericPlayer。Card类代表扑克牌,它有两个枚举类型的成员:一个表示花色,一个表示点数。Hand类表示一组牌,它包含一个vector,可以添加和删除Card对象。GenericPlayer类是Hand类的子类,其构造函数接受一个字符串,表示玩家名字。GenericPlayer类有一个友元函数,用于输出一个玩家的牌和总点数。

接下来,我们定义了Player和House类,它们都是GenericPlayer类的子类,用于表示玩家和庄家。每一个类都有一个虚函数is_hitting(),用于确定一个玩家是否要再要一张牌。Player类还包含了win()、lose()和push()函数,用于报告玩家的胜利、失败和平局。House类包含了一个额外的函数flip_first_card(),用于翻开被暗置的第一张底牌。

在定义了这些类之后,我们开始实现main()函数。首先,我们询问用户要创建多少个玩家,并使用一个vector 对象存储这些玩家。然后,我们创建一个House对象和一个Deck对象,Deck对象就是一副未洗过的扑克牌。我们将Deck中的牌分发给每个玩家和庄家,然后将庄家的第一张牌暗置。接着,每个玩家依次操作,直到他们选择停牌或爆牌,然后是庄家的轮次,庄家根据其手牌是否小于等于16决定是否需要要牌,直到庄家结束自己的回合并报告胜负。

最后,在上述过程结束后,我们将所有玩家的牌和庄家的牌全部清理,并结束程序。

总的来说,这是一个非常简单,但是足够展示C++程序设计能力的扑克牌游戏程序,通过修改和扩展代码,你可以很轻松地创建你自己的扑克牌游戏,这个程序只是一个开始。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复