无聊用JS写个中国象棋,自娱自乐版 ^_^

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

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

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

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

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

// 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;

}








...全文
4797 128 打赏 收藏 转发到动态 举报
写回复
用AI写文章
128 条回复
切换为时间正序
请发表友善的回复…
发表回复
longsheng1966 2012-10-06
  • 打赏
  • 举报
回复
henhao
曹西 2011-08-10
  • 打赏
  • 举报
回复
何时偶能达到那个境界再来看看我现在的回复,哈哈,脚印哦
小弟 2011-07-11
  • 打赏
  • 举报
回复
[Quote=引用 125 楼 annkou 的回复:]

虽然javascript不是很了解,但觉得在设计上还可以再进一步!
[/Quote]
谢谢关照~
不仅是设计,还有很多地方都需要改进,有时间再做吧 。。
annkou 2011-07-09
  • 打赏
  • 举报
回复
虽然javascript不是很了解,但觉得在设计上还可以再进一步!
chongjingsky 2011-07-09
  • 打赏
  • 举报
回复
很厉害啊
annkou 2011-07-09
  • 打赏
  • 举报
回复
lj牛X了!小弟来膜拜了!
Fallen_Devil 2011-07-08
  • 打赏
  • 举报
回复
果然很强大
yo010yo 2011-07-08
  • 打赏
  • 举报
回复
试着玩了一下,感觉还行
就是每走一步就要刷新一次有点郁闷,眼花了
liuwei88212 2011-07-07
  • 打赏
  • 举报
回复
我那个去,图片缓冲了N久还没显示出来。。。
zings1986 2011-07-07
  • 打赏
  • 举报
回复
楼主编码习惯不好,下次发点有注释的。
jcl007_ 2011-07-07
  • 打赏
  • 举报
回复
强强强强强强强
缘缘 2011-07-07
  • 打赏
  • 举报
回复
拿来看看,学习!
果-果 2011-07-06
  • 打赏
  • 举报
回复
LZ太牛C了~~顶
dingcho 2011-07-06
  • 打赏
  • 举报
回复
大强 膜拜
nobbycode 2011-07-06
  • 打赏
  • 举报
回复
原来不能人机对战。。有点失望
nobbycode 2011-07-06
  • 打赏
  • 举报
回复
我镇精了。
Zeroing-X 2011-07-06
  • 打赏
  • 举报
回复
我兄弟好强大啊。。支持。。。
看来我还真他妈的菜鸟。。。
支持。。。
Freedom8286 2011-07-06
  • 打赏
  • 举报
回复
我嘞个天,写这玩意很伤神滴啊。
xue315308 2011-07-06
  • 打赏
  • 举报
回复
好,对我太有用了,不知道从哪里下载源码?
LinusLan 2011-07-06
  • 打赏
  • 举报
回复
能写出这的,也是牛人呀。
加载更多回复(105)

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧