初めてのヘビーなポスターです!私はC + +とコーディングのような不正な練習するいくつかのランダムウォークアルゴリズムをコーディングしようとしたが、私の関数は永遠にループし、実際に何もしません。<の前に< < endl; cin.getとループ内のもう1つはエラーチェックのためのもので、エラーがhループのためにあるたびに、私のコアを100%クロックして何もしません。 ?誰かが私を助けてくださいすることができますループループは永遠にネストされています
#include <iostream>
#include <cstdlib>
#include <ctime>
#define MAP_WIDTH 64
#define MAP_HEIGHT 24
#define WALK_DIRECTION 4
#define TILE_FLOOR 0
#define TILE_WALL 1
using namespace std;
int MapArray[MAP_HEIGHT][MAP_WIDTH];
int StepCount = 0;
const int MaxStepCount = 60;
void SetWalls(void);
int GenerateRandom(int a);
void DrunkardsWalk(void);
int main(void){
SetWalls();
DrunkardsWalk();
for(int h = 0; h < MAP_HEIGHT; h++){
cout << endl;
for(int w = 0; w < MAP_WIDTH; w++){
switch(MapArray[h][w]){
case TILE_WALL:
cout << "#";
break;
case TILE_FLOOR:
cout << ".";
break;
}
}
}
return 0;
}
void SetWalls(void){
for(int h = 0; h < MAP_HEIGHT; h++){
cout << endl;
for(int w = 0; w < MAP_WIDTH; w++){
MapArray[h][w] = TILE_WALL;
}
}
}
int GenerateRandom(int a){
int b;
srand(time(NULL));
b = rand() % a;
return b;
}
void DrunkardsWalk(void){
int RandHeight = GenerateRandom(MAP_HEIGHT);
int RandWidth = GenerateRandom(MAP_WIDTH);
int RandDirection = GenerateRandom(WALK_DIRECTION);
cout << "Before while" << endl;
cin.get();
while(StepCount != MaxStepCount){
cout << "After while" << endl;
cin.get();
for(int h = 0; h < RandHeight; h++){
cout << "For h" << endl;
cin.get();
for(int w = 0; w < RandWidth; w++){
cout << "For w" << endl;
cin.get();
if(RandDirection == 1){
MapArray[h+1][w] = TILE_FLOOR;
StepCount++;
}
else if(RandDirection == 2){
MapArray[h-1][w] = TILE_FLOOR;
StepCount++;
}
else if(RandDirection == 3){
MapArray[h][w+1] = TILE_FLOOR;
StepCount++;
}
else if(RandDirection == 4){
MapArray[h][w-1] = TILE_FLOOR;
StepCount++;
}
}
}
}
}
乱数が必要なときはいつでも発電機を再投入することはうまくやっていません。それは個人がむしろ冷たく見えるようにするでしょう。 – Bathsheba
数学をチェックする必要があります。 'random_number%4'は' [1,4] 'ではなく[[0,3]' – NathanOliver
になります。デバッガを使ってコードをステップ実行する方法を学ぶことで、この種の間違いを見つけることができます。良いデバッガを使用すると、プログラムを1行ずつ実行し、どこからずれているかを確認することができます。これはプログラミングをする場合に不可欠なツールです。詳しい読書:** [小規模プログラムのデバッグ方法](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver