Overview write a program that rebuilds a robot over and over using a computer ge
ID: 3908533 • Letter: O
Question
Overview
write a program that rebuilds a robot over and over using a computer generated dice to determine what pieces to add,
then count the number of dice rolls needed to build a single robot.
Robot parts table
if dice roll is
you may add this part
if you already have this number of parts
add no more than this amount
1
foot
nothing needed
2
2
leg
A free Foot. The number of Feet is > the number of Legs
2
3
torso
2 legs
1
4
arm
torso
2
5
hand
A free Arm. The number of Arms is > the number of Hands
2
6
head
torso
1
7
antenna
head
1
8
laser
hand
1
Requirements:
A function that generates the correct dice roll, either with a return value or use a reference parameter.
A function that determines if the robot is complete. To receive any credit, this function CANNOT use any global variables, all the information required must be passed as parameters and it must return a bool .
A function (not main()!) that completes one robot. This is where the variables are declared. This function must call the function that generates the random number and the function the determines if the robot is complete.
main() will call the function that completes one robot. It has to have a loop so I can make robot after robot as long as I want. "Enter 'y' to make another robot"
Output:
The program MUST report all the robot part counts and dice roll count after each dice roll. It can just scroll by if you want or you can ask that 'return' be pressed.
After a robot is built, it MUST pause and ask if the user wishes to build another robot. 'Y' will build another robot from the beginning, 'N' will exit the program.
Your comments do not need to match mine exactly.
The parts MUST match exactly.
if dice roll is
you may add this part
if you already have this number of parts
add no more than this amount
1
foot
nothing needed
2
2
leg
A free Foot. The number of Feet is > the number of Legs
2
3
torso
2 legs
1
4
arm
torso
2
5
hand
A free Arm. The number of Arms is > the number of Hands
2
6
head
torso
1
7
antenna
head
1
8
laser
hand
1
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<time.h>
int roll(){
return rand() % 7 + 1;
}
bool isComplete(int a[]){
if (a[0] == 2 && a[1] ==2 && a[2] ==1 && a3[3] == 1 && a[4] == 2 && a[5] == 2 && a[6] == 1 && a[7] == 1 && a[8] == 1)
return true;
else
return false;
}
void completeRobolt(int a[]){
int count = 0;
while (isComplete(a[]){
cout << "Feet:" << a[0] << endl;
cout << "Legs:" << a[1] << endl;
cout << "Torso:" << a[1] << endl;
cout << "Arm:" << a[1] << endl;
cout << "Hand:" << a[1] << endl;
cout << "Head:" << a[1] << endl;
cout << "Antenna:" << a[1] << endl;
cout << "Laser:" << a[1] << endl;
int a = roll();
switch(a){
case 1 : if (a[0] < 2){
a[0]++;
}
break;
case 2 : if (a[0] > a[1] && a[1] < 2){
a[1]++;
}
break;
case 3 : if (a[1] == 2 && a[2] < 0){
a[2]++;
}
break;
case 4 : if (a[2] == 1 && a[3] < 2){
a[3]++;
}
break;
case 5 : if (a[3] > a[4] && a[4] < 2){
a[4]++;
}
break;
case 6 : if (a[5] == 1 && a[5] < 1){
a[5]++;
}
break;
case 7 : if (a[6] == 1 && a[6] < 1){
a[6]++;
}
break;
case 8 : if (a[6] == 1 && a[7] < 1){
a[7]++;
}
break;
}
}
}
int main(){
int robot[8];
for (int i = 0;i<8; i++){
robot[i] = 0;
}
while(true){
completeRobol(robot);
cout << "Do you want to build another robot (Y/N) :";
string ch;
cin >> ch;
if (ch[0] == 'N')
break;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.