(Random Walk Robot) A robot is initially located at position (0, 0) in a grid [−5, 5] × [−5, 5]. The robot can move randomly in any of the directions: up, down, left, right. The robot can only move...





(Random Walk Robot) A robot is initially located at position (0, 0) in a grid [−5, 5] × [−5, 5]. The robot can move randomly in any of the directions: up, down, left, right. The robot can only move one step at a time.


For each move, print the direction of the move in and the current position of the robot. Use formatted output to print the direction (Down, Up, Left or Right) in the left. The direction takes 10 characters in total and fill in the field with empty spaces. The statement to print results in such format is given below:


cout < setw(10)="">< left="">< ‘down’="">< ...="" ;="" cout="">< setw(10)="">< left="">< ‘up’=""><>


If the robot moves back to the original place (0,0), print “Back to the origin!” to the console and stop the program. If it reaches the boundary of the grid, print “Hit the boundary!” to the console and stop the program. A successful run of your code may look like: Due to randomness, your results may have a different trajectory path than examples. Your code should generate different robot trajectories for each run.





Hint: Use two int variables x and y to represent robot’s position and use the random integer generator to pick a moving direction at each step. Use a loop to keep moving the robot as long as the robot doesn’t hit the boundary or go back to the origin.









(-1,0)<br>(-2,0)<br>(-1,0)<br>(-2,0)<br>(-2,1)<br>(-2,2)<br>(-2,1)<br>(-2,0)<br>(-3,0)<br>(-2,0)<br>(-2,-1)<br>(-1,-1)<br>(-1,-2)<br>(-2,-2)<br>(-2,-3)<br>(-2,-4)<br>(-2,-5)<br>Hit the boundary!<br>Left<br>Left<br>Right<br>Left<br>Right<br>Up<br>Right<br>(1,0)<br>(1,1)<br>(2,1)<br>(2,0)<br>(2,1)<br>(2,2)<br>(2,1)<br>(1,1)<br>(0,1)<br>(-1,1)<br>(0,1)<br>(1,1)<br>(1,0)<br>(0,0)<br>Back to the origin!<br>Up<br>Up<br>Down<br>Down<br>Up<br>Down<br>Up<br>Left<br>Down<br>Right<br>Left<br>Down<br>Left<br>Right<br>Left<br>Down<br>Right<br>Right<br>Left<br>Down<br>Down<br>Down<br>Left<br>Down<br>or<br>

Extracted text: (-1,0) (-2,0) (-1,0) (-2,0) (-2,1) (-2,2) (-2,1) (-2,0) (-3,0) (-2,0) (-2,-1) (-1,-1) (-1,-2) (-2,-2) (-2,-3) (-2,-4) (-2,-5) Hit the boundary! Left Left Right Left Right Up Right (1,0) (1,1) (2,1) (2,0) (2,1) (2,2) (2,1) (1,1) (0,1) (-1,1) (0,1) (1,1) (1,0) (0,0) Back to the origin! Up Up Down Down Up Down Up Left Down Right Left Down Left Right Left Down Right Right Left Down Down Down Left Down or
Jun 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here