2016-12-28 53 views
0

編譯我的一個推箱子游戲的Java實現,當我收到以下錯誤時:Java的面向對象的遊戲:在對象生成錯誤編譯

<<< Process finished. (Exit code 1) javac Sokobantest.java Process started >>> .\Level.java:44: error: expected myPlayer = new Player(this.room); ^.\Level.java:44: error: cannot find symbol myPlayer = new Player(this.room);^ symbol: class myPlayer location: class Level Sokobantest.java:43: error: cannot find symbol Level myLevel = new Level(this.room);

這裏是我到目前爲止的代碼:

Sokobantest的.java

import java.io.BufferedReader; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.Scanner; 

/** 
* This class is the second part for the Sokoban game 
* 
* @author Jane Doe 1234567 Group 42h 
* @author John Doe 1234567 Group 42h 
*/ 
class Sokobantest { 

    private final static int X = 0; 
    private final static int Y = 1; 

    private final static char WALL = '#'; 
    private final static char PLAYER = '@'; 
    private final static char BOX = '$'; 
    private final static char GOAL = '.'; 
    private final static char PLAYER_ON_GOAL = '+'; 
    private final static char BOX_ON_GOAL = '*'; 
    private final static char FREE = ' '; 

    private final static int[] UP = {0, -1}; 
    private final static int[] DOWN = {0, 1}; 
    private final static int[] LEFT = {-1, 0}; 
    private final static int[] RIGHT = {1, 0}; 

    //private static char[][] room; 
    private static int freeBox; 
    private static int emptyGoal; 

    private static int[] size = {-1, 0}; 
    private static int[] player; 





    Level myLevel = new Level(); 
    myLevel = new Level(this.room); 


    /** 
    * Function for vector addition 
    * 
    * @param first first vector 
    * @param second second vector 
    * @return new vector = first + second 
    */ 
    private static int[] add(int[] first, int[] second) { 
     return new int[]{first[X] + second[X], first[Y] + second[Y]}; 
    } 



    /** 
    * The Main method for the Sokoban game with contains all of the game  logic 
    * 
    * @param args args[0] the path to the level 
    */ 
    public static void main(String[] args) { 
     String file = "sokoban.txt"; 
     if (args.length > 0) { 
      file = args[0]; 
     } 
     if (!myLevel.isValidLevel(file)) { 
      System.err.println("Level has an invalid format"); 
      return; 
     } 
     if (myLevel.isCompleted()) { 
      System.out.println("Yeah you have solved the level :)"); 
     } else { 
      System.out.println("You have not solved the level :("); 
     } 
     System.out.println(myLevel.toString()); 
     System.out.println("Goodbye"); 
    } 
} 

Player.java

class Player{ 

    //Standardkontruktor 
    public Player() 
    { 

    } 

    //Parametrisierter Konstruktor 

     public Player(char[][] room) 
     { 
      this.room = room; 
     } 

    //Attribut Raum 
    private static char[][] room; 


//Attribut Spilerposition 

//move Methode 
/** 
    * Makes a move 
    * 
    * @param direction as a vector 
    * @return true iff it was successful, otherwise false 
    */ 
    private static boolean move(int[] direction) { 
     int[] next = add(player, direction); 

     switch (room[next[Y]][next[X]]) { 
      case BOX_ON_GOAL: 
      case BOX: 
       int[] behind = add(next, direction); 
       if (!(room[behind[Y]][behind[X]] == FREE || room[behind[Y]] [behind[X]] == GOAL)) { 
        return false; 
       } 

       if (room[next[Y]][next[X]] == BOX_ON_GOAL) { 
        emptyGoal++; 
        freeBox++; 
       } 

       if (room[behind[Y]][behind[X]] == GOAL) { 
        room[behind[Y]][behind[X]] = BOX_ON_GOAL; 
        emptyGoal--; 
        freeBox--; 
       } else { 
        room[behind[Y]][behind[X]] = BOX; 
       } 

       if (room[next[Y]][next[X]] == BOX_ON_GOAL) { 
        room[next[Y]][next[X]] = GOAL; 
       } else { 
        room[next[Y]][next[X]] = FREE; 
       } 
      case GOAL: 
      case FREE: 
       if (room[player[Y]][player[X]] == PLAYER_ON_GOAL) { 
        room[player[Y]][player[X]] = GOAL; 
       } else { 
        room[player[Y]][player[X]] = FREE; 
       } 

       player = next; 

       if (room[player[Y]][player[X]] == FREE) { 
        room[player[Y]][player[X]] = PLAYER; 
       } else { 
        room[player[Y]][player[X]] = PLAYER_ON_GOAL; 
       } 
       return true; 
      default: 
       return false; 
     } 
    } 

} 

Level.java

class Level{ 

    //Standardkontruktor 
    public Level() 
    { 

    } 

    //Parametrisierter Konstruktor 

     public Level(char[][] room) 
     { 
      this.room = room; 
     } 

    //Objekt Namens myPlayer vom Typ Player als Attribut eines Levels 



Player myPlayer = new Player(); 
myPlayer = new Player(this.room); 


    //Attribut Raum 
    private static char[][] room; 

     private boolean isValidLevel(String file){ 
     return this.loadLevel(file); 
    } 

    //Methode LoadLevel 
     /** 
    * Loads the level from the "file" and validate it 
    * 
    * @param file path to the file 
    * @return false iff an error occurs or the level is invalid, true otherwise 
    */ 
    private static boolean loadLevel(String file) { 
     BufferedReader bufferedReader; 
     try { 
      bufferedReader = Files.newBufferedReader(Paths.get(file)); 
      bufferedReader.mark(100 * 100); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       size[Y]++; 
       if (size[X] > -1 && size[X] != line.length()) { 
        return false; 
       } else { 
        size[X] = line.length(); 
       } 
      } 

      bufferedReader.reset(); 
      room = new char[size[Y]][]; 

      int i = 0; 
      while ((line = bufferedReader.readLine()) != null) { 
       room[i] = new char[line.length()]; 
       for (int j = 0; j < line.length(); j++) { 
        room[i][j] = line.charAt(j); 
       } 
       i++; 
       // oder room[i++] = line.toCharArray(); 
      } 
      bufferedReader.close(); 
     } catch (IOException e) { 
      return false; 
     } 

     for (int i = 0; i < room.length; i++) { 
      for (int j = 0; j < room[i].length; j++) { 
       switch (room[i][j]) { 
        case FREE: 
        case BOX_ON_GOAL: 
        case WALL: 
         break; 
        case PLAYER_ON_GOAL: 
         emptyGoal++; 
        case PLAYER: 
         if (player != null) { 
          return false; 
         } else { 
          player = new int[]{j, i}; 
         } 
         break; 
        case BOX: 
         freeBox++; 
         break; 
        case GOAL: 
         emptyGoal++; 
         break; 
        default: 
         return false; 
       } 
      } 
     } 
     return !(player == null || emptyGoal != freeBox); 
    } 


       //Methode toString für die Ausgabe des Spielfeldes 

    /** 
    * Prints the level to the output stream 
    */ 
    public String toString() { 
     String safwensTempString= ""; 
     for (char[] row : room) { 
      safwensTempString=safwensTempString+row; 
     } 
    } 

    /** 
    * Game logic for Sokoban 
    * 
    * @return true if the level was solved, otherwise false 
    */ 
    private static boolean isCompleted() { 
     // create new Scanner that reads from console 
     Scanner input = new Scanner(System.in); 

     // flag if we quit the program 
     boolean run = true; 
     int[] direction; 
     do { 
      System.out.println(myLevel.toString()); 
      System.out.println("Do you want to go up, down, left, right or exit the program?"); 

     // check which command was chosen and execute it 
     switch (input.next()) { 
      case "w": 
      case "up": 
       direction = UP; 
       break; 
      case "s": 
      case "down": 
       direction = DOWN; 
       break; 
      case "a": 
      case "left": 
       direction = LEFT; 
       break; 
      case "d": 
      case "right": 
       direction = RIGHT; 
       break; 
      case "exit": 
       run = false; 
       continue; 
      default: // if the user input is not one of our commands print help 
       System.out.println("Command unknown! Please type up, down, left or right to move or exit to quit this program"); 
       continue; 
     } 

     if (!myPlayer.move(direction)) { 
      System.out.println("You can not go there!"); 
     } 
    } while (run && emptyGoal != 0 && freeBox != 0); 
    return run; 
} 

} 

預先感謝任何提示或幫助!

EDIT

我編譯在記事本++這樣的:

enter image description here

+1

爲什麼'myPlayer'分配兩次?你可以在任何方法之外聲明**和**,如果你想分配給一個變量 – SomeJavaGuy

+1

並且你使用了一個'static char [] []',不確定你不應該在這裏使用'this' – AxelH

回答

0

語句必須被放置內的方法(或其中添加到構造的初始化劑塊)。您只能聲明和初始化方法外的字段。

而不是

Level myLevel = new Level(); 
myLevel = new Level(this.room); 

你可以寫

Level myLevel = new Level(this.room); 

讓我舉一個Hello World程序的例子有兩個類

$ vi A.java 

class A { 
static B b = new B(); 
public static void main(String...a) { 
    System.out.println(b.message); 
} 
} 

$ vi B.java 

class B { 
    String message = "Hello World"; 
} 

$ javac A.java 

$ java A 

打印

Hello World 
+0

我應用了你的答案,並得到以下錯誤:'過程開始>>> Fehler:Hauptklasse Sokobantest konnte nicht gefunden oder geladen werden <<<過程已完成。 (退出代碼1) javac Sokobantest.java 過程開始>>> Sokobantest.java:42:錯誤:找不到符號 Level myLevel = new Level(this.room);' –

+0

我會用英文翻譯它':主要課堂Sokobantest無法找到或加載。 –

+1

我不喜歡這樣的選擇。語句也可以在方法外的塊中重新組合。 – AxelH