概述
Hi!大家好!我是你们的编程小王子,今天我们制作一个简单的21点小游戏,叫做“Blackjack”。
21点我相信很多小伙伴们都听说过甚至玩过,所以规则我就不多讲了
Card 类
成员 | 描述 |
---|---|
rank m_Rank | 牌的大小(A、2、3等)。rank是表示所有13个等级的枚举类型 |
suit m_Suit | 牌的花色。suit是表示4种可能花色的枚举类型 |
bool m_IsFaceUp | 指示扑克牌是否正面朝上。它影响牌的显示以及它的点数 |
int GetValue() | 返回牌的点值 |
void Flip() | 翻牌。调用函数后,正面朝上的牌将朝下,朝下的牌将朝上 |
Hand 类
成员 | 描述 |
---|---|
vector<Card*>m_Cards | 扑克牌的集合。它储存 Card 对象的指针 |
void Add(Card* pCare) | 向所持牌中添加一张牌。在向量 m_Cards 中添加一个Card指针 |
void Clear() | 清空所有持有的牌。移除 m_Cards 中的所有指针,删除堆中的相关的 Card 对象 |
int GetTotal() const | 返回所持牌的点数总和表 |
GenericPlayer 类(抽象类)
成员 | 描述 |
---|---|
string m_Name | 玩家的姓名 |
virtual bool IsHitting() const = 0 | 指示玩家是否跟牌,是纯虚函数 |
bool IsBusted() const | 指示玩家是否失败 |
void Bust() const | 宣布玩家失败 |
Player 类
成员 | 描述 |
---|---|
virtual bool IsHitting() const | 指示人类玩家是否跟牌 |
void Win() const | 宣布人类玩家获胜 |
void Lose() const | 宣布人类玩家失败 |
void Push() const | 宣布人类玩家与庄家打平 |
House 类
Deck 类
Game 类
最后一部分是拟出每轮游戏的基本流程,下面所列为Game类中Play()函数的伪代码
Deal players ans the house two initial cards
Hide the house's first card
Display player's ans house's hands
Deal additional cards to players
Reveal house's first card
Deal additionalcards to house
If house is busted
Everyone who is not busted wins
Otherwise
For each player
If player isn't busted
If player's total is greater than the house's total
Player wins
Otherwise if player's total is less than house's total
Player loses
Otherwise
Player pushes
Remove everyone's cards
OK!整体代码思路介绍完了,上代码!
Card 类
#include<bits/stdc++.h>
#include<windows.h>
#include<unistd.h>
#include<vector>
using namespace std;
class Card {
public:
enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING
};
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};
friend ostream& operator<<(ostream& os, const Card& aCard);
Card(rank r = ACE, suit s = SPADES, bool ifu = true);
int GetValue() const;
void Flip();
private:
rank m_Rank;
suit m_Suit;
bool m_IsFaceUp;
};
Card::Card(rank r, suit s, bool ifu) : m_Rank(r), m_Suit(s), m_IsFaceUp(ifu) {
}
int Card::GetValue() const {
int value = 0;
if(m_IsFaceUp) {
value = m_Rank;
if(value > 10) {
value = 10;
}
}
return value;
}
void Card::Flip() {
m_IsFaceUp = !(m_IsFaceUp);
}
Hand 类
class Hand {
public:
Hand();
virtual ~Hand();
void Add(Card* pCadr);
void Clear();
int GetTotal() const;
protected:
vector<Card*> m_Cards;
};
Hand::Hand() {
m_Cards.reserve(1000000);
}
Hand::~Hand() {
Clear();
}
void Hand::Add(Card* pCard) {
m_Cards.push_back(pCard);
}
void Hand::Clear() {
vector<Card*> ::iterator iter = m_Cards.begin();
for(iter = m_Cards.begin(); iter != m_Cards.end(); iter++) {
delete *iter;
*iter=0;
}
m_Cards.clear();
}
int Hand::GetTotal() const {
if(m_Cards.empty()) {
return 0;
}
if(m_Cards[0]->GetValue() == 0) {
return 0;
}
int total = 0;
vector<Card*>::const_iterator iter;
for(iter = m_Cards.begin(); iter != m_Cards.end(); iter++) {
total += (*iter)->GetValue();
}
bool constainsAce = false;
for(iter = m_Cards.begin(); iter != m_Cards.end(); iter++) {
if((*iter)->GetValue() == Card::ACE) {
constainsAce = true;
}
}
if(constainsAce && total <=11) {
total+=10;
}
return total;
}
GeniricPlayer 类
class GenericPlayer : public Hand {
friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);
public:
GenericPlayer(const string& name = "");
virtual ~GenericPlayer();
virtual bool IsHitting() const = 0;
bool IsBusted() const;
void Bust() const;
protected:
string m_Name;
};
GenericPlayer::GenericPlayer(const string& name):
m_Name(name) {
}
GenericPlayer::~GenericPlayer() {
}
bool GenericPlayer::IsBusted() const {
return (GetTotal() > 21);
}
void GenericPlayer::Bust() const {
cout<<m_Name<<" busts.n";
}
Player 类
class Player : public GenericPlayer {
public:
Player(const string& name = "");
virtual ~Player();
virtual bool IsHitting() const;
void Win() const;
void Lose() const;
void Push() const;
};
Player::Player(const string& name):
GenericPlayer(name) {
}
Player::~Player() {
}
bool Player::IsHitting() const {
cout<<m_Name<<", do you want a hit? (Y/N): ";
char response;
cin>>response;
return (response =='y' || response == 'Y');
}
void Player::Win() const {
cout<<m_Name<<" wins.n";
}
void Player::Lose() const {
cout<<m_Name<<" loses.n";
}
void Player::Push() const {
cout<<m_Name<<" pushes.n";
}
House 类
class House : public GenericPlayer {
public:
House(const string& name = "House");
virtual ~House();
virtual bool IsHitting() const;
void FlipFirstCard();
};
House::House(const string& name):
GenericPlayer(name) {
}
House::~House() {
}
bool House::IsHitting() const {
return (GetTotal() <= 16);
}
void House::FlipFirstCard() {
if(!(m_Cards.empty())) {
m_Cards[0]->Flip();
} else {
cout<<"No card to flip!n";
exit(1);
}
}
Deck 类
class Deck : public Hand {
public:
Deck();
virtual ~Deck();
void Populate();
void Shuffle();
void Deal(Hand& aHand);
void AdditionalCards(GenericPlayer& aGenericPlayer);
};
Deck::Deck() {
m_Cards.reserve(100000);
Populate();
}
Deck::~Deck() {
}
void Deck::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)));
}
}
}
void Deck::Shuffle() {
random_shuffle(m_Cards.begin(), m_Cards.end());
}
void Deck::Deal(Hand& aHand) {
if(!m_Cards.empty()) {
aHand.Add(m_Cards.back());
m_Cards.pop_back();
} else {
m_Cards.reserve(1000000);
if(m_Cards.empty()) {
cout<<"Out of cards. Unable to deal.";
exit(1);
}
}
}
void Deck::AdditionalCards(GenericPlayer& aGenericPlayer) {
cout<<endl;
while(!(aGenericPlayer.IsBusted()) && aGenericPlayer.IsHitting()) {
Deal(aGenericPlayer);
cout<<aGenericPlayer<<endl;
if(aGenericPlayer.IsBusted()) {
aGenericPlayer.Bust();
}
}
}
Game 类
class Game {
public:
Game(const vector<string>& names);
~Game();
void Play();
private:
Deck m_Deck;
House m_House;
vector<Player> m_Players;
};
Game::Game(const vector<string>& names) {
vector<string>::const_iterator pName;
for(pName = names.begin(); pName != names.end(); ++pName) {
m_Players.push_back(Player(*pName));
}
srand(static_cast<unsigned int>(time(0)));
m_Deck.Populate();
m_Deck.Shuffle();
}
Game::~Game() {
}
void Game::Play() {
vector<Player>::iterator pPlayer;
for(int i = 0; i < 2; ++i) {
for(pPlayer=m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
m_Deck.Deal(*pPlayer);
}
m_Deck.Deal(m_House);
}
m_House.FlipFirstCard();
for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
m_Deck.AdditionalCards(*pPlayer);
}
m_House.FlipFirstCard();
cout<<endl<<m_House;
m_Deck.AdditionalCards(m_House);
if(m_House.IsBusted()) {
for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
if( !(pPlayer->IsBusted()) ) {
pPlayer->Win();
}
}
} else {
for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
if( !(pPlayer->IsBusted()) ) {
if(pPlayer->GetTotal() > m_House.GetTotal() ) {
pPlayer->Win();
} else if(pPlayer->GetTotal() < m_House.GetTotal() ) {
pPlayer->Lose();
} else {
pPlayer->Push();
}
}
}
}
for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
pPlayer->Clear();
}
m_House.Clear();
}
main()函数
ostream& operator<<(ostream& os, const Card& aCard);
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);
int main() {
system("color 3e");
cout<<"ttWelcome to BLACKJACK!nn";
int numPlayers = 0;
while(numPlayers < 1 || numPlayers > 7) {
cout<<"How many players? ( 1 - 7 ): ";
cin>>numPlayers;
}
vector<string> names;
string name;
for(int i=0; i<numPlayers; ++i) {
cout<<"Enter player name: ";
cin>>name;
names.push_back(name);
}
cout<<endl;
Game aGame(names);
char again = 'y';
while(again != 'n' && again != 'N') {
aGame.Play();
cout<<"nDo you want to play again? ( Y / N ): ";
cin>>again;
}
return 0;
}
重载operator<<()函数
ostream& operator<<(ostream& os, const Card& aCard) {
const string RANKS[] = {"0","A","2","3","4","5","6","7","8","9",
"10","J","Q","K"
};
const string SUITS[] = {"c","d","h","s"};
if(aCard.m_IsFaceUp) {
os<<RANKS[aCard.m_Rank]<<SUITS[aCard.m_Suit];
} else {
os<<"XXX";
}
return os;
}
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer) {
os<<aGenericPlayer.m_Name<<":t";
vector<Card*>::const_iterator pCard;
if(!aGenericPlayer.m_Cards.empty()) {
for(pCard = aGenericPlayer.m_Cards.begin();
pCard != aGenericPlayer.m_Cards.end();
++pCard) {
os<< *(*pCard) <<"t";
}
if(aGenericPlayer.GetTotal() != 0) {
cout<<"("<<aGenericPlayer.GetTotal()<<")";
}
} else {
os<<"<empty>";
}
return os;
}
最后是完整代码
#include<bits/stdc++.h>
#include<windows.h>
#include<unistd.h>
#include<vector>
using namespace std;
class Card {
public:
enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING
};
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};
friend ostream& operator<<(ostream& os, const Card& aCard);
Card(rank r = ACE, suit s = SPADES, bool ifu = true);
int GetValue() const;
void Flip();
private:
rank m_Rank;
suit m_Suit;
bool m_IsFaceUp;
};
Card::Card(rank r, suit s, bool ifu) : m_Rank(r), m_Suit(s), m_IsFaceUp(ifu) {
}
int Card::GetValue() const {
int value = 0;
if(m_IsFaceUp) {
value = m_Rank;
if(value > 10) {
value = 10;
}
}
return value;
}
void Card::Flip() {
m_IsFaceUp = !(m_IsFaceUp);
}
class Hand {
public:
Hand();
virtual ~Hand();
void Add(Card* pCadr);
void Clear();
int GetTotal() const;
protected:
vector<Card*> m_Cards;
};
Hand::Hand() {
m_Cards.reserve(1000000);
}
Hand::~Hand() {
Clear();
}
void Hand::Add(Card* pCard) {
m_Cards.push_back(pCard);
}
void Hand::Clear() {
vector<Card*> ::iterator iter = m_Cards.begin();
for(iter = m_Cards.begin(); iter != m_Cards.end(); iter++) {
delete *iter;
*iter=0;
}
m_Cards.clear();
}
int Hand::GetTotal() const {
if(m_Cards.empty()) {
return 0;
}
if(m_Cards[0]->GetValue() == 0) {
return 0;
}
int total = 0;
vector<Card*>::const_iterator iter;
for(iter = m_Cards.begin(); iter != m_Cards.end(); iter++) {
total += (*iter)->GetValue();
}
bool constainsAce = false;
for(iter = m_Cards.begin(); iter != m_Cards.end(); iter++) {
if((*iter)->GetValue() == Card::ACE) {
constainsAce = true;
}
}
if(constainsAce && total <=11) {
total+=10;
}
return total;
}
class GenericPlayer : public Hand {
friend ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);
public:
GenericPlayer(const string& name = "");
virtual ~GenericPlayer();
virtual bool IsHitting() const = 0;
bool IsBusted() const;
void Bust() const;
protected:
string m_Name;
};
GenericPlayer::GenericPlayer(const string& name):
m_Name(name) {
}
GenericPlayer::~GenericPlayer() {
}
bool GenericPlayer::IsBusted() const {
return (GetTotal() > 21);
}
void GenericPlayer::Bust() const {
cout<<m_Name<<" busts.n";
}
class Player : public GenericPlayer {
public:
Player(const string& name = "");
virtual ~Player();
virtual bool IsHitting() const;
void Win() const;
void Lose() const;
void Push() const;
};
Player::Player(const string& name):
GenericPlayer(name) {
}
Player::~Player() {
}
bool Player::IsHitting() const {
cout<<m_Name<<", do you want a hit? (Y/N): ";
char response;
cin>>response;
return (response =='y' || response == 'Y');
}
void Player::Win() const {
cout<<m_Name<<" wins.n";
}
void Player::Lose() const {
cout<<m_Name<<" loses.n";
}
void Player::Push() const {
cout<<m_Name<<" pushes.n";
}
class House : public GenericPlayer {
public:
House(const string& name = "House");
virtual ~House();
virtual bool IsHitting() const;
void FlipFirstCard();
};
House::House(const string& name):
GenericPlayer(name) {
}
House::~House() {
}
bool House::IsHitting() const {
return (GetTotal() <= 16);
}
void House::FlipFirstCard() {
if(!(m_Cards.empty())) {
m_Cards[0]->Flip();
} else {
cout<<"No card to flip!n";
exit(1);
}
}
class Deck : public Hand {
public:
Deck();
virtual ~Deck();
void Populate();
void Shuffle();
void Deal(Hand& aHand);
void AdditionalCards(GenericPlayer& aGenericPlayer);
};
Deck::Deck() {
m_Cards.reserve(100000);
Populate();
}
Deck::~Deck() {
}
void Deck::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)));
}
}
}
void Deck::Shuffle() {
random_shuffle(m_Cards.begin(), m_Cards.end());
}
void Deck::Deal(Hand& aHand) {
if(!m_Cards.empty()) {
aHand.Add(m_Cards.back());
m_Cards.pop_back();
} else {
m_Cards.reserve(1000000);
if(m_Cards.empty()) {
cout<<"Out of cards. Unable to deal.";
exit(1);
}
}
}
void Deck::AdditionalCards(GenericPlayer& aGenericPlayer) {
cout<<endl;
while(!(aGenericPlayer.IsBusted()) && aGenericPlayer.IsHitting()) {
Deal(aGenericPlayer);
cout<<aGenericPlayer<<endl;
if(aGenericPlayer.IsBusted()) {
aGenericPlayer.Bust();
}
}
}
class Game {
public:
Game(const vector<string>& names);
~Game();
void Play();
private:
Deck m_Deck;
House m_House;
vector<Player> m_Players;
};
Game::Game(const vector<string>& names) {
vector<string>::const_iterator pName;
for(pName = names.begin(); pName != names.end(); ++pName) {
m_Players.push_back(Player(*pName));
}
srand(static_cast<unsigned int>(time(0)));
m_Deck.Populate();
m_Deck.Shuffle();
}
Game::~Game() {
}
void Game::Play() {
vector<Player>::iterator pPlayer;
for(int i = 0; i < 2; ++i) {
for(pPlayer=m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
m_Deck.Deal(*pPlayer);
}
m_Deck.Deal(m_House);
}
m_House.FlipFirstCard();
for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
m_Deck.AdditionalCards(*pPlayer);
}
m_House.FlipFirstCard();
cout<<endl<<m_House;
m_Deck.AdditionalCards(m_House);
if(m_House.IsBusted()) {
for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
if( !(pPlayer->IsBusted()) ) {
pPlayer->Win();
}
}
} else {
for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
if( !(pPlayer->IsBusted()) ) {
if(pPlayer->GetTotal() > m_House.GetTotal() ) {
pPlayer->Win();
} else if(pPlayer->GetTotal() < m_House.GetTotal() ) {
pPlayer->Lose();
} else {
pPlayer->Push();
}
}
}
}
for(pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer) {
pPlayer->Clear();
}
m_House.Clear();
}
ostream& operator<<(ostream& os, const Card& aCard);
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);
int main() {
system("color 3e");
cout<<"ttWelcome to BLACKJACK!nn";
int numPlayers = 0;
while(numPlayers < 1 || numPlayers > 7) {
cout<<"How many players? ( 1 - 7 ): ";
cin>>numPlayers;
}
vector<string> names;
string name;
for(int i=0; i<numPlayers; ++i) {
cout<<"Enter player name: ";
cin>>name;
names.push_back(name);
}
cout<<endl;
Game aGame(names);
char again = 'y';
while(again != 'n' && again != 'N') {
aGame.Play();
cout<<"nDo you want to play again? ( Y / N ): ";
cin>>again;
}
return 0;
}
ostream& operator<<(ostream& os, const Card& aCard) {
const string RANKS[] = {"0","A","2","3","4","5","6","7","8","9",
"10","J","Q","K"
};
const string SUITS[] = {"c","d","h","s"};
if(aCard.m_IsFaceUp) {
os<<RANKS[aCard.m_Rank]<<SUITS[aCard.m_Suit];
} else {
os<<"XXX";
}
return os;
}
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer) {
os<<aGenericPlayer.m_Name<<":t";
vector<Card*>::const_iterator pCard;
if(!aGenericPlayer.m_Cards.empty()) {
for(pCard = aGenericPlayer.m_Cards.begin();
pCard != aGenericPlayer.m_Cards.end();
++pCard) {
os<< *(*pCard) <<"t";
}
if(aGenericPlayer.GetTotal() != 0) {
cout<<"("<<aGenericPlayer.GetTotal()<<")";
}
} else {
os<<"<empty>";
}
return os;
}
OK!今天的游戏创作结束啦!如果觉得还不错的话,记得点赞+关注哦!ByeBye!
最后
以上就是调皮项链为你收集整理的Dev-C++5.11游戏创作之21点的全部内容,希望文章能够帮你解决Dev-C++5.11游戏创作之21点所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复