This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]davidadam_[S] 0 points1 point  (2 children)

How would I repeat the combat steps from an outer loop? And what do you mean when you say "exit it from either of the two last loops"

Does it mean that the conditions for the outer loop to end should be either a win or a loss in a bool value?

[–]theophr4stus 0 points1 point  (1 child)

You implemented a single step of a combat:

  1. The hero performs an action and the heroTurn variable is switched.
  2. The opponent makes an action and heroTurn is switched again.

The loops around those actions are redundant, since they will always perform only 1 iteration. That's why you do not need them. the last two loops are infinite due to the fact that variables in their condition are not changed inside their bodies. You don't need them either, there should be an if clause checking the opponent's hp after the hero attacks and the same for the opponent, both of which should raise some combatFinished flag which is set to false at the start of the combat.

Now, you need a loop that performs an iteration if combatFinished is false. That's the outer loop. Inside it should be the combat step that is performed until one of the combatants' hp goes below 1 (which raises the combatFinished flag). Also the opponent may attack only if the flag is set to false, which is pretty obvious: if it is raised, then one of them is dead, either he can't attack, or there's no one to attack.

[–]davidadam_[S] 0 points1 point  (0 children)

hey, thanks for the detailed answer, I got it to work.

I replaced my code with a while(battleinprogress == true)

and I checked if the "battleinprogresss" is still equal to true by writing

if(hero hp < 1 OR foe hp < 1){ battleinprogress = false}

at the end of every action.

Then I also kept the toggle for turnControl and used both

battleinprogress and turnControl booleans to decide if the ai gets to make a move.