简单点吧
import java.util.Random;
import java.util.Scanner;public class RogueGame {
private static final int BOARD_SIZE = 10;
private static final int MAX_ENEMIES = 5;
private static final int MAX_HEALTH = 10;
private static final int MAX_DAMAGE = 3;private char[][] board;
private int playerX;
private int playerY;
private int playerHealth;
private int enemiesRemaining;
private Random random;
private Scanner scanner;public RogueGame() {
board = new char[BOARD_SIZE][BOARD_SIZE];
random = new Random();
scanner = new Scanner(System.in);
}public void play() {
initializeBoard();
initializePlayer();
initializeEnemies();while (playerHealth > 0 && enemiesRemaining > 0) {
printBoard();
System.out.print("Enter your move (w/a/s/d): ");
String move = scanner.nextLine();
processMove(move);
moveEnemies();
}if (playerHealth <= 0) {
System.out.println("Game Over! You were defeated.");
} else {
System.out.println("Congratulations! You defeated all enemies.");
}
}private void initializeBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = '.';
}
}
}private void initializePlayer() {
playerX = random.nextInt(BOARD_SIZE);
playerY = random.nextInt(BOARD_SIZE);
playerHealth = MAX_HEALTH;
board[playerY][playerX] = '@';
}private void initializeEnemies() {
enemiesRemaining = MAX_ENEMIES;
for (int i = 0; i < MAX_ENEMIES; i++) {
int enemyX = random.nextInt(BOARD_SIZE);
int enemyY = random.nextInt(BOARD_SIZE);
if (enemyX != playerX && enemyY != playerY) {
board[enemyY][enemyX] = 'E';
}
}
}private void printBoard() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}private void processMove(String move) {
int newPlayerX = playerX;
int newPlayerY = playerY;if (move.equalsIgnoreCase("w")) {
newPlayerY--;
} else if (move.equalsIgnoreCase("s")) {
newPlayerY++;
} else if (move.equalsIgnoreCase("a")) {
newPlayerX--;
} else if (move.equalsIgnoreCase("d")) {
newPlayerX++;
}if (isValidMove(newPlayerX, newPlayerY)) {
board[playerY][playerX] = '.';
playerX = newPlayerX;
playerY = newPlayerY;
board[playerY][playerX] = '@';
checkEncounter();
} else {
System.out.println("Invalid move! Try again.");
}
}private boolean isValidMove(int x, int y) {
return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE;
}private void checkEncounter() {
if (board[playerY][playerX] == 'E') {
int enemyDamage = random.nextInt(MAX_DAMAGE) + 1;
playerHealth -= enemyDamage;
System.out.println("You encountered an enemy and took " + enemyDamage + " damage!");
enemiesRemaining--;
}
}private void moveEnemies() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 'E') {
int newX = j;
int newY = i;
while ((newX == j && newY == i) || !isValidMove(newX, newY)) {
int direction = random.nextInt(4);
if (direction == 0) {
newX = j - 1;
} else if (direction == 1) {
newX = j + 1;
} else if (direction == 2) {
newY = i - 1;
} else if (direction == 3) {
newY = i + 1;
}
}
char target = board[newY][newX];
if (target == '@') {
int playerDamage = random.nextInt(MAX_DAMAGE) + 1;
playerHealth -= playerDamage;
System.out.println("An enemy attacked you and dealt " + playerDamage + " damage!");
} else {
board[i][j] = '.';
board[newY][newX] = 'E';
}
}
}
}
}public static void main(String[] args) {
RogueGame game = new RogueGame();
game.play();
}
}