java - Something is off with my code, can't find it -
for reason code below isn't working properly. structure of code i'm trying make here one-on-one fighting game. pick character 3 different options , battle against computer, picked character. 1 reaches 0 hp loses.
so problem in code computer wins in every scenario. put character3 character, made overpowered (1000 hp, 20 strength compared 120hp , 8 strength), character still losing. i'm guessing problem simple, can't see it/figure out.
thanks in advance.
import java.util.random; public class test{ public static void main(string[] args){ character character1 = new character("test1", 100, 10); character character2 = new character("test2", 120, 8); character character3 = new character("test3", 1000, 20); character player = character3; character enemy = character2; boolean playersturn = true; while (player.gethp() > 0 || enemy.gethp() > 0){ if (playersturn == true){ enemy.damage(player.getstrength()); } else { player.damage(enemy.getstrength()); } playersturn = !playersturn; } if (player.gethp() > 0){ system.out.println("player won!"); } else { system.out.println("enemy won!"); } } } class character{ protected string name; protected int hp; protected int strength; public character(string name, int hp, int strength){ this.name = name; this.hp = hp; this.strength = strength; } public string getname(){ return this.name; } public int gethp(){ return this.hp; } public int getstrength(){ return this.strength; } public void damage(int amount){ int sum = this.hp - amount; this.hp = sum; } }
try changing
while (player.gethp() > 0 || enemy.gethp() > 0){
to
while (player.gethp() > 0 && enemy.gethp() > 0){
Comments
Post a Comment