Dies ist meine erste Frage, und ja, es ist eine "Hausaufgabe" Aufgabe. Ich arbeite seit Stunden daran, kann aber den Algorithmus nicht zum Laufen bringen. Das Programm, das ich geschrieben habe, soll eine Funktion enthalten, die ein 12 mal 12 Array akzeptiert und den Pfad bis zum Ende findet. Die Hauptfunktion findet den Anfang des Labyrinths und ändert es in ein "x", um die Position des "Spielers" darzustellen. Die Hauptfunktion ruft dann die mazePrint-Funktion auf, die das Array akzeptiert, nachdem sie den Bildschirm gelöscht hat. Dann ruft es die mazeTraversal-Funktion auf, die das Array akzeptiert. Der erste Teil von mazeTraversal versucht, die Position von "x" zu finden und durch "." Zu ersetzen. Der nächste Teil soll bestimmen, in welche Richtung das "x" zeigt. Ich habe 4, 5, 6 und 8 (West, Süd, Ost und Nord) verwendet, um darzustellen, wie es aussieht. Je nachdem, wie es aussieht, versucht mazeTraversal zu bestimmen, ob es einen offenen Pfad nach rechts, dann vor, dann nach links und dann nach hinten gibt, um dann ein X in diese Position zu setzen und die Art zu ändern, wie das x ist zugewandt. Nach dem zweiten Zug geht etwas schief, wenn ich das Programm starte. Danke für jede Hilfe, und tut mir leid, wenn dies nicht der richtige Ort für solche Fragen ist.Rechte Labyrinth Traversal in C
#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);
}
Sie haben die Quelle um vier Felder für stackoverflow eingerückt; Leider wird die Einrückung einiger Linien mit Hilfe von harten Tabs erreicht, so dass es zu einem Chaos wird. – Kaz
Hallo Wald, willkommen auf StackOverflow. Es wäre gut, wenn Sie Ihren Code formatieren würden, da er kaum eingerückt ist, was das Lesen erschwert. Versuchen Sie außerdem, Ihr Problem einzugrenzen und zu verallgemeinern. – bitmask
@ Shingetsu: Scrollrad defekt? – bitmask