好的,我已经修改了代码来使用字符'一'来代表地面。以下是更新后的代码示例:

import java.util.Scanner;

public class PushBoxGame {
    private static char[][] board;
    private static int playerRow, playerCol;
    private static int boxRow, boxCol;
    private static int goalRow, goalCol;

    public static void main(String[] args) {
        initializeBoard();
        printBoard();

        Scanner scanner = new Scanner(System.in);
        while (true) {
            char move = scanner.next().charAt(0);
            if (isMoveValid(move)) {
                makeMove(move);
                printBoard();

                if (isGameWon()) {
                    System.out.println("恭喜!你赢了!");
                    break;
                }
            } else {
                System.out.println("无效的移动,请重新输入。");
            }
        }
    }

    private static void initializeBoard() {
        board = new char[10][10];

        // 初始化墙
        for (int i = 0; i < 10; i++) {
            board[i][0] = '墙';
            board[i][9] = '墙';
            board[0][i] = '墙';
            board[9][i] = '墙';
        }

        // 随机生成'我'、'箱'和'目'的位置
        playerRow = (int) (Math.random() * 8) + 1;
        playerCol = (int) (Math.random() * 8) + 1;
        board[playerRow][playerCol] = '我';

        boxRow = (int) (Math.random() * 8) + 1;
        boxCol = (int) (Math.random() * 8) + 1;
        board[boxRow][boxCol] = '箱';

        goalRow = (int) (Math.random() * 8) + 1;
        goalCol = (int) (Math.random() * 8) + 1;
        board[goalRow][goalCol] = '目';
    }

    private static void printBoard() {
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (board[i][j] == 0) {
                    board[i][j] = '一';
                }
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }
    }

    private static boolean isMoveValid(char move) {
        int newRow = playerRow;
        int newCol = playerCol;

        switch (move) {
            case 'w':
                newRow--;
                break;
            case 'a':
                newCol--;
                break;
            case 's':
                newRow++;
                break;
            case 'd':
                newCol++;
                break;
            default:
                return false; // 无效的移动
        }

        if (board[newRow][newCol] == '墙') {
            return false; // 移动到墙上
        } else if (board[newRow][newCol] == '箱') {
            int boxNewRow = newRow + (newRow - playerRow);
            int boxNewCol = newCol + (newCol - playerCol);

            if (board[boxNewRow][boxNewCol] == '墙' || board[boxNewRow][boxNewCol] == '箱') {
                return false; // 试图将箱子推向墙或另一个箱子
            }

            // 推动箱子
            board[boxRow][boxCol] = '一';
            boxRow = boxNewRow;
            boxCol = boxNewCol;
            board[boxRow][boxCol] = '箱';
        }

        return true;
    }

    private static void makeMove(char move) {
        board[playerRow][playerCol] = '一';

        switch (move) {
            case 'w':
                playerRow--;
                break;
            case 'a':
                playerCol--;
                break;
            case 's':
                playerRow++;
                break;
            case 'd':
                playerCol++;
                break;
        }

        board[playerRow][playerCol] = '我';
    }

    private static boolean isGameWon() {
        return boxRow == goalRow && boxCol == goalCol;
    }
}

现在,在初始化时,使用字符'一'来代表地面。希望这次修改能符合你的需求!

推箱子游戏代码优化:使用 '一' 代表地面

原文地址: https://www.cveoy.top/t/topic/Vg8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录