Simple Terminal Game

Firstly, as I'm new to C++ and this forum, I'd like to say a quick hello to everyone.
*Side note: I'm not sure if this belongs here, but I can't see where else to put it*

I've learned the very basics of C++ and wanted to test out my knowledge to see if I was actually learning it, or simply wasting my time watching youtube. The following code is for a simple combat game between player 1 and player 2, played in the console. (I realise that using "System("Cls")" isn't the best option, but I feel like it serves my purpose in just testing out some simple code.)

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
#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;
}


If I could get some feedback, along with possible improvement's in the code, it would be greatly appreciated, as I coded this entirely from scratch on my own. (Apart from the system("cls") function which I didn't know about.)
A few future updates I'd like to make would be to turn this into a RPG of sorts, as this would allow me to build upon it etc as I learn more and more C++.
Looks like good code. Better than the first game I ever made! :D

Only thing you could add at this point, is validation of input. i.e. Make sure the user inputs what makes the program work.
1
2
3
4
5
cout << "\nPlayer 1's Turn.\n" << potionNUM1 << " potions left." << "\nY = Attack, N = Potion: ";
char player1Attack;
cin >> player1Attack;
//Player 1 Attacks
if(player1Attack == 'y')


Here, a user could see that you ask for 'Y'( UPPER case ). But then you check for 'y'( lower case ).
Here's a function that could be useful:
http://www.cplusplus.com/reference/clibrary/cctype/tolower/

Also, other validation, if the user enter's something other than expected, i.e. a number. Maybe catch it and output an error saying incorrect data entered.

And, here's a function I use to clear a console screen:
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
void cls()
{
	//include windows in a header file:
	//#include <windows.h>
	
	//get the HANDLE for the console window
	HANDLE hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

	COORD coordScreen = { 0, 0 };    // home for the cursor 
	DWORD cCharsWritten;
	CONSOLE_SCREEN_BUFFER_INFO csbi; 
	DWORD dwConSize;

	// Get the number of character cells in the current buffer. 

	if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
		return;
	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

	// Fill the entire screen with blanks.

	if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
		dwConSize, coordScreen, &cCharsWritten ))
		return;

	// Get the current text attribute.

	if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
		return;

	// Set the buffer's attributes accordingly.

	if( !FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
		dwConSize, coordScreen, &cCharsWritten ))
		return;

	// Put the cursor at its home coordinates.

	SetConsoleCursorPosition( hConsole, coordScreen );
}


Personally, I think that tolower example looks a bit, uhmmm... Well, I don't see the need for the extra 'c' char and putchar( I've never used putchar )... lol

I just wrote this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>		//cout
#include <conio.h>		// _kbhit()

int main()
{
	char text[] = "I'M UPPER CASE TEXT";

	std::cout << "This is the text before conversion:\n" << text << '\n';

	for( int i = 0; i < 20; ++i )
	{
		//access each element and convert to lower case
		text[ i ] = tolower( text[ i ] );
	}

	std::cout << "\nThis is the text now:\n" << text << '\n';

	//press a key to continue...
	//while there isn't a key being pressed
	while( ! _kbhit() )
	{ /* do nothing - wait for a key press, before exiting */ }

	return 0;
}


A few future updates I'd like to make would be to turn this into a RPG of sorts, as this would allow me to build upon it etc as I learn more and more C++.

I've found, that when I learn more C++ / the use of extra, before unknown functions, I usually end up re-writing the program. I find( sometimes ), it's easier to re-write instead of sitting there replacing code. As, again, sometimes, you can be replacing code and you are not able to compile and run because functions/variables etc. don't match up yet. And when you finish, you could be at a loss.

But this is just from personal experience! I like to learn by doing, rather than reading 5 chapters then trying stuff out.
Last edited on
Thanks for the ideas. Completely forgot about adding the exception when players enter something other than Y or N. (I did test it though and found it just skipped the turn, without attacking or using a potion.)
That's because of this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(player2Attack == 'y') //////if 'y'
           {
               /* code */
           }
           //PLayer 2 Uses Potion
           else if (player2Attack == 'n')//////if 'n'
           {
              /* code */
           }
           //Player 2 Skips
           else/////if anything other than 'y' or 'n'
           {
               //edit this to be an error message
               
               //take this out. That way it will still be a players turn if the wrong input is encountered
               turn = 1;
           }
Last edited on
Topic archived. No new replies allowed.