gpt4 book ai didi

java - 使用Notepad++编译多个java类

转载 作者:行者123 更新时间:2023-12-01 21:09:54 25 4
gpt4 key购买 nike

我使用 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;
}
}

}

我的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;
}

}

我的推箱子.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");
}
}

我的推箱子.txt(游戏 map )

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

谁能告诉我如何用Notepad++编译这个游戏?

我尝试这样编译:

第 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

预先感谢您的回答和提示!

最佳答案

为了编译您使用的文件:

javac Sokoban.java

由于您使用的是包,我认为您必须提供全名才能运行您的程序:

java sokoban.Sokoban

也许看看这个问题:How do I run a Java program from the command line on Windows?

关于java - 使用Notepad++编译多个java类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41364256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com