1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int M = 10;
const int N = 6;
void initialize_grid(char grid[][N]);
void set_current_position(int i, int j, char grid[][N], char CurrentChar);
void print_grid(char grid[][N]);
bool can_go_left(int i, int j,char grid[][N]);
bool can_go_right(int i, int j, char grid[][N]);
bool can_go_up(int i, int j, char grid [][N]);
bool can_go_down(int i, int j, char grid[][N]); // REPAIRED
int main()
{
srand(time(NULL));
char grid[M][N];
int i = 0;
int j = 0;
char CurrentChar = 'A'; // <---
initialize_grid(grid);
set_current_position(i, j, grid, CurrentChar);
while(CurrentChar <= 'Z' && i < M
&& j < N)
{ int direction = rand() % 4;
switch (direction)
{
case 0:
if(can_go_down(i, j, grid) == true)
{
set_current_position(++i, j, grid, ++CurrentChar);
}
case 1:
if (can_go_up(i, j, grid) == true)
{
set_current_position(--i, j, grid, ++CurrentChar);
}
case 2:
if(can_go_left(i, j, grid) == true)
{
set_current_position(i, --j, grid, ++CurrentChar);
}
case 3:
if(can_go_right(i, j, grid) == true)
{
set_current_position(i, ++j, grid, ++CurrentChar);
}
}
}
print_grid(grid);
return 0;
}
bool can_go_left(int i, int j, char grid[M][N])
{
if ( j > 0 && grid[j-1][i] == '.')
return true;
else
return false;
}
bool can_go_right(int i, int j, char grid[M][N])
{
if ( j < N-1 && grid[j+1][i] == '.')
return true;
else
return false;
}
bool can_go_down(int i, int j, char grid[M][N])
{
if ( i < M-1 && grid[i + 1][j] == '.')
return true;
else
return false;
}
bool can_go_up(int i, int j, char grid[M][N])
{
if ( i > 0 && grid[j][i-1] == '.')
return true;
else
return false;
}
void set_current_position(int i, int j, char grid[M][N], char CurrentChar)
{
grid[i][j] = CurrentChar;
}
void initialize_grid(char grid[M][N])
{
for(int i = 0; i < M; i++)
{
for(int j = 0; j < N; j++)
{
grid[i][j] = '.';
}
}
}
void print_grid(char grid[M][N])
{
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
cout << grid[i][j];
}
cout << endl;
}
}
|