Thread: C++ Text game code

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Buxton UK
    Posts
    14

    C++ Text game code

    Hello.
    I have been experimenting with some code iv found and iv made a 2-player game. I am just learning how to write code and this is the first project I have done that can be expanded and changed.

    This is definitely not finished and im sorry if its a bit sloppy and the comments don't make sense. Iv been too busy learning and having fun to keep tidy. You should see my flat!

    I plan to add the following later on:

    menu's and to be able to pick character.

    new characters like mage or wizard.

    basic AI for single player game.

    Any comments or suggestions would be awesome.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {   
        int damage = 0;
        int player1HP = 100;
        int player2HP = 100;
        int player1MP = 0;
        int player1ATK = 2;
        int player2ATK = 2;
        int potionSTR = 3;
        int potionNUM1 = 3;
        int potionNUM2 = 3;
        int turn = 1;
        char enter;
    
    
        //Combat
    
        cout << "welcome to Atco's savage fighting game!\n\nEnter 1: ";
        cin >> enter;
        
        if (enter == '1')
             cout << system("CLS");
        else system ("CLS");
    
        while (player1HP > 0 && player2HP > 0)
        {
            system("CLS");
            cout << "Berserker Warrior's HP = " << player1HP << endl;
            cout << "Berserker Warrior's rage = " << player1MP << endl;
            cout << "Shadow Priest's HP = " << player2HP << endl;
            cout << "damage is " << damage << endl;
    
            //Player 1's Turn
            if(turn == 1)
            {
               cout << "\nWarrior's Turn.\n" << potionNUM1  << " potion's left." << "\nY = Bash, N = Zerg Potion, X =  Special Attack, M for devastate: ";
                                                    cout <<       "               " << "\n                               (cost's 5  rage) (cost's 20 rage)";             
               char player1Attack;
               cin >> player1Attack;
               //Player 1 Attacks plus gains 2 health       P1attck
               if(player1Attack == 'y')
               {
                   player2HP = player2HP - player1ATK;
                   player1HP = player1HP + 2;
                   damage = player1ATK;
                   if(player2HP <= 0)
                    {
                        player2HP = 0;
                    }
                   player1MP = player1MP + 2;
                   turn = 2;
               }
               //Player 1 Uses Potion                       P1 attck potion
               else if (player1Attack == 'n')
               {
                   if (potionNUM1 >= 1)
                   {player1ATK = player1ATK + potionSTR;
                   potionNUM1--;
    
                   if (player1HP > 51)
                       player1HP = 75;
                   turn =2;}
                   
                   if (potionNUM1 <= 0)
                   turn = 2;
                       
                   player1MP = player1MP + 2;
                   damage = 0;
                   turn = 2;
               }
               //Player 1 Special attck                     5 RAGE
               else if(player1Attack == 'x')
               {
                   if (player1MP >= 5)
                   {player2HP = player2HP - (player1ATK+4);
                   player1HP = player1HP - 2;
                   player1MP = player1MP - 5;
                   turn = 2;}
                   damage =  (player1ATK+4);
    
                   if (player1MP <= 4)
                       turn = 2;
                   
                   if(player2HP <= 0)
                    {
                        player2HP = 0;
                    }
                   player1MP = player1MP + 2;
                   turn = 2;
               
                   
               }
            
               //Player 1 Special attck                     20 RAGE
               else if(player1Attack == 'm')
               {
                   if (player1MP >= 20)
                   {player2HP = player2HP - (player1ATK+50);
                   player1HP = player1HP - 2;
                   player1MP = player1MP - 20;
                   turn = 2;}
                   damage = player1ATK+50;
    
                   if (player1MP <= 19)
                       turn = 2;
                   
                   if(player2HP <= 0)
                    {
                        player2HP = 0;
                    }
                   player1MP = player1MP + 2;
                   turn = 2;
               
                   
               }
            }
    
            //Player 2's Turn
            else
            {
               cout << "\nPriest's Turn.\n" << potionNUM2  << " potions left." << "\nY = Leach health, N = Health  potion, X = Special Attack: ";
               char player2Attack;
               cin >> player2Attack;
               //Player 2 Attacks                                       p2
               if(player2Attack == 'y')
               {
                   player1HP = player1HP - (player2ATK);
                   player2HP = player2HP + (player1MP/2) + 4;
                   player1MP = player1MP + 3;
                   damage = player2ATK + player1MP/2;
                   if(player1HP  <= 0)
                    {
                        player1HP = 0;
                    }
                   turn = 1;
               }
               //PLayer 2 Uses Potion                                    P2
               else if (player2Attack == 'n')
               {   if (potionNUM2 >= 1) 
               {player2HP = player2HP + 40;
               potionNUM2--;}
    
               if (potionNUM2 <= 0)
                   turn =1;
    
    
                   if (player2HP > 101)
                   {
                       player2HP = 100;
                   }damage = 0;
                   turn = 1;
               }
               //Player 2 Special attck
               else if(player2Attack == 'x')
               {
                   player1HP = player1HP - (player2ATK + player1MP/2);
                   damage = (player2ATK + player1MP/2);
                   if(player1HP  <= 0)
                    {
                        player1HP = 0;
                    }
                   turn = 1;
                   cout << "test";
    
                   
                   
               }
            }
        }
    
        // Game Over - Either Player 1 wins or Player 2 Wins.
        //Player 2 Wins
        if(player1HP == 0)
            {
            system("cls");
            cout << "Warrior's HP = " << player1HP << endl;
            cout << "Priest's HP = " << player2HP << endl;
            cout << "Shadow Priest Wins!" << endl;
            }
        //Player 1 Wins
        else
            {
            system("cls");
            cout << "Warrior's HP = " << player1HP << endl;
            cout << "Priest's HP = " << player2HP << endl;
            cout << "Berserker Warrior Wins!" << endl;
            }
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    It's ok, but you should also check for upper case letters input.
    And also, this makes no sense:
    Code:
    cout << "welcome to Atco's savage fighting game!\n\nEnter 1: ";
    cin >> enter;
         
    if (enter == '1')
         cout << system("CLS");
    else system ("CLS");
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Buxton UK
    Posts
    14
    It is pointless lol. I made that bit just so i could understand how clearscreen worked and left it.

    I'm don't know how to get it to check for upper case yet so i will have to do some research. Thanks for the input

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    The first thing that comes to mind is that the players should be a class, instead of some loosely related variables. If you want each player to be able to choose a type of character, you should use an enum with all the types, as a member variable in the classes. Or maybe pass the type to the constructor.

    Shouldn't take long to make, and the code will be alot prettier.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Buxton UK
    Posts
    14
    I have heard of and seen this thing called class but up till now have only used main. I have no idea what an enum is and iv not read anything about the constructor. You have given me much to think about my next priority.

    I will post what i do into the games section next. Thank you for finding the time to read my post and give me advice so I can learn more. If I carry on the way im going its going to get far too complicated so nice one for giving me a heads up.

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by Ben Atkins View Post
    I have heard of and seen this thing called class but up till now have only used main. I have no idea what an enum is and iv not read anything about the constructor. You have given me much to think about my next priority.

    I will post what i do into the games section next. Thank you for finding the time to read my post and give me advice so I can learn more. If I carry on the way im going its going to get far too complicated so nice one for giving me a heads up.
    Okay, well forget about enums for now then. Focus on classes, learn what they are and why they are useful. It's an absolutely central feature of C++.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Buxton UK
    Posts
    14
    I'l get on it but now it's time for sleep! thanks

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    About the letter checking, it's pretty straight-forward, you can to either:
    Code:
    if (c == 'A' || c == 'a')
    or:
    Code:
    if (toupper(c) == 'A') /* if (tolower(c) == 'a') */
    For "toupper()"/"tolower()" you'll need to include <ctypes.h>
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text Game in C.
    By Anthony Brown in forum C Programming
    Replies: 10
    Last Post: 04-08-2012, 07:13 PM
  2. Two questions for a text game
    By GlitchGuy2 in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2009, 07:24 AM
  3. Help with my text adventure game.
    By Haggarduser in forum Game Programming
    Replies: 15
    Last Post: 10-26-2007, 01:53 AM
  4. Text Game Review
    By punkrockguy318 in forum Game Programming
    Replies: 16
    Last Post: 10-26-2003, 07:39 AM
  5. Game Text
    By Kuplex in forum C++ Programming
    Replies: 10
    Last Post: 03-07-2002, 12:42 PM