我编译了以下面向对象的Sokoban游戏的java实现,NetBeans IDE8.2没有错误
我的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;
}
}
我的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(游戏地图)
#######
#.@ # #
#$* $ #
# $ #
# .. #
# * #
#######
有谁能告诉我如何用Notepad ++编译这个游戏?
我试着像这样编译:
第1步:F6
第2步:
cd C:\Users\Noureddine\Desktop\Sokoban finale Version
java Sokoban
javac Sokoban.java
并收到以下错误:
流程已开始&gt;&gt;&gt; Fehler:Hauptklasse Sokoban konnte nicht gefunden oder geladen werden&lt;&lt;&lt;流程结束了。 (退出代码1)javac Sokoban.java
备注:我尝试安装ViSimulator但安装失败。
提前感谢您的答案和提示!
答案 0 :(得分:3)
要编辑您使用的文件:
javac Sokoban.java
由于您使用的是软件包,我认为您必须提供运行程序的全名:
java sokoban.Sokoban
也许看看这个问题:How do I run a Java program from the command line on Windows?
答案 1 :(得分:0)
打开保存Java文件的目录。在那里,您可以打开命令行,然后输入:
步骤1: javac filename.java
步骤2: java filename
确保文件名与您在Java程序中编写的类的文件名相同。