画一张卡,并打电话给它

Drawing a card, and calling it

本文关键字:打电话 一张      更新时间:2023-10-16

我正在创建二十一点游戏。我已经设置了一个卡片甲板,我只需要立即实施游戏。

所以我有一个称为 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中为每个玩家/经销商保留他/她所吸引的卡片。有了数组,这是可能的,但很麻烦。但是,如果您已经了解了向量,那就去吧。