-7

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;
    }
}
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196

2 Answers2

1

Currently int eyes=0, antennas=0, legs=0, wings=0, head=0, body=0; are local variables inside Die::process, this means that they are bound to the scope in which they are declared.

Every time you execute the function they will be initialized to 0, and there's no way of maintaining state between function calls as it is currently written.


If you want the variables to be bound to the instance of Die itself you should add them as data-members (1), such as in the below example.

This way the values will remain between calls to member-functions within Die.

class Die
{
    private:
        int num;
        int eyes, antennas, legs, wings, head, body; // (1)

    public:
        int roll();
        int getNum();
        void process();

        Die () : eyes (0), antennas (0), legs (0), wings (0), head (0), body (0) { }; (2)
};

Above we are using the constructor of class Die to initialize them all to 0 (2) upon creating an instance of Die, and remove the declarations of the variable names in question from within void Die::Process ().

Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
1

You are declaring the variables as local to the function. That is why they are being reset every time you call the function. To preserve the values between function calls, you have to move the variables into members of the class instead.

Your Die class is also not well designed, either. You should be seeding the generator only one time, so do it in the constructor, and then create the Die object once, not on every loop iteration. And you are not using the last roll in the new roll, so there is no need to have a num member in the class.

You should also separate the beetle logic from the Die class. A dice does not control a game. A game uses a dice to make decisions. Your code should reflect that.

Try something more like this instead:

#include <iostream>
#include <ctime>
#include <stdlib.h>

using namespace std;

class Dice
{
public:
    Dice();
    int roll();
};

class Beetle
{
private:
    int eyes;
    int antennas;
    int legs;
    int wings;
    int head;
    int body;

public:
    Beetle();
    void takeATurn(Dice &dice);
};

Beetle::Beetle() :
    eyes(0),
    antennas(0),
    legs(0),
    wings(0),
    head(0),
    body(0)
{
}

Dice::Dice() // initialize the dice
{
    srand((unsigned)time(0));
}

int Dice::roll() //roll one dice to get random number
{
    int num = (rand() % 6)+1;
    cout << num;
    return num;
}

void Beetle::takeATurn(Dice &dice) //player is building the beetle
{
    int num = dice.roll();

    if ((num != 6) && (body < 1))
    {
        cout << endl << "You need a body first";
        return;
    }

    switch (num)
    {
        case 1:
            if (eyes < 2)
            {
                eyes++;
                cout << endl << "You got an eye";
            }
            else
                cout << endl << "Sorry, the beetle has only 2 eyes!";
            break;
        case 2:
            if (antennas < 2)
            {
                antennas++;
                cout << endl << "You got an antenna";
            }
            else
                cout << endl << "No more than 2 antennas";
            break;
        case 3:
            if (legs < 6)
            {
                legs++;
                cout << endl << "You got a leg";
            }
            else
                cout << endl << "Opps, you can't have more legs!";
            break;
        case 4:
            if (wings < 2)
            {
                wings++;
                cout << endl << "You got a wing";
            }
            else
                cout << endl << "nope, the beetle got 2 wings only";
            break;
        case 5:
            if (head < 1)
            {
                head++;
                cout << endl << "You got the head";
            }
            else
                cout << endl << "One head is enough";
            break;
        case 6:
            if (body < 1)
            {
                body++;
                cout << endl << "You got the body";
            }
            else
                cout << endl << "You got the body already";
            break;
        }
    }
}  

int main()
{
    int n = 0;
    cout << "start?";
    cin >> n;
    if (n == 1)
    {
        Dice dice1; 
        Beetle beetle;

        do
        {
            beetle.takeATurn(dice1);
            cout << endl << "Repeat?: ";
            cin >> n;
        }
        while (n == 1);
    }

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770