一个JavaScript写的黑白棋A
Nov 25, 2016 am 10:26 AM首先,这个代码不是我写的,但注释是我加上去的。
第二,目前这个代码只是使用了 alpha-beta 剪枝,棋力还弱,有很大的优化空间。但是代码写得非常清晰,如果有朋友对人机弈棋方面的课题有兴趣又还没有入门,这份代码作为一个例子是很棒的。
第三,目前计算机只能搜索 3 层,我觉得加上迭代深化和历史启发算法之后,搜索到 5 层是不成问题的。现代 JavaScript 的性能不错。
第四,作者在代码里展示了不少技巧,值得学习和借鉴,哪怕不懂 JavaScript 也很容易看懂代码(我也不懂)。
第五,试试这个 AI 的棋力:html">http://shaofei.name/OthelloAI/othello.html
以下是代码:
view plaincopy to clipboardprint?
var AI = {};
new function(){
AI.Pattern= pattern;
// 定义了 8 个偏移量
// 可以简单通过加法得到任一点周围 8 个点的坐标
// -11 -10 -9
// -1 x 1
// 9 10 11
// 如左上角的坐标为 x + (-11)
var directions=[-11,-10,-9,-1,1,9,10,11];
function pattern()
{
// 把整个棋盘填满 0
for(var i=0;i<100;i++)this[i]=0;
// 中间的 4 个格子,先放上两黑两白的棋子
this[54]=this[45]=1;this[55]=this[44]=2;
// 黑净胜外围子数目(黑减去白),估值时用。
this.divergence=0;
// 当前可走棋方为黑棋
this.color=1;
// 已经走了几步棋
this.moves=0;
// 稳定原型
// 0 是空白,1 是黑棋,2 是白棋,3 是边界
// 把 8 * 8 的棋盘扩展成 10 * 10,是一种技巧
// 可以简化坐标有效性的判断
var stableProto = [
3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 0, 0, 0, 0, 0, 0, 0, 0, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3
]
// 从一个 8 * 8 的棋盘载入状态
this.load=function(arr)
{
for(var y=1;y<=8;y++)
{
for(var x=1;x<=8;x++)
{
this[y*10+x]=arr[y-1][x-1];
}
}
}
// 判断能不能 pass
// 如果能,则当前可走棋方变更
this.pass=function()
{
for(var y=1;y<=8;y++)
{
for(var x=1;x<=8;x++)
{
if(this[y*10+x]==0)
{
// 有任何一步棋可走,都不可以 Pass
if(this.move(x,y,this.color))
{
return false;
}
}
}
}
//alert("pass");
// 这是一个技巧,因为 this.color 的值域是 {1, 2}
// 所以当 color 是 1 时,执行完下一语句后就是 2
// 当 color 是 2 时,执行完下一语句后就是 1
this.color = 3 - this.color;
return true;
}
this.clone=function()
{
function pattern(){}
pattern.prototype=this;
return new pattern();
}
this.toString=function()
{
var icon=[" ","*","o"]
var r="";
for(var y=1;y<=8;y++)
{
for(var x=1;x<=8;x++)
{
r+=icon[this[y*10+x]]+" ";
//r+=stableDiscs[y*10+x]+" ";
}
r+=" ";
}

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Comment mettre en œuvre un système de reconnaissance vocale en ligne à l'aide de WebSocket et JavaScript

WebSocket et JavaScript : technologies clés pour mettre en œuvre des systèmes de surveillance en temps réel

Comment mettre en œuvre un système de réservation en ligne à l'aide de WebSocket et JavaScript

Comment utiliser JavaScript et WebSocket pour mettre en œuvre un système de commande en ligne en temps réel

Tutoriel JavaScript simple : Comment obtenir le code d'état HTTP

JavaScript et WebSocket : créer un système efficace de prévisions météorologiques en temps réel

Comment utiliser insertBefore en javascript

Comment obtenir facilement le code d'état HTTP en JavaScript
