You are on page 1of 5

Main package:

package com.ryerbs.main;

import com.ryerbs.model.Human;
import com.ryerbs.model.Monster;

public class Main {

public static void main(String[] args) {

String winner = null;

Monster monster1 = new Monster("Orc", "axe", (long) 1000);


Human human1 = new Human("Luis", "sword", (long) 1000);

boolean noWinner = true;


int turn = 0;
while (noWinner) {

// odd = monster will attack


// even = human will attack

// randomly generate whose turn to attack base on chance to get an even or


odd
// number
Integer turnToAttack = (int) (Math.random() * 20);

// checks if the value is odd or even digit

if (turnToAttack % 2 == 0) {
// System.out.println()
// result is even - human will attack
Long health = monster1.getHealth();
monster1.setHealth(health - human1.getDamage());

System.out.println("Human attack damage = " + human1.getDamage());


System.out.println("Monster health = " + monster1.getHealth());

// if monster health goes below zero , end of the game


if (monster1.getHealth() <= 0) {
System.out.println("Monster has been defeated");
System.out.println(
"Human health: " + human1.getHealth() + "
Monster health: " + monster1.getHealth());
winner = human1.getName();
noWinner = false;
}
} else {

// result is odd - monster will attack


Long health = human1.getHealth();
human1.setHealth(health - monster1.getDamage());

System.out.println("Monster attack damage = " +


monster1.getDamage());
System.out.println("Human health = " + human1.getHealth());

// if human health goes below zero , end of the game


if (human1.getHealth() <= 0) {
System.out.println("Human has been defeated");
System.out
.println("Monster health: " +
monster1.getHealth() + " Human health: " + human1.getHealth());
winner = monster1.getName();
noWinner = false;
}

} // end of while loop


System.out.print("The winner is " + winner);
}

}
OOP HUMAN:

package com.ryerbs.model;

/**
*
* @author KaiZeR
*/
public class Human {

private String name;


private String weapon;
private Long health;
private Long damage;

public Human(String name, String weapon, Long health) {


this.name = name;
this.weapon = weapon;
this.health = health;

// this part here is to set the damage of the human base on weapon used
Long damage;
if (this.getWeapon().equals("sword")) {
damage = (long) 10;
this.setDamage(damage);
} else if (this.getWeapon().equals("woodstick")) {
damage = (long) 5;
this.setDamage(damage);
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getWeapon() {


return weapon;
}

public void setWeapon(String weapon) {


this.weapon = weapon;
}

public Long getHealth() {


return health;
}

public void setHealth(Long health) {


this.health = health;
}

public Long getDamage() {


return damage;
}

public void setDamage(Long damage) {


this.damage = damage;
}

}
OOP.Monster:

package com.ryerbs.model;

/**
*
* @author KaiZeR
*/
public class Monster {

private String name;


private String weapon;
private Long health;
private Long damage;

public Monster(String name, String weapon, Long health) {


this.name = name;
this.weapon = weapon;
this.health = health;

// this part here is to set the damage of the monster base on weapon used
Long damage;
if (this.getWeapon().equals("axe")) {
damage = (long) 10.5;
this.setDamage(damage);
} else if (this.getWeapon().equals("woodstick")) {
damage = (long) 5.5;
this.setDamage(damage);
}
}

public String getWeapon() {


return weapon;
}

public void setWeapon(String weapon) {


this.weapon = weapon;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}
public Long getHealth() {
return health;
}

public void setHealth(Long health) {


this.health = health;
}

public Long getDamage() {


return damage;
}

public void setDamage(Long damage) {


this.damage = damage;
}

OUTPUT:

You might also like