Java角色扮演游戏

时间:2016-09-23 02:55:38

标签: java

尝试在java中设计角色扮演游戏,代码在选择“a”并且名称作为目标输入后停止。不确定发生了什么。尝试改变为for,但没有任何工作。 非常感谢提前

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());
                }
    }
    }
}

生物类

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; }



}

1 个答案:

答案 0 :(得分:3)

你这里有一个无限循环

suppressmessage(174);
f = ((x+0.055)/1.055)^2.4;
p0 = fpminimax(f, 7, [|D...|], [0.04045;1], relative);
p = fpminimax(f/(p0(1)+1e-18), 7, [|D...|], [0.04045;1], relative);
print("relative:", dirtyinfnorm((f-p)/f, [s;1]));
print("absolute:", dirtyinfnorm((f-p), [s;1]));
print(canonical(p));

s = 0.0523;
z = 3;
f = 1.055 * x^(1/2.4) - 0.055;

p = fpminimax(1.055 * (x^(z+1/2.4) - 0.055*x^z/1.055), 7, [|D...|], [0.0031308;s], relative)/x^z;
print("relative:", dirtyinfnorm((f-p)/f, [0.0031308;s]));
print("absolute:", dirtyinfnorm((f-p), [0.0031308;s]));
print(canonical(p));

p = fpminimax(1.055 * (x^(z+1/2.4) - 0.055*x^z/1.055), 7, [|D...|], [s;1], relative)/x^z;
print("relative:", dirtyinfnorm((f-p)/f, [s;1]));
print("absolute:", dirtyinfnorm((f-p), [s;1]));
print(canonical(p));

将i ++改为m ++

您的代码中还有另一个问题:

$('.modal').on('show.bs.modal', function (event) {
        var idx = $('.modal:visible').length;
        $(this).css('z-index', 1040 + (10 * idx));
    });
    $('.modal').on('shown.bs.modal', function (event) {
        var idx = ($('.modal:visible').length) - 1; // raise backdrop after animation.
        $('.modal-backdrop').not('.stacked').css('z-index', 1039 + (10 * idx));
        $('.modal-backdrop').not('.stacked').addClass('stacked'); 
    });
   $('.modal').on('hide.bs.modal', function (event) { 
        var modal_length =  $('.modal:visible').length;
        if (modal_length>1)
        {
            $("body").css("overflow","hidden");
            $('.modal:visible').css("overflow-y","auto");
        }
    });

这里你得到了第j个玩家,但是你不在任何地方使用它,你只需将targetName分配给正在攻击的当前玩家名称。正确的方法是

            for(int m=0; m<Player.size()-1; i++){

一些建议:

  • 将播放器重命名为播放器,java约定是为变量使用lowerscore名称,复数将记住你是一个列表

  • java内置for循环更直观,我会将上面的for循环更改为:

                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++;
                }