受到伤害并使对手HP达到0

Taking damage and getting the opponents HP to 0

本文关键字:HP 达到 对手 伤害      更新时间:2023-10-16

我正在尝试为我正在制作的基于文本的RPG创建一个生命伤害计。我希望HP达到0。但是,我不断得到这样的输出:

opponents hp is now 70
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No
1
hp has been reduced to50
opponents hp is now 50
will you attack?
1. yes
2.No

请记住,在正常损坏完美之前,我没有对骰子做任何事情,有什么方法可以在不使用多个if语句的情况下做到这一点?

代码如下:

#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
void Dice();
void MainMenu();
//global variable
int result = 0;
//
void Dice() {
//dice functionality
srand( time( NULL ) );
int number = 100;
result = rand() % number + 1;
///
}
void MainMenu() {//variables
int HP = 70;
int HPtotal = 0;
int damage[7] = { 20, 20, 20, 20, 20, 20, 20 };
int choice;
//
HPtotal = HP;
// repeat infinetly
for ( int i = 0; i < 999; i++ ) {
//main menu
cout << "opponents hp is now " << HPtotal << endl;
cout << "will you attack?" << endl;
cout << "1. yes" << endl;
cout << "2.No" << endl;
cin >> choice;
//
//damage system
if ( choice == 1 ) {
HPtotal = HP - damage[i];
cout << "hp has been reduced to" << HPtotal << endl;
//while HPtotal is less than 70
//END OF SYSTEM
}
if ( choice == 2 ) {//if pressed 2
cout << "END OF PROGRAM BAKA" << endl;
system( "PAUSE" );
}//end if
}//end for
}
int main() {
MainMenu();
Dice();
}

你的程序有问题HPtotal = HP - damage[i];它应该HPtotal = HPtotal - damage[i];

为了你的意志无限期地持续下去,你应该使用一个while循环

int i = 0;
while(true)
{
//main menu
cout << "opponents hp is now " << HPtotal << endl;
cout << "will you attack?" << endl;
cout << "1. yes" << endl;
cout << "2.No" << endl;
cin >> choice;
//
//damage system
if ( choice == 1 ) {
HPtotal = HPtotal - damage[i];
cout << "hp has been reduced to" << HPtotal << endl;
//while HPtotal is less than 70
//END OF SYSTEM
}
if ( choice == 2 ) {//if pressed 2
cout << "END OF PROGRAM BAKA" << endl;
system( "PAUSE" );
}//end if
}//end of while loop

要修复恢复 i 的问题,只需在 while 循环顶部添加i

如果你想知道为什么你的HP没有减少,因为它HPtotal = HP - Damage[i]和HP的值是70。 70 - 20 = 50 但是因为你没有改变HP的值,所以它的所有方式都是70,你只是将HPtotal的值设置为50