I am making game about a dog, there should be 2 players finishing building a dog
ID: 3541123 • Letter: I
Question
I am making game about a dog, there should be 2 players finishing building a dog. dice rolls that decide which part is selected
After dice rolls number defines the part of dog. After first roll I want to move to second player, after his roll back to first player and so on till all of the parts of dog are created.
I dont know how i should make two player game, swich from first to second and back.
Here is some parts of code:
int dog::addface(){
if (dogarray [0] == 0)
dogarray [0] = 1;
cout<< "You got a body"<<endl; // arrays store the body parts.
return true;
}
int dog::addeyes(){
if (dogarray [1] == 0)
doglearray [1] = 1;
cout<< "You got eyes<<endl;
return true;
}
..........................................................
Explanation / Answer
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<vector>
using namespace std;
class dog
{
private:
int dogarray[10];
public:
dog()
{
for(int i=0; i<10; i++) dogarray[i] = 0;
}
void getpart(int k)
{
switch(k)
{
case 0:addface(); break;
case 1:addeyes(); break;
case 2:addchin(); break;
case 3:add_front_legs(); break;
case 4:add_back_legs(); break;
case 5:add_stomach(); break;
case 6:add_head(); break;
case 7:add_tail(); break;
case 8:add_ears(); break;
case 9:add_mouth(); break;
}
}
int addface();
int addeyes();
int addchin();
int add_front_legs();
int add_back_legs();
int add_stomach();
int add_head();
int add_tail();
int add_ears();
int add_mouth();
bool is_dog_done();
};
bool dog::is_dog_done()
{
for(int i=0; i<10; i++)
if(!dogarray[i]) return false;
return true;
}
int dog::add_mouth()
{
if (dogarray [9] == 0)
dogarray [9] = 1;
cout<< "You got mouth"<<endl; // arrays store the body parts.
return true;
}
int dog::add_ears()
{
if (dogarray [8] == 0)
dogarray [8] = 1;
cout<< "You got ears"<<endl; // arrays store the body parts.
return true;
}
int dog::add_tail()
{
if (dogarray [7] == 0)
dogarray [7] = 1;
cout<< "You got head"<<endl; // arrays store the body parts.
return true;
}
int dog::add_head()
{
if (dogarray [6] == 0)
dogarray [6] = 1;
cout<< "You got head"<<endl; // arrays store the body parts.
return true;
}
int dog::add_stomach()
{
if (dogarray [5] == 0)
dogarray [5] = 1;
cout<< "You got stomach"<<endl; // arrays store the body parts.
return true;
}
int dog::addface()
{
if (dogarray [0] == 0)
dogarray [0] = 1;
cout<< "You got a body"<<endl; // arrays store the body parts.
return true;
}
int dog::addeyes()
{
if (dogarray [1] == 0)
dogarray [1] = 1;
cout<< "You got eyes" <<endl;
return true;
}
int dog::addchin()
{
if (dogarray [2] == 0)
dogarray [2] = 1;
cout<< "You got Chin "<<endl;
return true;
}
int dog::add_front_legs()
{
if (dogarray [3] == 0)
dogarray [3] = 1;
cout<< "You got front legs "<<endl;
return true;
}
int dog::add_back_legs()
{
if (dogarray [4] == 0)
dogarray [4] = 1;
cout<< "You got back legs "<<endl;
return true;
}
int main()
{
dog d1_player;
dog d2_player;
srand(time(NULL));
vector<int> d1_rolls;
vector<int> d2_rolls;
int rand_num =0;
while(true)
{
cout << "Player 1 turn now" << endl;
// PLAYER 1 TURN
rand_num = rand()%10;
d1_player.getpart(rand_num);
if(d1_player.is_dog_done())
{
cout << "player 1 completed first" << endl;
break;
}
// PLAYER 2 TURN
cout << "Player 2 turn now" << endl;
rand_num = rand()%10;
d2_player.getpart(rand_num);
if(d2_player.is_dog_done())
{
cout << "player 2 completed first" << endl;
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.