编辑代码

// 定义游戏地图
type Map = string[][];

// 定义汽车类
class Car {
    x: number;
    y: number;

    constructor(x: number, y: number) {
        this.x = x;
        this.y = y;
    }

    // 移动汽车
    move(direction: string, map: Map): boolean {
        let newX = this.x;
        let newY = this.y;

        switch (direction) {
            case 'up':
                newX--;
                break;
            case 'down':
                newX++;
                break;
            case 'left':
                newY--;
                break;
            case 'right':
                newY++;
                break;
            default:
                console.log("Invalid direction!");
                return false;
        }

        // 检查是否撞墙或超出地图范围
        if (
            newX < 0 || newX >= map.length ||
            newY < 0 || newY >= map[0].length ||
            map[newX][newY] === 'W'
        ) {
            console.log("Cannot move there!");
            return false;
        }

        // 更新汽车位置
        this.x = newX;
        this.y = newY;
        return true;
    }
}

// 定义游戏类
class ParkingGame {
    map: Map;
    car: Car;
    exit: { x: number; y: number };

    constructor() {
        // 初始化地图
        this.map = [
            [' ', ' ', 'W', ' ', ' '],
            [' ', 'W', 'W', ' ', ' '],
            [' ', ' ', 'C', ' ', ' '],
            [' ', 'W', 'W', ' ', ' '],
            [' ', ' ', 'E', ' ', ' '],
        ];

        // 初始化汽车和出口位置
        this.car = new Car(2, 2);
        this.exit = { x: 4, y: 2 };
    }

    // 打印地图
    printMap(): void {
        for (let i = 0; i < this.map.length; i++) {
            let row = '';
            for (let j = 0; j < this.map[i].length; j++) {
                if (i === this.car.x && j === this.car.y) {
                    row += 'C '; // 汽车位置
                } else if (i === this.exit.x && j === this.exit.y) {
                    row += 'E '; // 出口位置
                } else {
                    row += this.map[i][j] + ' ';
                }
            }
            console.log(row);
        }
    }

    // 检查是否胜利
    checkWin(): boolean {
        return this.car.x === this.exit.x && this.car.y === this.exit.y;
    }

    // 开始游戏
    start(): void {
        console.log("Welcome to the Parking Game!");
        console.log("Use 'up', 'down', 'left', 'right' to move the car (C) to the exit (E).");

        const readline = require('readline').createInterface({
            input: process.stdin,
            output: process.stdout,
        });

        const play = () => {
            this.printMap();

            readline.question("Enter direction: ", (direction: string) => {
                if (this.car.move(direction, this.map)) {
                    if (this.checkWin()) {
                        console.log("Congratulations! You won!");
                        readline.close();
                    } else {
                        play(); // 继续游戏
                    }
                } else {
                    play(); // 重新输入方向
                }
            });
        };

        play();
    }
}

// 启动游戏
const game = new ParkingGame();
game.start();