I'm trying to write a small game to help with my Java skills. I have a class called "Zombie" and "Player" and I created instances of these classes as so:
Zombie zombie = new Zombie(50, "Infected Zombie", "Slash");
Player defaultPlayer = new Player(100, "Default Player");
Next I requested user input for the attack style of the player:
System.out.println("Which attack style would you like to use?");
defaultPlayer.printAttackStyles();
int option = scanner.nextInt();
scanner.nextLine();
switch(option) {
case 0:
System.out.println("You backed out of the fight.");
break;
case 1:
System.out.println("Punching...");
defaultPlayer.attack(1);
break;
case 2:
System.out.println("Kicking...");
defaultPlayer.attack(2);
break;
case 3:
System.out.println("Headbutting...");
defaultPlayer.attack(3);
break;
case 4:
System.out.println("Tackling...");
defaultPlayer.attack(4);
break;
default:
System.out.println("Not a valid attack style");
}
In my "Player" class I have a method called attack which inflicts a certain amount of damage based on attack style:
public int attack(int attackStyle) {
int damage = 0;
switch(attackStyle) {
case 0:
damage = 0;
break;
case 1:
damage = random.nextInt(20) + 1;
zombie.removeHealth(damage);
break;
case 2:
damage = random.nextInt(25) + 1;
zombie.removeHealth(damage);
break;
case 3:
damage = random.nextInt(30) + 1;
zombie.removeHealth(damage);
this.health -= random.nextInt(5) + 1;
break;
case 4:
damage = random.nextInt(45) + 1;
zombie.removeHealth(damage);
this.health -= random.nextInt(10) + 1;
break;
}
return damage;
}
In each case of the attack method, I have a line of code that says
zombie.removeHealth(damage);
Since the instance is only declared in the Main class how can I access that instance in order to access the method removeHealth() in the zombie class? Sorry if this question is simple but I can't figure this out.