我正在为我的编程类制作基于文本的Rpg并遇到错误

时间:2015-01-22 17:17:21

标签: java loops

在我的代码中我使用x和y线在游戏中移动并且它应该正常工作但是在线约490它给了我一个没有,如果即使有一个当我尝试使用它时它去了它应该在哪里 (对不起代码转储我无法确定我是java新手的问题)

  /**
     * @(#)FSE.java
     *
     * FSE application
     *
     * @author 
     * @version 1.00 2015/1/7
     */
         import java.io.*;
         import java.util.Random;
        public class FSE {

        public static void main(String[] args) {    

          BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
          // Declaring Variables
            String Name, Move, Attack;
            int x, y, Combat, Creature, HP, HP2, Strike, Strike2, loop;
            x=0;
            y=0; 
            Combat=0;
            Creature=0;
            Move="";
            Attack="";
            HP=0;
            HP2=0;
            Strike=0;
            Strike2=0;
            loop=0;
                    // Introduction to start the game off
            System.out.println("Your Awake Traveler Do You Remember What   Happened");
            try {
          Thread.sleep(2500L);
            }
    catch (Exception e) {}  
                System.out.println("You Look Around At What looks Like A Old Cave And See A Colapsed Entrance To The North");
                    try {
          Thread.sleep(2500L);
            }
    catch (Exception e) {}
            System.out.println("Do Remember What Your Name Is");
                try{
                    Name=input.readLine();
                 }

                 catch (IOException ioe){
                    System.out.println("input error");
                 }
                 while(loop==0){
                    System.out.println("What Way Would You Like To Navigate Use WASD To Choose A Direction");
                 try{
                    Move=input.readLine();
                 }
                 // Movement Control Keys
                 catch (IOException ioe){
                    System.out.println("input error");          
                 }  
                 if(Move.equals("w")){ // Upward Movement
                    y+=1;   
                    System.out.println("*****"+y);
                 }  
                 else if(Move.equals("d")){ // Right Movement
                    x+=1;
                 }  
                 else if(Move.equals("s")){ // Downward Movement
                    y-=1;
                 }  
                 else{
                    x-=1; // Left Movement Key ("a")
                 }
                 if (y==-1 && x==0){ // First-Entrance into the game's combat
                    System.out.println("You have now entered the cave.");
                    Combat=1;
                    while(Combat==1);{
                        System.out.println("A Goblin Has Attacked");
                        HP=20;
                        HP2=8;
                        System.out.println("Press A To Attack");
                        try{
                            Attack=input.readLine();
                        }
                        catch (IOException ioe){
                            System.out.println("input error");          
                        }   
                        if(Attack.equals("A")){
                            Random randomGenerator=new Random();
                            Strike=randomGenerator.nextInt(10);
                            Strike+=5;
                            HP2-=Strike;
                            System.out.println("You Did "+Strike+" Damage");
                            System.out.println("The Goblin Has "+HP2+" Life Left");
                            Strike2=randomGenerator.nextInt(5);
                            Strike2+=5;
                            HP-=Strike2;
                            System.out.println("The Goblin Did "+Strike2+" Damage");
                            System.out.println("You Have "+HP+" Life Left");
                            if(HP<=0){
                                System.out.println("You Died. Game Over.");
                                System.exit(0);
                            }
                            else if(HP2<=0){
                                Combat=0;
                                System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");  
                            }

                            }
                            }               
                             }
                else if (y==-2 && x==0){ // Path leading downwards from entrance pointt
    System.out.println("You have entered a new room in the cave!");
    Combat=1;
    while(Combat==1);{
    System.out.println("A Imp Has Attacked");
    HP=20;
    HP2=7;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Imp Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(2);
    Strike2+=2;
    HP-=Strike2;
    System.out.println("The Imp Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
            System.out.println("You Died. Game Over.");
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Imp And May Continue In Your Quest"); 
    }

    }
    } 
                }   
    else if (y==-3 && x==0){
       System.out.println("You have entered the next room.");
       Combat=1;
           while(Combat==1);{
              System.out.println("A Goblin Has Attacked"); 
              HP=20;
              HP2=8;
              System.out.println("Press A To Attack");
              try{
                 Attack=input.readLine();
              }
              catch (IOException ioe){
                 System.out.println("input error");          
              } 
              if(Attack.equals("A")){
                 Random randomGenerator=new Random();
                 Strike=randomGenerator.nextInt(10);
                 Strike+=5;
                 HP2-=Strike; 
                 System.out.println("You Did "+Strike+" Damage");
                 System.out.println("The Goblin Has "+HP2+" Life Left");
                 Strike2=randomGenerator.nextInt(5);
                 Strike2+=5;
                 HP-=Strike2;
        System.out.println("The Goblin Did "+Strike2+" Damage");
        System.out.println("You Have "+HP+" Life Left");
        if(HP<=0){
                System.out.println("You Died. Game Over.");
        System.exit(0);
        }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");  
    }

    }
    }    
    }
    else if (y==-4 && x==0){
    System.out.println("You have entered the final room on the path! The path does not go any further.");
    Combat=1;
    while(Combat==1);{
    System.out.println("A Troll Has Attacked");
    HP=20;
    HP2=10;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Troll Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(7);
    Strike2+=7;
    HP-=Strike2;
    System.out.println("The Troll Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
            System.out.println("You Died. Game Over.");
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Troll And May Continue In Your Quest");   
    }

    }
    }    
    }
    else if (y==-1 && x==1){ // Right path from the entrance point
    System.out.println("You have entered a new room in the cave!");
    Combat=1;
    while(Combat==1);{
    System.out.println("A Goblin Has Attacked");
    HP=20;
    HP2=8;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Goblin Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(5);
    Strike2+=5;
    HP-=Strike2;
    System.out.println("The Goblin Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
            System.out.println("You Died. Game Over.");
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");  
    }

    }
    }    
    }
    else if (y==-1 && x==2){
    System.out.println("You have entered the next room.");
    Combat=1;
    while(Combat==1);{
    System.out.println("A Giant Spider Has Attacked");
    HP=20;
    HP2=8;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Giant Spider Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(6);
    Strike2+=6;
    HP-=Strike2;
    System.out.println("The Giant Spider Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
            System.out.println("You Died. Game Over.");
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Giant Spider And May Continue In Your Quest");    
    }

    }
    }    
    }
    else if (y==-1 && x==3){
    System.out.println("You have entered the next room.");
    Combat=1;
    while(Combat==1);{
    System.out.println("A Imp Has Attacked");
    HP=20;
    HP2=8;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Imp Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(2);
    Strike2+=2;
    HP-=Strike2;
    System.out.println("The Imp Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
        System.out.println("You Died. Game Over."); 
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Imp And May Continue In Your Quest"); 
    }

    }
    }    
    }
    else if (y==-2 && x==3){ //Path going downwards towards the boss
    System.out.println("You have entered the next room.");
    Combat=1;
    while(Combat==1);{
    System.out.println("A Giant Spider Has Attacked");
    HP=20;
    HP2=8;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Goblin Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(5);
    Strike2+=5;
    HP-=Strike2;
    System.out.println("The Goblin Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
            System.out.println("You Died. Game Over.");
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");  
    }

    }
    }    
    }
    else if (y==-3 && x==3){
    System.out.println("You have entered the next room.");
    Combat=1;
    while(Combat==1);{
    System.out.println("A Goblin Has Attacked");
    HP=20;
    HP2=8;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Goblin Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(5);
    Strike2+=5;
    HP-=Strike2;
    System.out.println("The Goblin Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
            System.out.println("You Died. Game Over.");
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");  
    }

    }
    }    
    }
    else if (y==-4 && x==3){
    System.out.println("You have entered the next room.");
    Combat=1;
    while(Combat==1);{
    System.out.println("A Goblin Has Attacked");
    HP=20;
    HP2=8;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Goblin Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(5);
    Strike2+=5;
    HP-=Strike2;
    System.out.println("The Goblin Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
            System.out.println("You Died. Game Over.");
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Goblin And May Continue In Your Quest");  
    }

    }
    }    
    }
    else if (y==-4 && x==1){
    System.out.println("You have entered the final room in the cave! You are now in the Demon's Domain!"); //Final Boss Battle
    System.out.println("*"+y+"*"+x+"*");
    Combat=1;
    while(Combat==1);{
    System.out.println("The Demon has attacked");
    HP=20;
    HP2=15;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Goblin Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(8);
    Strike2+=5;
    HP-=Strike2;
    System.out.println("The Goblin Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
            System.out.println("You Died. Game Over.");
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Demon! Conagradulations!");   
    }

    }
    }    
    }
    }
        else if (y==-4 && x==2){
    System.out.println("You have entered the final room in the cave! You are now in the Demon's Domain!"); //Final Boss Battle
    system.out.println("*"+y+"*"+x+"*");
    Combat=1;
    while(Combat==1);{
    System.out.println("The Demon has attacked");
    HP=20;
    HP2=15;
    System.out.println("Press A To Attack");
    try{
    Attack=input.readLine();
    }
    catch (IOException ioe){
    System.out.println("input error");          
    }   
    if(Attack.equals("A")){
    Random randomGenerator=new Random();
         Strike=randomGenerator.nextInt(10);
    Strike+=5;
    HP2-=Strike;
    System.out.println("You Did "+Strike+" Damage");
    System.out.println("The Goblin Has "+HP2+" Life Left");
         Strike2=randomGenerator.nextInt(8);
    Strike2+=5;
    HP-=Strike2;
    System.out.println("The Goblin Did "+Strike2+" Damage");
    System.out.println("You Have "+HP+" Life Left");
    if(HP<=0){
            System.out.println("You Died. Game Over.");
    System.exit(0);
    }
    else if(HP2<=0){
    Combat=0;
    System.out.println("You Have Defeated The Demon! Conagradulations!");   
    }

    }
    }    
    }
    }
    // Spaces in the game where you are not allowed to go
    if (y==1 && x==0);{
    System.out.println("You cannot go this way.");
    y=0;
    x=0;
    }
    else if (y==0 && x==-1){
    System.out.println("You cannot go this way.");
    y=0;
    x=0;
    }
    else if (y==0 && x==1){
    System.out.println("You cannot go this way.");
    y=-1;
    x=0;
    }
    else if (y==0 && x==2){
    System.out.println("You cannot go this way.");
    y=-1;
    x=2;
    }
    else if (y==0 && x==3){
    System.out.println("You cannot go this way.");
    y=-1;
    x=3;
    }
    else if (y==-1 && x==4){
    System.out.println("You cannot go this way.");
    y=-1;
    x=3;
    }
    else if (y==-2 && x==4){
    System.out.println("You cannot go this way.");
    y=-2;
    x=3;
    }
    else if (y==-3 && x==4){
    System.out.println("You cannot go this way.");
    y=-3;
    x=3;
    }
    else if (y==-4 && x==4){
    System.out.println("You cannot go this way.");
    y=-4;
    x=3;
    }
    else if (y==-5 && x==0){
    System.out.println("You cannot go this way.");
    y=-4;
    x=0;
    }
    else if (y==-2 && x==1){
    System.out.println("You cannot go this way.");
    y=-1;
    x=0;
    }
    else if (y==-2 && x==-2){
    System.out.println("You cannot go this way.");
    y=-1;
    x=3;
    }   
    else if (y==-3 && x==1){
    System.out.println("You cannot go this way.");
    y=-3;
    x=0;
    }
    else if (y==-5 && x==3){
    System.out.println("You cannot go this way.");
    y=-4;
    x=3;
    }
    else{
        System.out.println("You cannot go this way")
            y=-3;
            x=3;
    }       

            }
    }

1 个答案:

答案 0 :(得分:0)

我花了3次尝试从Stack Overflow复制并粘贴你的代码。

您有4个语法错误。您粘贴的代码甚至无法编译。

您最大的问题是整个Java应用程序是一种方法(主要)。

Java的设计使您可以将代码分解为许多,许多,许多方法,并在必要时将许多类分解。当您创建涉及的内容时,应使用model / view / controller (MVC) pattern。 MVC模式允许您分离您的疑虑,并使您能够一次关注程序的一小部分。

让我们先看看你的介绍。您两次显示一条消息,然后等待。以下是创建方法的方法。

private static void displayMessageAndWait(String message) {
    System.out.println(message);
    try {
        Thread.sleep(2500L);
    } catch (InterruptedException e) {
    }
}

使用此方法,您的介绍现在看起来像这样。

    // Introduction to start the game off
    String message = "You're Awake Traveler. Do You Remember What Happened?";
    displayMessageAndWait(message);
    message = "You Look Around At What looks Like A Old Cave " +
            "And See A Collapsed Entrance To The North.";
    displayMessageAndWait(message); 

您是否看到创建方法如何记录代码正在执行的操作?通过将代码分解成微小的小块,并将这些部分放在方法中,可以使Java应用程序更易于理解,也可以让其他人理解。

接下来,我们将查看获取名称。我们将代码放在一个名为getName的方法中。聪明,呃。

private static String getName(BufferedReader input) {
    System.out.println("Do You Remember What Your Name Is?");
    try {
        String name = input.readLine();
        return name;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return "";
    }
}

现在,main方法中的代码如下所示。

    name = getName(input);

Java变量名称和方法名称以小写字母开头。这样我们就可以一目了然地区分类名和方法名或变量名。

到目前为止,我所做的一切都是破解你的代码。 Java的真正强大之处在于您创建可用于为Java应用程序建模的类。

这里以Room模型类为例。

public class Room {

    private boolean isCreatureInRoom;

    private int x;
    private int y;

    private String description;

    public Room(int x, int y, String description) {
        this.x = x;
        this.y = y;
        this.description = description;
        this.isCreatureInRoom = false;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public String getDescription() {
        return description;
    }

    public boolean isCreatureInRoom() {
        return isCreatureInRoom;
    }

    public void setCreatureInRoom(boolean isCreatureInRoom) {
        this.isCreatureInRoom = isCreatureInRoom;
    }

}

现在,我们可以为每个我们想要的房间制作Room类的实例,从而创建尽可能多的房间。

希望这足以让您回过头来将您的程序分解为许多方法和类。