日期:2014-05-16  浏览次数:20622 次

无聊用JS写个中国象棋,自娱自乐版 ^_^
最近迷上了象棋,苦于QQ上被高手欺负
想练技术,网上下载几个象棋游戏,居然连电脑也下不过,杯具啊 > <
网上下载的,大多都太专业,不方便吾等小菜鸟研究,看不懂棋谱 0 0 
遂按照自己的想法写了个简单版,潜心修炼。。希望有朝一日能叱咤棋坛 (想多了 ……) 

不敢独享,拿出来让大家一块玩玩

演示地址 http://jtcpp.4.huyi2.com/JScheese/index.html

用到了JQUERY库
代码写的比较随意,大家见谅,欢迎拍砖~

完整代码可以到我的资源中下载 ^_^
JScript code

 // author:小弟
// MY QQ : 285037039
// PLAY FOR FUN
// OH i love POINT   :-)
// 如果您对该CODE有什么建议和意见,请MAIL:285037039@qq.com
// 功能简单,支持下棋、悔棋和棋盘翻转
function Character() {
    this.x = 0;
    this.y = 0;
    this.width = 0;
    this.height = 0; 
    /**0普通1DEAD*/
    this.state = 0;
    this.id = "";
    /** 1车2马3相4士5帅6炮7卒子*/
    this.type = 0;
    /**方向,为卒子准备的 1↓走 2↑ - -。。*/
    this.dir = 1;
    this.boardpos = 0;
    this.instance = {};
    this.prepare = 0;
    this.aniTime = 500;
    
    this.show = function() {
        $("#"+this.id).css("left",this.x);
        $("#"+this.id).css("top",this.y);
    };
    
    
    this.init = function() {
        
        var _this = this;
        $("#"+this.id).bind("click",function(){
            _this.updateShow();
            
        });
        
        $("#"+this.id).show();
    }
    
    this.aniMove = function(despos) {
         
         var _this = this;
         var _srcpos = this.boardpos;
        $("#"+this.id).animate({left:this.instance.board[despos].x,top:this.instance.board[despos].y},200,"swing",function(){
            _this.aniEat(_srcpos,despos);
            
        });
        
        
    }
    
    this.aniEat = function(_srcpos,despos) {
         
        var hasindex = this.hasChessIndex(this.id,despos);
        if(hasindex != 0) {
            this.deleteChess(hasindex);
            var posstr = this.id +  "|" + _srcpos + "|" + despos + "|" + this.instance.chessman[hasindex].id;
            this.instance.history.push(posstr);
        } else {
            var posstr = this.id +  "|" + _srcpos + "|" + despos + "|" + "";
            this.instance.history.push(posstr); 
        }
        //alert(this.boardpos);
        
        this.instance.filp();
    }
    
    this.deleteChess = function(index) {
        if(index != 0) {
            $("#" + this.instance.chessman[index].id).hide();
            this.instance.chessman[index].boardpos = 0; 
            
            if(this.instance.chessman[index].id == "a5"  
             || this.instance.chessman[index].id == "b5"
                ) {
                alert("GAME OVER!");
            }
        }
    }
    
    this.hasChessIndex = function(exceptid,pos) {
        for( var ic = 1;ic <= 32; ic++) {
            if(this.instance.chessman[ic].boardpos == pos && this.instance.chessman[ic].id != exceptid) {
                return ic;
            }
        }
        return 0;
    }
    
    this.updateShow = function() {
             var id = (this.id.substring(0,1));
            //alert(this.dir  + " " + this.instance.turn);
            if(id == "a" && this.instance.turn == 1) {
                
                //alert(this.prepare);
                if(this.prepare == 0) {
                
                    this.prepare0();
                    this.prepare = 1;    
                } else {
                
                    if(this.instance.indacatorsrcPos == this.boardpos) {
                     
                        return false;
                    }
                    if(!this.validation(this.boardpos,this.instance.indacatorsrcPos)) {
                        
                        return false;
                    }
                    this.prepare1();