我是新来的,我有一个基本的编程水平,问题是我一直试图将这个推箱子板展示一周,我不知道是什么再做一次。 由于我是新来的,而且我不太清楚这个论坛是如何运作的,所以我向您展示了我认为重要的计划部分。
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;
}
}
我的文件只包含一个级别,格式为:
Level 0
###################
##### ###########
#####$ ###########
##### $###########
### $ $ ##########
### # ## ##########
# # ## ##### ..#
# $ $ ..#
##### ### #@## ..#
##### #########
###################
问题在于,当程序读取文件并且必须显示其内容时,它只是没有,我不知道问题出在哪里,所以我现在绝望了。如果有人可以帮助我,我会非常感激:)。谢谢大家。