PathFinding算法不起作用

时间:2017-03-08 20:12:33

标签: c++ path coordinates path-finding

我有一个学校项目,我们必须在C ++上创建一个游戏。我们被分成几组,我不得不为它创建寻路。该程序编译(虽然它很慢),我的算法不起作用。它并不总能找到最短的路径。 游戏如下:你有一个迷宫,一个玩家和三个敌人。任务是让敌人找到他们通往玩家的路径,他们只能移动一个"角色"每次玩家移动。

#include "MapGeneration.cpp"
#include <iostream>
#include <string>
#include <stdlib.h>
#include "EnemyClass.cpp" //use the enemy stuff to get the entity stuff
                          //in EntityClass see if the xPos and yPos are changing                            

using namespace std;

/* Variables */
int row; // rows
int col; // columns
const int enemnumb = 3;
const int playnumb = 1;
int playcount = 1;       // Counting how much players are left
int enemcount = 3;       // Counting how much enemies are left
int enemcordr[enemnumb]; // Enemy row number
int enemcordc[enemnumb]; // Enemy column number
int playcordr[playnumb]; // Player row number
int playcordc[playnumb]; // Player column number


/* Find enemies */
int findAllEnem(){
    bool found = false;
    while(!found){                                // If all enemies are found while loop stops
        for(row=0;row<MapRowSize;row++){          // Searches in rows
            for(col=0;col<MapColumnSize;col++){   // Searches in columns
                if(arrCurrentMap[row][col]=='e'){ // If it finds the letter 'e'
                    enemcordr[enemcount]=row;     // Save the row number
                    enemcordc[enemcount]=col;     // Save the column number
                    enemcount--;                  // Substracts 1 from the enemy count in order to show how much are left
                }
                if(enemcount==0){                 // If there are no left enemies
                    found=true;                   // All enemies will be found and will stop the while loop
                }
            }
        }
    }
}


/* Find players */
int findPlayer(){
    bool found = false;
    while(!found){                                // If player is found
        for(row=0;row<MapRowSize;row++){          // Searches in rows
            for(col=0;col<MapColumnSize;col++){   // Searches in columns
                if(arrCurrentMap[row][col]=='p'){ // If it finds the letter 'e'
                    playcordr[playcount]=row;     // Save the row number
                    playcordc[playcount]=col;     // Save the column number
                    playcount--;                  // Substracts 1 from the enemy count in order to show how much are left
                }
                if(playcount==0){                 // If there are no left enemies
                    found=true;                   // All enemies will be found and will stop the while loop
                }
            }
        }
    }
}

int enemMovement() {
    srand(time(NULL));
    bool makeMove = false; // to check if the enemy has already made a move
    bool legit = false;
    const int possible = 1500;
    int possmov = 1;
    int min[possible]; // minimum distance
    int countMove[possible][enemnumb]; // counts how many moves have been made with road
    int moveR[possible][enemnumb]; // gets virtual row without moving the enemy
    int moveC[possible][enemnumb]; // gets virtual column without moving the enemy
    int found[possible][enemnumb]; // if enemy found the player
    for (playcount = playnumb; playcount > 0; playcount--) { // for every player
      for (enemcount = enemnumb; enemcount > 0; enemcount--) { // the three enemies

        for (possmov = 0; possmov < possible; possmov++) { // possible directions
          moveR[possmov][enemcount] = enemcordr[enemcount];
          moveC[possmov][enemcount] = enemcordc[enemcount];
          while (legit != true) {
            int previous;
            int nRand = rand()%(4-0)+1; //4 possible directions
            if (nRand == 1) { //move right
              if (arrCurrentMap[moveR[possmov][enemcount]][moveC[possmov][enemcount] + 1] != '#') {
                  if (arrCurrentMap[moveR[possmov][enemcount]][moveC[possmov][enemcount] + 1] == 'p') {
                    found[possmov][enemcount]=countMove[possmov][enemcount];
                    legit=true;
                  }
                  moveC[possmov][enemcount]++;
                  countMove[possmov][enemcount]++;

                }
              }
              if (nRand == 2) // move  left
              {
                if (arrCurrentMap[moveR[possmov][enemcount]][moveC[possmov][enemcount] - 1] != '#') {
                  if (arrCurrentMap[moveR[possmov][enemcount]][moveC[possmov][enemcount] - 1] == 'p') {
                    found[possmov][enemcount]=countMove[possmov][enemcount];
                    legit=true;
                  }
                  moveC[possmov][enemcount]--;
                  countMove[possmov][enemcount]++;

                }
              }
              if (nRand == 3) // move up
              {
                if (arrCurrentMap[moveR[possmov][enemcount] - 1][moveC[possmov][enemcount]] != '#') {
                  if (arrCurrentMap[moveR[possmov][enemcount] - 1][moveC[possmov][enemcount]]== 'p') {
                    found[possmov][enemcount]=countMove[possmov][enemcount];
                    legit=true;
                  }
                  moveR[possmov][enemcount]--;
                  countMove[possmov][enemcount]++;
                }
              }
              if (nRand == 4) // move down
              {
                if (arrCurrentMap[moveR[possmov][enemcount] + 1][moveC[possmov][enemcount]] != '#') {
                  if (arrCurrentMap[moveR[possmov][enemcount] + 1][moveC[possmov][enemcount]]== 'p') {
                    found[possmov][enemcount]=countMove[possmov][enemcount];
                    legit=true;
                  }
                  moveR[possmov][enemcount]++;
                  countMove[possmov][enemcount]++;
                }
              }
            }
            legit=false; // returns the value of legit to false for next move road
          }
          min[enemcount]=possible;
          for(int i=0;i<=possible;i++){ // finds the shortest path
             if(found[i][enemcount]<min[enemcount] && found[i][enemcount] != 0){
                 min[enemcount]=found[i][enemcount];   
             }
          }
        }
      }
}


/* Main function */
int main (){

    GenerateLoadingMap(1); // Generate map
    findPlayer();          // Finds players
    findAllEnem();         // Find enemies
    enemMovement();        // enemy movement
    return 0;
}

这是我写的代码。有时它会找到最短的路径,但并非总是如此。另外,我想了解如何在每次移动后存储坐标。

0 个答案:

没有答案