Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this programming assignment is to give you practice in while loop

ID: 3729169 • Letter: T

Question

The purpose of this programming assignment is to give you practice in while loops, function calls and if statements. Your program will do a little animation/simulation of a robot walking through a minefield.

The robot starts in the northwest corner (x = 0 and y = 0) of a 650px x 650px minefield. There will be 4 randomly placed mines is the minefield, each represented by a 35px x 35px red square. Also located in the minefield are 4 randomly placed “safe zones”. These are blue circular regions with a width and height of 12 pixels.

The robot takes steps of length -30 to 30 pixels vertically and horizontally, but is confined to the minefield square. If it steps outside of the minefield, make sure that you bring it back to the edge of the minefield. Limit the number of steps the robot takes to 700 steps.

If the robot steps into one of the 4 red mines, pop up a message box saying "Boom!" and end the program.

If the robot reaches a safe zone then pop up a message box that says “You have reached a safety zone!!” and end the program.

If instead, the robot moves around the minefield for 700 total steps without triggering any mine or reaching a safety zone, then you should pop up a message box saying "You have made it 700 steps and are still alive!!!” and then end the program.

Here is some pseudocode:

Create and display a 650px x 650px white picture.

Display 4 mines as 4 red squares, each 35px x 35px, randomly placed within the perimeter of the minefield – use a while statement that repeats 4 times to create and display the mines. Use addRectFilled( ) function to draw the mines.

Also display the 4 safety zones randomly within the perimeter of the minefield. Use something like   addArcFilled( pic, xSafety, ySafety, 12, 12, 0, 360, blue) where pic is the variable storing your minefield (picture) and xSafety and ySafety are the random x and y coordinates of the center of the circle. I will explain the other parameters in class.

Note: the random placement of the mines and safety zones means that every time your program is run, it picks a different placement of them both. BE SURE that the mines and safety zones are kept completely within the minefield. Do not try to draw them outside the minefield. Show the picture again with repaint.

Initialize the robot’s current position’s x and y coordinates to 0, 0 (because the initial position is in the northwest corner).

You need to also record in your program the old position of the robot in the minefield, in order to be able to draw its move between the previous position and the current one (at the beginning of the program, there is no previous position, but we can still initialize xOld and yOld to 0, 0).

Also, initialize to False a variable called triggered that records whether the robot triggered a mine.

Initialize a variable safety to false that records whether the robot entered a safety zone.

Initialize the current step count to 0. We will use this to ensure that the game ends if the robot moves at least 700 steps without blowing up.

Repeat the following steps for as long as triggered remains False and the number of steps hasn’t reached 700:

Save the old x, y coordinates: xOld = x, yOld = y

Make the next move and update the current x, y coordinates: x = x + randrange(-30, 31), y = y + randrange(-30, 31)

Increase the step count by 1

If the robot goes off the minefield to the left, reset its x coordinate to 0; if the robot goes off the minefield to the right, reset its x coordinate to 649

Do a similar thing for y if the robot goes off the top or bottom

Check if the robot stepped on a mine – do that by checking the color of the pixel where the robot landed after the last move; if the robot triggered a mine (ended up on a red pixel), set triggered to True.

Check if the robot ended up in a safety zone in a similar manner (ended up in a blue pixel). If so, set safety to true.

Draw a line between the old position (xOld, yOld) and the current position (x, y) of the robot

Re-display the picture

Notify the user of the result of the loop. Note that the while loop can end for three possible reasons: either the robot walked for more than 700 steps, or it stepped on a mine, or it landed in a safety zone.

Finally, show an information box with “Game over!”.

NOTE

Do NOT try to write the entire program at once!!! First try to just get the “robot” moving through an empty minefield for 700 steps.

Next, add the 4 red square mines and make sure they appear randomly.

Next, add the code to determine if the robot ended up on a mine.

Next add the 4 safety zones and determine if the robot ended up in one of them.

Below is a sample of a 700 step walk in which the robot did not step ion a mine or end in a safety zone.

Explanation / Answer

#include <Servo.h>

Servo rightfoot;

Servo rightthigh;

Servo leftfoot;

Servo leftthigh;

int posleftfoot;

int posrightfoot;

int posrighttigh;

int posleftthigh;

void setup()

{

rightfoot.attach(9);

rightthigh.attach(5);

leftfoot.attach(3);

leftthigh.attach(11);

leftfoot.write(90);

leftthigh.write(90);

rightthigh.write(90);

rightfoot.write(90);

}

void loop()

{

//primer movimiento pata derecha

for(posleftfoot = 90; posleftfoot <= 180; posleftfoot++)

{

leftfoot.write(posleftfoot);

delay(50);

}

for(posrightfoot = 90; posrightfoot <= 135; posrightfoot++)

{

rightfoot.write(posrightfoot);

delay(50);

--------------------------------------------------------------------------------------------------------------------------

#include <><> This command is used for including the servo library.

Servo rightfoot; <><> This command creates a Servo object with the name 'rightfoot'. This will be used to address the servo later.

Servo rightthigh;<><> This command creates a Servo object with the name 'rightthigh'. This will be used to address the servo later.

Servo leftfoot;<><> This command creates a Servo object with the name 'leftfoot'. This will be used to address the servo later.

Servo leftthigh;<><> This command creates a Servo object with the name 'leftthigh'. This will be used to address the servo later.

void setup() <><> This includes the intial commands that the Arduino will go through once before going through the loops.

{ <><> Marks the beginning of the setup commands.

rightfoot.attach(9); <><> This command attached the Servo object 'rightfoot' to pin 9.

rightthigh.attach(5); <><> This command attached the Servo object 'rightthigh' to pin 9.

leftfoot.attach(3); <><> This command attached the Servo object 'leftfoot' to pin 9.

leftthigh.attach(11); <><> This command attached the Servo object 'leftthigh' to pin 9.

} <><> Marks the ending of the setup commands.

void loop() <><> This is the code that will be repeated multiple times to do the actions.

{ <><> Marks the beginning of the loop of commands.

leftfoot.write(10); <><> This command is used to make the Servo go to it's initial position after each loop.

leftthigh.write(90); <><> This command is used to make the Servo go to it's initial position after each loop.

rightthigh.write(105); <><> This command is used to make the Servo go to it's initial position after each loop.

rightfoot.write(180); <><> This command is used to make the Servo go to it's initial position after each loop.

delay(1000); <><> This creates a 1 sec delay before the next command is executed. The value is in milliseconds.

leftfoot.write(17); <><> This command is used to make the 'leftfoot' servo go to 17 degrees.

delay(25); <><> This command creates a delay of 25 ms before the next command is executed. It is added to make the actions look smoother. If you want to make the dance slower, you can increase the value of all the delay commands and vice versa.

leftthigh.write(95); <><> This command is used to make the 'leftfoot' servo go to 17 degrees.

delay(25);<><> This command creates a delay of 25 ms before the next command is executed. It is added to make the actions look smoother. If you want to make the dance slower, you can increase the value of all the delay commands and vice versa.

......... <><> I did not write all the commands because all the commands that follow are basically the same as these.

.........<><> I did not write all the commands because all the commands that follow are basically the same as these.

} <><> Marks the ending of the loop of commands.

(I have attached a picture which shows how the servo degrees work.)

}

for(posrighttigh = 90; posrighttigh <= 135; posrighttigh++)

{

rightthigh.write(posrighttigh);

delay(50);

}

//cuarto movimiento pata derecha

for(posleftfoot = 180; posleftfoot >= 90; posleftfoot--)

{

leftfoot.write(posleftfoot);

delay(50);

}

for(posrightfoot = 135; posrightfoot >= 90; posrightfoot--)

{

rightfoot.write(posrightfoot);

delay(50);

}

//sexto movimiento pata derecha

for(posrighttigh = 135; posrighttigh >= 90; posrighttigh--)

{

rightthigh.write(posrighttigh);

delay(50);

}

//primer movimiento pata izda

for(posrightfoot = 90; posrightfoot >= 0; posrightfoot--)

{

rightfoot.write(posrightfoot);

delay(50);

}

//segundo movimiento pata izda

for(posleftfoot = 90; posleftfoot >=45; posleftfoot--)

{

leftfoot.write(posleftfoot);

delay(50);

}

//tercer movimento pata izda

for(posleftthigh = 90; posleftthigh >= 45; posleftthigh--)

{

leftthigh.write(posleftthigh);

delay(50);

}

//cuarto movimiento pata izda

for(posrightfoot = 0; posrightfoot <= 90; posrightfoot++)

{

rightfoot.write(posrightfoot);

delay(50);

}

//quinto movimiento pata izda

for(posleftfoot = 45; posleftfoot <= 90; posleftfoot++)

{

leftfoot.write(posleftfoot);

delay(50);

}

//sexto movimiento pata izda

for(posleftthigh = 45; posleftthigh <= 90; posleftthigh++)

{

leftthigh.write(posleftthigh);

delay(50);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote