I created an RPG arena type game where the user plays against a computer. A randomly generated wizard and fighter also play against each other. The user entered a health, defense, and strength and then attacks the computer generated player. I also created a monk class that can save the the user based on a random dice roll. The fighter and wizard battle each other and the wizard has a special power of throwing a fireball to attack, while the fighter has the power of super strength.
import java.util.Random;
import java.util.Scanner;
public class Arena {
public static Random generator = new Random();
public static void main(String[] args) {
Character player1 = new Character(10, 2, 100);
Character player2 = new Rogue();
Character player3 = new Fighter();
Character player4 = new Wizard();
Character player5 = new Monk();
Scanner scan = new Scanner(System.in);
System.out.println("Enter a player health: ");
int health = scan.nextInt();
System.out.println("Enter a player defense: ");
int defense = scan.nextInt();
System.out.println("Enter a player strength: ");
int strength = scan.nextInt();
//player1.name = name;
player1.health = health;
player1.defense = defense;
player1.strength = strength;
int randomValues = generator.nextInt(100) +1;
player2.name = "Computer";
player2.health =randomValues;
player2.defense = randomValues;
player2.strength = randomValues;
player3.name = "Fighter";
player3.health = 70;
player3.defense = 1;
player3.strength = 5;
player4.name = "Wizard";
player4.health = 100;
player4.defense = 87;
player4.strength = 35;
player5.name = "Monk";
System.out.println(player1.name + " vs. " + player2.name);
System.out.println(player1.health + " vs. " + player2.health);
fight(player1, player2, player5);
fight(player3, player4, player5);
}
public static void fight(Character character, Character character2, Character monk){
while (character.isAlive() && character2.isAlive()){
System.out.println(character.name + ":" + character.health);
System.out.println(character2.name + ":" +character2.health);
int damage;
damage = generator.nextInt(100) +1;
System.out.println(character.name + " attacks " + character2.name + " for " + damage + " damage");
damage = generator.nextInt(100) +1;
System.out.println(character2.name + " attacks " + character.name + " for " + damage + " damage");
character.attack(character2);
character2.attack(character);
monk.attack(character);
Monk(character);
Monk(character2);
}
if (character.isAlive()){
System.out.println(character.name + " wins!");
}else if(character2.isAlive()){
System.out.println(character2.name + " wins!");
}else{
System.out.println("it's a tie!");
}
}
public static void Monk(Character character){
System.out.println("roll a die to determine if the monk can save " + character.name);
Random generator = new Random();
int number = generator.nextInt(6) + 1;
if (number == 1 || number ==2){
System.out.println("Sorry, the monk didn't save " + character.name);
}else if (number == 3 || number == 4){
System.out.println("The monk offers a healing potion to increase strength");
character.strength += 10;
System.out.println(character.name + " strength is now " + character.strength);
}else if (number == 5){
System.out.println("The monk teaches " + character.name + " defense");
character.defense += 10;
System.out.println(character.name + " defense is now " + character.defense);
}else{
System.out.println("The monk declares " + character.name + " the prophet and increases your health");
character.health += 10;
System.out.println(character.name + " health is now " + character.health);
}
}
}
public class Wizard extends Character {
public int attack(Character Target){
System.out.println("The wizard throws a fireball");
int fireBall = Arena.generator.nextInt(100)+1;
int damage = this.strength *2;
if (fireBall > 50){
damage *= 2;
System.out.println("Fighter has been hit by the wizard's fireball!");
}
int damageDealt = Target.takeDamage(damage);
return damageDealt;
}
public Wizard(){
super();
this.strength = 2;
this.defense = 50;
this.health = 100;
}
}
public class Fighter extends Character {
public int takeDamage(int damage){
int damageTaken = damage - this.defense;
this.health -= damageTaken;
int strengthAttack = this.strength + damageTaken;
System.out.println("Fighter used his strength attack to counter!");
return damageTaken;
}
public Fighter(){
super();
this.strength = 18;
this.defense = 14;
this.health = 100;
}
}
public class Rogue extends Character {
public int dexterity = 20;
public Rogue(){
super();
this.strength = 8;
this.defense = 4;
this.health = 100;
}
public int attack(Character target){
boolean criticalHit = Arena.generator.nextInt(100) < dexterity;
int damage = this.strength *2;
if (criticalHit){
damage *= 2;
System.out.println("Critical Hit!");
}
int damageDealt = target.takeDamage(damage);
return damageDealt;
}
}
public class Character {
public String name;
public int strength;
public int health;
public int defense;
public int takeDamage(int damage){
int damageTaken = damage - this.defense;
this.health -= damageTaken;
return damageTaken;
}
public int attack(Character target){
int damage = this.strength *2;
int damageDealt = target.takeDamage(damage);
return damageDealt;
}
public boolean isAlive(){
return this.health > 0;
}
public static String[] names = {"Bob", "Joe", "Steve", "Karen", "Slayer"};
public Character(){
this.name = names[Arena.generator.nextInt(names.length)];
}
public Character(int str, int def, int health){
this();
this.strength = str;
this.defense = def;
this.health = health;
}
public class Monk extends Character {
public Monk(){
super();
this.strength = 0;
this.defense = 0;
this.health = 50;
}
public int attack(Character target){
System.out.println("Don't attack! I come in peace!");
return 1;
}
}