My deadline is approaching fast, here is what I have translated, changed for my code. Something is WRONG because it only prints the first and last value, as in 1 and 30. If you guys can fix it in like, o, an hour, I would be so appreciative.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10 /* number of colums and rows of the array */
#define MOVES 30 /* try to build our number-row til MOVES */
int a[N][N];
int wrong[] = { 0, 0, 0, 0 }; /* contains information which direction can be accessed */
/* 0 = accessible, 1 = not accessible */
/*defines array*/
int init_array(int r, int c) {
for(r=0; r<=9; r++) {
for(c=0; c<=9; c++) {
a[c][r]=0;
}
}
}
int computeNextPos(int r, int c, int m) { /* does the walk and combines it all */
int rand_value;
rand_value = rand() % 4;
if(wrong[0] == 1 && wrong[1] == 1 && wrong[2] == 1 && wrong[3] == 1) {
return 0;
}
switch(rand_value) {
case 0: if( N > c + 1 && a[r][c+1] == 0) {
a[c+1][r]=m;
return 1;
} else {
wrong[rand_value] = 1;
}
break;
case 1: if( N > r + 1 && a[r+1][c] == 0) {
a[c][r+1]=m;
return 1;
} else {
wrong[rand_value] = 1;
}
break;
case 2: if( 0 <= r - 1 && a[r-1][c] == 0) {
a[c][r-1]=m;
return 1;
} else {
wrong[rand_value] = 1;
}
break;
case 3: if( 0 <= c - 1 && a[r][c-1] == 0) {
a[c-1][r]=m;
return 1;
} else {
wrong[rand_value] = 1;
}
break;
default:
break;
}
return 2;
}
void printArray() {
int i, j;
for(i=0; i<N; i++) {
for(j=0; j<N; j++) {
printf("%3d ", a[j][i]);
}
printf("\n");
}
system("PAUSE");
}
int main() {
int counter = 1, end = 0, x, y, i, j, m, ret;
srand(time(0));
x = rand() % N;
y = rand() % N;
a[x][y] = counter;
while(!end && counter < MOVES) {
ret = computeNextPos(x, y, m);
if(ret == 0) {
end = 1;
printf("dead end with counter #%2d\n", counter);
} else if(ret == 1) {
a[x][y] = ++counter;
wrong[0] = wrong[1] = wrong[2] = wrong[3] = 0;
} else {
continue;
}
}
printArray();
return 0;
}