1

I'm new here, and I have a basic level of programming, and the problem is that I've been trying to display this Sokoban board for a week, and I don't know what to do anymore. As I'm new here and I don't know very well how this forum works, I show you the part of my program that I consider important.

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;
    }
}

My file just contains one level, which is in this format:

Level 0
###################
#####   ###########
#####$  ###########
#####  $###########
###  $ $ ##########
### # ## ##########
#   # ## #####  ..#
# $  $          ..#
##### ### #@##  ..#
#####     #########
###################

The problem is that when the program reads the file and has to display its contents, it just doesn't, and I don't know where the problem is, so I'm desperate now. If anyone could help me with this I would appreciate it very much :). Thank you all.

Dani B.
  • 11
  • 1
  • I recommend you read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert, and learn to use a debugger. Problems like this is usually solved with the help of a debugger. – Some programmer dude Sep 02 '17 at 11:00
  • I also recommend you read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude Sep 02 '17 at 11:01
  • You never set game.sokoban.nrows – stark Sep 02 '17 at 11:49
  • @stark I do, at the beggining of the loadLevel function, and I increase it after the for loop of that function :S – Dani B. Sep 02 '17 at 12:26

0 Answers0