私は自分のプロジェクト用にランダム パス ジェネレーターを作成しましたが、動作すると意図したとおりに動作します。ただし、時々しか機能しません。
メイン関数には、このプロジェクトの 3 つのタスクを呼び出す単純な 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 を入力すると、この関数が呼び出され、配列が作成され、他の 2 つの関数が呼び出されます。
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 を入力すると、時々しか機能しません。他の関数を呼び出すと、毎回完全に機能します。