Sep 24, 2013 at 3:02pm UTC
Um, what? This feels like coming in at the end of a conversation. Did you accidentally delete some of your explanatory text?
To answer your final comment - bool
is a type that can only take one of two values: true
and false
.
Last edited on Sep 24, 2013 at 3:03pm UTC
Sep 24, 2013 at 4:38pm UTC
Well here's a simple way to test if the Critter is still alive or dead, if it's dead then the program will display 'Game Over' and exit.
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
#include <iostream>
using namespace std;
class Critter
{
public :
Critter(int hunger = 0 , int boredom = 0);
void talk();
void eat(int food = 4);
void play(int fun = 4);
void getcurrentstats();
private :
bool DeadorAlive;
int m_Hunger;
int m_Boredom;
void passtime (int time = 1);
int getmood() const ;
};
void Critter::getcurrentstats()
{
cout<<"My hunger level is " <<m_Hunger;
cout<<"\nand my boredom level is " <<m_Boredom<<endl;
if ( m_Hunger > 15 && m_Boredom > 15 )
{
DeadorAlive = true ;
}
if ( DeadorAlive == true )
{
cout<<"You have failed to take care of your Critter.\n" ;
cout<<"Game over" <<endl;
exit(0);
}
}
Critter::Critter(int hunger, int boredom):
m_Hunger(hunger) , m_Boredom(boredom)
{
}
int Critter::getmood() const
{
return (m_Hunger + m_Boredom);
}
void Critter::passtime(int time)
{
m_Hunger += time;
m_Boredom += time;
}
void Critter::talk()
{
cout<<"Hi! I'm a Critter and i'm feeling " ;
int mood = getmood();
if ( mood > 15 )
{
cout<<"Mad!\n" ;
}
if ( mood > 10 )
{
cout<<"Frustrated\n" ;
}
if ( mood > 5 )
{
cout<<"okay\n" ;
}
else
{
cout<<"Happy!\n" ;
}
passtime();
if ( m_Hunger > 15 && m_Boredom > 15 )
{
DeadorAlive = true ;
}
if ( DeadorAlive == true )
{
cout<<"You have failed to take care of your Critter.\n" ;
cout<<"Game over" <<endl;
exit(0);
}
}
void Critter::eat(int hunger)
{
cout<<"Num num num\n" ;
m_Hunger -= hunger;
if ( m_Hunger < 0 )
{
m_Hunger = 0;
}
passtime();
if ( m_Hunger > 15 && m_Boredom > 15 )
{
DeadorAlive = true ;
}
if ( DeadorAlive == true )
{
cout<<"You have failed to take care of your Critter.\n" ;
cout<<"Game over" <<endl;
exit(0);
}
}
void Critter::play(int fun)
{
cout<<"Wheeeeee\n" ;
m_Boredom -= fun;
if ( m_Boredom < 0 )
{
m_Boredom = 0;
}
passtime();
if ( m_Hunger > 15 && m_Boredom > 15 )
{
DeadorAlive = true ;
}
if ( DeadorAlive == true )
{
cout<<"You have failed to take care of your Critter.\n" ;
cout<<"Game over" <<endl;
exit(0);
}
}
int main()
{
Critter Crit;
Crit.talk();
cout<<"\nCritter Caretaker" ;
cout<<"\n-----------------\n\n" ;
int choice;
do
{
cout<<"0 - Quit\n" ;
cout<<"1 - Listen to your Critter\n" ;
cout<<"2 - Feed your Critter\n" ;
cout<<"3 - Play with your Critter\n" ;
cout<<"4 - Show Critter's current stats\n" ;
cout<<"\nChoice: " ;
cin>>choice;
switch (choice)
{
case 0:
cout<<"Thank you for playing\n" ;
break ;
case 1:
Crit.talk();
break ;
case 2:
Crit.eat();
break ;
case 3:
Crit.play();
break ;
case 4:
Crit.getcurrentstats();
break ;
default :
cout<<"Illegal choice, please try again.\n" ;
}
}while ( choice != 0 );
return 0;
}
Last edited on Sep 24, 2013 at 4:38pm UTC