机器人走迷宫游戏脚本

美女机器人 2025-11-19 14:18www.robotxin.com机器人女友

机器人迷宫寻宝

机器人迷宫寻宝

  • 迷宫区域 -->
  • 控制面板 -->
  • 游戏控制

    手动控制

    游戏信息

    步数: 0

    状态: 等待开始

    class MazeGame {

    constructor {

    this.size = 15;

    this.maze = [];

    this.robotPos = {x:1, y:1};

    this.treasurePos = {x:13, y:13};

    this.steps = 0;

    thisit;

    init {

    this.generateMaze;

    this.renderMaze;

    this.setupControls;

    generateMaze {

    // 初始化空白迷宫

    this.maze = Array(this.size).fill.map( =>

    Array(this.size).fill(0));

    // 随机生成墙壁(30%概率)

    for(let y=0; yif(Math.random < 0.3 && !(x==1 && y==1) && !(x==13 && y==13)) {

    this.maze[y][x] = 1; // 1表示墙壁

    renderMaze {

    const mazeEl = document.getElementById('maze');

    mazeElnerHTML = '';

    mazeEl.style.gridTemplateColumns = `repeat(${this.size}, 40px)`;

    for(let y=0; yconst cell = document.createElement('div');

    cell.className = 'cell w-10 h-10 flex items-center justify-center';

    if(this.maze[y][x] === 1) {

    cell.className += ' bg-gray-800';

    } else if(x === this.robotPos.x && y === this.robotPos.y) {

    cell.className += ' robot bg-cover bg-center';

    } else if(x === this.treasurePos.x && y === this.treasurePos.y) {

    cell.className += ' treasure bg-cover bg-center';

    } else {

    cell.className += ' bg-white';

    mazeEl.appendChild(cell);

    setupControls {

    document.getElementById('generateBtn').addEventListener('click', => {

    this.generateMaze;

    this.robotPos = {x:1, y:1};

    this.treasurePos = {x:13, y:13};

    this.steps = 0;

    document.getElementById('steps').textContent = this.steps;

    document.getElementById('status').textContent = '游戏中';

    this.renderMaze;

    });

    document.querySelectorAll('.moveBtn').forEach(btn => {

    btn.addEventListener('click', => this.moveRobot(btn.dataset.dir));

    });

    document.getElementById('solveBtn').addEventListener('click', => {

    this.solveMaze;

    });

    moveRobot(direction) {

    let newX = this.robotPos.x;

    let newY = this.robotPos.y;

    switch(direction) {

    case 'up': newY--; break;

    case 'down': newY++; break;

    case 'left': newX--; break;

    case 'right': newX++; break;

    if(this.isValidMove(newX, newY)) {

    this.robotPos = {x: newX, y: newY};

    this.steps++;

    document.getElementById('steps').textContent = this.steps;

    this.renderMaze;

    if(newX === this.treasurePos.x && newY === this.treasurePos.y) {

    document.getElementById('status').textContent = '恭喜找到宝藏!';

    isValidMove(x, y) {

    return x >= 0 && x < this.size &&

    y >= 0 && y < this.size &&

    this.maze[y][x] !== 1;

    solveMaze {

    // A寻路算法实现

    const path = this.findPath;

    if(path.length > 0) {

    this.animateSolution(path);

    } else {

    document.getElementById('status').textContent = '无法找到路径';

    findPath {

    // 简化的A算法实现

    const openSet = [this.robotPos];

    const cameFrom = {};

    const gScore = {[this.getKey(this.robotPos)]: 0};

    const fScore = {[this.getKey(this.robotPos)]: this.heuristic(this.robotPos)};

    while(openSet.length > 0) {

    openSet.sort((a,b) => fScore[this.getKey(a)]

  • fScore[this.getKey(b)]);
  • const current = openSet.shift;

    if(current.x === this.treasurePos.x && current.y === this.treasurePos.y) {

    return this.reconstructPath(cameFrom, current);

    const neighbors = this.getNeighbors(current);

    for(const neighbor of neighbors) {

    const tentativeGScore = gScore[this.getKey(current)] + 1;

    const neighborKey = this.getKey(neighbor);

    if(tentativeGScore < (gScore[neighborKey] || Infinity)) {

    cameFrom[neighborKey] = current;

    gScore[neighborKey] = tentativeGScore;

    fScore[neighborKey] = tentativeGScore + this.heuristic(neighbor);

    if(!openSet.some(p => p.x === neighbor.x && p.y === neighbor.y)) {

    openSet.push(neighbor);

    return [];

    heuristic(pos) {

    return Math.abs(pos.x

  • this.treasurePos.x) +
  • Math.abs(pos.y

  • this.treasurePos.y);
  • getNeighbors(pos) {

    const dirs = [{x:0,y:-1}, {x:1,y:0}, {x:0,y:1}, {x:-1,y:0}];

    return dirs

    .map(d => ({x: pos.x + d.x, y: pos.y + d.y}))

    .filter(p => this.isValidMove(p.x, p.y));

    getKey(pos) {

    return `${pos.x},${pos.y}`;

    reconstructPath(cameFrom, current) {

    const path = [current];

    while(this.getKey(current) in cameFrom) {

    current = cameFrom[this.getKey(current)];

    path.unshift(current);

    return path;

    animateSolution(path) {

    let i = 0;

    const interval = setInterval( => {

    if(i < path.length) {

    this.robotPos = path[i];

    this.renderMaze;

    i++;

    } else {

    clearInterval(interval);

    if(this.robotPos.x === this.treasurePos.x &&

    this.robotPos.y === this.treasurePos.y) {

    document.getElementById('status').textContent = '自动寻路完成!';

    }, 200);

    // 初始化游戏

    new MazeGame;

    网页游戏无需额外依赖

    Copyright © 2016-2025 www.robotxin.com 人工智能机器人网 版权所有 Power by