c ++:套牌没有显示第一张牌

c++ : Deck not showing first card

我实际上是想用 C++ 创建一个虚张声势的纸牌游戏。当我调用函数 showAllCards 时,甲板上的卡片(红心王牌)没有显示在输出中,显示它的唯一方法是在程序运行时在 main body.And 中添加两个甲板结构(没有顶牌), dev 也会生成一些警告。我究竟做错了什么? 也请你参考程序中的一些improvements/changes。 这是代码:

#include <iostream>
#include<Strings.h>
#include<cstdlib>

using namespace std;

struct link{
    int data;
    link *link;
};

struct deck
    {
        string suit[32];
        string value[32];
    }card;

struct cardsDeck{
    string nxt[20];
    string suitlist[4] = {"hearts","spades","clubs","diamonds"};
    string vals[8] = {"ace","two","three","four","five","jack","queen","king"};

}cdeck;

class Game
{
    private: 
    link *first;
    public:
        Game();
        void check();
        void mask();
        void pass();
        void enterCard();
        void showAllCards();
        void shuffle(); 
        void rearrangeCards();  
};

Game::Game(){
    first=NULL;
}

void Game::rearrangeCards(){
    short int x = 0, y = 0, z = 0;
    while(x < 32)
    {
        card.suit[x] = cdeck.suitlist[y];
        card.value[x] = cdeck.vals[z];
        ++x;
        ++z;
        if(x % 8 == 0)
        {
            ++y;    
        }
        if(z % 8 == 0)
        {
            z = 0;
        }
    }

}

void Game::showAllCards(){
    int x;
    while(x < 32)
    {

        if(card.suit[x]!=card.suit[x-1]){
            cout<<"\n";
        }
        cout << card.value[x] << " of " << card.suit[x] << "\n";
        ++x;
    }
}

int main(){
    Game game;
    int op;
    game.rearrangeCards();
    cout<<"Welcome to Bluff Mashter 20/12/15::6:46\nEnter the Operation please:\n\n";
    cin>>op;
    while(op!=0){
        switch(op){
            case 1:
                game.showAllCards();
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            case 8:
                break;

        }
    }

    return 1;   
}

这是警告

20  63  E:\Projjects\mainbluuf.cpp  [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
21  78  E:\Projjects\mainbluuf.cpp  [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
20  63  E:\Projjects\mainbluuf.cpp  [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
21  78  E:\Projjects\mainbluuf.cpp  [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11

假设你的方法是这样的:

void Game::showAllCards(){
    int x = 0;
    while(x < 32)
    {

        if(card.suit[x]!=card.suit[x-1]){
            cout<<"\n";
        }
        cout << card.value[x] << " of " << card.suit[x] << "\n";
        ++x;
    }
}

在您的第一次迭代中,您这样做:card.suit[0]!=card.suit[-1] 这是错误的。

void Game::showAllCards(){
    int x = 1;
    while(x < 32)
    {

        if(card.suit[x]!=card.suit[x-1]){
            cout<<"\n";
        }
        cout << card.value[x] << " of " << card.suit[x] << "\n";
        ++x;
    }
}

虽然我没有测试代码,但我想这可以解决问题。

注意:我将 x 设置为 1 这是唯一的变化。由于默认情况下你是 C++ 的新手 uninitialized 变量可以包含存储在该内存地址中的任何不可预测的值,所以好的做法是 always 初始化你的变量。

更新:

试试这个:

void Game::showAllCards(){
    for(x = 0; x < 32; ++x)
    {
        cout << card.value[x] << " of " << card.suit[x] << "\n";
    }
}

更新 2:

int main(){
    Game game;
    int op;
    game.rearrangeCards();
    cout<<"Welcome to Bluff Mashter 20/12/15::6:46\nEnter the Operation please:\n\n";

    cin >> op;
    while(op!=0){
        switch(op){
            case 1:
                game.showAllCards();
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                break;
            case 7:
                break;
            case 8:
                break;

        }

        cin >> op;
    }

    return 0;   
}

更新3:

void Game::showAllCards(){
    string lastSuit = "";

    for(x = 0; x < 32; ++x)
    {
        if(lastSuit != card.suit[x])
            cout << "\n";

        cout << card.value[x] << " of " << card.suit[x] << "\n";
        lastSuit = card.suit[x];
    }
}

经过 Boynux 的一些逻辑折磨和提示后,我解决了这两个问题。

for(int j=0;j<32;j++){
        cout<<card.value[j] << " of " << card.suit[j]<<"\n";
        if(j==7 || j==15 || j==23 || j==31)
        {
            cout<<"\n";
        }
    }