My assignment was to make a game called 'World of Warcrap', and I finished, but there are errors.
the errors are in respawnNPC(), attack(), and fightToTheDeath() methods.
here are the instructions on what each methods are suppose to do:
1) Define the respawnNPC method inside the WorldOfWarcrap class such that it makes and returns a new character object that has the level argument as its level and where the characters is randomly named either Orc, Tauren, or Undead, and the class is randomly selected as either a Mage, Ranger, or Warrior. Also, if a request for a level 0 character is given, clamp it at level 1 (no level 0 characters).
2) attack: This method is used for a single attack of the referenced character object on the method argument character. How should we implement our battle system? Well, think of an attack as two separate things:
1. Did the attack score a hit?
2. If a hit was achieved, how many hit points damage did it inflict?
For the first calculation, we'll say the hit rate (likelihood of hit) is the attacker's xp /(the enemy's xp + the attacker's xp). So if Joe has 10 xp and Jane has 30 xp, the hit rate for Joe to successfully hit Jane would be 10/(30 + 10) = .25, which means a hit will only be achieved about 25% of the time. Each time this method is called you can simply generate a random number to see if a hit was successful or not.
For the second calculation, randomly scale the damage inflicted by the attacker and then reduce it according to the armor of the target enemy (armor of 90 would reduce the damage 90%). This will be the amount of hp lost by the target. Note that if an attack kills the enemy, you should increase the attacker's xp by 2 if the target is a lower level, 4 if the target is the same level, 6 if the target is a higher level. Note that for every 10 xp earned, a character should increase a level. So a level 1 character starts with 10 xp, when 20 xp is reached, the player levels up, which should increase stats accordingly.
3) fightToTheDeath: When called, the two characters (the referenced one and the argument) should attack each other until one of the characters is dead.
here is my code
import java.util.Random;
enum RPGClass { MAGE, RANGER, WARRIOR };
public class RPGCharacter
{
private static RPGCharacter RPGCharacter;
public static int level;
public static int armor;
public static int damage;
public static int XP;
public static int xpLimit;
public int HP;
private static String name;
private static RPGClass characterClass;
// Makes new character
public RPGCharacter(String characterName, RPGClass characterClassNew, int characterLevel)
{
name = characterName;
characterClass = characterClassNew;
level = characterLevel;
XP =0;
levelStat();
}
// Sets the status for the newly made character initially according to the level
public void levelStat()
{
if (characterClass == RPGClass.MAGE)
{
if (armor <= 40)
{
armor = 25 + (level - 1);
}
else
{
armor = 40;
}
damage = 100 + ((level - 1) * 4);
HP = level * 100;
}
else if (characterClass == RPGClass.RANGER)
{
if (armor <= 65)
{
armor = 50 + (level - 1);
}
else
{
armor = 65;
}
damage = 66 + ((level - 1) * 2);
HP = level * 100;
}
else if (characterClass == RPGClass.WARRIOR)
{
if (armor <= 90)
{
armor = 75 + (level - 1);
}
else
{
armor = 90;
}
damage = 33 + (level - 1);
HP = level * 100;
}
xpLimit = level * 10;
}
// Attack behavior
public int attack(RPGCharacter opponent)
{
int attackRatio = (int)((getXPLimit() / (opponent.getXPLimit() + getXPLimit())) * 100);
Random generator = new Random();
int attackChance = generator.nextInt(100) + 1;
int appliedAttack = 0;
// Attacks if the attackChance is among the attackRatio based on opponent's armor stat
if (attackChance <= (int)attackRatio)
{
double opponentDefense = opponent.getArmor() / 100;
appliedAttack = (int)(damage * opponentDefense);
// Applies the attack
opponent.HP -= appliedAttack;
// Adds XP if opponent is dead
if (opponent.isAlive() == false)
{
if (level > opponent.getLevel())
{
XP += 2;
}
else if (level == opponent.getLevel())
{
XP += 4;
}
else if (level < opponent.getLevel())
{
XP += 6;
}
}
}
return appliedAttack;
}
public void fightToTheDeath(RPGCharacter opponent)
{
while ((isAlive() == true) && (opponent.isAlive() == true))
{
attack(opponent);
opponent.attack(RPGCharacter);
}
}
// Level up
public void incLevel()
{
if (XP >= xpLimit)
{
level += 1;
}
}
public void rejuvinate()
{
// Resets HP
HP = level * 100;
}
public boolean isAlive()
{
boolean status = true;
if (HP > 0)
{
status = true;
}
else if (HP == 0)
{
status = false;
}
return status;
}
public void currentCharacter()
{
}
public String toString()
{
return getName() + " (Level " + getLevel() + " " + getCharacterClass() + ")" +
"\nXP: " + getXP() + "\n" +
"\nHP: " + getHP() + "\n" +
"\nArmor: " + getArmor() + "\n" +
"\nDamage: " + getDamage() + "\n";
}
public RPGClass getCharacterClass()
{
return characterClass;
}
public String getName()
{
return name;
}
public int getLevel()
{
return level;
}
public int getXP()
{
return XP;
}
public int getXPLimit()
{
return xpLimit;
}
public int getArmor()
{
return armor;
}
public int getDamage()
{
return (int)damage;
}
public int getHP()
{
return HP;
}
}
and here is the respawnNPC() method:
public static RPGCharacter respawnNPC(int level)
{
Random generator1 = new Random();
Random generator2 = new Random();
int nameSelector = generator1.nextInt(3) + 1;
int classSelector = generator2.nextInt(3) + 1;
String name = "";
if (level == 0)
{
level += 1;
}
if (nameSelector == 1)
{
name = "Orc";
if (classSelector == 1)
{
characterClass = RPGClass.MAGE;
}
else if (classSelector == 2)
{
characterClass = RPGClass.RANGER;
}
else if (classSelector == 3)
{
characterClass = RPGClass.WARRIOR;
}
}
else if (nameSelector == 2)
{
name = "Tauren";
if (classSelector == 1)
{
characterClass = RPGClass.MAGE;
}
else if (classSelector == 2)
{
characterClass = RPGClass.RANGER;
}
else if (classSelector == 3)
{
characterClass = RPGClass.WARRIOR;
}
}
else if (nameSelector == 3)
{
name = "Undead";
if (classSelector == 1)
{
characterClass = RPGClass.MAGE;
}
else if (classSelector == 2)
{
characterClass = RPGClass.RANGER;
}
else if (classSelector == 3)
{
characterClass = RPGClass.WARRIOR;
}
}
RPGCharacter NPC = new RPGCharacter(name, characterClass, level);
return NPC;
}
and the error is the following:
What does Undead want to do?
1) Fight a Level 1 Undead RANGER
2) Fight a Level 1 Undead RANGER
3) Fight a Level 1 Undead RANGER
X) Return to the main menu
Selection: 1
Exception in thread "main" java.lang.NullPointerException
at RPGCharacter.attack(RPGCharacter.java:79)
at RPGCharacter.fightToTheDeath(RPGCharacter.java:122)
at WorldOfWarcrap.processGameInput(WorldOfWarcrap.java:275)
at WorldOfWarcrap.runGame(WorldOfWarcrap.java:353)
at WorldOfWarcrap.processMainMenuInput(WorldOfWarcrap.java:258)
at WorldOfWarcrap.main(WorldOfWarcrap.java:65)
it is suppose to generate 3 random NPCs but it doesn't, and attack() and fightToTheDeath() shows error but I am stuck.