PROBLEM DESCRIPTION
I am trying to write an implementation of the beetle game for kids. The following code is initializing wings
, legs
, head
, etc, to zero whenever I call the function.
Where should the initialization be so that I can increment wings
, legs
, head
, etc, without losing their value upon calling the function a second time?
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
class Die
{
private:
int num;
public:
int roll();
int getNum();
void process();
};
int Die::roll() //roll one dice to get random number
{
srand((unsigned)time(0));
num=(rand() % 6)+1;
cout<<num;
}
int Die::getNum()
{
return num;
}
void Die::process() //player is building the beetel
{
int eyes=0, antennas=0, legs=0, wings=0, head=0, body=0;
if (eyes>2)
cout<<"Sorry, the beetle has only 2 eyes!";
else if (antennas>2)
cout<<"No more than 2 antennas";
else if (wings>2)
cout<<"nope, the beetle got 2 wings only";
else if (head>1)
cout<<"One head is enough";
else if (body>1)
cout<<"You got the body already";
else if (legs>6)
cout<<"Opps, you can't have more legs!";
else
{
switch (num)
{
case 1:
cout<<"You got an eye";
eyes++;
cout<<eyes;
break;
case 2:
cout<<"\nYou got an antenna";
antennas++;
break;
case 3:
cout<<"\nYou got a leg";
legs++;
break;
case 4:
cout<<"You got a wing";
wings++;
break;
case 5:
cout<<"\nYou got the head";
head++;
break;
case 6:
cout<<"\nYou got the body";
body++;
break;
}
}
}
int main()
{
int n=0;
cout<<"start?";
cin>>n;
while (n==1)
{
Die dice1;
dice1.roll();
dice1.getNum();
dice1.process();
cout<<"\nRepeat?: ";
cin>>n;
}
}