Using function to create random walk of robot

MY QUESTION:
Write a function randomWalk that takes as input the initial position of a robot moving on the screen. The function should place a character at the initial position and then randomly decide which direction to move next and place a character at the new position. The robot has to keep walking randomly till the space key is pressed. You can use the CheckKeyPressed function of myconsole.h library.

To generate a random number use the rand() function of the math library (so include <math.h>). The rand function will generate a random number within a large range. However, you can convert this large number to a number from 0-3 by taking the modulus with 4.

If the random number generated is 0, you could move the robot one step up, if it is 1 then you could move the robot left, for 2 move the robot right and for 3 move the robot one step down. To watch this as an animation use the Sleep() function within your loop.

#include <iostream>
#include "myconsole.h"
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <windows.h>
using namespace std;
void RandomWalk()
{
srand(time(0));
int inputy = 0,inputx = 0;
cout<<"Enter the initial position(x,y) of ROBOT : ";
cin>>inputx;
cin>>inputy;

PlaceCursor(inputx,inputy);
cout<<" @ ";
int key;
while(key != 32)
{
int key = CheckKeyPressed(1000);
int random = (rand() % 4);
if(random = 0)
{
PlaceCursor(inputx,(inputy+2));
}
else if (random = 1)
{
PlaceCursor(inputx,(inputy-2));
}
else if (random = 2)
{
PlaceCursor((inputx + 2),inputy);
}
else
{
PlaceCursor((inputx -2),inputy);
}

cout<<"@";
Sleep(1000);
ClearScreen();
}




}
int main()
{
RandomWalk();
}
I'm not understanding what you are asking but I see you are using assignment = instead of comparing for equality == in the if conditions.
Topic archived. No new replies allowed.