홈>
최근에 C ++에서 텍스트 기반 RPG를 만들었고 이제 간단한 경로 찾기 알고리즘을 통합하려고합니다. 알고리즘 자체를 테스트하여 작동하지만 RPG에 통합하려고 시도 할 때 함수의 while 루프 조건이 무시되기 시작했습니다. 내 코드는 다음과 같습니다.
int Strategy::path(char (&map)[25][100], int current_y, int current_x, int Target_x, int Target_y)
{
int i;
int n;
bool up=false;
bool down=false;
bool left=false;
bool right=false;
while (map[Target_y-1][Target_x]!='_'&&map[Target_y-1][Target_x]!='-'&&map[Target_y-1][Target_x]!='='&&map[Target_y+1][Target_x]!='_'&&map[Target_y+1][Target_x]!='-'&&map[Target_y+1][Target_x]!='='&&map[Target_y][Target_x-1]!='_'&&map[Target_y][Target_x-1]!='-'&&map[Target_y][Target_x-1]!='='&&map[Target_y][Target_x+1]!='_'&&map[Target_y][Target_x+1]!='-'&&map[Target_y][Target_x+1]!='=')
{
bool f=false;
int m=0;
system("cls");
printf("Time left: %i\n", time);
int x;
int y;
using namespace std;
for (y = 0; y<25; y++)
{
for (x = 0; x<100; x++)
{
cout << map[y][x];
}
printf("\n");
}
if (current_y > 0 && map[current_y - 1][current_x] ==' ')
{
map[current_y - 1][current_x] = '-';
}
if (current_y < 25 && map[current_y + 1][current_x] ==' ')
{
map[current_y + 1][current_x] = '-';
}
if (current_x > 0 && map[current_y][current_x - 1] ==' ')
{
map[current_y][current_x - 1] = '-';
}
if (current_x<100 && map[current_y][current_x + 1] ==' ')
{
map[current_y][current_x + 1] = '-';
}
map[current_y][current_x] = '=';
for (i = 0; i < 25; i++)
{
for (n = 0; n < 100; n++)
{
if (map[i][n] == '_')
{
current_y = i;
current_x = n;
f=true;
break;
}
}
if (f==true)
{
break;
}
}
for (i = 0; i < 25; i++)
{
for (n = 0; n < 100; n++)
{
if (map[i][n] == '-')
{
map[i][n] = '_';
m++;
}
}
}
if (m==0)
{
return 4;
}
}
bool r=false;
while (true)
{
int d=rand()%4;
if (map[Target_y + 1][Target_x] == '_' && d==0)
{
for (i = 0; i < 25; i++)
{
for (n = 0; n < 100; n++)
{
if (map[i][n] == '-'||map[i][n]=='_'||map[i][n]=='=')
{
map[i][n] = ' ';
}
}
}
return 0;
}
else if (map[Target_y - 1][Target_x] == '_' && d==1)
{
for (i = 0; i < 25; i++)
{
for (n = 0; n < 100; n++)
{
if (map[i][n] == '-'||map[i][n]=='_'||map[i][n]=='=')
{
map[i][n] = ' ';
}
}
}
return 1;
}
else if (map[Target_y][Target_x+1] == '_' && d==2)
{
for (i = 0; i < 25; i++)
{
for (n = 0; n < 100; n++)
{
if (map[i][n] == '-'||map[i][n]=='_'||map[i][n]=='=')
{
map[i][n] = ' ';
}
}
}
return 2;
}
else if (map[Target_y][Target_x-1] == '_' && d==3)
{
for (i = 0; i < 25; i++)
{
for (n = 0; n < 100; n++)
{
if (map[i][n] == '-'||map[i][n]=='_'||map[i][n]=='=')
{
map[i][n] = ' ';
}
}
}
return 3;
}
}
}
- 답변 # 1
관련 자료
- python - 및 조건과 함께 while 루프를 사용하여 두 값 사이에 숫자 입력
- c# - 조건이 준수되면 while 루프를 사용하고 그렇지 않으면 if 문을 사용하십시오
- swift - DispatchGroup을 사용하여 while 루프에서 값을 조건으로 설정하는 방법은 무엇입니까?
- python - 조건이 거짓이더라도 루프는 여전히 한 번 더 반복됩니다
- r - 할당 중 동적 합계/카운트 조건
- sh - 2 개의 조건을 반복하고 실제 상태에서 멈추는 방법
- java - Newton-Raphson 방법을 사용하여 제곱근을 계산할 때 while 루프 조건
- python - while 루프가 0 또는 그보다 작은 값을 가지지 않는이 조건을 해결하는 방법
- c - 감지 if 조건 내에서 while 루프를 끊는 방법
- python - while 조건에 값을 추가하려고 시도했지만 되돌아 가지 않습니다
- bash - 쉘 스크립트에서 while 루프 조건
- c++ - 내부 상태를 해석하는 방법? while (+ (+ k--)! = 0)
다른 모든 의견에서 지적했듯이, 조건에 따라 기능을사용해야합니다. 이것은 실제로 읽을 수없는 표현입니다.
이것이 도움이 될 것입니다 :
알고있는 즉시 루프가 중단 된 이유는
std::cout
를 제거 할 수 있습니다. 의