Post: [C++] Girby's Farm (GAME)
03-13-2011, 09:57 PM #1
Girby2K11
☮ ☯ ☢ ✔ ➝
(adsbygoogle = window.adsbygoogle || []).push({}); Ok basically, im reading a book, and in the book the guy made a critter farm game and i thought that was really cool, so yesterday in under like 30 minutes i made this small game its not currently finished.

I had to use goto's because i miss understood loops, and they turned into infinite which pissed me off. ok anyway heres the code.

    #include <iostream>
#include <cstdlib>

using namespace std;

// Farm Game

void cName()
{
string name;
int clrscrl;

naughty:

cout << "Enter a name: ";

cin >> name;

cout << "You have named your critter: " << "> " << name << " <" << "\n\n";
cout << "Do you want to clear the screen? \n\nPress 1 - Clear. \nPress 2 - Dont Clear\n";
cout << "\nChoice: ";

cin >> clrscrl;

switch (clrscrl)
{
case 1:
{
cout << "Clearing screen.";
_sleep(3000);
system("cls");
return;
}
case 2:
{
cout << "Not clearing.\n\n";
_sleep(2000);
return;
}
}


}

int cPlay(int &play)
{
cout << "Critter's Hunger level has increased by 1\n";
++play;

cout << "Hunger level: " << play << "\n";

if (play == 0)
{
cout << "You critter has had a heart attack...\n\nit is DEAD!\n\nExiting...\n\n";
exit(1);
}

if (play == 1)
{
cout << "Your crit is fullup. Dont feed it anymore!\n\n";
}

if (play == 2)
{
cout << "Your crit feels fine now.\n\n";
}

if (play == 3)
{
cout << "your crit is okay.\n\n";
}

if (play == 4)
{
cout << "your crit wants a tiny bit more food - but not too much.\n\n";
}

if (play == 5)
{
cout << "your crit is 50/50 with hunger.\n";
}

if (play == 6)
{
cout << "Your crit says that it will be quite hungry if you \ncontinue playing with it\n\n";
}

if (play == 7)
{
cout << "Your crit wants to eat some food.\n";
}

if (play == Cool Man (aka Tustin)
{
cout << "Your crit is hungry\n";
}

if (play == 9)
{
cout << "Your crit is showing signs of headaches and sickness\nGive it some food\n";
}

if (play == 10)
{
cout << "Your crit has collapsed, and has gone cold...\nSorry its DEAD!";
exit(1);
}
}

int cHunger(int &hunger)
{
cout << "Critter's Hunger level has decreased by 1\n\n";
--hunger;

cout << "Hunger level: " << hunger << "\n";

if (hunger == 0)
{
cout << "You critter has had a heart attack...\n\nit is DEAD!\n\nExiting...\n\n";
exit(1);
}

if (hunger == 1)
{
cout << "Your crit is fullup. Dont feed it anymore!\n\n";
}

if (hunger == 2)
{
cout << "Your crit feels fine now.\n\n";
}

if (hunger == 3)
{
cout << "your crit is okay.\n\n";
}

if (hunger == 4)
{
cout << "your crit wants a tiny bit more food - but not too much.\n\n";
}

if (hunger == 5)
{
cout << "your crit is 50/50 with hunger.\n";
}

if (hunger == 6)
{
cout << "Your crit says that it will be quite hungry if you \ncontinue playing with it\n\n";
}

if (hunger == 7)
{
cout << "Your crit wants to eat some food.\n";
}

if (hunger == Cool Man (aka Tustin)
{
cout << "Your crit is hungry\n";
}

if (hunger == 9)
{
cout << "Your crit is showing signs of headaches and sickness\nGive it some food\n";
}

if (hunger == 10)
{
cout << "Your crit has collapsed, and has gone cold...\nSorry its DEAD!";
exit(1);
}

}



int main()
{
int select;

cout << "Welcome to Girby's Farm. Select one of the following:\n\n";

tryagain:

cout << "Grow a Critter: Press 1\n\n";
cout << "Choice: ";

cin >> select;


switch (select)
{
case 1:
{
cout << "You have grown a Critter: Choose a name for it.\n\n";
cName();
break;
}
default:
cout << "Sorry but you have chosen an incorrect choice - try again\n\n";
goto tryagain;
}


int select2;
int cHungerLev = 5;


cLife:


cout << "What would you like to do with your new critter?\n\n";

cout << "Feed: Press 1\n";
cout << "Play: Press 2\n";
cout << "Check Hunger Level: Press 3\n";
cout << "Clear Screen: Press 4\n\n";

cout << "Choice: ";

cin >> select2;

system("cls");


if (select2 == 1)
{
cHunger(cHungerLev);
}

if (select2 == 2)
{
cPlay(cHungerLev);
}

if (select2 == 3)
{
cout <<"Hunger Level = " << cHungerLev << "\n";
}

if (select2 == 4)
{
cout << "Clearing Screen.";
_sleep(3000);
system("cls");
}

goto cLife;

return 0;
}
03-14-2011, 09:43 PM #11
Girby2K11
☮ ☯ ☢ ✔ ➝
Originally posted by TheUberFail View Post
at the very least wrap parts in

for(;Winky Winky
{


}

and use break/continue;


ahhh, that is what used the for (;Winky Winky but it kept infinite looping, but i forgot the break.
03-15-2011, 04:42 PM #12
vSaBoTeuR x
< ^ > < ^ >
Originally posted by kiwimoosical View Post
continue;
Skips through the rest of a loop suddenly.

break;
Ends a loop suddenly.

return;
Ends a function (void) suddenly.

There are better ways of doing what you are doing in most occasions without interrupting the process of the loop.


There is no way around the fact that break; is one piece of a damn handy code. It makes my life alot easier, and will stop the further rape of if-statements. (or whatever they're called in C++, I'm a webdeveloper (PHP))
03-15-2011, 04:53 PM #13
malanof5
Error… Cat invasion!
Lol I am making actually a unfinished game which will be like star wars lol.
03-19-2011, 06:54 AM #14
XDev
Banned
hahah very nice. i like this game

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo