Thread: How would you guys write this?

  1. #1
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19

    How would you guys write this?

    ok so this is the little turn based game i was working on...and i just got it working...kinda lol but i was curious as to how you guys would make this thing work. im sure you have ways to do the same thing with less code and im interested in learning how to make mine alittle better/cleaner

    here goes
    Code:
    //Written By : Zero_X
    //"Turn Based Battle System"
    //"Works but it is a mess..."
    
    #include<iostream>
    #include<string> 
    
    #define cls system("cls");
    #define pause system("pause");
    
    using namespace std;
    
    int turn =1;
    int main();
    class player1
    {
            public:
                    string name;
                    int classSel;
                    string type;
                    int health;
                    void player1::clSel();
                    
    };
    
    class player2 : public player1{};
    
    void inStats(player1& play1, player2& play2)
    {
         play1.health = 100;
         play2.health = 100;
     }
    void dead(player1& play1,player2& play2)
    {
       string loser,winner;
       if(play1.health <= 0){
       loser = play1.name;
       winner = play2.name;}
       else if(play2.health <=0){
       loser = play2.name;
       winner = play1.name;}
       cls
       cout<<"....As The Dust Settles....\n";
       cout<<"\t"<<loser<<" Has Fallen...\n";
       cout<<"\t\t"<<winner<<" You Are Victorious!!!\n\n";
       cout<<"Thanks For Playing!"<<endl;
       pause;
       main();
    }
    
    void battle(player1& play1, player2& play2)
    {
    
         cls 
         string activeName;
         string activeType;
         int activeHealth;
         string enemyName;
         string enemyType;
         int enemyHealth;
         
         if(play1.health <= 0){
         dead(play1, play2);}
         if(play2.health <= 0){
         dead(play1, play2);}
         
         if(turn == 1){
         activeName = play1.name;
         activeType = play1.type;
         activeHealth = play1.health;
         enemyName = play2.name;
         enemyType = play2.type;
         enemyHealth = play2.health;}
    
         if(turn == 2){
         activeName = play2.name;
         activeType = play2.type;
         activeHealth = play2.health;
         enemyName = play1.name;
         enemyType = play1.type;
         enemyHealth = play1.health;}
    
    	 cout<<"\t\t"<<activeName<<"'s Health: "<<activeHealth;
    	 cout<<"\n\t\t Enemy's Health: "<<enemyHealth<<endl<<endl;
    	 if(activeType == "Knight")
    	 {
             cout<<"1)Stab\n";
             cout<<"2)Slash\n";
             cout<<"3)Power Slash\n";
             cout<<"Please Select Attack: ";
             int atkSel =0;
             cin>>atkSel;
             switch(atkSel){
                            case 1:{
                                 enemyHealth -= 20;
                                 break;}
                            case 2:{
                                 enemyHealth -= 30;
                                 break;}
                            case 3:{
                                 enemyHealth -= 35;
                                 break;}
                            }
                            if(turn == 1){
                                    play2.health = enemyHealth;
                                    turn = 2;}
                            else if(turn == 2){
                                    play1.health = enemyHealth;
                                    turn = 1;}
                            battle(play1, play2);
             }
             
             if(activeType == "Mage")
    	 {
             cout<<"1)Magic Arrow\n";
             cout<<"2)FireBall\n";
             cout<<"3)Falling Stars\n";
             cout<<"Please Select Attack: ";
             int atkSel =0;
             cin>>atkSel;
             switch(atkSel){
                            case 1:{
                                 enemyHealth -= 25;
                                 break;}
                            case 2:{
                                 enemyHealth -= 30;
                                 break;}
                            case 3:{
                                 enemyHealth -= 40;
                                 break;}
                            }
                            if(turn == 1){
                                    play2.health = enemyHealth;
                                    turn = 2;}
                            else if(turn == 2){
                                    play1.health = enemyHealth;
                                    turn = 1;}
                            battle(play1, play2);
             }
      
          
                                 
    	 pause
     
    }
    
    
    
    int main()
    {
        cls
        player1 play1;
        player2 play2;
        inStats(play1, play2);
        cout<<"\n\n\t*------------------------*\n";
        cout<<"\t|  TURN-BASED            |\n";
        cout<<"\t|      BATTLE SYSTEM     |\n";
        cout<<"\t|                        |\n";
        cout<<"\t|         By:  Zero_X    |\n";
        cout<<"\t*------------------------*\n\n\t\t";
        pause
        cls
        cout<<"Please enter player 1's name: ";
        cin>>play1.name;          
        play1.clSel();
        cls
        cout<<"Please enter player 2's name: ";
        cin>>play2.name;          
        play2.clSel();
        cls
        cout<<play1.name<<" The "<<play1.type<<" VS "<<play2.name<<" The "<<play2.type<<"!\n\n";
        pause
        battle(play1,play2);
    }
    
    void player1::clSel()          
    { 
        cls
        cout<<name<<" Please Select a Class...\n";
        cout<<"1)Knight\n";
        cout<<"2)Mage\n";
        cin>>classSel;
        switch(classSel)
        {
            case 1:{
                cls
                type = "Knight";
                cout<<name<<" Has Selected to Be A "<<type<<endl;
                pause
                break;
                }
           case 2:{
                cls
                type = "Mage";
                cout<<name<<" Has Selected to Be A "<<type<<endl;
                pause
                break;
                }
           default:{
                cout<<"ERROR\n";
                classSel = 0;
                clSel();        
                }
         }
    }
    thanks
    Last edited by Zero_X; 04-12-2006 at 07:09 AM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    11
    I don't see anything wrong with it. It's actually pretty nice.

  3. #3
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    hey thanks man, Comments like that make me feel like i might actally be learning some of this c++ stuff lol

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    11
    =)

    on topic: I would actually enjoy that game in a multiplayer version against two computers.

  5. #5
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    heh yeah that would be pretty kool....but a little bit down the road for me

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #define cls system("cls");
    #define pause system("pause");
    be careful with system() and be careful with macros. They're both fairly bad practice. If you are going to use a macro, though, don't stick the semicolon in the macro. It makes your code confusing to others who see a statement cls without a semicolon and it compiling properly.

    In your classes. You shouldn't make data members public. They should be private and only dealt with by some sort of public accessor or mutator function in the class. Also, you don't need to qualify your functions with scope inside the class.

    I'm not gonna look much further right now, I've got some business to take care of. I'm sure there are other things to critique and I'm sure others are willing to do it.
    Sent from my iPadŽ

  7. #7
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Why should you make a prototype for the main() function?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  8. #8
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    i make a call from one of the functions above it, i ran into an error when i had the functions above main() below it. so i found the fastest way to get it running way to prototype it and keep the other functions above it

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    i make a call from one of the functions above it, i ran into an error when i had the functions above main() below it. so i found the fastest way to get it running way to prototype it and keep the other functions above it
    Unfortunately for you, the C++ Standard states that the function main shall not be used with a program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    the C++ Standard states that the function main shall not be used with a program.
    To clarify, you can't call main() in a C++ program (although you can in C).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    i make a call from one of the functions above it, i ran into an error when i had the functions above main() below it. so i found the fastest way to get it running way to prototype it and keep the other functions above it
    You should put prototypes in for the other functions, or main() above them in the file.

    Or better yet, get rid of your main() function call[s].
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Just a couple of points

    1 - Provide a menu that allows players to quit after a game or
    start over

    2 - It'd be cool if the attack points weren't fixed with a mage
    advantage - you could try mixing up the strengths of each choice,
    or switch the fixed strengths of the two types randomly -
    experimenting with rand could make the game a lot more
    playable!

    3 - The console text formatting is a little bit off, but thats only a
    very minor criticism

    all in all, a good program - i don't have much to add with regards
    to the code itself - i'd say that's mostly been covered already.
    One thing that did strike me as odd was the fact that you have
    two player classes, player 2 inherits from one, but no new
    implementations are provided for the derived class i would
    eliminate player 2 and just use a player class - then in main your
    instances of each could be the same type. it just seems a little
    pointless having two identical classes!

    Good luck with any improvements you make
    Last edited by Richie T; 04-12-2006 at 06:33 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  2. Write in many command prompts
    By cfriend in forum Windows Programming
    Replies: 1
    Last Post: 09-15-2004, 01:32 AM
  3. Some humour...
    By Stan100 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-06-2003, 10:25 PM