[英]Right hand maze traversal in C
這是我的第一個問題,是的,這是一項“作業”作業。 我已經工作了幾個小時,但無法使算法正常工作。 我編寫的程序應該包含一個接受12 x 12數組並找到末尾路徑的函數。 主要功能找到迷宮的起點並將其更改為“ x”以表示“玩家”的位置。 然后,主函數調用mazePrint函數,該函數接受數組在清除屏幕后將其打印出來。 然后,它調用接受數組的mazeTraversal函數。 mazeTraversal的第一部分嘗試找到“ x”的位置並將其替換為“。”。 下一部分應該確定“ x”面向的方向。 我用4、5、6和8(西,南,東和北(請看數字鍵盤))表示它的朝向。 根據其面對的方式,mazeTraversal嘗試確定右側是否有開放路徑,然后是前方,然后是左側,然后是背后,然后將X放置在該位置並更改x的方式面對。 當我運行程序時,第二步移動后出現了問題。 感謝您的任何幫助,如果不是這樣的問題,對不起。
#include <stdio.h>
#include <stdlib.h>
void mazePrint(char *maze[12][12]);
void mazeTraversal(char *maze[12][12]);
static int face = 6;
main()
{
int i = 0;
int j = 0;
int k = 0;
int start;
int xpos;
char *mazeone[12][12] = {
//0///1///2///3///4///5///6///7///8///9///10//11///
"#","#","#","#","#","#","#","#","#","#","#","#",//0
"#",".",".",".","#",".",".",".",".",".",".","#",//1
".",".","#",".","#",".","#","#","#","#",".","#",//2
"#","#","#",".","#",".",".",".",".","#",".","#",//3
"#",".",".",".",".","#","#","#",".","#",".",".",//4
"#","#","#","#",".","#",".","#",".","#",".","#",//5
"#","#",".","#",".","#",".","#",".","#",".","#",//6
"#","#",".","#",".","#",".","#",".","#",".","#",//7
"#",".",".",".",".",".",".",".",".","#",".","#",//8
"#","#","#","#","#","#",".","#","#","#",".","#",//9
"#",".",".",".",".",".",".","#",".",".",".","#",//10
"#","#","#","#","#","#","#","#","#","#","#","#",};//11
for (i = 0; i <12; i++)
if (mazeone[i][0] == "." ) {
start = i;
mazeone[start][0] = "x";
xpos = start;
break;
}
printf("X is the starting point.\n");
printf("Press Space Bar to watch the X move.\n\n\n");
getchar();
mazePrint(mazeone);
getchar();
return 0;
}
void mazePrint(char *maze[12][12])
{
int x = 0;
int y = 0;
system("cls");
for (x = 0; x < 12; x++) {
for (y = 0; y < 12; y++) {
printf("%s", maze[x][y]);
}
printf("\n");
}
getchar();
mazeTraversal(maze);
}
void mazeTraversal(char *maze[12][12])
{
int x = 0;
int y = 0;
for (x = 0; x < 12; x++) {
for (y = 0; y < 12; y++) {
if (maze[y][x] == "x")
break;
}
if(maze[y][x] == "x")
break;
}
for (y = 0; y < 12; y++) {
for (x = 0; x < 12; x++) {
if (maze[y][x] == "x")
break;
}
if (maze[y][x] == "x")
break;
}
maze[y][x] = ".";
switch (face) {
case 6:{
if (maze[y][x-1] == ".") {
maze[y][x-1] = "x";
face = 5;
} else if (maze[y + 1][x] == ".") {
maze[y + 1][x] = "x";
face = 6;
} else if (maze[y][x+1] == ".") {
maze[y][x+1] = "x";
face = 8;
} else if (maze[y - 1][x] == ".") {
maze[y - 1][x] = "x";
face = 4;
}
}
case 8:{
if (maze[y + 1][x] == ".") {
maze[y + 1][x] = "x";
face = 6;
} else if (maze[y][x+1] == ".") {
maze[y][x+1] = "x";
face = 8;
} else if (maze[y - 1][x] == ".") {
maze[y - 1][x] = "x";
face = 4;
} else if (maze[y][x-1] == ".") {
maze[y][x-1] = "x";
face = 5;
}
}
case 4:{
if (maze[y][x+1] == ".") {
maze[y][x+1] = "x";
face = 8;
} else if (maze[y - 1][x] == ".") {
maze[y - 1][x] = "x";
face = 4;
} else if (maze[y][x-1] == ".") {
maze[y][x-1] = "x";
face = 5;
} else if (maze[y + 1][x] == ".") {
maze[y + 1][x] = "x";
face = 6;
}
}
case 5:{
if (maze[y - 1][x] == ".") {
maze[y - 1][x] = "x";
face = 4;
} else if (maze[y][x-1] == ".") {
maze[y][x-1] = "x";
face = 5;
} else if (maze[y + 1][x] == ".") {
maze[y + 1][x] = "x";
face = 6;
} else if (maze[y][x+1] == ".") {
maze[y][x+1] = "x";
face = 8;
}
}
}
mazePrint(maze);
}
你錯過了break;
每種case:
后在switch(face)
中的語句case:
代碼塊。 沒有break;
語句,您的代碼將進入下case:
,這不是您想要的。
我認為您的路線指示有誤。 您應該從maze[2][0]
,面向'6'。 您想前進到maze[2][1]
,仍然面對'6'。 但是,如果您查看方向6的mazeTraversal
代碼,您會得到以下情況:
if(maze[y][x+1] == "."){
maze[y][x+1] = "x";
face = 8;
}
因此,您錯誤地設置了結果方向。
可能有助於保持直率的一件事是使用枚舉而不是隨機數字代碼:
enum Facing {
face_EAST,
face_SOUTH,
face_WEST,
face_NORTH } face = face_EAST;
我什至可能會忘記指南針方向,而使用針對該問題的方向。 這應該有助於保持代碼中的方向正確。
enum Facing {
face_Xplus,
face_Yplus,
face_Xminus,
face_Yminus } face = face_Xplus;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.