[非原创]2048游戏自动化算法

function AI(grid) {
  this.grid = grid;
}

// static evaluation function
AI.prototype.eval = function() {
  var emptyCells = this.grid.availableCells().length;

  var smoothWeight = 0.1,
      //monoWeight   = 0.0,
      //islandWeight = 0.0,
      mono2Weight  = 1.0,
      emptyWeight  = 2.7,
      maxWeight    = 1.0;

  return this.grid.smoothness() * smoothWeight
       + this.grid.monotonicity2() * mono2Weight
       + Math.log(emptyCells) * emptyWeight
       + this.grid.maxValue() * maxWeight;
};

//AI.prototype.cache = {}

// alpha-beta depth first search
AI.prototype.search = function(depth, alpha, beta, positions, cutoffs) {
  var bestScore;
  var bestMove = -1;
  var result;

  // the maxing player
  if (this.grid.playerTurn) {
    bestScore = alpha;
    for (var direction in [0, 1, 2, 3]) {
      var newGrid = this.grid.clone();
      if (newGrid.move(direction).moved) {
        positions++;
        if (newGrid.isWin()) {
          return { move: direction, score: 10000, positions: positions, cutoffs: cutoffs };
        }
        var newAI = new AI(newGrid);

        if (depth == 0) {
          result = { move: direction, score: newAI.eval() };
        } else {
          result = newAI.search(depth-1, bestScore, beta, positions, cutoffs);
          if (result.score > 9900) { // win
            result.score--; // to slightly penalize higher depth from win
          }
          positions = result.positions;
          cutoffs = result.cutoffs;
        }

        if (result.score > bestScore) {
          bestScore = result.score;
          bestMove = direction;
        }
        if (bestScore > beta) {
          cutoffs++
          return { move: bestMove, score: beta, positions: positions, cutoffs: cutoffs };
        }
      }
    }
  }

  else { // computer's turn, we'll do heavy pruning to keep the branching factor low
    bestScore = beta;

    // try a 2 and 4 in each cell and measure how annoying it is
    // with metrics from eval
    var candidates = [];
    var cells = this.grid.availableCells();
    var scores = { 2: [], 4: [] };
    for (var value in scores) {
      for (var i in cells) {
        scores[value].push(null);
        var cell = cells[i];
        var tile = new Tile(cell, parseInt(value, 10));
        this.grid.insertTile(tile);
        scores[value][i] = -this.grid.smoothness() + this.grid.islands();
        this.grid.removeTile(cell);
      }
    }

    // now just pick out the most annoying moves
    var maxScore = Math.max(Math.max.apply(null, scores[2]), Math.max.apply(null, scores[4]));
    for (var value in scores) { // 2 and 4
      for (var i=0; i) {
        if (scores[value][i] == maxScore) {
          candidates.push( { position: cells[i], value: parseInt(value, 10) } );
        }
      }
    }

    // search on each candidate
    for (var i=0; i) {
      var position = candidates[i].position;
      var value = candidates[i].value;
      var newGrid = this.grid.clone();
      var tile = new Tile(position, value);
      newGrid.insertTile(tile);
      newGrid.playerTurn = true;
      positions++;
      newAI = new AI(newGrid);
      result = newAI.search(depth, alpha, bestScore, positions, cutoffs);
      positions = result.positions;
      cutoffs = result.cutoffs;

      if (result.score < bestScore) {
        bestScore = result.score;
      }
      if (bestScore < alpha) {
        cutoffs++;
        return { move: null, score: alpha, positions: positions, cutoffs: cutoffs };
      }
    }
  }

  return { move: bestMove, score: bestScore, positions: positions, cutoffs: cutoffs };
}

// performs a search and returns the best move
AI.prototype.getBest = function() {
  return this.iterativeDeep();
}

// performs iterative deepening over the alpha-beta search
AI.prototype.iterativeDeep = function() {
  var start = (new Date()).getTime();
  var depth = 0;
  var best;
  do {
    var newBest = this.search(depth, -10000, 10000, 0 ,0);
    if (newBest.move == -1) {
      //console.log('BREAKING EARLY');
      break;
    } else {
      best = newBest;
    }
    depth++;
  } while ( (new Date()).getTime() - start < minSearchTime);
  //console.log('depth', --depth);
  //console.log(this.translate(best.move));
  //console.log(best);
  return best
}

AI.prototype.translate = function(move) {
 return {
    0: 'up',
    1: 'right',
    2: 'down',
    3: 'left'
  }[move];
}

Original: https://www.cnblogs.com/ryuasuka/p/3611839.html
Author: 飞鸟_Asuka
Title: [非原创]2048游戏自动化算法

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/608927/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • LinuxKernel(一)

    首先,回顾一下基础的宏操作: C语言宏 # 与 ## #的作用是字符串化:在一个宏中的参数前面使用一个#,预处理器会把这个参数转换为一个字符数组 #define ERROR_LOG…

    Linux 2023年6月8日
    0117
  • vi/vim编辑器tar 命令

    一开始进入的模式 此模式下,可使用方向键(上、下、左、右键)或 k、j、h、i 移动光标的位置 操作类型 操作键 功能 翻页 Pagedown Pageup 向下翻页 向上翻页 行…

    Linux 2023年6月6日
    079
  • jQuery 遍历

    下图展示了一个家族树。通过 jQuery 遍历,您能够从被选(当前的)元素开始,轻松地在家族树中向上移动(祖先),向下移动(子孙),水平移动(同胞)。这种移动被称为对 DOM 进行…

    Linux 2023年6月13日
    091
  • sublime text里面中文字体显示异常解决方案

    sublime text下载之后一开始转成中文之后,会出现中文显示异常的问题,比如下图中”门”字显示异常 通过如下的设置可以解决该问题: 首选项&#8211…

    Linux 2023年6月13日
    0201
  • PyTorch 介绍 | TENSORS

    Tensor是一种特殊的数据结构,非常类似于数组和矩阵。在PyTorch中,我们使用tensor编码模型的输入和输出,以及模型的参数。 Tensor类似于Numpy的ndarray…

    Linux 2023年6月16日
    0170
  • 什么?Android上面跑Linux?

    镜像下载、域名解析、时间同步请点击阿里云开源镜像站 前言 众所周知,现在程序员因为工作、个人兴趣等对各种系统的需求越来越大,部分人电脑做的还是双系统。其中,比较常见的有各种模拟器、…

    Linux 2023年5月27日
    0122
  • Typora详细教程以及下载

    发现一篇非常不错的 Typora 教程,分享给大家。 原文链接:https://www.cnblogs.com/hyacinthLJP/p/16123932.html 作者:MEl…

    Linux 2023年6月7日
    0150
  • k8s-简介

    Kubenetes是一个针对容器应用,进行自动部署,弹性伸缩和管理的开源系统,K8s 作为缩写的结果来自计算”K”和”s”之间的八个…

    Linux 2023年6月13日
    088
  • docker-部署jumpserver

    Docker 部署 jumpserver 堡垒机 容器部署 jumpserver-1.4.10 服务端 #最好单一个节点 容器运行Mysql 5.6.46 #myql , redi…

    Linux 2023年6月14日
    0103
  • 【河北科技大学数据结构课设】校园导航问题

    文档到我的资源下载 点击这里进入我的资源下载 1. 简单介绍 2. 代码 #include #include #include using namespace std; /*测试使…

    Linux 2023年6月8日
    0121
  • springboot整合redis的发布订阅模式

    所谓发布订阅模式,就是发布了消息丢到通道中,消费者进行消费 看代码: 1 首先需要配置这个连接工厂 ,这地方可能有人有疑问,为什么需要单独做一个类呢,是由与这个连接工厂其他地方需要…

    Linux 2023年5月28日
    0111
  • Nuget私服老是弹输入用户名密码框

    先把vs中私服删除,然后,控制面板\用户帐户\凭据管理器,删除私服 最后输入命令: NuGet.exe Sources Update -Name <feedname> …

    Linux 2023年6月13日
    0111
  • Gumbel_Softmax 概要

    解决argmax不可导,无法进行反向传播的问题 出现的原因: argmax(x,y)不可导的根本原因是其向量空间不是光滑的,有尖锐的点和面;而是某些任务中,argmax会被插入到反…

    Linux 2023年6月7日
    0114
  • 【Python】**kwargs和takes 1 positional argument but 2 were given

    Python的函数定义中可以在参数里添加kwargs——简单来说目的是允许 添加不定参数名称的参数,并作为 字典传递参数。但前提是——你必须提供 参数名**。 例如下述情况: 1 …

    Linux 2023年6月13日
    0113
  • redis中setbit的用法

    原文地址:http://www.zhihu.com/question/27672245 在redis中,存储的字符串都是以二级制的进行存在的。举例:设置一个 key-value ,…

    Linux 2023年5月28日
    094
  • MySQL事务隔离级别

    MySQL事务隔离级别 事务 事务是由单独的一个或者多个SQL语句组成,是一个最小的不可再分割的单元,这一组操作里面的所有的执行,要么全部成功、要么全部不成功。如果有一个执行不成功…

    Linux 2023年6月6日
    0122
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球