当前位置: 代码迷 >> JavaScript >> 无聊用JS写个中国象棋,自娱自乐版 ^解决方案
  详细解决方案

无聊用JS写个中国象棋,自娱自乐版 ^解决方案

热度:155   发布时间:2012-10-20 14:12:47.0
无聊用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();
                      this.instance.turn = 2;
                     
                    
                    
                    
                }
            } else if( id == "b" &&  this.instance.turn == 2) {
                
                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();
                     this.instance.turn = 1;
                     
                     
                }
                
            }
    }
    
    this.prepare0 = function() {
        $("#indacatordir").css("left",this.instance.board[this.boardpos].x);
        $("#indacatordir").css("top",this.instance.board[this.boardpos].y);
        $("#indacatordir").show();
        for( var i=1 ;i <=32;i++ ) {
            this.instance.chessman[i].prepare = 0;
        } 
        this.prepare = 1;
        return 1;
    }
    
    this.prepare1 = function() {
         
        this.prepare = 0;    
        $("#indacatordir").hide();

        this.aniMove(this.instance.indacatorsrcPos);
        this.boardpos = this.instance.indacatorsrcPos;
        
        
        return 1;
    
    }
    
    this.validation = function(srcpos,despos) {
        
        var chessindex = 0;
        var chesstype = 0;
        var chesscolor = 1;
        var isValidation = true;
        for( var ic = 1;ic <= 32;ic++) {
            if(this.instance.chessman[ic].boardpos == srcpos) {
                chessindex = ic;
                chesstype = this.instance.chessman[ic].type;
                chesscolor = this.dir;
                break;
            }
        }
        
        if(chessindex == 0) {
            return false;
        }
        
        switch(chesstype) {
        case 1:
            isValidation = this.validChe(chessindex,srcpos,despos);
            break;
        case 2:
            isValidation = this.validMa(chessindex,srcpos,despos);
            break;
        case 3:
             
            isValidation = this.validXiang(chessindex,srcpos,despos);
            break;
        case 4:
            isValidation = this.validShi(chessindex,srcpos,despos);
            break;
        case 5:
            isValidation = this.validJiang(chessindex,srcpos,despos);
            break;
        case 6:
            isValidation = this.validPao(chessindex,srcpos,despos);
            break;
        case 7:
            isValidation = this.validBing(chessindex,srcpos,despos);
            break;
        default:
            isValidation = false;
            break;
        
        }
        
        return isValidation;
        
    }
    
    this.getColumn = function(pos) {
        if(pos < 0 || pos > 90) {
            return 0;
        }
        return parseInt((pos-1)%9+1);
    }
    
    this.getRow = function(pos) {
        if(pos < 0 || pos > 90) {
            return 0;
        }
        return parseInt((pos-1)/9+1);
    }
    
    this.getPos = function(row,col) {
        if(row < 1 || row > 10) {
            return 0;
        }
        if(col < 1 || col > 9) {
            return 0;
        } 
        return parseInt( (row-1)*9 + Number(col) );
    }
    /**0空1红2黑*/
     this.hasChess = function(pos) {
        for(var i=1;i<=32;i++) {
            if(this.instance.chessman[i].boardpos == pos) {
                return this.instance.chessman[i].dir;    
            }
        }
        
        return 0;
    }
    
    this.validChe = function(chessindex,srcpos,despos) {
        
         var srccol = this.getColumn(srcpos);
        var srcrow = this.getRow(srcpos);
        var descol = this.getColumn(despos);
        var desrow = this.getRow(despos);
        var ishor = false;
        var isver = false; 
        
        if(srccol == descol) {
            isver = true;
        }
        
        if(srcrow == desrow) {
            ishor = true;
        }
        
        if(!isver && !ishor) {
            return false;
        }
        
        if(srcrow != desrow && srccol != descol) {
            return false;
        }
        
        if(this.hasChess(despos) == this.dir) {
            return false;
        } 
        
        var begin = 0;
        var end = 0;
        var chessCount = 0;
        if( ishor ) {
            
            if(srccol < descol) { 
                begin = srccol;
                end = descol;
            } else {
                begin = descol;
                end = srccol;    
            }
            
            for(var i = Number(begin)+1;i < end;i++) {
                if(this.hasChess(this.getPos(srcrow,i)) > 0) { 
                    return false;
                }
                
            }
            
        } else if(isver) {
            
            if(srcrow < desrow) { 
                begin = srcrow;
                end = desrow;
            } else {
                begin = desrow;
                end = srcrow;    
            }
            for(var i = Number(begin)+1;i < end;i++) {
                if(this.hasChess(this.getPos(i,srccol)) > 0) {
                    return false;
                } 
            } 
        }
        
        return true;
        
    }

 
  相关解决方案