This is my second post since I've started coding/joined this site so be ready for some bad code and beginner questions!
I am just playing around with Java right now, as I believe this is a fun way to learn the language, so I decided to make a simple fight simulator. The simulator is meant to have each player start with a set amount of health (hitPoints) and they take turns hitting each other. Their damage dealt determined by a random number roll.
I just started trying to use multiple classes to make my code easier to ready and more. Correct I guess.
I have a question but also hope someone can give me pointers on how to improve my code in some ways. My questions is. I want to make it so when a player hits 0 health the program goes to the outCome() method but it currently allows 1 more hit after a player loses. So if the person who attacks first dies, they get 1 more hit in before the program ends but this is wrong and I'm not sure how to fix this. Here is my code.
This is my main class that runs everything.
package com.company;
import java.util.Scanner;
public class Battle {
public static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please enter your username.");
Players.userName = scan.nextLine();
System.out.println(Players.userName + ", get ready for battle..");
System.out.println("Press enter when ready.");
scan.nextLine();
while(Players.pHP > 0 && Players.oHP > 0) {
Game.fight();
}
Game.outCome();
}
}
This is my Player class which I believe is very unnecessary right now and would like some feedback on how to make it more useful than it is. This class feels entirely wrong to me.
package com.company;
public class Players {
static String userName;
static int hitPoints = 99;
static int oHP = opponentHP();
static int pHP = playerHP();
public static int playerHP() {
return pHP = hitPoints;
}
public static int opponentHP() {
return oHP = hitPoints;
}
}
And finally my Game class which kind of has the game code in it. This class looks messy to me and I believe I will improve on my organization with practice.
package com.company;
import java.util.Random;
public class Game {
public static Random r = new Random();
private static boolean isTurn = true;
public static void fight() {
int damage = r.nextInt(20);
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
System.out.println("INTERUPTED DELAY");
}
if(isTurn) {
Players.oHP -= damage;
if(Players.oHP < 0) {
Players.oHP = 0;
}
System.out.println("You hit a(n) " + damage + " and now your
opponent has " + Players.oHP + " HitPoints.");
isTurn = false;
} else {
Players.pHP -= damage;
if(Players.pHP < 0) {
Players.pHP = 0;
}
System.out.println("Your opponent hit a(n) " + damage + " and now
you have " + Players.pHP + " HitPoints.");
isTurn = true;
}
}
public static void outCome() {
if (Players.oHP == 0 || Players.oHP < 0) {
victory();
} else {
defeat();
}
}
private static void victory() {
System.out.println("You are the ultimate warrior.");
System.out.println("Thanks for fighting..........");
}
private static void defeat() {
System.out.println("You have been slain.");
System.out.println("Thanks for fighting..........");
}
}