画一张卡,并打电话给它
Drawing a card, and calling it
我正在创建二十一点游戏。我已经设置了一个卡片甲板,我只需要立即实施游戏。
所以我有一个称为 deck.cpp
的文件,其中包含甲板数组,以及一个存储该值的卡文件,而不是。在deck.cpp
文件中,我有以下功能可以绘制卡:
void Deck::draw(int v){
cout << deck[v].toString();
}
然后,在我实际玩游戏的另一个文件中,我打电话给甲板类,然后洗牌,也可以正常工作。
#include "Deck.hpp"
#include "PlayingCard.hpp"
#include <string>
using namespace std;
class Blackjack{
private:
//Must contain a private member variable of type Deck to use for the game
Deck a;
int playerScore;
int dealerScore;
bool playersTurn();
bool dealersTurn();
public:
//Must contain a public default constructor that shuffles the deck and initializes the player and dealer scores to 0
Blackjack();
void play();
};
现在,我很难弄清楚如何绘制两张卡片将它们打印出来并获得它们的总和:
#include "Blackjack.hpp"
#include "Deck.hpp"
#include <iostream>
#include <iomanip>
using namespace std;
//Defaults the program to run
Blackjack::Blackjack(){
a.shuffle();
playerScore = 0;
dealerScore = 0;
}
void Blackjack::play(){
}
我意识到这可能会有问题,因为当用户决定击中时,我们可能不知道哪张卡在甲板上。相信我认为抽奖功能是错误的。
问题是我不知道如何从甲板上正确绘制卡片(并减少顶部卡)。然后,我还如何调整用户。我有一个getValue()
功能,该功能返回双重功能。
甲板中所需的更改
在现实世界中,甲板知道剩下多少张牌,下一张牌是什么。在您的班上应该相同:
class Deck{
private:
PlayingCard deck[52];
int next_card; //<=== just one approach
public:
...
};
当您在现实世界中绘制卡片时,您的手中有卡片。因此,图返回某物:
class Deck{
...
public:
Deck();
void shuffle();
void printDeck();
PlayingCard draw(); // <=== return the card
};
然后该功能看起来像:
PlayingCard Deck::draw(){
int v=next_card++;
cout << deck[v].toString();
return deck[v];
}
在此实现中,为简单起见,deck
不会更改。当构造甲板时,next_card
应初始化为0。在任何时候,next_card
下方的元素已经绘制了,并且甲板上剩余的元素是从next_card
到51开始的元素。尽管甲板上没有卡片,但卡还是一张卡。
如何继续游戏
图纸更容易,因为现在,游戏可以知道绘制的卡。这使您可以更新PlayCard
值的分数,基础。而且您不再需要跟踪顶级卡:
Blackjack::Blackjack(){
// Deck a; <====== this should be a protected member of Blackjack class
a.shuffle();
playerScore = 0;
dealerScore = 0;
}
我不确定玩家只能画2张卡。因此,我建议更改您的游戏玩法以进行循环。
void Blackjack::play(){
bool player_want_draw=true, dealer_want_draw=true;
while (player_want_draw || dealer_want_draw) {
if (player_want_draw) {
cout<<"Player draws ";
PlayCard p = a.draw();
cout<<endl;
// then update the score and ask user if he wants to draw more
}
if (dealer_want_draw) {
cout<<"Dealer draws ";
PlayCard p = a.draw();
cout<<endl;
// then update the score and decide if dealer should continue drawing
}
}
// here you should know who has won
}
您可以通过更新每个回合的分数和一些标志来实现二十一点游戏,而不必记住每个玩家绘制的卡的价值。但是,如果您愿意,可以像在现实世界中一样实现它,通过在Blackjack
中为每个玩家/经销商保留他/她所吸引的卡片。有了数组,这是可能的,但很麻烦。但是,如果您已经了解了向量,那就去吧。
- 如何从另一张地图构造一张地图
- C++显示图像并将其保持到下一张图像的最佳方法
- OpenGL Ping Pong可与一张通行证一起工作,而不是两个通行证
- 画一张卡,并打电话给它
- 洗牌一张牌
- 检查一张图片是否与另一张图片偏移
- 在 Windows 中自动将 xlsx 文件转换为多个 (MS-DOS) CSV 文件(每张一张一个)
- 把一个价格(例如89.95美元)读成一张双人床
- 如何将地图的反面复制到另一张地图上
- CUDA,我需要一张新卡吗?1.0、1.1、2.0、2.1的差异
- 如何找到不在另一张地图的键中的地图键
- 有没有一张多维地图
- 用c++中的printf制作一张表格
- 用简单的opengl图像库(SOIL)加载一张图像(颜色空间:GRAY)
- 找一张智能地图
- 使用openCv的嘴巴检测只在一张脸上返回很多圆圈
- 每隔一秒捕捉一张图像
- 一张未经设计的地图会是一个不错的选择吗
- 从地图中获取详细信息并将其保存到另一张地图中
- 在OpenCV中将两张撕破的纸合并成一张图像