2016-12-28 56 views
0

我與NetBeans IDE8.2編譯多個Java類,用記事本++

我Player.java

/* 
    * To change this license header, choose License Headers in Project Properties. 
    * To change this template file, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    package sokoban; 

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

    class Player { 

    //Standardkontruktor 
    public Player() { 

    } 

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

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

    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; 

    /** 
    * 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]}; 
    } 

    //move Methode 
    /** 
    * Makes a move 
    * 
    * @param direction as a vector 
    * @return true iff it was successful, otherwise false 
    */ 
    public 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; 
     } 
    } 

    } 

我的等級編制了推箱子游戲的以下面向對象的Java implemetation沒有任何錯誤。 java的

package sokoban; 

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

    class Level { 

    //Standardkontruktor 
    public Level() { 

    } 

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

    //Objekt Namens myPlayer vom Typ Player als Attribut eines Levels 
    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 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 static int freeBox; 
    private static int emptyGoal; 

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

    Player myPlayer = new Player(this.room); 

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

    public 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; 
     } 
     return safwensTempString; 
    } 

    /** 
    * Game logic for Sokoban 
    * 
    * @return true if the level was solved, otherwise false 
    */ 
    public 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(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; 
    } 

    } 

我Sokoban.java

package sokoban; 

    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 
    * 
    */ 
    public class Sokoban { 

    private static Level myLevel = new Level(); 

    /** 
    * 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"); 
    } 
    } 

我Sokoban.txt(遊戲中的地圖)

 ####### 
    #[email protected] # # 
    #$* $ # 
    # $ # 
    # .. # 
    # * # 
    ####### 

誰能告訴我如何編譯這個遊戲用記事本++?

我試圖編譯這樣的:

步驟1:F6

步驟2:

cd C:\Users\Noureddine\Desktop\Sokoban finale Version 
java Sokoban 
javac Sokoban.java 

,得到了以下錯誤:

Process started >>> Fehler: Hauptklasse Sokoban konnte nicht gefunden oder geladen werden <<< Process finished. (Exit code 1) javac Sokoban.java

備註:我嘗試安裝ViSimulator但安裝失敗。

enter image description here

預先感謝您的答案和提示!

+0

我猜cd的失敗是由於裏面的空間。可能你需要'cd「C:\ Users \ Noureddine \ Desktop \ Sokoban壓軸版」「(用qoutes)。 –

+0

不,這不是問題。謝謝你的評論。 –

+0

然後下一步是在cmd中單獨在記事本++之外嘗試它,看看需要什麼,直到它在那裏工作,我的意思是設置環境變量,如'PATH'和'CLASSPATH'等.. –

回答