Java面向对象游戏:编译时对象生成错误

时间:2016-12-28 12:20:36

标签: java oop

编译Sokoban游戏的java实现时出现以下错误:

  

<<<流程结束了。 (退出代码1)javac Sokobantest.java流程   已开始>>> 。\ Level.java:44:错误:期望myPlayer =   新玩家(this.room);           ^。\ Level.java:44:错误:找不到符号myPlayer = new Player(this.room); ^符号:类myPlayer位置:类   Level Sokobantest.java:43:错误:找不到符号Level myLevel =   新级别(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;
}

}

提前感谢任何提示或帮助!

修改

我在Notepad ++中编译如下:

enter image description here

1 个答案:

答案 0 :(得分: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