Jump to content
  • Advertisement
Sign in to follow this  
link161

First game ^_^

This topic is 4054 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok I've been learning C++ for about a week now and decided to see if I had really learned any thing, so I made this. Please, all suggestions are welcome. I plan on adding more to it as I learn and I just want to make sure this is a good way to start ^_^ I do have some questions about some things I'll ask a bit later so thanks! Source:
/*Monster prototype:   Coby ^_^      ver, 1.00 5/11/2007   
                                     ver, 1.01 5/12/2007 Added death,fixed life function  
                                     ver, 1.02 5/16/2007 Added random event to feed and walk            */    
#include<iostream>
#include<string>
#include<ctime>
using namespace std;

    
int monster(int a,int b);

int main(){
    string mname; 
    int health =100;
    int hunger =100;
    int life;
    int choice;
    int quit = 0;
    int randn;
    
    cout<<"Hello! Welcome to monster!\n";
    cout<<"What will your monsters name be? \n";
    cin>>mname;
    cout<<mname<<" huh? sounds great!\n";
   
  do{
                   life=monster(hunger,health);
    cout<<"\nWhat would you like to do with "<< mname <<" ?\n1-Feed \n2-Walk\n3-Veiw health\n";
    cin>>choice;
    switch(choice){
                   case 1:
                        cout<<"You fed "<<mname<<"!\n";
                        hunger +=10;
                        health -=9;
                        srand(time(NULL));
                        randn=rand()%40+1;
                        if(randn==10){
                                    cout<<"\nYou Got Food Poisoning!\n";
                                    hunger -=50;
                                    life-=20;
                                    cout<<"Hunger: "<<hunger<<"\nLife: "<<life<<"\n";
                                    }
                       cout<<"Your Hunger is: "<<hunger<<"\n\n";
                        break;
                        case 2:
                        cout<<"You took "<<mname<<" for a walk!\n";
                        health+=10;
                        hunger-=9;
                        srand(time(NULL));
                        randn=rand()%40+1;
                        if(randn == 10){
                                 health-=50;
                                 life-=20;
                                 cout<<"You Broke Your Leg!/nFitness: "<<health<<"\nLife: "<<life<<"\n";}
                        cout<<"Your Fittness is: "<<health<<"\n\n";
                        break;
                        case 3:
                             cout<<"Your current health is: "<<life<<"\nfittness "<<health<<"\nHunger"<<hunger<<"\n";
                             break;
                             default:
                                     cout<<"Please enter 1,2, or 3!";
                                     }}while(quit==0 && life>0 && hunger>-129 && health>-129);  
                                    
                                     if(life<0 ||hunger<-130||health<-130){
                                                cout<<mname<<" has died!\n";
                                                }
                                                
end:                        
system("pause");

                   }
int monster(int a,int b)
{
    int life;
    life = (a+b)/2;

    return life;
}
[Edited by - link161 on May 17, 2007 2:34:00 AM]

Share this post


Link to post
Share on other sites
Advertisement
I'm way too tired right now to actually read it and give proper advice, but one thing that caught my eye while scrolling: You only need to call srand once in your application, probably right at the start. It just sets a seed (time in this case) that ensures rand doesn't return the same sequence everytime you run your app.

Share this post


Link to post
Share on other sites
congratz on your first game!!!

Next time use [ source ] and [ /source ] without the spaces.

Share this post


Link to post
Share on other sites
Seems like a nice game for a beginner :)
One thing that caught my eye though is the system("PAUSE"); line. Using that is not recommended as it tends to eat all available CPU. Instead I'd advice to use cin.get(); It's basically the same thing, only that you won't get that terminate text.

Share this post


Link to post
Share on other sites
There was one thing that I noticed. You have in your variable declarations and also in your while loop condition "quit", but nowhere else in the program is there anything that modifies the value of the variable. I think this just simply got overlooked, but is important none the less if someone wants to stop playing. :D :D Maybe make it option/case number zero or something so that when choice equals 0 it sets quit to 1 etc...

Anyhow good luck with your future games/programs. Just keep on going and one day you can be making monsters attack, run, make scary faces, beg for cookies, and all sorts of things :) Too bad you can't make them program for you the game of your dreams though :| Well not without it being way more trouble than it's worth anyways, haha. Good night all.

Share this post


Link to post
Share on other sites
Source:

/*Monster prototype:   Coby ^_^      ver, 1.00 5/11/2007   
ver, 1.01 5/12/2007 Added death,fixed life function
ver, 1.02 5/16/2007 Added random event to feed and walk */

#include<iostream>
#include<string>
#include<ctime>
using namespace std;


int monster(int a,int b);

int main(){
string mname;
int health =100;
int hunger =100;
int life;
int choice;
int quit = 0;
int randn;

cout<<"Hello! Welcome to monster!\n";
cout<<"What will your monsters name be? \n";
cin>>mname;
cout<<mname<<" huh? sounds great!\n";

do{
life=monster(hunger,health);
cout<<"\nWhat would you like to do with "<< mname <<" ?\n1-Feed \n2-Walk\n3-Veiw health\n";
cin>>choice;
switch(choice){
case 1:
cout<<"You fed "<<mname<<"!\n";
hunger +=10;
health -=9;
srand(time(NULL));
randn=rand()%40+1;
if(randn==10){
cout<<"\nYou Got Food Poisoning!\n";
hunger -=50;
life-=20;
cout<<"Hunger: "<<hunger<<"\nLife: "<<life<<"\n";
}
cout<<"Your Hunger is: "<<hunger<<"\n\n";
break;
case 2:
cout<<"You took "<<mname<<" for a walk!\n";
health+=10;
hunger-=9;
srand(time(NULL));
randn=rand()%40+1;
if(randn == 10){
health-=50;
life-=20;
cout<<"You Broke Your Leg!/nFitness: "<<health<<"\nLife: "<<life<<"\n";}
cout<<"Your Fittness is: "<<health<<"\n\n";
break;
case 3:
cout<<"Your current health is: "<<life<<"\nfittness "<<health<<"\nHunger"<<hunger<<"\n";
break;
default:
cout<<"Please enter 1,2, or 3!";
}}while(quit==0 && life>0 && hunger>-129 && health>-129);

if(life<0 ||hunger<-130||health<-130){
cout<<mname<<" has died!\n";
}

end:
system("pause");

}
int monster(int a,int b)
{
int life;
life = (a+b)/2;

return life;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by HomerSp
One thing that caught my eye though is the system("PAUSE"); line. Using that is not recommended


Correct.

Quote:
as it tends to eat all available CPU.


Eh? No, it doesn't. It's not recommended because:

1. It's not standard (not all computers have a program called 'pause') EDIT: Clarification -- yes, system() is standard but 'pause' is not
2. It's not safe -- the program 'pause' might well be replaced with one elsewhere in the $PATH directory which eats the user's children
3. It's ugly -- programs like this are meant to be run from a command shell, so it goes against the principle of least surprise; it's better just to learn to use a command shell (or a batch script, if nothing else

[Edited by - TheUnbeliever on May 16, 2007 12:59:50 PM]

Share this post


Link to post
Share on other sites
Original post by TheUnbeliever
Quote:
Original post by HomerSp
One thing that caught my eye though is the system("PAUSE"); line. Using that is not recommended


Correct.

Quote:
as it tends to eat all available CPU.


Eh? No, it doesn't. It's not recommended because:

1. It's not standard (not all computers have a program called 'pause')/quote]

I take issue with that only because that function is from the stdlib.h library, so if that is loaded then the PAUSE functionality is also there. N'est-ce pas?

Share this post


Link to post
Share on other sites
system() is pure, 100% grade-A standard C.

However, everything you put in the quotes is completely platform specific. system() basically passes the string to the underlying shell; if you compile and run on Linux, for example, the program will not pause at the end, instead you'll likely get a message to the effect of "pause: Command not found."

Basically, while system() itself must exist and support a certain well-defined behavior (determine if a command processor exists if passed a null pointer), it can do pretty much anything it wants (including behavior inconsistently, in an undefined manner, or outright terminate) when passed a non-null pointer. It's a bad idea to use it in general, and it's just a hack to use it to "keep the window open" as the OP is probably doing here (use the appropriate command from the IDE to hold the window open, it's usually "Start without Debugging").

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
system() is pure, 100% grade-A standard C.

However, everything you put in the quotes is completely platform specific. system() basically passes the string to the underlying shell; if you compile and run on Linux, for example, the program will not pause at the end, instead you'll get a message to the effect of "pause: Command not found."


@Alpha_ProgDes -- That was what I meant. (Also, I've gone back to fix my quote tags)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!