【Java】使用AI助手写一个走迷宫游戏,手写代码0

使用Java写一个走迷宫游戏

import java.util.Scanner;

public class MazeGame {
    private static char[][] maze = {  // 迷宫地图
            {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
            {'#', 'S', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#'},
            {'#', '#', '#', ' ', '#', ' ', '#', '#', ' ', '#'},
            {'#', '#', '#', ' ', ' ', ' ', '#', '#', ' ', '#'},
            {'#', ' ', ' ', ' ', '#', '#', '#', '#', ' ', '#'},
            {'#', '#', '#', '#', '#', '#', '#', '#', ' ', '#'},
            {'#', '#', '#', '#', '#', '#', '#', '#', ' ', '#'},
            {'G', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
            {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
    };

    private static int startRow = 1;  // 起始位置行号
    private static int startCol = 1;  // 起始位置列号

    public static void main(String[] args) {
        playMazeGame();
    }

    private static void playMazeGame() {
        int row = startRow;
        int col = startCol;

        while (true) {
            printMaze(maze);
            System.out.println("请输入移动方向:(W上、S下、A左、D右)");
            Scanner scanner = new Scanner(System.in);
            String direction = scanner.nextLine().toUpperCase();

            int newRow = row;
            int newCol = col;

            switch (direction) {
                case "W":
                    newRow--;
                    break;
                case "S":
                    newRow++;
                    break;
                case "A":
                    newCol--;
                    break;
                case "D":
                    newCol++;
                    break;
                default:
                    System.out.println("请输入有效的移动方向!");
                    continue;
            }

            if (isPositionValid(newRow, newCol) && maze[newRow][newCol] != '#') {
                if (maze[newRow][newCol] == ' ') {
                    maze[newRow][newCol] = 'X';  // 未走过的位置
                } else if (maze[newRow][newCol] == 'G') {
                    System.out.println("恭喜你成功走出迷宫!");
                    break;
                }
                maze[row][col] = ' ';  // 修改当前位置为可通过
                row = newRow;
                col = newCol;
            } else {
                System.out.println("无法向该方向移动!");
            }
        }
    }

    private static boolean isPositionValid(int row, int col) {
        return row >= 0 && row < maze.length && col >= 0 && col < maze[0].length;
    }

    private static void printMaze(char[][] maze) {
        for (char[] row : maze) {
            for (char cell : row) {
                System.out.print(cell + " ");
            }
            System.out.println();
        }
        System.out.println();
    }
}

说明:在地图上随便替换一个外围的# 为 G,作为出口,游戏就可以玩出啦,如图:

在这里插入图片描述

  • 16
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值