Code:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<time.h>
#define MIN 0
#define MAX 9
#define N 10
#define D 4
#define TRUE 1
#define FALSE 0
#define OPEN (arr[i][j] < 'A') || (arr[i][j] > 'Z')
#define WITHIN_ARR0 ((i > MIN) && (i <= MAX)) && ((j >= MIN) && (j <= MAX))
#define WITHIN_ARR1 ((i >= MIN) && (i <= MAX)) && ((j >= MIN) && (j < MAX))
#define WITHIN_ARR2 ((i >= MIN) && (i < MAX)) && ((j >= MIN) && (j <= MAX))
#define WITHIN_ARR3 ((i >= MIN) && (i <= MAX)) && ((j > MIN) && (j <= MAX))
main()
{
char arr[N][N]={{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'}};
int i=0, j=0, x=0, d=4, direction=0, flag=TRUE, arr2[]={0,0,0,0};
char alph='A';
srand((unsigned) time(NULL));
while (alph <= 'Z'){
if ((arr2[0]>0) && (arr2[1]>0) && (arr2[2]>0) && (arr2[3]>0)){
for (i=0;i<N;i++){
for (j=0;j<N;j++){
printf("%c", arr[i][j]);}
printf("\n");}
printf("\narr2[0]=%d", arr2[0]);
printf("\narr2[1]=%d", arr2[1]);
printf("\narr2[2]=%d", arr2[2]);
printf("\narr2[3]=%d\n", arr2[3]);
system("pause");
return 0;}
direction = (rand() % D);
switch (direction){
case 0:
if (OPEN){
arr[i][j]=alph;
printf("assign %c to arr[%d][%d]\n", alph, i, j);
printf("direction is %d\n", direction);
alph++;
for (x=0;x<3;x++){
arr2[x]=FALSE;
printf("arr2[%d]=%d ",x,arr[x]);}
if (WITHIN_ARR0) i--;
break;}
else {arr2[direction] = TRUE; printf("\narr2[%d]=%d\n", direction, arr2[0]); break;}
case 1:
if (OPEN){
arr[i][j]=alph;
printf("assign %c to arr[%d][%d]\n", alph, i, j);
printf("direction is %d\n", direction);
alph++;
for (x=0;x<3;x++){
arr2[x]=0;}
if (WITHIN_ARR1) j++;
break;}
else {arr2[direction] = TRUE; printf("\narr2[%d]=%d\n", direction, arr2[1]); break;}
case 2:
if (OPEN){
arr[i][j]=alph;
printf("assign %c to arr[%d][%d]\n", alph, i, j);
printf("direction is %d\n", direction);
alph++;
for (x=0;x<3;x++){
arr2[x]=0;}
if (WITHIN_ARR2) i++;
break;}
else {arr2[direction] = TRUE; printf("\narr2[%d]=%d\n", direction,arr2[2]);break;}
case 3:
if (OPEN){
arr[i][j]=alph;
printf("assign %c to arr[%d][%d]\n", alph, i, j);
printf("direction is %d\n", direction);
alph++;
for (x=0;x<3;x++){
arr2[x]=0;}
if (WITHIN_ARR3) j--;
break;}
else {arr2[direction] = TRUE; printf("\narr2[%d]=%d\n", direction,arr2[3]);break;}
default: {
printf("direction is: %d, and not correct", direction);
break;}
}
}
for (i=0;i<N;i++){
for (j=0;j<N;j++){
printf("%c", arr[i][j]);}
printf("\n");}
system("pause");
return 0;
}
ps. i have not worked with functions yet (at this point in the book), so i'd rather not for this program.