画一张牌,然后叫它
我正在制作一个二十一点游戏。我已经建立了一个牌组,我只需要现在执行游戏。画一张牌,然后叫它
所以我有一个名为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(){ 
} 
我意识到有可能是这个问题,因为当用户决定击中,我们可能不知道牌中有哪张牌。相信我认为抽签功能是错误的。
问题是我无法弄清楚如何从甲板上正确地绘制卡片(递减顶部卡片)。那么我该如何调整用户核心呢?我有一个返回double的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; 
} 
我不确定玩家只能抽两张牌。所以我建议改变你的游戏环境。
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他/她已绘制的卡片。对于数组,这可能很麻烦。但是如果你已经了解了矢量,那就去做吧。
以上是 画一张牌,然后叫它 的全部内容, 来源链接: utcz.com/qa/257999.html





