0

I am making a textbased rpg and this is the battle system code. I need to finish this question.

2nd Apr 2018, 2:18 AM
Comedy Co.
Comedy Co. - avatar
6 Respuestas
+ 2
You need to show the code you already have really. This is a classic domain for object orientation though, and it sounds immensely fun!
2nd Apr 2018, 2:25 AM
Emma
+ 1
How can I have the player be able to attack the enemy as long as the enemy has hp?
2nd Apr 2018, 2:19 AM
Comedy Co.
Comedy Co. - avatar
+ 1
void battle() { string input; int hp = 30; int enemyhp = 10; int attack = 2; int enemyattack = 1; cout << "You entered the fight with the enemy what will you do? cout << "\n1.Attack\n2.Magic" <<endl; if (input == "Attack" || input == "attack") { cout << "You attacked the enemy" << endl; cout << "The enemy now has " << enemyhp - hp << "hp" << endl; cout << "The enemy attacked" << endl; cout << "You now have " << hp - enemyattack << "hp" << endl; } } Magic right now is irrelevant. I want to have a loop where the enemyhp can be checked and if it is zero the battle sequence will end and a victory message will appear. This also means that I want the attack option to show up and give the player the option to keep attacking if the enemy's hp is not zero. Also as a little extra is there a way I can have attacks miss by using rand and srand?
2nd Apr 2018, 2:46 AM
Comedy Co.
Comedy Co. - avatar
+ 1
Here's the first improvement to your code: https://code.sololearn.com/c0vfEoyFGrQN/#cpp Note, in theory you should really use classes / objects. For example, you would have a class called Person, you would then have two other classes (one called Player, and the other called Enemy, which would both inherit from Person). A Person object can be attacked, and can attack, and can die etc. However, I'm guessing this is an early programming exercise for you, so we should probably keep it fairly simple for now. Note also, this won't work within SoloLearn, you'll have to compile it somewhere else... as SoloLearn struggles to do user input correctly.
2nd Apr 2018, 3:08 AM
Emma
+ 1
Thank you very much I will be sure to implement and use this.
2nd Apr 2018, 3:39 AM
Comedy Co.
Comedy Co. - avatar
+ 1
You're welcome. I've now added the randomness that you wanted. There are so many ways this can be improved! You might want to check out case-insensitive string comparisons at some point, so the user doesn't have to type 'attack' or 'Attack', but could also type 'AttAcK' etc. You would also just need to check once for any of these combinations of upper and lower case versions of the word 'attack'. You could also just ask for a single keypress ('1' or '2') etc, so the user can just type a single key. Let me know if you'd like to know how to do that too. There are also possibilities of implementing the classes like I suggested. Wishing you good luck. Any questions, just let me know :-)
2nd Apr 2018, 3:44 AM
Emma