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

c++ : Deck not showing first card

本文关键字:一张 显示 c++      更新时间:2023-10-16

我实际上正试图用c++创建一个虚张声势的纸牌游戏。当我调用函数showAllCards时,牌组(红心王牌)的牌不会在输出中显示,显示它的唯一方法是在主体中添加两个牌组结构。当程序运行时(没有顶级卡),dev也会生成一些警告。我做错了什么?你也可以参考一下程序中的一些改进/更改。这是代码:

#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:46nEnter the Operation please:nn";
    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:Projjectsmainbluuf.cpp  [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
21  78  E:Projjectsmainbluuf.cpp  [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
20  63  E:Projjectsmainbluuf.cpp  [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
21  78  E:Projjectsmainbluuf.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变量可以包含存储在该内存地址中的任何未预测值,因此最佳做法是始终初始化您的变量。

更新

试试这个:

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:46nEnter the Operation please:nn";
    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";
        }
    }