功能未正确执行

时间:2015-07-23 10:26:39

标签: c++11

所以我正在尝试编写一个解决“Dungeon Crawl”问题的程序:http://www.cplusplus.com/forum/articles/12974/。如果你懒得阅读链接(这完全可以理解),游戏的基本前提是玩家在10x10网格上移动并试图在避开陷阱和移动敌人的同时到达宝藏。因为一切都是向玩家揭示的,所以它并不是真正的游戏,但我认为这是一个很好的编码练习。可悲的是,我在用户输入上生气,并且从未正确地学习如何将整个程序放在一起,这意味着我在正确执行函数方面存在严重问题。

主要功能是:

int main()
{
    int turn=0,enemyCount=0;
    bool end=false;
    srand (time(NULL));
    node* world;
    world = new node[100];
    generateWorld(world);
    drawWorld(world);
    do {
        playerMove(world);
        enemiesMoves(world,enemyCount);
        drawWorld(world);
        end = endCheck(world);
        turn++;
    } while (end == false);
   return 0;
}

我认为这很好,因为它遵循收集动作,更新世界,然后检查是否已达到结束条件的简单过程。问题是许多功能都没有像我期望的那样起作用,我无法弄清楚为什么它们不是。这包括用户输入验证,更新敌人的移动,绘制世界,以及检查游戏是否结束。我会按顺序进行。

这是playerMove()函数:

void playerMove(node* array){
    int i,playerLocation,oldLocation;
    bool valid=false;
    char move;
    for (i=0;i<100;i++){
        if (array[i].player == true){
            playerLocation = i;
            oldLocation = playerLocation;
        }
    }
    while(!valid){
            if (cin >> move){
                    if (move=='w' || move=='s' || move=='a' || move=='d')
                            valid = true;
            }
            if (move=='w'){
                    playerLocation = oldLocation-10;
                    if (playerLocation < 0){
                            cout << "You can't swim." << endl;
                            valid = false;
                    }
            }
            else if (move=='s'){
                    playerLocation = oldLocation+10;
                    if (playerLocation > 99){
                            cout << "You can't swim." << endl;
                            valid = false;
                    }
            }
            else if (move=='a'){
                    playerLocation = oldLocation-1;
                    if (playerLocation % 10 ==  9){
                            cout << "You can't swim." << endl;
                            valid = false;
                    }
            }
            else if (move=='d'){
                    playerLocation = oldLocation+1;
                    if (playerLocation % 10 == 0){
                           cout << "You can't swim." << endl;
                           valid = false;
                    }
            }
    }
    array[oldLocation].player = false;
    array[playerLocation].player = true;
}

它应该收集玩家输入,如果玩家输入不是WASD的键(他们只能在四个基本方向上移动),则不做任何事情,如果玩家试图离开世界边缘则拒绝输入,并更新玩家位置。它确实成功地执行了后者,但它不会捕获无效的输入,如果移动失败,它将拒绝显示它应该的消息。

这是enemiesMoves函数:

void enemiesMoves(node* array,int enemyCount){
    int i,j=0,n,enemyLocations[enemyCount],oldLocations[enemyCount];
    bool valid = false;
    for (i=0;j<enemyCount;i++){
        if (array[i].enemy==true){
            enemyLocations[j] = i;
            cout << enemyLocations[j] << endl;
            j++;
        }
    }
    for (j=0;j<enemyCount;j++){
        oldLocations[j] = enemyLocations[j];
    }
    for (j=0;j<enemyCount;j++){
        while (!valid){
            n = rand() % 4 + 1;
            if (n = 1){
                enemyLocations[j] = oldLocations[j]-10;
                if (enemyLocations[j] < 0)
                    valid = false;
                        }
                        else if (n = 2){
                enemyLocations[j] = oldLocations[j]+10;
                if (enemyLocations[j] > 100)
                    valid = false;
                        }
                        else if (n = 3){
                enemyLocations[j] = oldLocations[j]-1;
                if (enemyLocations[j] % 10 == 9)
                    valid = false;
                        }
                        else if (n = 1){
                enemyLocations[j] = oldLocations[j]+1;
                if (enemyLocations[j] % 10 == 0)
                    valid = false;
                        }
                }
                array[enemyLocations[j]].enemy = true;
                array[oldLocations[j]].enemy = false;
        }               
}

它应该以随机方向移动敌人,但如果敌人离开世界则拒绝移动。虽然我认为我已经正确地复制了playerMove()代码,但它拒绝更新敌人的位置;他们在转弯后转到同一个地方。

这是drawWorld()函数:

void drawWorld(node* array){
    int i;
    for (i=0;i<100;i++){
        if (array[i].player==true)
            cout << "P";
        else if (array[i].trap==true)
            cout << "T";
        else if (array[i].enemy==true)
            cout << "E";
        else if (array[i].treasure==true)
            cout << "X";
        else if (array[i].player==true && array [i].treasure==true)
            cout << "W";
        else if ((array[i].player==true && array[i].trap==true) || (array[i].player==true && array[i].enemy==true))
            cout << "L";
        else {
            cout << "O";
        }
        if ((i+1 % 10) == 0){
            cout << endl;
        }
    }
    cout << endl << endl;
}

它吸引世界的美好,除了它不会每十个字符包装文本,因为它明确告诉他们这样做。我无法理解我在这里失踪的东西。

最后,这是endCheck()函数:

bool endCheck(node* array){
    int i;
    for (i=0;i<100;i++){
        if (array[i].player==true && array[i].treasure==true){
            cout << "You found the treasure and now enjoy a life of unmitigated opulence." << endl;
            return true;
        }
        else if (array[i].player==true && array[i].trap==true){
            cout << "You fell into a conspicuous trap and became tiger food." << endl;
            return true;
        }
        else if (array[i].player==true && array[i].enemy==true){
            cout << "You were captured alive by angry natives and enjoyed as part of their New Year's feast." << endl;
            return true;
        }
        else


            return false;
    }
}

根本就没有执行。我可以将玩家移到陷阱上,但没有任何反应。

这些问题非常令人沮丧,因为我根本无法辨别出代码有什么问题。我知道帖子很长,但是如果有人能指出什么是错的,那就非常感激。还应该注意的是,我在发布之前搜索了答案,但是因为我认为问题是我的代码所固有的,所以我无法找到任何可用的答案。

2 个答案:

答案 0 :(得分:2)

您将在第一次迭代结束时返回。所以它只检查第一个字段然后返回false(如果第一个字段中没有玩家和宝藏/陷阱/敌人)。

将您的功能更改为:

bool endCheck(node* array){
 int i;
 for (i=0;i<100;i++){
    if (array[i].player==true && array[i].treasure==true){
        cout << "You found the treasure and now enjoy a life of unmitigated opulence." << endl;
        return true;
    }
    else if (array[i].player==true && array[i].trap==true){
        cout << "You fell into a conspicuous trap and became tiger food." << endl;
        return true;
    }
    else if (array[i].player==true && array[i].enemy==true){
        cout << "You were captured alive by angry natives and enjoyed as part of their New Year's feast." << endl;
        return true;
    }
 }
 return false;
}

在返回false之前,您必须检查所有可能的字段,表示没有冲突。

答案 1 :(得分:2)

enemiesMoves(),您正在检查if(n = 1),我确定您的意思是if(n == 1)

这样做:

....
n = rand() % 4 + 1;
if (n == 1){
.....

此外,n == 1案例被检查两次!进入if然后进入最后else if

要使rand()函数在每次运行时实际生成随机数,请先使用srand()初始化随机种子,如下所示:

/* initialize random seed: */
  srand (time(NULL));

n = rand() % 4 + 1;
相关问题