Reputation: 3
I'm new to Stackoverflow (as well as coding) and I'm really hoping you could help me out here. I'm making a text-based butterfly effect game on BlueJ. I have my main "RPG" code first, then my "Level1ZombieClass" after it. My problem is that I keep getting an initialization error for my userclass variable (Because I need to have it in my zombie class in order for the "if" statements/combat work, and I have no idea how to fix it. In the first set of code I wanted to add my zombie class so I don't have a huge chunk of code that can be easily replaced by a couple lines. Again, the problem being the userclass variable is something that the user defines, not something that I define in the code.Just a screenshot here. Thank you so much guys, really appreciate your help.
import java.util.Random;
import java.util.Scanner;
public class RPG
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
String name;
int playerClass;
int wincount=0;
int userhealth=100+(wincount)*3;
int comphealth=100;
int hc;
int userChoice=1;
int killcount;
int regularAttackD;
int regularAttackHC;
int powerAttackD;
int powerAttackHC;
int shield;
int quickAttackHC;
int quickAttackD;
int compChoice=1;
int playerchoice;
Random a = new Random ();
Random b = new Random ();
Random c = new Random ();
Random d = new Random ();
Random e = new Random ();
Random f = new Random ();
Random g = new Random ();
Random h = new Random ();
System.out.println ("Welcome to the zombie apocalypse...\nIn this text-driven game you will need to make decisions based on game prompts.\nYour decisions will determine your own fate.");
System.out.println ("What would you like to be called?");
name = in.next();
System.out.println (name + ",in this game you may need to fight enemies in a turn-based combat style.\nYou will now enter a number for the class you want.\n(1)Scout: Increased hit chance for all attacks.\n(2)Brute: Increased damage for all attacks\n(3)Medic:Increased health during combat.");
playerClass = in.nextInt();
switch (playerClass)
{
case 1:
System.out.println ("You have selected to be a scout! Hit chance increased!");
break;
case 2:
System.out.println ("You have selected to be a brute! Power increased!");
powerAttackD=b.nextInt(15)+28;
regularAttackD=d.nextInt(8)+18;
quickAttackD=f.nextInt (4)+4;
break;
case 3:
System.out.println ("You have selected to be a medic! Health increased!");
userhealth=115;
break;
}
System.out.println ("You now find yourself in an deserted town center. Pharmacy to your left (Looks to be completely ravaged), restaurant to your right, town hall is a couple hundred feet ahead of you.\nWhere do you want to go?\nPharmacy:1\nRestaurant:2\nTown Hall:3");
playerchoice=in.nextInt();
switch (playerchoice)
{
case 1:
System.out.println ("Nothing of use seems to be apparent when you walk in the door. There is a dead body slouched against the far wall to your right.\nYou make your way down the 5th and middle aisle. Still nothing of use. You get to the end of the store and hit a glass jar of a shelf.\nStill silence in the store. You walk to the entrance of the store empty-handed, and the presumed dead body grabs a hold of you!");
//BEGINNING OF COMBAT
Level1ZombieClass Level1ZombieClassObject = new Level1ZombieClass ();
Level1ZombieClassObject.ZombieLevel1Class();
}
} }
Then my zombie class:
//Turn-based combat with a user input for what they want to do with their attack.
import java.util.Random;
import java.util.Scanner;
public class Level1ZombieClass
{
public void ZombieLevel1Class()
{
Scanner in = new Scanner(System.in);
int userhealth=100;
int comphealth=100;
int playerClass;
int hc;
int userChoice=1;
int critdamage;
int regularAttackD;
int regularAttackHC;
int powerAttackD;
int powerAttackHC;
int shield;
int quickAttackHC;
int quickAttackD;
int compChoice=1;
int wincount=0;
boolean winCondition = true;
Random a = new Random ();
Random b = new Random ();
Random c = new Random ();
Random d = new Random ();
Random e = new Random ();
Random f = new Random ();
Random g = new Random ();
Random h = new Random ();
do {
hc=a.nextInt(99)+1;
powerAttackD=b.nextInt(15)+25;
regularAttackD=d.nextInt(8)+15;
quickAttackD=f.nextInt (4)+3;
shield=g.nextInt(15)+10;
System.out.println ("What move would you like to use?");
System.out.println ("1:Regular Attack");
System.out.println ("2:Power Attack: Higher damage at a lower hit chance.");
System.out.println ("3:Quick attacks: 3 weaker attacks at a higher hit chance.");
System.out.println ("4:Shield: 100% chance to work and you do not attack, but the damage you receive is reduced for the next incoming attack.");
userChoice=in.nextInt();
if (userChoice==1){
if (playerClass==2){
powerAttackD=b.nextInt(15)+28;
regularAttackD=d.nextInt(8)+18;
quickAttackD=f.nextInt (4)+4;
}
if (compChoice==4){
regularAttackD=regularAttackD-shield;
System.out.println ("Level 1 zombie used a shield! Damage reduced by " + shield + ".");
}
if (playerClass==1){
if (hc>=14){
if(regularAttackD<0){
regularAttackD=0;
}
System.out.println ("Attack successful for " + regularAttackD + " damage.");
comphealth=comphealth-regularAttackD;
}else{
System.out.println ("Attack failed!");}
}else{
if (hc>=18){
if (regularAttackD<0){
regularAttackD=0;
}
System.out.println ("Attack successful for " + regularAttackD + " damage.");
comphealth=comphealth-regularAttackD;
}else{
System.out.println ("Attack failed.");
}
}
if (comphealth <=0) {
System.out.println ("Level 1 zombie health is critically low! You win!");
wincount++;
winCondition = false;
break;
}
System.out.println ("Level 1 zombie health now at " + comphealth + ".");
}
if (userChoice==2){
if (compChoice==4){
powerAttackD=powerAttackD-shield;
System.out.println ("Level 1 zombie used a shield! Damage reduced by " + shield + ".");
}
if (playerClass==1){
if (hc>=26){
if(powerAttackD<0){
regularAttackD=0;
}
System.out.println ("Attack successful for " + powerAttackD + " damage.");
comphealth=comphealth-powerAttackD;
}else{
System.out.println ("Attack failed!");}
}else{
if (hc>=30){
if (powerAttackD<0){
powerAttackD=0;
}
System.out.println ("Attack successful for " + powerAttackD + " damage.");
comphealth=comphealth-powerAttackD;
}else{
System.out.println ("Attack failed.");
}
}
if (comphealth <=0) {
System.out.println ("Level 1 zombie health is critically low! You win!");
wincount++;
winCondition = false;
break;
}
System.out.println ("Level 1 zombie health is now at " + comphealth + ".");
}
if (userChoice==3)
{
quickAttackD=quickAttackD*3;
if (compChoice==4){
quickAttackD=quickAttackD-shield;
System.out.println ("Level 1 zombie used a shield! Damage reduced by " + shield + ".");
}
if (playerClass==1){
if (hc>=14){
if(quickAttackD<0){
quickAttackD=0;
}
System.out.println ("Attack successful for " + quickAttackD + " damage.");
comphealth=comphealth-quickAttackD;
}else{
System.out.println ("Attack failed!");}
}else{
if (hc>=18){
if (quickAttackD<0){
quickAttackD=0;
}
System.out.println ("Attack successful for " + quickAttackD + " damage.");
comphealth=comphealth-quickAttackD;
}else{
System.out.println ("Attack failed.");
}
}
if (comphealth <=0) {
System.out.println ("Level 1 zombie health is critically low! You win!");
wincount++;
winCondition = false;
break;
}
System.out.println ("Level 1 zombie health is now at " + comphealth + ".");
}
//BEGINNING OF COMPUTER'S TURN
compChoice=h.nextInt(3)+1; //the computer's choice of attack.Placed after user turn to prevent a first turn mess up
hc=a.nextInt(99)+1;
powerAttackD=b.nextInt(15)+20;
regularAttackD=d.nextInt(8)+10;
quickAttackD=f.nextInt(4)+1;
shield=g.nextInt(15)+6;
if (userhealth<15)
{
compChoice=h.nextInt(2)+1;
}
if (compChoice==1){
if (userChoice==4){
regularAttackD=regularAttackD-shield;
System.out.println ("You have used a shield! Damage reduced by " + shield + ".");
}
if (hc>=20){
if (regularAttackD<0){
regularAttackD=0;
}
System.out.println ("Level 1 zombie's attack successful for " + regularAttackD + " damage.");
userhealth=userhealth-regularAttackD;
}else{
System.out.println ("Level 1 zombie's attack failed.");
}
if (userhealth <=0) {
System.out.println ("You have died.");
winCondition = false;
break;
}
System.out.println ("Your health now at " + userhealth + ".");
}
if (compChoice==2){
if (userChoice==4){
powerAttackD=powerAttackD-shield;
System.out.println ("You have used a shield! Damage reduced by " + shield + ".");
}
if (hc>=40){
if (powerAttackD<0){
powerAttackD=0;
}
System.out.println ("Level 1 zombie's power attack hits successfully for " + powerAttackD + " damage!");
userhealth=userhealth-powerAttackD;
}else{
System.out.println ("Oppenent's attack failed!");
}
if (userhealth <=0) {
System.out.println ("You have died.");
winCondition = false;
break;
}
System.out.println ("Your health is now at " + userhealth + ".");
}
if (compChoice==3)
{
quickAttackD=quickAttackD*3;
if (userChoice==4){
quickAttackD=quickAttackD-shield;
System.out.println ("You have used a shield! Damage reduced by " + shield + ".");
}
if (hc>=10){
if (quickAttackD<0){
quickAttackD=0;
}
System.out.println ("Level 1 zombie's quick attacks hit for a total of " + quickAttackD + " damage!");
userhealth=userhealth-quickAttackD;
}else{
System.out.println ("Level 1 zombie's attack missed!");
}
if (userhealth <=0) {
System.out.println ("You have died.");
winCondition = false;
break;
}
System.out.println ("Your health is now at " + userhealth + ".");
}
} while (winCondition); //don't define winCondition, IT IS ALREADY TRUE
} }
Upvotes: 0
Views: 41
Reputation: 2317
Since you have a class you should add to it a constructor that have all the parameters entered by the user and pass these parameters when creating the game instance.
For exemple if you need to pass playerClass
to Level1ZombieClass add in Level1ZombieClass :
public Level1ZombieClass (int playerClass, int userhealth, <type> otherParam ...){
this.playerClass = playerClass;
this.userhealth = userhealth;
this.otherParam = otherParam;
...
}
And when creating the level instance add the variables you need to pass
Level1ZombieClass Level1ZombieClassObject = new Level1ZombieClass (playerClass,userhealth,otherParam,...);
You will have to (and should) put these variables outside the methods (function) of your class. Your code should be something like this
public class Level1ZombieClass
{
int playerClass;
int userhealth;
<type> otherParam;
...
public Level1ZombieClass (int playerClass){
this.playerClass = playerClass;
this.userhealth = userhealth;
this.otherParam = otherParam;
...
}
public void ZombieLevel1Class()
{
Scanner in = new Scanner(System.in);
//Variables that are not user-dependent
int comphealth=100;
int hc;
int userChoice=1;
int critdamage;
//rest of code
Upvotes: 1
Reputation: 1232
You need to pass the playerClass
value when the ZombieLevel1Class()
method is called. For example:
public void ZombieLevel1Class(int pClass)
{
int playerClass = pClass;
//etc etc
}
Then you would call the method in the other class like this:
Level1ZombieClassObject.ZombieLevel1Class(playerClass);
Upvotes: 0