0

I compiled the following object oriented java implemetation of a Sokoban game with no errors with NetBeans IDE8.2

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

    }

My Level.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;
    }

    }

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

My Sokoban.txt (The map of the game)

     #######
     #.@ # #
     #$* $ #
     #   $ #
     # ..  #
     #  *  #
     #######

Can anyone tell me how to compile this game with Notepad++?

I tried to compile like this:

Step 1: F6

Step 2:

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

and got the following error:

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

Remark: I tried to install the ViSimulator but the installation failed.

enter image description here

Thanks in advance for your answers and hints!

5
  • I guess the cd fails due to the spaces inside. Probably you need cd "C:\Users\Noureddine\Desktop\Sokoban finale Version" (with the qoutes). Commented Dec 28, 2016 at 14:44
  • No this is not the problem. Thanks for your comment. Commented Dec 28, 2016 at 14:44
  • Then the next step is to try it in cmd alone, outside of notepad++ and see what is needed until it works there, I mean setting of environment variables like PATH and CLASSPATH, etc.. Commented Dec 28, 2016 at 14:47
  • BTW. is the order of commands correct? First java and then javac? I would have done it the other way around. Commented Dec 28, 2016 at 14:49
  • Also I think there is a naming clash between the directory Sokoban finale Version and the package name sokoban. Commented Dec 28, 2016 at 15:02

2 Answers 2

3

For compiling your files you use:

javac Sokoban.java

Since you are using packages I think you have to give the full name to run your program:

java sokoban.Sokoban

Maybe take a look at this question: How do I run a Java program from the command line on Windows?

Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, it appears the only real issue was inverting java and javac during the compiling process.
0

Open the directory where you have saved your java file. There you can open command line then type:

step 1: javac filename.java

step 2: java filename

make sure that filename is same as that of the class you have written in your java program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.