Reputation: 3
Trying to design a role playing game in java, code stops after "a" is selected and name is entered as target. Not sure what is going on. Tried changing while to a for, but nothing has worked. Much thanks in advance
package fantasy.rpg;
import java.util.Scanner;
import java.util.ArrayList;
public class FantasyRPG {
public static void main(String[] args) {
String name;
String newName;
String race;
ArrayList<Creature> Player = new ArrayList();
Scanner reader = new Scanner(System.in);
System.out.println("Welcome to the Fantasy Game"+"\n");
System.out.println(" 1. Add the game players");
System.out.println(" 2. Playes play in turn until only one is left"+"\n");
System.out.println("Good Luck!"+"\n");
System.out.println("First, let's add some players:"+"\n");
System.out.println("");
System.out.println("Enter player's name ('quit' when no more players):");
name= reader.nextLine();
System.out.println(name + ", what race are you?");
System.out.println(" H/h: human \n C/c: Cyberdemon \n E/e: Elf \n B/b: Balrog \n");
System.out.print("Please enter your race: ");
race = reader.next();
Creature player = new Creature(name, race);
Player.add(player);
System.out.println("");
System.out.println("Enter player's name ('quit' when no more players):");
newName = reader.next();
while(!"quit".equals(newName)){
System.out.println(newName + ", what race are you?");
System.out.println(" H/h: human \n C/c: Cyberdemon \n E/e: Elf \n B/b: Balrog \n");
System.out.print("Please enter your race: ");
race = reader.next();
player = new Creature(newName, race);
Player.add(player);
System.out.println("");
System.out.println("Enter player's name ('quit' when no more players):");
newName = reader.next();
}
System.out.println("Congratulations new fighters. ");
System.out.println("You will pitted against each other \nin a fight to the death");
System.out.println("The last one standing wins. \nGood luck.");
System.out.println(" |Name| |Species| |Strength| |Hit Points|");
for(int count = 0; count <= Player.size()-1; count++ ){
Creature p = Player.get(count);
System.out.println(p.toString());}
System.out.println("The Players are ready! \nLet the Battle Begin!");
for(int i=0; i<=Player.size()-1; i++){
Player.get(i);
System.out.println(name.toString()+", select one of the following options:");
System.out.println(" a/A: Attack an opponent");
System.out.println(" p/P: Pass (go to the next player");
System.out.println(" q/Q: Quit the game");
String choice=reader.next();
if(choice.equals("a")||choice.equals("A")){
System.out.println("Which player are you attacking? ");
String target=reader.next();
int hitp=0;
for(int j=0; j<Player.size()-1; j++){
Player.get(j);
String targetName=name;
if(target.equals(targetName)){
hitp=Creature.getHitpoints(name);
}
else{
j++;
}
}
int HitDam=Creature.getDamage(race, hitp);
Creature.Damage(HitDam);
}
else if(choice.equals("p")||choice.equals("P")){
return;
}
else if(choice.equals("q")||choice.equals("Q")){
Player.remove(i);
}
for(int m=0; m<Player.size()-1; i++){
int stren=0;
Player.get(m);
stren=Creature.getStrength();
if (Creature.isDead(stren)){
System.out.println("Sorry, "+name.toString()+", but you are dead. Thanks for playing!");
Player.remove(m);
}
}
System.out.println(" |Name| |Species| |Strength| |Hit Points|");
for(int count = 0; count <= Player.size()-1; count++ ){
Creature p = Player.get(count);
System.out.println(p.toString());
}
}
}
}
Creature class
package fantasy.rpg;
public class Creature {
static int Strength;
static int HitPoints;
String Name = "";
String Species = "";
int damage;
public Creature(String name, String species){
Name = name;
if(species.equals("h")||species.equals("H")){
Species = "Human";
Strength = 115;
HitPoints = 15;}
else if(species.equals("C")||species.equals("c")){
Species = "Cyberdemon";
Strength = 135;
HitPoints = 20;}
else if(species.equals("E")||species.equals("e")){
Species = "Elf";
Strength = 185;
HitPoints = 18;
}
else if(species.equals("B")||species.equals("b")){
Species = "Balrog";
Strength = 105;
HitPoints = 30;}
}
public static int getHitpoints(String player){
return HitPoints;
}
public String getSpecies(){
return Species;
}
public static int getStrength(){
return Strength;
}
public void setStrength(int newStrength){
Strength = newStrength;}
public void setHitPoints(int newHit){
HitPoints = newHit;}
public static int getDamage(String Species, int Hit){
int hit = 0+ (int)(Math.random()*(Hit-0));
if(Species == "demon"){
int r =(int)(Math.random());
if(r < .05)
hit = hit + 50;
}
else if(Species == "elf"){
int r = (int)(Math.random());
if(r < .1)
hit = hit + 50;
}
else if(Species == "balrog"){
int balrogDouble = 0 + (int)(Math.random() * Hit - 0);
hit = hit + balrogDouble;}
int damage = hit;
return damage;
}
public static void Damage(int damage){
Strength= Strength-damage;}
public static boolean isDead(int str){
if(str <= 0)
return true;
else
return false;
}
public String getName(){
return Name;
}
public boolean isNamed(String aName){
return aName.equals(Name);
}
public String toString(){
return " " +Name + " " + Species + " " + Strength + " " + HitPoints; }
}
Upvotes: 0
Views: 1950
Reputation: 423
You have an infinite loop here
for(int m=0; m<Player.size()-1; i++){
change i++ to m++
You also have another problem in your code:
for(int j=0; j<Player.size()-1; j++){
Player.get(j);
String targetName=name;
if(target.equals(targetName)){
hitp=Creature.getHitpoints(name);
}
else{
j++;
}
here you are getting the j-th player but you don't use it anywhere, you just assign targetName to the current player name that is attacking. Proper way to do it is
String targetName = Player.get(j).getName();
Some suggestions:
rename Player to players, java convention is to use lowerscore names for variables, and the plural will remember you is a list
java has a built in for loop that is more intuitive, I would change the for loop above to be like this:
for(Creature targetPlayer: Player){
String targetName = targetPlayer.getName();
if(target.equals(targetName)){
hitp=targetPlayer.getHitpoints(name);
}
}
Upvotes: 3