我为我的项目编写了一个随机路径生成器,当它工作时,它按预期工作。然而,它有时只起作用。
在我的 main 函数中,我有一个简单的 switch 语句来调用这个项目的三个任务。
while(Continue)
{
switch(Example_number)
{
default: printf("No such program exists.\n");
break;
case 1: path();
break;
case 2: Caesar_cipher();
break;
case 3: anagram();
break;
}
printf("Would you like to test another?(Y/N)\n");
scanf("\n%c",&ch);
if(ch == 'Y' || ch == 'y')
{
NULL;
}
else
{
Continue = false;
}
}
当你输入 1 时,它会调用这个函数,它会创建一个数组并调用其他两个函数。
void path(void)
{
//Creates the array walk.
char walk[10][10] = {{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'}};
//Creates a randomly generated path to travel along walk.
generate_path(walk);
print_array(walk);
}
生成路径的函数。
void generate_path(char walk[10][10])
{
int move_n = 0;
int row, column, i;
const char alph[] = {'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
//Array that holds all previous data.
int move[3][25] = {0};
bool block = false;
//Allows for a random variable.
srand((unsigned) time(NULL));
row = rand() % 10;
column = rand() % 10;
while(!block)
{
walk[row][column] = 'A';
for(i = 0; i < 25; i++)
{
//goto comes here
restart:
move_n = rand() % move_dir;
//If space is open continue the alph array one row below.
if(move_n == 0 && walk[row+1][column] == '.' && row+1 < 10)
{
row += 1;
move[0][i] = i;
move[1][i] = row;
move[2][i] = column;
walk[row][column] = alph[i];
}
else if(move_n == 1 && walk[row][column+1] == '.' && column+1 < 10) //If space is open continue the alph array one column to the right.
{
column += 1;
move[0][i] = i;
move[1][i] = row;
move[2][i] = column;
walk[row][column] = alph[i];
}
else if(move_n == 2 && walk[row-1][column] == '.' && row-1 >= 0) //If space is open continue the alph array one row above.
{
row -= 1;
move[0][i] = i;
move[1][i] = row;
move[2][i] = column;
walk[row][column] = alph[i];
}
else if(move_n == 3 && walk[row][column-1] == '.' && column-1 >= 0) //If space is open continue the alph array one column to the left.
{
column -= 1;
move[0][i] = i;
move[1][i] = row;
move[2][i] = column;
walk[row][column] = alph[i];
}
else if((walk[row][column-1] == '.') || (walk[row-1][column] == '.') || (walk[row][column+1] == '.') || (walk[row+1][column] == '.'))
{
if(i == 25)
break;
goto restart;
}
else
{
//Resets data to point such that the path can continue.
row = move[1][i-1];
column = move[2][i-1];
i--;
walk[row][column] = '.';
}
}
block = true;
}
}
以及打印数组功能。
void print_array(char walk[10][10])
{
int i = 0;
int k = 0;
for(i = 0; i < 10; i++)
{
for(k = 0; k < 10; k++)
{
printf(" %c", walk[i][k]);
}
printf("\n");
}
}
但是由于某种原因,在 switch 语句中输入 1 时,它有时只能工作。每次调用任何其他函数都可以完美运行。