Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何显示此Sokoban板?_C++_Visual Studio - Fatal编程技术网

C++ 如何显示此Sokoban板?

C++ 如何显示此Sokoban板?,c++,visual-studio,C++,Visual Studio,我是新来的,我有一个基本的编程水平,问题是我已经试着展示这个Sokoban板一周了,我不知道该怎么做了。 因为我是新来的,我不太清楚这个论坛是如何工作的,我告诉你我认为重要的程序的一部分。 bool loadLevel(ifstream &file, tSokoban &sokoban, int level) { //Falta leer el nivel hasta que vuelva a leer Level tGame game; bool found =

我是新来的,我有一个基本的编程水平,问题是我已经试着展示这个Sokoban板一周了,我不知道该怎么做了。 因为我是新来的,我不太清楚这个论坛是如何工作的,我告诉你我认为重要的程序的一部分。
bool loadLevel(ifstream &file, tSokoban &sokoban, int level) { //Falta leer el nivel hasta que vuelva a leer Level
    tGame game;
    bool found = false;
    string line;
    int index, x = 0;
    game.sokoban.ncol = 0;
    game.sokoban.nrows = 0;
    file >> line;
    while (!file.eof())
    {
        if (line == "Level")
        {
            file >> index;
            if (index == level)
            {
                found = true;
            }
        }
        while (found && !file.eof())
        {
            if (found)
            {
                getline(file, line);//File reads "" , so I put it again so the program works
                getline(file, line);
                if (line.length() > game.sokoban.ncol)
                {
                    game.sokoban.ncol = line.length();
                }

                for (int i = 0; i < line.size(); i++)
                {
                    switch (line[i])
                    {
                    case '#':
                        sokoban.board[x][i] = Wall;
                        break;
                    case ' ':
                        sokoban.board[x][i] = Free;
                        break;
                    case '.':
                        sokoban.board[x][i] = GoalFree;
                        break;
                    case '*':
                        sokoban.board[x][i] = GoalBox;
                        break;
                    case '+':
                        sokoban.board[x][i] = GoalPlayer;
                        break;
                    case '@':
                        sokoban.board[x][i] = Player;
                        game.sokoban.playerX = x;
                        game.sokoban.playerY = i;
                        break;
                    case '$':
                        sokoban.board[x][i] = Box;
                        sokoban.boxCount++;
                        break;
                    }
                }
                x++;
                sokoban.nrows++;
                getline(file, line);
            }
            else
            {
                cout << "Could not find the level you were looking for..." << endl;
            }
        }
    }
    return found;
}


void draw(const tGame &game){
    system("cls");
    cout << "File: " << game.fileName << endl;
    cout << "Level " << game.curLevel << endl;
    cout << endl;
    cout << endl;
    for (int i = 0; i < game.sokoban.ncol; i++)
    {
        for (int f = 0; f < game.sokoban.nrows; f++)
        {
            drawTile(game.sokoban.board[i][f]);
        }
        cout << endl;
    }
    cout << game.numMoves;
}


void drawTile(tTile tile){
    switch (tile)
    {
    case Free:
        backgroundColor(1);
        cout << "  ";
        break;
    case Wall:
        backgroundColor(3);
        cout << "  ";
        break;
    case GoalFree:
        backgroundColor(7);
        cout << "..";
        break;
    case GoalBox:
        backgroundColor(8);
        cout << "**";
        break;
    case GoalPlayer:
        backgroundColor(11);
        cout << "++";
        break;
    case Player:
        backgroundColor(11);
        cout << "00";
        break;
    case Box:
        backgroundColor(13);
        cout << "()";
        break;
    }
}

问题是,当程序读取文件并显示其内容时,它就是不显示,我不知道问题出在哪里,所以我现在非常绝望。如果有人能帮我,我将非常感激:)。谢谢大家。

我建议大家阅读Eric Lippert的著作,学习使用调试器。像这样的问题通常是通过调试器来解决的。我还建议你阅读你从不设置game.sokoban。nrows@stark在loadLevel函数的begining中,我增加了它,在该函数的for循环之后:我建议您阅读Eric Lippert的文章,并学习使用调试器。像这样的问题通常是通过调试器来解决的。我还建议你阅读你从不设置game.sokoban。nrows@stark在loadLevel函数的beging处,我会增加它,在该函数的for循环之后:S
Level 0
###################
#####   ###########
#####$  ###########
#####  $###########
###  $ $ ##########
### # ## ##########
#   # ## #####  ..#
# $  $          ..#
##### ### #@##  ..#
#####     #########
###################