Help with text-based fighting game health bar

Ok so I've been messing with this all morning, and I fix one output and just break the others. I need this to end in 2 ways:

player wins:

HURRICANE KICK (or other move)

===========

==YOU WIN==

===========

Player loses:

HURRICANE KICK (or other move)

The opponent attacked you!!! (-20)

=========

==YOU LOSE===

========

Issue is with 'opponent attacked you!!!'. It cant show up before saying the player wins, but has to show before player loses. I've moved it around so "YOU WIN" works right now, but when the player's health hits 0 the code goes one more turn instead of displaying "YOU LOSE".

Example of what I mean:

The opponent attacked you!!! (-20) ///it should display you lose after this line

player health 0 === vs === opponent health: 50

choose one of the movies:

HADOUKEN

===========

====YOU LOSE=====

===========

'opponent attacked you!!!" also cant show up after an invalid input, which is why I used goto.

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
 # include <iostream>
# include <string>
using namespace std;
int main()
{

    bool quit = false;
    char inputkey;
    int health=100;
    int ophealth=100;

    cout << "==============================================" << endl;
    cout << "Welcome ot the Street Fighter Tutorial Game" << endl;
    cout << "Attack the opponent until the healthbar is 0" << endl;
    cout << "==============================================" << endl;

    while (!quit){
        cout << endl;
        cout << "Player Health: " << health << " ===== VS ===== Opponent Health: " << ophealth << endl;
        cout << endl;
 tstart:  cout << "Choose one of the moves:" << endl;
        cout << "(1) Hadouken: 10 HP (2) Shoryuken: 15 HP (3) Hurricane Kick: 20 HP" <<endl;

        cin >> inputkey;

        if(inputkey=='1'){
            ophealth-=10;
            cout<<"HADOUKEN"<<endl;
            cout<<endl;
        }else if(inputkey=='2'){
            ophealth-=15;
            cout<<"SHORYUKEN"<<endl;
            cout<<endl;
        }else if(inputkey=='3'){
            ophealth-=20;
            cout<<"HURRICANE KICK"<<endl;
            cout<<endl;\
        }else{
            cout<<"Invalid menu selection. Please try again."<<endl;
            cout<<endl;
            goto tstart;
        }


        if(ophealth<=0&&health>=1){
            cout<<"=============================================="<<endl;
            cout<<"====================YOU WIN==================="<<endl;
            cout<<"=============================================="<<endl;
            quit=true;
        }else if(ophealth>=1&&health==0){
            cout<<"=============================================="<<endl;
            cout<<"====================YOU LOSE=================="<<endl;
            cout<<"=============================================="<<endl;
            quit=true;
        }else{
            cout<<"The opponent attacked you!!! (-20)"<<endl;
            health-=20;
        }
    }

    return 0;
}
You need to move line 56/57 before line 45.

But since line 57 is always the maximum damage the player can't win in this scenario.

So either the opponent does not strike with the maximum damage each time or not each time. You may use srand()/rand() to achieve that.
I think you want logic like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        if(ophealth<=0&&health>=1){
            cout<<"=============================================="<<endl;
            cout<<"====================YOU WIN==================="<<endl;
            cout<<"=============================================="<<endl;
            quit=true;
        }else {
            cout<<"The opponent attacked you!!! (-20)"<<endl;
            health-=20;
            if(health<=0){
                cout<<"=============================================="<<endl;
                cout<<"====================YOU LOSE=================="<<endl;
                cout<<"=============================================="<<endl;
                quit=true;
            }
        }


See your previous thread for how to read input without having to mess about with dealing with errant newlines.
https://cplusplus.com/forum/beginner/283766/


Don't use goto statements! These are NOT good programming practice. Use a switch statement instead of multiple if-statements and a loop.
Consider:

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
#include <iostream>
#include <limits>
using namespace std;

int main() {
	int health { 100 };
	int ophealth { 100 };

	cout << "==============================================\n";
	cout << "Welcome to the Street Fighter Tutorial Game\n";
	cout << "Attack the opponent until the healthbar is 0\n";
	cout << "==============================================\n";

	for (bool quit {}; !quit; ) {
		cout << "\nPlayer Health: " << health << " ===== VS ===== Opponent Health: " << ophealth << "\n\n";

		for (bool menu { true }; menu; ) {
			char inputkey {};

			menu = false;

			cout << "Choose one of the moves:\n";
			cout << "(1) Hadouken: 10 HP (2) Shoryuken: 15 HP (3) Hurricane Kick: 20 HP: ";

			cin >> inputkey;
			cin.ignore(numeric_limits<streamsize>::max(), '\n');

			switch (inputkey) {
				case '1':
					ophealth -= 10;
					cout << "HADOUKEN\n\n";
					break;

				case '2':
					ophealth -= 15;
					cout << "SHORYUKEN\n\n";
					break;

				case '3':
					ophealth -= 20;
					cout << "HURRICANE KICK\n\n";
					break;

				default:
					cout << "Invalid menu selection. Please try again.\n\n";
					menu = true;
					break;
			}
		}

		if (ophealth <= 0 && health >= 1) {
			cout << "==============================================\n";
			cout << "====================YOU WIN===================\n";
			cout << "==============================================\n";
			quit = true;
		} else {
			cout << "The opponent attacked you!!! (-20)\n";
			if ((health -= 20) <= 0) {
				cout << "==============================================\n";
				cout << "====================YOU LOSE==================\n";
				cout << "==============================================\n";
				quit = true;
			}
		}
	}
}

Last edited on
Topic archived. No new replies allowed.