RE: Simple Terminal Game C++

Hi all this is my first post. I am a beginner in C++ and i came across a topic on this forum called Simple Terminal Game posted by Novak in Jan 2012. I would like to thank you for writing and sharing this code! This is exactly the sort of program i wanted to play around with, and iv had ALOT of fun tinckering with it.

(in this program there are 2 players that can attack each other. they each have 10hp, 2 attack power and 3 potions each that raises hp by 3.)

he posted this code:

[code]
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int player1HP = 10;
int player2HP = 10;
int player1ATK = 2;
int player2ATK = 2;
int potionSTR = 3;
int potionNUM1 = 3;
int potionNUM2 = 3;
int turn = 1;

//Combat
while (player1HP > 0 && player2HP > 0)
{
system("CLS");
cout << "Player 1's HP = " << player1HP << endl;
cout << "Player 2's HP = " << player2HP << endl;

//Player 1's Turn
if(turn == 1)
{
cout << "\nPlayer 1's Turn.\n" << potionNUM1 << " potions left." << "\nY = Attack, N = Potion: ";
char player1Attack;
cin >> player1Attack;
//Player 1 Attacks
if(player1Attack == 'y')
{
player2HP = player2HP - player1ATK;
if(player2HP <= 0)
{
player2HP = 0;
}
turn = 2;
}
//Player 1 Uses Potion
else if (player1Attack == 'n')
{
player1HP = player1HP + potionSTR;
potionNUM1--;
if (player1HP > 10)
{
player1HP = 10;
}
turn = 2;
}
//Player 1 Skips
else
{
turn = 2;
}
}

//Player 2's Turn
else
{
cout << "\nPlayer 2's Turn.\n" << potionNUM2 << " potions left." << "\nY = Attack, N = Potion: ";
char player2Attack;
cin >> player2Attack;
//Player 2 Attacks
if(player2Attack == 'y')
{
player1HP = player1HP - player2ATK;
if(player1HP <= 0)
{
player1HP = 0;
}
turn = 1;
}
//PLayer 2 Uses Potion
else if (player2Attack == 'n')
{
player2HP = player2HP + potionSTR;
potionNUM2--;
if (player2HP > 10)
{
player2HP = 10;
}
turn = 1;
}
//Player 2 Skips
else
{
turn = 1;
}
}
}

// Game Over - Either Player 1 wins or Player 2 Wins.
//Player 2 Wins
if(player1HP == 0)
{
system("cls");
cout << "Player 1's HP = " << player1HP << endl;
cout << "Player 2's HP = " << player2HP << endl;
cout << "Player 2 Wins!" << endl;
}
//Player 1 Wins
else
{
system("cls");
cout << "Player 1's HP = " << player1HP << endl;
cout << "Player 2's HP = " << player2HP << endl;
cout << "Player 1 Wins!" << endl;
}
return 0;
}
[\code]
I have changed it a little bit.

both players now start with 100hp

Potions:
player 1's potion increases attack power by 3 points.
player 2's potion raises hp by 40

attack: player 1's attack decreases player 2's hp but also raises player 1's hp
by 2.
player 2's attack decreases player 1's hp but also raises player 2's hp
+ 7

I have also given them a new choice which is Special Attack. This gives them the option of hitting hard without healing. Each ability is slightly different.

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
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    int player1HP = 100;
    int player2HP = 100;
    int player1ATK = 2;
    int player2ATK = 2;
    int potionSTR = 3;
    int potionNUM1 = 3;
    int potionNUM2 = 3;
    int turn = 1;

    //Combat
    while (player1HP > 0 && player2HP > 0)
    {
        system("CLS");
        cout << "Berserker Warrior's HP = " << player1HP << endl;
        cout << "Shadow Priest's HP = " << player2HP << 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: ";
           char player1Attack;
           cin >> player1Attack;
           //Player 1 Attacks
           if(player1Attack == 'y')
           {
               player2HP = player2HP - player1ATK;
			   player1HP = player1HP + 2;
               if(player2HP <= 0)
                {
                    player2HP = 0;
                }
               turn = 2;
           }
           //Player 1 Uses Potion
           else if (player1Attack == 'n')
           {
               player1ATK = player1ATK + potionSTR;
               potionNUM1--;
               if (player1HP > 51)
               {
                   player1HP = 75;
               }
               turn = 2;
           }
           //Player 1 Skips
           else if(player1Attack == 'x')
           {
               player2HP = player2HP - (player1ATK+4);
			   player1HP = player1HP - 2;
               if(player2HP <= 0)
                {
                    player2HP = 0;
                }
               turn = 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
           if(player2Attack == 'y')
           {
               player1HP = player1HP - player2ATK;
			   player2HP = player2HP + 7;
               if(player1HP  <= 0)
                {
                    player1HP = 0;
                }
               turn = 1;
           }
           //PLayer 2 Uses Potion
           else if (player2Attack == 'n')
           {
               player2HP = player2HP + 40;
               potionNUM2--;
               if (player2HP > 101)
               {
                   player2HP = 100;
               }
               turn = 1;
           }
           //Player 2 Skips
           else if(player2Attack == 'x')
           {
               player1HP = player1HP - 6;
               if(player1HP  <= 0)
                {
                    player1HP = 0;
                }
               turn = 1;

			   
               
           }
        }
    }

    // Game Over - Either Player 1 wins or Player 2 Wins.
    //Player 2 Wins
    if(player1HP == 0)
        {
        system("cls");
        cout << "Priest'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 << "Warrior's HP = " << player2HP << endl;
        cout << "Berserker Warrior Wins!" << endl;
        }
    return 0;
}


Im not after any advice. I just wanted to show any newbies that read this how much fun they can have with very small changes to the code with a beginner will understand. Thanks again for this original post. At the moment I feel like a kid at christmas and will keep playing.
Topic archived. No new replies allowed.