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 and the current position of the robot. If the robot makes a circle, which means it moves back to the original place, 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: Down (0,-1) Down (0,-2) Up (0,-1) Left (-1,-1) Left (-2,-1) Up (-2,0) Left (-3,0) Left (-4,0) Left (-5,0) Hit the boundary! or Left (-1,0) Down (-1,-1) Right (0,-1) Up (0,0) Back to the origin! Instructions: This program is to give you practice using the control flow, the random number generator, and output formatting. You may not use stdafx.h. Include header comments. Include to format your output. Name your file randomWalk.cpp.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 and the current position of the robot. If the robot makes a circle, which means it moves back to the original place, 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:

Down (0,-1)

Down (0,-2)

Up (0,-1)

Left (-1,-1)

Left (-2,-1)

Up (-2,0)

Left (-3,0)

Left (-4,0)

Left (-5,0)

Hit the boundary!

or

Left (-1,0)

Down (-1,-1)

Right (0,-1)

Up (0,0)

Back to the origin!

Instructions: This program is to give you practice using the control flow, the random number generator, and output formatting. You may not use stdafx.h. Include header comments. Include <iomanip> to format your output. Name your file randomWalk.cpp.

This is the question and I'm supposed to build this program with while loop, string, random number generator. I'm not allowed to use switch for this assignment.

I'm having trouble assigning random direction with appropriate coordinate corresponding with that direction.

This is what I have so far:

#include<iostream>

#include<iomanip>

#include<ctime>

#include<cstdlib>

#include<string>

using namespace std; i

 

nt x1 = 5;

int x2 = -5;

int y1_ = 5;

int y2 = -5;

 

bool gameOver = false;

bool Origin(int x, int y)

{ if (x == 0 && y == 0) {

cout << "Back to Origin!" << endl;

gameOver = true;

return true;

}

return false;

}

bool boundary(int x, int y) { if (x<x1 || x>x2) {

cout << "Hit the boundary!" << endl;

gameOver = true; return true;

}

if (y<y1_ || y>y2) {

cout << "Hit the boundary!" << endl;

gameOver = true;

return true;

}

return false;

}

 

int main(int argc, char** argv)

{ srand(static_cast<int>(time(0)));

int robotx = 0;

int roboty = 0;

while (!gameOver) {

int r = rand() % 4+1;

int x = robotx;

int y = roboty;

 

if (r = 0)

cout << left<<setw(7) <<"Down"<<"(" << x << "," << y << ")" << endl;

y --;

 

if (r=1) cout << left<<setw(7)<<"Right"<<"(" << x << "," << y << ")" << endl;

x ++;

 

if (r=2) cout << left<<setw(7)<<"Up" << "(" << x << "," << y << ")" << endl; y ++;

 

if (r=3) cout << left<<setw(7)<<"Left" << "(" << x << "," << y << ")" << endl; x --;

}

return 0;

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 5 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education