I am finally setting out on my own instead of following courses online and trying to create something on my own, it's just a simple text based dungeon type of game.
However I know I'm messing up when I'm passing in the new values from one class to another class, the class where I try to set the values to pass to another
package com.company;
import java.util.Random;
import java.util.Scanner;
public class CharBuilder {
static public Random randNum = new Random();
static public Scanner sc = new Scanner(System.in);
private String mCharacterName;
private int mStrength = 6;
private int mIntelligence = 6;
private int mConstitution = 6;
private int mDexterity = 6;
private int mHitPoints = 40;
private int mManaPoints = 40;
private String mCharClass;
private CharBuilder hero;
public CharBuilder(String charClass,int str, int intl, int con, int dex, int hp, int mp) {
mCharClass = charClass;
mStrength = str;
mIntelligence = intl;
mConstitution = con;
mDexterity = dex;
mHitPoints = hp;
mManaPoints = mp;
}
public CharBuilder() {
}
public CharBuilder(String charClass) {
mCharClass = charClass;
}
public void characterNameCreator() {
System.out.println("Greetings, what is your name adventurer? ");
mCharacterName = sc.nextLine();
System.out.printf("I see, so your name is %s very well, very well \n", mCharacterName);
}
public void createHero() {
hero = new CharBuilder(mCharClass,mStrength,mIntelligence,mConstitution,mDexterity,mHitPoints,mManaPoints);
String acceptedAnswers = "warrior wizard thief";
System.out.printf("%s would you say you are more of a warrior, wizard or more of the thievery type \n", mCharacterName);
String classAnswer = sc.nextLine();
boolean isAcceptableClass = classAnswer.contains(acceptedAnswers);
do {
if (classAnswer.equalsIgnoreCase("warrior")) {
mCharClass = "warrior";
hero.setStr(mStrength + 3);
hero.setIntelligence(mIntelligence - 1);
hero.setmConstitution(mConstitution + 4);
hero.setmDexterity(mDexterity + 1);
hero.setHitPoints(mHitPoints + 20);
hero.setManaPoints(mManaPoints - 10);
} else if (classAnswer.equalsIgnoreCase("Wizard")) {
mCharClass = "wizard";
hero.setStr(mStrength -1);
hero.setIntelligence(mIntelligence + 3);
hero.setmConstitution(mConstitution + 2);
hero.setmDexterity(mDexterity);
hero.setHitPoints(mHitPoints - 10);
hero.setManaPoints(mManaPoints + 30);
} else if (classAnswer.equalsIgnoreCase("thief") || classAnswer.equalsIgnoreCase("thievery")) {
mCharClass = "thief";
hero.setStr(mStrength + 2);
hero.setIntelligence(mIntelligence + 1);
hero.setmConstitution(mConstitution + 1);
hero.setmDexterity( mDexterity + 5);
hero.setHitPoints(mHitPoints + 10);
hero.setManaPoints(mManaPoints + 10);
} else {
System.out.println("I'm sorry, that is not an acceptable class, please pick warrior, wizard, or thief");
createHero();
}
}while (isAcceptableClass);
}
public int getStrength() {
return mStrength;
}
public int getIntelligence() {
return mIntelligence;
}
public int getConsitution() {
return mConstitution;
}
public int getDex() {
return mDexterity;
}
public int getHP() {
return mHitPoints;
}
public int getMP() {
return mManaPoints;
}
public CharBuilder getHero() {
return hero;
}
public void setStr(int str) {
mStrength = str;
}
public void setIntelligence(int intl) {
mIntelligence = intl;
}
public void setmConstitution(int con) {
mConstitution = con;
}
public void setmDexterity(int dex) {
mDexterity = dex;
}
public void setHitPoints( int hitPoints) {
mHitPoints = hitPoints;
}
public void setManaPoints( int manaPoints) {
mManaPoints = manaPoints;
}
public void setmCharClass(String charClass) {
mCharClass = charClass;
}
}
and the class where I'm trying to pass the variables into doesn't seem to be getting anything as it is giving me a nullPointerException so I know there's no value when I ask it go it, and I've included that class below,
package com.company;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Combat {
Scanner sc = CharBuilder.sc;
Random randNum = CharBuilder.randNum;
List<Enemy> enemyList = new Enemy().createList();
Enemy enemy = enemyList.get(randNum.nextInt(enemyList.size()));
CharBuilder character = new CharBuilder().getHero();
int charStrength = character.getStrength();
int charHp = character.getHP();
int enemyHp = enemy.getHP();
int enemyStr =enemy.getStr();
String enemyName = enemy.getName();
public void initiateCombat() {
System.out.printf("A %s appears, it has %d hitpoints, and it's strength is %s \n", enemyName, enemyHp, enemyStr);
}
public void playersTurn() {
System.out.println("It is your turn, your available actions are attack, or use a skill");
String actionAnswer = sc.nextLine();
if (actionAnswer.equalsIgnoreCase("attack")) {
enemyHp -= charStrength;
System.out.printf("You attack the enemy, doing %s damage, the enemy currently has %s hp left \n", charStrength,
enemyHp);
}
else {
System.out.println("Sorry, please choose attack or skill ");
playersTurn();
}
}
public void enemyTurn() {
charHp -= enemyStr;
System.out.printf("It is the enemy's turn, it attacks doing %s damage, your current hp is %s \n", enemyStr, charHp);
}
public void combat() {
while(enemyHp >= 0 && charHp >= 0) {
initiateCombat();
playersTurn();
if (enemyHp <= 0) {
System.out.println("You have defeated the enemy, congratulations");
break;
}
enemyTurn();
}
}
}
any tips for a new guy on what I am doing wrong, and how I can get these values over? I know that I could just declare the hero static but is there a better way?
I'm getting NullPointerException in the first possible area where they try to pull data from the CharBuilder class. Which is
int charStrength = character.getStrength();