能帮用java写一个迷宫小游戏码
时间: 2023-12-21 16:04:21 浏览: 85
精选_基于java实现的迷宫小游戏_源码打包
以下是一个简单的迷宫小游戏的Java代码,仅供参考:
```java
import java.util.Random;
import java.util.Scanner;
public class MazeGame {
private static final int ROWS = 10;
private static final int COLS = 10;
private static final char WALL = '#';
private static final char PATH = ' ';
private static final char START = 'S';
private static final char EXIT = 'E';
private static final int MAX_MOVES = 20;
private char[][] maze;
private int startRow;
private int startCol;
private int exitRow;
private int exitCol;
private int playerRow;
private int playerCol;
private int movesLeft;
public MazeGame() {
maze = new char[ROWS][COLS];
playerRow = -1;
playerCol = -1;
movesLeft = MAX_MOVES;
generateMaze();
}
public void play() {
Scanner scanner = new Scanner(System.in);
while (true) {
displayMaze();
System.out.printf("Moves left: %d%n", movesLeft);
System.out.print("Enter a direction (N/S/E/W): ");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("quit")) {
System.out.println("Goodbye!");
break;
}
if (input.length() != 1) {
System.out.println("Invalid input!");
continue;
}
char dir = input.toUpperCase().charAt(0);
if (dir != 'N' && dir != 'S' && dir != 'E' && dir != 'W') {
System.out.println("Invalid direction!");
continue;
}
if (makeMove(dir)) {
System.out.println("Congratulations! You escaped the maze!");
break;
} else if (movesLeft == 0) {
System.out.println("Game over! You ran out of moves!");
break;
}
}
}
private void generateMaze() {
Random rand = new Random();
// fill maze with walls
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
maze[row][col] = WALL;
}
}
// generate random start and exit positions
startRow = rand.nextInt(ROWS);
startCol = rand.nextInt(COLS);
do {
exitRow = rand.nextInt(ROWS);
exitCol = rand.nextInt(COLS);
} while (exitRow == startRow && exitCol == startCol);
// carve a path from start to exit
maze[startRow][startCol] = START;
maze[exitRow][exitCol] = EXIT;
int row = startRow;
int col = startCol;
while (row != exitRow || col != exitCol) {
// randomly choose a direction to move in
int dir = rand.nextInt(4);
switch (dir) {
case 0: // north
if (row > 0 && maze[row - 1][col] == WALL) {
maze[row - 1][col] = PATH;
row--;
}
break;
case 1: // south
if (row < ROWS - 1 && maze[row + 1][col] == WALL) {
maze[row + 1][col] = PATH;
row++;
}
break;
case 2: // east
if (col < COLS - 1 && maze[row][col + 1] == WALL) {
maze[row][col + 1] = PATH;
col++;
}
break;
case 3: // west
if (col > 0 && maze[row][col - 1] == WALL) {
maze[row][col - 1] = PATH;
col--;
}
break;
}
}
}
private void displayMaze() {
System.out.println();
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (row == playerRow && col == playerCol) {
System.out.print('X');
} else {
System.out.print(maze[row][col]);
}
}
System.out.println();
}
System.out.println();
}
private boolean makeMove(char dir) {
int newRow = playerRow;
int newCol = playerCol;
switch (dir) {
case 'N':
newRow--;
break;
case 'S':
newRow++;
break;
case 'E':
newCol++;
break;
case 'W':
newCol--;
break;
}
if (newRow < 0 || newRow >= ROWS || newCol < 0 || newCol >= COLS) {
System.out.println("Sorry, you cannot move there!");
return false;
}
if (maze[newRow][newCol] == WALL) {
System.out.println("Sorry, you cannot move there!");
return false;
}
playerRow = newRow;
playerCol = newCol;
movesLeft--;
if (playerRow == exitRow && playerCol == exitCol) {
return true;
}
return false;
}
public static void main(String[] args) {
MazeGame game = new MazeGame();
game.play();
}
}
```
这个游戏生成一个随机的迷宫,玩家需要通过输入方向键来移动,直到找到出口并逃脱迷宫。游戏还有一个移动次数限制,如果玩家用完所有移动次数仍未找到出口,则游戏结束。
阅读全文