Bunny Exercise .

http://www.cplusplus.com/forum/articles/12974/


I have been spending some times on working on this exercise.
I have done some basic functions and some structures to it.
However, Im stuck at this point:

//These are the code I've got so far. And it all works. What I am stuck at is what to do next, how to create new generation of bunny after the first 5.


//Any suggestion on developing this exercise would be deeply appreciated. Even any discussion would be a big help too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//Bunny.h

#include <string.h>

using namespace std;

static char* NameArr[] =
{
       "A",
       "B",
       "C",
       "D",
       "E",
       "F",
       "G",
       "H",
       "I",
       "J",
       "K",
       "L",
       "M",
       "N",
       "O",
       "P",
       "Q",
       "R",
       "S",
       "T",
       "U",
       "V",
       "W",
       "X",
       "Y",
       "Z"
};

static char* SexArr[] =
{
       "MALE",
       "FEMALE"
};

static char* ColorArr[] =
{
       "WHITE",
       "BLACK",
       "SPOTTED",
       "BROWN"
};

typedef enum t_Name 
{
        A = 0,      // 0
        B,          // 1   
        C,          // 2
        D,          // 3
        E,          // 4
        F,          // 5
        G,          // 6
        H,          // 7
        I,          // 8
        J,          // 9
        K,          // 10
        L,          // 11
        M,          // 12
        N,          // 13
        O,          // 14
        P,          // 15
        Q,          // 16
        R,          // 17 
        S,          // 18
        T,          // 19
        U,          // 20
        V,          // 21
        W,          // 22
        X,          // 23
        Y,          // 24
        Z,          // 25
        MAX_NAME    // 26
};

typedef enum t_Sex
{
        MALE = 0,   // 0
        FEMALE,     // 1
        MAX_SEX     // 2   
};

typedef enum t_Color
{
        WHITE = 0,  // 0
        BLACK,      // 1
        SPOTTED,    // 2
        BROWN,      // 3   
        MAX_COLOR   // 4   
};

class Rabbit
{
      public:
                //Constructor
                Rabbit();
                //Destructor
                ~Rabbit();
                
                //Get Function
                char* GetName()     {return NameArr[Name];}
                char* GetColor()    {return ColorArr[Color];}
                char* GetSex()      {return SexArr[Sex];}
                int  GetAge()       {return Age;}
                bool GetEvil()      {return RadioActive;}
                t_Name MyNameIs()   {return Name;}
                t_Color MyColorIs() {return Color;}
                t_Sex MySexuality() {return Sex;}
                
                //Set Function
                void SetName(t_Name NewName);
                void SetColor(t_Color NewColor);
                void SetSex(t_Sex NewSex);
                void SetAge(int age);
                void BeEvil(bool Demon);
                
                //Main functionalities
                void GetOld();
                void Die();
                void TurnVampire();              
                void Print();
                int  Count(); 
                void IAmGod(); //Kill off half of the population //Power of God
                
                //Point to next Rabbit
                Rabbit* Next;
                
      private:
                t_Name Name;
                t_Color Color;
                t_Sex Sex;
                int Age;
                bool RadioActive;
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//Bunny.cpp

#include <cstdlib>
#include <iostream>
#include <cstddef>
#include "Bunny.h"
using namespace std;


static Rabbit* head;
static Rabbit* First;
static Rabbit* New;
static Rabbit* Last;

//***************************************************************************************
// RABBIT CLASS FUNCTION
//***************************************************************************************

/* Constructor */
Rabbit::Rabbit()
{              
         //Generating random name, color, sex
         t_Name name = t_Name(rand() % MAX_NAME);        
         t_Color color = t_Color(rand() % MAX_COLOR);
         t_Sex sex = t_Sex(rand() % MAX_SEX);
                   
         //Assign random name, color, sex
         Name = name;
         Color = color;
         Sex = sex;
       
         //New born baby age 1
         Age = 1;
       
         //any luck being a mutant
         int low = 0;
         int high = 99;
         int range = (high - low) + 1;
         int Luck = low + int(range*rand()/(RAND_MAX+1));
         if (Luck < 2)  //2% of being born a mutant
         {
                  RadioActive = true;     
         }
         else
         {
                  RadioActive = false;
         }
         Next = NULL;
}


/* Destructor */
Rabbit::~Rabbit()
{
                  
}

/* Set Age */
void Rabbit::SetAge(int age)
{
     Age = age;
}

/* Set Name */
void Rabbit::SetName(t_Name NewName)
{
     Name = NewName;
}

/* Set Color */
void Rabbit::SetColor(t_Color NewColor)
{
     Color = NewColor;         
}

/* Set Sex */
void Rabbit::SetSex(t_Sex NewSex)
{
     Sex = NewSex;                     
}

/* Be Evil */
void Rabbit::BeEvil(bool Demon)
{
     RadioActive = Demon;
}

/* Get Old */
void Rabbit::GetOld()
{
     int CurrentAge;
     int NewAge;
     CurrentAge = GetAge();
     NewAge = CurrentAge + 1;
     this->SetAge(NewAge);
}

/* Turn Vampire */
void Rabbit::TurnVampire()
{
     this->BeEvil(true);
}

/* Die */
void Rabbit::Die()
{    
     Rabbit* TempPtr;
     Rabbit* DelPtr;

     //Pointer to top of the linked list
     TempPtr = head;          
     
     if (TempPtr->Next != this->Next)
     {
         TempPtr = TempPtr->Next;
     }
     
     if (TempPtr != head)
     {
         DelPtr = TempPtr->Next;
         TempPtr->Next = DelPtr->Next;
     }
     else
     {
         DelPtr = TempPtr;
         head = DelPtr->Next;
     }
     
     delete DelPtr;
     
     cout << "Poor " << this->GetName() << " is DEAD" << endl; 
}

/* Print */
void Rabbit::Print()
{
     cout << "Bunny " 
          << this->GetName()  << " was born" << endl
          << this->GetName()  << " ages " 
          << this->GetAge()   << endl
          << this->GetName()  << " is " 
          << this->GetColor() << " and is " 
          << this->GetSex()   << endl;
          
     
     if (this->GetEvil() == true)
     {
         cout << this->GetName() << " is EVIL" << endl;
     }
     
     cout << endl;
}

/* I Am God */
void Rabbit::IAmGod()
{
     
}


//***************************************************************************************
// MAIN FUNCTION
//***************************************************************************************
int main()
{   
    int Population = 0;
    
    //Seed a random
    srand((unsigned)time(0));  

    //First Rabbit Born    
    First = new Rabbit();
    head = First;
    Population++;              //Population: 1

    //Following by 2 3 4 5
    Last = First;
    int i = 4;
    while (i != 0)
    {
          New = new Rabbit();  
          Last->Next = New;
          Last = New;
          i--;
          Population++;        //Population: 5
    }

    Rabbit* localDude = First;
    
    //Loop through the whole list.
    do
    {
           localDude = localDude->Next;
    }while (localDude != NULL);
    
    cout << Population << endl;
    
    cin.get();
    return 0;
};
@ThangDo

Very impressive program. But, how do I see what the Rabbits are? All it does is print the Population as 5, but I don't see the types, names, etc.
ah rite. because I took out the part where it print out the rabbits in main()

1
2
3
4
5
6
7
8
Rabbit* localDude = First;
    
    //Loop through the whole list.
    do
    {
           //localDude->Print(); //This part was taken out so it didnt display anything 
           localDude = localDude->Next;
    }while (localDude != NULL);


Do you have any suggestion ?
@ThangDo

I fooled around a bit with it, and came up up with this code.
1
2
3
4
5
6
 //Loop through the whole list.
    do
    {
           localDude->Print();
	   localDude = localDude->Next;
    }while (localDude);

Once, a bunny came up as 'EVIL', and a few times two rabbits had the same name. I changed the while (localDude != NULL); because I kept getting a run error with it. Now it doesn't.
Well,
since I used only 1 array of name (from A-Z), it is normal that 2 or more rabbit come up with the same name .

EVIL rabbit is created in 2% of chance, so its not very often to see that evil bastard .lol

about
while (localDude != NULL);
it seems to work fine for me somehow?

I'll give it a try with your chance.

If you wanna test the die() function also, you can hard-code it to kill off one of two rabbits, even all.


I've been trying to do the IAmGod() which kills off half the population, but its not going anywhere yet. Linked list is a real pain in the butt
Sorry ThangDo..

I first had the Print() located AFTER the ->Next statement. That's why I kept getting the error. I changed the do/while back to the original posting, and I don't get the error. But I'm going to leave the NULL off anyway, as I feel it makes it easier to understand, for me at least.
I'm not good enough at C++ programming to understand linked lists. But, hopefully by studying your program, I will be able to better understand it a bit. Looking forward to seeing if you add more to this. I have no idea at present, how to get them to age, etc.
After each turn in
1
2
3
4
5
6
7
8
9
10
11
12
//Loop through the whole list.
    do
    {
           localDude->Print();

           //Do something
           //Do something

           //Get them to age
           localDude->GetOld();
	   localDude = localDude->Next;
    }while (localDude);


I guess it is simple as that.
@ThangDo

I made a loop to get the bunnies to age, and it stops when the population is three. But I find that after one dies, that name is still there, and the bunny before or after it is gone. So, if the names are 'A','B','C','D','E', and it show 'D' dies, the ones left show as 'A','B','D','E' or sometimes 'A','C','D','E'. And sometimes, actually quite often, it'll show one dies, and the crashes. I think because it's trying to remove a bunny that isn't there.
Here is the code I'm using. Maybe I have it wrong, and that's the reason. Not quite sure..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
do
	{
		Rabbit* localDude = First;
		ClearScreen();
		
		//Loop through the whole list.
		do
		{
			localDude->Print();
			localDude->GetOld();
			die = rand()%100;
			if (die < 3) // 3% chance of bunny dying
			{
				Population--;
				localDude->Die();
				cout << "\n*** Poor " << localDude->GetName() << " is DEAD ***\n_________________________________" << endl << endl;
				Sleep(3000); // So I see what's on screen 
			}
			localDude = localDude->Next;
		}while (localDude != NULL);

		Sleep(2000);  // So I see what's on screen
	} while (Population > 3);

hah , you are rite .
I didnt see it before . I must've changed something at some points and messed it up .

It's like it is deleting something which is not there to be deleted.


Then problem lies somewhere in the logic of this function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Die */
void Rabbit::Die()
{    
     Rabbit* TempPtr;
     Rabbit* DelPtr;

     //Pointer to top of the linked list
     TempPtr = head;          
     
     if (TempPtr->Next != this->Next)
     {
         TempPtr = TempPtr->Next;
     }
     
     if (TempPtr != head)
     {
         DelPtr = TempPtr->Next;
         TempPtr->Next = DelPtr->Next;
     }
     else
     {
         DelPtr = TempPtr;
         head = DelPtr->Next;
     }
     
     delete DelPtr;
     
     cout << "Poor " << this->GetName() << " is DEAD" << endl; 
}
There's a mistake at line 82:
1
2
3
4
5
6
typedef enum t_Sex
{
        MALE = 0,   // 0
        FEMALE,     // 1
        MAX_SEX     // 2   
};

Male should always be 1.

I'm surprised it even compiles this nonsense!
How the heck should it always be 1 ?

It starts off with 0 value, dude.
Moreover, you can set the value to each one of them as well .

You mite wanna check it out again.
Do that before you say its nonsense !
...that was a joke. Because 1 > 0. It's the C++ way of saying Male > Female. Ah, forget it.
Holly hell .

that was a joke ?
Thats weird, cuz I've been hearing people telling me programming jokes all day long .

All the weird ones like the boolean joke.
Topic archived. No new replies allowed.