Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions 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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education