Today I
finished a program that generates a random walk across a 10x10 array, using the letters of the alphabet. If there is nowhere else to walk, the program was supposed to self-terminate, and you weren't supposed to let the program collide into a previously moved into spot (this means I have very basic collision detection!). A final outcome should look something like this:
a . . . . . . . . .
b c . . . . . . . .
e d . . . p q r . .
f . . . . o . s . .
g h i j . n u t . .
. . . k l m v . . .
. . . . . . w . . .
. . . . z y x . . .
. . . . . . . . . .
. . . . . . . . . .
Luckily our forums have a nice codebox, so I'll post my source code here:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int bool;
int randomInt(int low, int high)
{
return (rand() % (high - low + 1) + low);
}
int main(void)
{
int board[10][10] ={{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.',},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.',},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.',},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.',},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.',},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.',},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.',},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.',},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.',},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}};
int letters[26] = {'a', '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'};
int x = 0, y = 0;
int move, row, column;
srand(time(0));
for (int i = 0; i < sizeof(letters) / sizeof(letters[0]); i++)
{
board[y][x] = letters[i];
for (column = 0; column < 10; column++)
{
for (row = 0; row < 10; row++)
{
printf("%c ", board[column][row]);
}
printf("\n");
}
printf("\n");
while (1) //Randomize 0-3, and update x or y accordingly.
{
move = randomInt(1, 4);
if (move == 1)
{
if (x != 0)
{
if (board[y][x - 1] == '.')
{
x -= 1;
break;
}
}
}
if (move == 2)
{
if (y != 9)
{
if (board[y + 1][x] == '.')
{
y += 1;
break;
}
}
}
if (move == 3)
{
if (x != 9)
{
if (board[y][x + 1] == '.')
{
x += 1;
break;
}
}
}
if (move == 4)
{
if (y != 0)
{
if (board[y - 1][x] == '.')
{
y -= 1;
break;
}
}
}
if (board[y][x + 1] != '.' && board[y][x - 1] != '.' && board[y + 1][x] != '.' && board[y - 1][x] != '.')
{
printf("\n\nThere is no where else to walk. Hit enter to leave.");
getchar();
return 0;
}
if (x == 0 && board[y][x + 1] != '.' && board[y + 1][x] != '.' && board[y - 1][x] != '.')
{
printf("\n\nThere is no where else to walk. Hit enter to leave.");
getchar();
return 0;
}
if (x == 9 && board[y][x - 1] != '.' && board[y + 1][x] != '.' && board[y - 1][x] != '.')
{
printf("\n\nThere is no where else to walk. Hit enter to leave.");
getchar();
return 0;
}
if (y == 0 && board[y][x + 1] != '.' && board[y][x - 1] != '.' && board[y + 1][x] != '.')
{
printf("\n\nThere is no where else to walk. Hit enter to leave.");
getchar();
return 0;
}
if (y == 9 && board[y][x + 1] != '.' && board[y][x - 1] != '.' && board[y - 1][x] != '.')
{
printf("\n\nThere is no where else to walk. Hit enter to leave.");
getchar();
return 0;
}
}
}
getchar();
return 0;
}
I didn't learn anything new while writing this program, although, I did get more familiar with C syntax, which was my main goal.
If you'd like to see the source code file or the compiled .exe, you can find them here:
http://cecilsunkure.blogspot.com/2010/11/2d-array-practice.html
None.