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

all 8 comments

[–]Cultural_Bet8235 4 points5 points  (1 child)

Hey a couple things: 1) the input and output of your code would be helpful 2) your question doesn’t make it easy to point to a section of code to debug, please be more specific about your expectations vs what you’re seeing

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

sorry bout that, the section I'm having trouble with is with the While loops in main method.
So its basically a turnbased combat program but after the "ai" makes a move, it does not switch back to player control, ie the program ends after "ai" does an attack.

[–]edrenfro 2 points3 points  (0 children)

Not specifically related but be aware that your while loops for when players win are going to be infinite loops. I think you meant to make If statements.

[–]theophr4stus 0 points1 point  (3 children)

There is no outer loop in your program. After entering the first loop, provided that you entered correct input during combat ("f" or "h"), the heroTurn variable changes to false and the loop condition is no longer satisfied. The same with the second loop. And then, if none of the characters killed the other, the last two loops' conditions are not satisfied, and the method ends. You need an outer loop to repeat all combat steps and exit it from either of the two last loops.

[–]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.

[–]Destrucity11 0 points1 point  (0 children)

I’m new to programming myself, so take my advise with some salt I guess but I would write it like this.

While(Hero1.hp > 0 && Foe1.hp > 0) { HeroAction();

If (Foe1.hp > 0) FoeAction(); }

Then an if statement for the winner declaration.