Write a C++ program which generates and displays a random walk across a grid. A 2-
dimensional array of characters will be used for this grid. The cells visited by the walk will be
labeled by capital letters with A being the first cell, B the cell moved to from A, and so on,
ending either when Z is reached or when it is not possible to move to another cell.
The following libraries, constant definition, and type definition are needed in the program:
#include
#include // for srand and rand
#include // for time
using namespace std;
const int SIZE = 10;
typedef char Grid[SIZE][SIZE]
The grid must be set to have all its elements be the character ’.’ before the walk is begun.
The program must contain the following function prototypes, the main function, and the function
definitions for all the other functions:
// Function prototypes
bool can_move_up(int i, int j, Grid walk);
bool can_move_down(int i, int j, Grid walk);
bool can_move_left(int i, int j, Grid walk);
bool can_move_right(int i, int j, Grid walk);
void init_array(Grid walk);
void generate_random_walk(Grid walk);
void print_array(Grid walk);
int main(void)
{
Grid walk; // the grid in which the random walk occurs
srand((unsigned) time(NULL));
init_array(walk);
generate_random_walk(walk);
print_array(walk);
return 0;
}
The four functions can_move _* must return whether or not it is possible to move in the
particular direction in the grid. For example, a function call like if (can_move_up(i, j, walk))
could be used to determine whether or not it is possible to move up. The rule for a move being
possible is that if it doesn’t go outside of the grid and isn’t already part of the walk, then that
move is possible. The function init_array must set all characters in the grid to ’.’
The function generate_random_walk must modify the cells of grid by changing ones that are
part of the walk to letters.
The function print_array must print the resulting grid in the form shown in the sample run
below.
In the function generate_random_walk, at each step of the walk, randomly select one of the 4
directions and try to move in that direction. If that move isn’t available, try the other moves in
turn until one is possible. If all 4 directions are blocked, the walk must terminate, and the
program must continue and print out the walk. To select one of the 4 directions randomly, use
the expression: rand() % 4
The function rand returns an integer, and finding the remainder of this integer divided by 4
results in getting one of the numbers 0, 1, 2, or 3. Use these 4 numbers to represent the 4
directions up, down, left and right.
The call to the function srand from the main function seeds the pseudo-random number
generator used by rand so that different sequences of numbers will be generated by calls to rand
each time the program is run.
Solution
#include //input output functions in C++
#include //rand() and srand() functions
#include //time() function for creating seed
#include
#include // for srand and rand
#inclu.
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
Write a C++ program which generates and displays a random walk acros.pdf
1. Write a C++ program which generates and displays a random walk across a grid. A 2-
dimensional array of characters will be used for this grid. The cells visited by the walk will be
labeled by capital letters with A being the first cell, B the cell moved to from A, and so on,
ending either when Z is reached or when it is not possible to move to another cell.
The following libraries, constant definition, and type definition are needed in the program:
#include
#include // for srand and rand
#include // for time
using namespace std;
const int SIZE = 10;
typedef char Grid[SIZE][SIZE]
The grid must be set to have all its elements be the character ’.’ before the walk is begun.
The program must contain the following function prototypes, the main function, and the function
definitions for all the other functions:
// Function prototypes
bool can_move_up(int i, int j, Grid walk);
bool can_move_down(int i, int j, Grid walk);
bool can_move_left(int i, int j, Grid walk);
bool can_move_right(int i, int j, Grid walk);
void init_array(Grid walk);
void generate_random_walk(Grid walk);
void print_array(Grid walk);
int main(void)
{
Grid walk; // the grid in which the random walk occurs
srand((unsigned) time(NULL));
init_array(walk);
generate_random_walk(walk);
print_array(walk);
return 0;
}
The four functions can_move _* must return whether or not it is possible to move in the
particular direction in the grid. For example, a function call like if (can_move_up(i, j, walk))
could be used to determine whether or not it is possible to move up. The rule for a move being
possible is that if it doesn’t go outside of the grid and isn’t already part of the walk, then that
2. move is possible. The function init_array must set all characters in the grid to ’.’
The function generate_random_walk must modify the cells of grid by changing ones that are
part of the walk to letters.
The function print_array must print the resulting grid in the form shown in the sample run
below.
In the function generate_random_walk, at each step of the walk, randomly select one of the 4
directions and try to move in that direction. If that move isn’t available, try the other moves in
turn until one is possible. If all 4 directions are blocked, the walk must terminate, and the
program must continue and print out the walk. To select one of the 4 directions randomly, use
the expression: rand() % 4
The function rand returns an integer, and finding the remainder of this integer divided by 4
results in getting one of the numbers 0, 1, 2, or 3. Use these 4 numbers to represent the 4
directions up, down, left and right.
The call to the function srand from the main function seeds the pseudo-random number
generator used by rand so that different sequences of numbers will be generated by calls to rand
each time the program is run.
Solution
#include //input output functions in C++
#include //rand() and srand() functions
#include //time() function for creating seed
#include
#include // for srand and rand
#include // for time
using namespace std;
class Grid_Walk
{
const int SIZE = 10;
#define UP 0 //flag for up
#define DOWN 1 //flag for down
#define LEFT 2 //flag for left
#define RIGHT 3 //flag for right
#define DOT '.'//flag for dot
public:
void init_array(Grid_Walk walk);
3. void generate_random_walk(Grid_Walk walk);
void print_array(Grid_Walk walk);
bool can_move_up(int i, int j, Grid_Walk walk);
bool can_move_down(int i, int j, Grid_Walk walk);
bool can_move_left(int i, int j, Grid_Walk walk);
bool can_move_right(int i, int j, Grid_Walk walk);
};
//Member function definitions
void Grid_Walk:: init_array(Grid_Walk walk)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
walk[i][j] = DOT; // Xcode gives me error for '=' "Expected unqualified-id"
walk[0][0] = 'A'; // Xcode gives me error for '=' "Expected unqualified-id"
}
}
}
// I am honestly not sure what to do with this funciton or what should be included in it's body
void Grid_Walk:: generate_random_walk(Grid_Walk walk)
{
int move;
int i,j,letters;
move = rand() % 4;
bool blocked = false;
while(!blocked){
for (letter = 1; letter < 26; letter++)
{
switch (move)
{
case 0:
if (can_move_up(i,j,walk))
{ //unoccupied position
++j; ++letters;
walk[i][j] ="A";
4. break;
}
else{
blocked = true;
break;
}
case 1:
if (can_move_down(i,j,walk))
{ //unoccupied position
j--; ++letters;
walk[i][j] ="B";
break;
}
else{
blocked = true;
break;
}
case 2:
if (can_move_left(i,j,walk))
{ //unoccupied position
i--; ++letters;
walk[i][j] ="C";
break;
}
else{
blocked = true;
break;
5. }
case 3:
if (can_move_right(i,j,walk))
{ //unoccupied position
i++; ++letters;
walk[i][j] ="D";
break;
}
else{
blocked = true;
break;
} default: break;
}//close switch
}
}
if (blocked)
break;
}
// Will print the random walk to the grid
void Grid_Walk::print_array(Grid_Walk walk)
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
cout << walk[i][j]; // Xcode error "Unexpected type name 'Grid'"
}
cout << endl;
}
}
bool Grid_Walk::can_move_up(int i, int j, Grid_Walk walk)
{
6. if (i > 0 && walk[i - 1][j] == DOT)
{
return true;
}
else
{
return false;
}
}
bool Grid_Walk:: can_move_down(int i, int j, Grid_Walk walk)
{
if (i < 9 && walk[i + 1][j] == DOT)
{
return true;
}
else
{
return false;
}
}
bool Grid_Walk:: can_move_left(int i, int j, Grid_Walk walk)
{
if (j > 0 && walk[i][j - 1] == DOT)
{
return true;
}
else
{
return false;
}
}
bool Grid_Walk:: can_move_right(int i, int j, Grid_Walk walk)
{
if (j < 9 && walk[i][j + 1] == DOT)
{
return true;
7. }
else
{
return false;
}
}
int main(void)
{
const int SIZE = 10;
char walk[SIZE][SIZE]; //array variable
char flag = 'A'; //flag for visited position; initially set to A
int i, j; //counter variables for loops
int rand_num; //variable for temporary storage of random numbers
int cycle_counter = 0; //count cycles through while loop
for(i = 0; i < SIZE; i++)
{//initiating array
for(j = 0; j < SIZE; j++) //elements
{
walk[i][j] = DOT;
}
}
DOT i = 5; j = 5; //initial position in array
srand((unsigned) time(0)); //create seed for function rand(), based on CPU time
while(flag <= 'Z')
{
rand_num = rand() % 4; //random number in current cycle
if( (rand_num == UP) && (i > 0) && (walk[i - 1][j] == DOT) )
{
//rand_num == UP - tells which direction!
//i > 0 - tests for up array bound
//walk[i - 1][j] == DOT will test if there is a dot up from current position
walk[--i][j] = flag; //--i = move to the up from current position
flag++; //C treats integers and chars the same (0-255, ASCII...)!
}
else if( (rand_num == DOWN) && (i < SIZE - 1) && (walk[i + 1][j] == DOT) )
8. {
//rand_num == DOWN - tells which direction!
//i < SIZE - 1 - tests for down array bound (C counts from zero!)
//walk[i + 1][j] == DOT- test if there is a dot down from current position
walk[++i][j] = flag; //++i = move to the down from current position flag++;
//C treats integers and chars the same (0-255, ASCII...)!
}
else if( (rand_num == LEFT) && (j > 0) && (walk[i][j - 1] == DOT) )
{
//rand_num == LEFT - tells which direction!
//j > 0 - test for left array bound (C counts from zero!)
//walk[i][j - 1] == DOT - tests if there is a dot left from current position
walk[i][--j] = flag; //--j = move to the left from current position flag++;
//C treats integers and chars the same (0-255, ASCII...)!
}
else if( (rand_num == RIGHT) && (j < SIZE - 1) && (walk[i][j + 1] == DOT) )
{
//rand_num == RIGHT - tells which direction!
//j < SIZE - 1- test for right array bound (C counts from zero!)
//walk[i][j + 1] == DOT - tests if there is a dot right from current position
walk[i][++j] = flag; //++j = move to the right from current position flag++;
//C treats integers and chars the same (0-255, ASCII...)!
}
else
//in every other case continue;
//loop goes on the next cycle
}
//following loops print results
for(i = 0; i < SIZE; i++)
{
//these two loops
for(j = 0; j < SIZE; j++)
//access all array elements