当前位置:编程学习 > JAVA >>

一个JavaScript写的黑白棋A

赖勇浩(http://laiyonghao.com)

首先,这个代码不是我写的,但注释是我加上去的。作者是shaofei cheng,他的网站:http://shaofei.name

第二,目前这个代码只是使用了 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+=" ";  
        } &nb

补充:web前端 , JavaScript ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,