Reputation: 19
I am completely new to programming and I was making a program for class. For the project, I need to make a "self directed" program. I wanted to make a simple program; I wanted a fighting simulator! So the point of the program is to do this: Introduce you, and start the fight. The attack is completely random out of 5, and you and the "enemy" fight until one of you hits 0 hp. I want the program to end, and display the health of you and your enemy and tell you if you have won or not. Pretty simple, but it's harder than you'd think especially with the lack of programming I have learned. I just need help fixing it or simplifying it. Thanks for the information if you can help! Code below:
import java.util.Scanner;
public class TG_UN4EX13 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String rumble = "startvalue";
System.out.println("Welcome to the Ultimate Battle Game!");
System.out.println("Have Fun!");
// Attack Section //
int attack1 = 5;
int attack2 = 10;
int attack3 = 20;
int attack4 = 40;
int cower = 0;
// Character Section//
int playerhealth = 100;
// Character Section//
// Enemy Section//
int enemyhealth = 100;
// Enemy Section//
while (playerhealth >= 0 || enemyhealth >= 0 || rumble.equalsIgnoreCase("Yes")) {
int attacknumber = (int) (Math.random() * (5 - 1 + 1) + 1);
int attacknumber2 = (int) (Math.random() * (5 - 1 + 1) + 1);
if (attacknumber == 1) {
enemyhealth = enemyhealth - attack1;
System.out.println("You used Punch!");
System.out.println("You did " + attack1 + " damage! \n");
} else if (attacknumber == 2) {
enemyhealth = enemyhealth - attack2;
System.out.println("You used Kick!");
System.out.println("You did " + attack2 + " damage! \n");
} else if (attacknumber == 3) {
enemyhealth = enemyhealth - attack3;
System.out.println("You used Jab!");
System.out.println("You did" + attack3 + " damage! \n");
} else if (attacknumber == 4) {
enemyhealth = enemyhealth - attack4;
System.out.println("You used Roundhouse Kick!");
System.out.println("You did " + attack4 + " damage! \n");
} else if (attacknumber == 5) {
enemyhealth = enemyhealth - cower;
System.out.println("You cowered in fear!");
System.out.println("You did " + cower + " damage! \n");
}
if (attacknumber2 == 1) {
playerhealth = playerhealth - attack1;
System.out.println("They used Punch!");
System.out.println("They did " + attack1 + " damage! \n");
} else if (attacknumber2 == 2) {
playerhealth = playerhealth - attack2;
System.out.println("They used Kick!");
System.out.println("They did " + attack2 + " damage! \n");
} else if (attacknumber2 == 3) {
playerhealth = playerhealth - attack3;
System.out.println("They used Jab!");
System.out.println("They did " + attack3 + " damage! \n");
} else if (attacknumber2 == 4) {
playerhealth = playerhealth - attack4;
System.out.println("They used Roundhouse Kick!");
System.out.println("They did " + attack4 + " damage! \n");
} else if (attacknumber2 == 5) {
playerhealth = playerhealth - cower;
System.out.println("They cowered in fear!");
System.out.println("They did " + cower + " damage! \n");
}
System.out.println("You have " + playerhealth + " health!");
System.out.println("They have " + enemyhealth + " health! \n");
}
if (playerhealth > enemyhealth) {
System.out.println("You won!");
System.out.println("Do you want to play again?");
rumble = scan.nextLine();
} else {
System.out.println("You lost!");
System.out.println("Do you want to play again?");
rumble = scan.nextLine();
}
if (rumble.equalsIgnoreCase("no")) {
System.out.println("Well, goodbye!");
System.exit(0);
}
}
}
P.S. This is a quick edit but here is an example:
You have 20 health! They have 20 health!
You used Jab! You did20 damage!
They used Roundhouse Kick! They did 40 damage!
You have -20 health! They have 0 health!
You used Jab! You did20 damage!
They used Roundhouse Kick! They did 40 damage!
You have -60 health! They have -20 health!
You lost! Do you want to play again?
Upvotes: 2
Views: 84
Reputation: 2065
Take a look at the conditions for your loop.
while (playerhealth >= 0 || enemyhealth >= 0 || rumble.equalsIgnoreCase("Yes")) {
//do stuff
}
You're saying, "Keep fighting while any of the following conditions are true":
Player health is positive
Enemy health is positive
Rumble = Yes
Now think about what it will take to exit that loop (ie, end the game).
Exiting the loop is the opposite of staying in the loop, so you want the opposite of those conditions, which is, "Stop fighting when all of the following are true":
Player health is less than 0
Enemy health is less than 0
Rumble != Yes
Do you mean for the fight to only end when both the player and enemy have negative health, or should it end when either the player or the enemy have negative health?
Upvotes: 1
Reputation: 1209
I'm not positive, but I think you may need to change:
while(playerhealth>=0 || enemyhealth>=0 || rumble.equalsIgnoreCase("Yes"))
to
while((playerhealth>0 && enemyhealth>0) || rumble.equalsIgnoreCase("Yes"))
If the health is =0
, it will run again, so remove the =
as well
Upvotes: 3