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
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;
}
Trending nowThis is a popular solution!
Step by stepSolved in 6 steps with 5 images
- You roll a number cube numbered from 1 to 6. You then spin a spinner with 3 sections each with a different color. The spinner has the colors yellow, pink, and green. P(not green and not 2).arrow_forwardPrint numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. import java.util.Scanner;public class NestedLoop {public static void main (String [] args) {Scanner scnr = new Scanner(System.in);int userNum;int i;int j; userNum = scnr.nextInt();arrow_forwardGiven code: int s=0; for ( int i=1; i<5; i++ ){ s = s + i ; } Which one is correct, after running the code. Group of answer choices s=1 s=15 s=10 s=5arrow_forward
- Assignment 5B: Keeping Score. Now that we know about arrays, we can keep track of when events occur during our program. To prove this, let's create a game of Rock-Paper-Scissors. For those not familiar with this game, the basic premise is that each player says "Rock-Paper- Scissors!" and then makes their hand into the shape of one of the three objects. Winners are determined based on the following rules: Rock beats Scissors Scissors beats Paper Paper beats Rock At the start of the program, it will ask the player how many rounds of Rock-Paper- Scissors they want to play. After this, the game will loop for that many number of times. Each loop, it will ask the player what item they want to use – Rock, Paper, or Scissors. The computer will randomly generate its own item, and a winner will be determined. The game will then save the result as an element of an array, and the next round will begin. Once all the rounds have been played, the program will say "Game Over" and display a list of who…arrow_forwardPlease written by computer source Please solve with C - NIM Gamearrow_forwardYou are hired by a game design company and one of their most popular games is The Journey. The game has a ton of quests, and for a player to win, the player must finish all the quests. There are a total of N quests in the game. Here is how the game works: the player can arbitrarily pick one of the N quests to start from. Once the player completes a quest, they unlock some other quests. The player can then choose one of the unlocked quests and complete it, and so on. For instance, let’s say that this game had only 4 quests: A, B, C, and D. Let’s say that after you complete • quest A, you unlock quests [B, D]. • quest B, you unlock quests [C, D]. • quest C, you unlock nothing [ ]. • quest D, you unlock quest [C]. Is this game winnable? Yes, because of the following scenario: The player picks quest A to start with. At the end of the quest A, the unlocked list contains [B, D]. Say that player chooses to do quest B, then the…arrow_forward
- Assume you are playing a card game using a standard 52-card deck. You are dealt a hand of cards that are all the same suit, hearts. You are dealt first a 5, then a 3, then 8, and finally 4. You first grab the 8. Next you grab the 5 and place it in front of the 8. Then you take the 4 and place it in front of the 5. Finally, you take the 3 and place it in front of the 4. Which sorting algorithm is most similar to how you sorted your hand?Group of answer choices 1.Merge Sort 2.Selection Sort 3.Quick Sort 4.Insertion Sortarrow_forwardThe text presented the Sierpinski triangle fractal. Inthis exercise, you will write a program to display another fractal, called the Kochsnowflake, named after a famous Swedish mathematician. A Koch snowflake iscreated as follows:1. Begin with an equilateral triangle, which is considered to be the Koch fractalof order (or level) 0, as shown in Figure a.2. Divide each line in the shape into three equal line segments and draw an outwardequilateral triangle with the middle line segment as the base to create aKoch fractal of order 1, as shown in Figure b.3. Repeat Step 2 to create a Koch fractal of order 2, 3, . . . , and so on, as shownin Figures c and d.arrow_forwardMipMaps I Suppose that a pixel projects to an area of 10x28 texels in a texture map. From what mipmap level does the texture unit look up the texture value for this pixel, if the texture unit is using "nearest" lookup for mipmaps? Enter a single integer that is the mipmap level. A/arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education