JS小游戏-象棋暗棋

news/2024/10/22 18:36:05/

游戏图片:


源文件下載地址:点击下载源文件


Javascript 部分:

/** chinese chess
*	Author:	fdipzone
*	Date:	2012-06-24
*	Ver:	1.0
*/var gameimg = ['images/a1.gif','images/a2.gif','images/a3.gif','images/a4.gif','images/a5.gif','images/a6.gif','images/a7.gif','images/b1.gif','images/b2.gif','images/b3.gif','images/b4.gif','images/b5.gif','images/b6.gif','images/b7.gif','images/bg.gif','images/bg_over.gif','images/bg_sel.gif'];
var chess_obj = new ChessClass();window.onload = function(){$('init_btn').onclick = function(){chess_obj.init();}var callback = function(){chess_obj.init();}img_preload(gameimg, callback);
}// chess class
function ChessClass(){this.chess = [];this.boardrows = 4;this.boardcols = 8;this.area = 82;this.player = 1;	// 1:red 2:greenthis.selected = null;	// selected chessthis.chesstype = ['', 'a', 'b'];this.isover = 0;
}// init
ChessClass.prototype.init = function(){this.reset_grade();	this.create_board();this.create_chess();this.create_event();this.player = 1;this.selected = null;this.isover = 0;disp('init_div','hide');
}// create board
ChessClass.prototype.create_board = function(){var board = '';for(var i=0; i<this.boardrows; i++){for(var j=0; j<this.boardcols; j++){board = board + '<div id="' + i + '_' + j + '"><img src="images/chessbg.gif" /></div>';}}$('board').innerHTML = board;$('board').style.width = this.boardcols * (this.area + 2) + 'px';$('board').style.height = this.boardrows * (this.area + 2) + 'px';
}// create random chess
ChessClass.prototype.create_chess = function(){// 32 chessesvar chesses = ['a1','b7','a2','b7','a2','b7','a3','b7','a3','b7','a4','b6','a4','b6','a5','b5','a5','b5','a6','b4','a6','b4','a7','b3','a7','b3','a7','b2','a7','b2','a7','b1'];this.chess = [];while(chesses.length>0){var rnd = Math.floor(Math.random()*chesses.length);var tmpchess = chesses.splice(rnd, 1).toString();this.chess.push({'chess':tmpchess, 'type':tmpchess.substr(0,1), 'val':tmpchess.substr(1,1), 'status':0});}
}// create event
ChessClass.prototype.create_event = function(){var self = this;var chess_area = $_tag('div', 'board');for(var i=0; i<chess_area.length; i++){chess_area[i].onmouseover = function(){	// mouseoverif(this.className!='onsel'){this.className = 'on';}}chess_area[i].onmouseout = function(){	// mouseoutif(this.className!='onsel'){this.className = '';}}chess_area[i].onclick = function(){	// onclickself.action(this);}}
}// id change index
ChessClass.prototype.getindex = function(id){var tid = id.split('_');return parseInt(tid[0])*this.boardcols + parseInt(tid[1]);
}// index change id
ChessClass.prototype.getid = function(index){return parseInt(index/this.boardcols) + '_' + parseInt(index%this.boardcols);
}// action
ChessClass.prototype.action = function(o){if(this.isover==1){	// game overreturn false;}var index = this.getindex(o.id);if(this.selected == null){	// 未选过棋子if(this.chess[index]['status'] == 0){	// not openedthis.show(index);	}else if(this.chess[index]['status'] == 1){	// openedif(this.chess[index]['type'] == this.chesstype[this.player]){this.select(index);}}		}else{	// 已选过棋子if(index != this.selected['index']){				// 與selected不是同一位置if(this.chess[index]['status'] == 0){			// 未打开的棋子this.show(index);}else if(this.chess[index]['status'] == -1){	// 點空白位置this.move(index);}else{											// 點其他棋子if(this.chess[index]['type']==this.chesstype[this.player]){this.select(index);}else{			this.kill(index);}}}}
}// show chess
ChessClass.prototype.show = function(index){$(this.getid(index)).innerHTML = '<img src="images/' + this.chess[index]['chess'] + '.gif" />';this.chess[index]['status'] = 1;	// openedif(this.selected!=null){			// 清空選中$(this.getid(this.selected.index)).className = '';this.selected = null;}	this.change_player();this.gameover();
}// select chess
ChessClass.prototype.select = function(index){if(this.selected!=null){$(this.getid(this.selected['index'])).className = '';}this.selected = {'index':index, 'chess':this.chess[index]};$(this.getid(index)).className = 'onsel';
}// move chess
ChessClass.prototype.move = function(index){if(this.beside(index)){this.chess[index] = {'chess':this.selected['chess']['chess'], 'type':this.selected['chess']['type'], 'val':this.selected['chess']['val'], 'status':this.selected['chess']['status']};this.remove(this.selected['index']);this.show(index);}
}// kill chess
ChessClass.prototype.kill = function(index){if(this.beside(index)==true && this.can_kill(index)==true){this.chess[index] = {'chess':this.selected['chess']['chess'], 'type':this.selected['chess']['type'], 'val':this.selected['chess']['val'], 'status':this.selected['chess']['status']};this.remove(this.selected['index']);var killed = this.player==1? 2 : 1;$('grade_num' + killed).innerHTML = parseInt($('grade_num' + killed).innerHTML)-1;	this.show(index);}
}// remove chess
ChessClass.prototype.remove = function(index){this.chess[index]['status'] = -1;	// empty$(this.getid(index)).innerHTML = '';$(this.getid(index)).className = '';
}/* check is beside
* @param index		目標棋子index
* @param selindex	执行的棋子index,可为空, 为空则读取选中的棋子
*/
ChessClass.prototype.beside = function(index,selindex){if(typeof(selindex)=='undefined'){if(this.selected!=null){selindex = this.selected['index'];}else{return false;}}if(typeof(this.chess[index])=='undefined'){return false;}var from_info = this.getid(selindex).split('_');var to_info = this.getid(index).split('_');var fw = parseInt(from_info[0]);var fc = parseInt(from_info[1]);var tw = parseInt(to_info[0]);var tc = parseInt(to_info[1]);if(fw==tw && Math.abs(fc-tc)==1 || fc==tc && Math.abs(fw-tw)==1){	// row or colunm is same and interval=1return true;}else{return false;}
}/* check can kill
* @param index		被消灭的棋子index
* @param selindex	执行消灭的棋子index,可为空, 为空则读取选中的棋子
*/
ChessClass.prototype.can_kill = function(index,selindex){if(typeof(selindex)=='undefined'){	// 没有指定执行消灭的棋子if(this.selected!=null){		// 有选中的棋子selindex = this.selected['index'];}else{return false;}}if(this.chess[index]['type']!=this.chesstype[this.player]){if(parseInt(this.chess[selindex]['val'])==7 && parseInt(this.chess[index]['val'])==1){	// 7 can kill 1return true;}else if(parseInt(this.chess[selindex]['val'])==1 && parseInt(this.chess[index]['val'])==7){ // 1 can't kill 7return false;}else if(parseInt(this.chess[selindex]['val']) <= parseInt(this.chess[index]['val'])){	// small kill bigreturn true;}}return false;
}// change player
ChessClass.prototype.change_player = function(){if(this.player == 1){this.player = 2;	// to green$('grade_img2').className = 'img_on';$('grade_img1').className = 'img';}else{this.player = 1;	// to red$('grade_img1').className = 'img_on';$('grade_img2').className = 'img';}
}// reset grade
ChessClass.prototype.reset_grade = function(){$('grade_img1').className = 'img_on';$('grade_img2').className = 'img';$('grade_num1').innerHTML = $('grade_num2').innerHTML = 16;$('grade_res1').className = $('grade_res2').className = 'none';$('grade_res1').innerHTML = $('grade_res2').innerHTML = '';
}// game over
ChessClass.prototype.gameover = function(){if($('grade_num1').innerHTML==0 || $('grade_num2').innerHTML==0){	// 任一方棋子为0this.isover = 1;this.show_grade();disp('init_div','show');}else{if(this.can_action()==false){this.isover = 1;this.show_grade();disp('init_div','show');}}
}// show grade
ChessClass.prototype.show_grade = function(){var num1 = parseInt($('grade_num1').innerHTML);var num2 = parseInt($('grade_num2').innerHTML);if(num1>num2){ // 红方胜$('grade_res2').innerHTML = 'LOSS';$('grade_res2').className = 'loss';$('grade_res1').innerHTML = 'WIN';$('grade_res1').className = 'win';}else if(num1<num2){ // 黑方胜$('grade_res1').innerHTML = 'LOSS';$('grade_res1').className = 'loss';$('grade_res2').innerHTML = 'WIN';$('grade_res2').className = 'win';}else{	// 平局$('grade_res1').innerHTML = $('grade_res2').innerHTML = 'DRAW';$('grade_res1').className = $('grade_res2').className = 'draw';}
}// check chess can action
ChessClass.prototype.can_action = function(){var chess = this.chess;for(var i=0,max=chess.length; i<max; i++){if(chess[i].status==0){	// 有未翻开的棋子return true;}else{if(chess[i].status==1 && chess[i].type==this.chesstype[this.player]){	// 己方已翻开的棋子if(this.beside(i-this.boardcols, i) && (chess[i-this.boardcols].status==-1 || this.can_kill(i-this.boardcols,i) )){	// 上return true;}if(this.beside(i+this.boardcols, i) && (chess[i+this.boardcols].status==-1 || this.can_kill(i+this.boardcols,i) )){	// 下return true;}if(this.beside(i-1, i) && (chess[i-1].status==-1 || this.can_kill(i-1,i) )){	// 左return true;}if(this.beside(i+1, i) && (chess[i+1].status==-1 || this.can_kill(i+1,i) )){	// 右return true;}}}}return false;
}/** common function */// get document.getElementBy(id)
function $(id){this.id = id;return document.getElementById(id);
}// get document.getElementsByTagName
function $_tag(name, id){if(typeof(id)!='undefined'){return $(id).getElementsByTagName(name);}else{return document.getElementsByTagName(name);	}
}/* div show and hide
* @param id dom id
* @param handle show or hide
*/
function disp(id, handle){if(handle=='show'){$(id).style.display = 'block';}else{$(id).style.display = 'none';	}
}/* img preload
* @param img		要加载的图片数组
* @param callback	图片加载成功后回调方法
*/
function img_preload(img, callback){var onload_img = 0;var tmp_img = [];for(var i=0,imgnum=img.length; i<imgnum; i++){tmp_img[i] = new Image();tmp_img[i].src = img[i];if(tmp_img[i].complete){onload_img ++;}else{tmp_img[i].onload = function(){onload_img ++;}}}var et = setInterval(function(){if(onload_img==img.length){	// 定时器,判断图片完全加载后调用callbackclearInterval(et);callback();}},200);
}



http://www.ppmy.cn/news/545777.html

相关文章

《游戏学习》纯JS中国象棋人机对战html游戏源码

源码下载地址&#xff1a;chinese_chess.zip_象棋机器人对战js-网络游戏文档类资源-CSDN下载 下载解压后&#xff0c;文件如下 html页面源码如下 <!DOCTYPE html> <head> <title>中国象棋人机对战</title> <script type"text/javascript&quo…

【180927】智能象棋游戏源码

一、源码特点 采用winform进行开发,象棋游戏&#xff0c;欢迎下载 二、功能介绍 本源码是一个中国象棋游戏源码&#xff0c;可以单人游戏&#xff0c;也可以双人对战&#xff0c;系统默认有几种游戏模式&#xff0c;可以先择别人未战完的残局作战&#xff0c;也可以让电…

中国象棋的人机博弈程序

对此题目有兴趣的朋友们&#xff1a; 感谢你们抽出宝贵的时间来阅读这些浅显的文字。 很多人可能都知道chess的人机大战&#xff1a; 1996年&#xff0c;卡斯帕罗夫以4比2战胜“深蓝”(Deep Blue)&#xff1b; 1997年&#xff0c;“更深的蓝”(Deeper Blue)以3.5比2.5击败了…

介绍中国象棋对弈程序ElephantEye(象眼)

ElephantEye 是一款自由的中国象棋程序&#xff0c;在遵循《GNU宽松通用公共许可协议》(GNU Lesser General Public Licence)的前提下&#xff0c;广大象棋爱好者和程序设计师可以自由使用 ElephantEye 及其源程序。 中国象棋对弈程序ElephantEye(象眼) 版本&#xff1a;3.15 …

一个使用ELEEYE超强AI引擎的中国象棋游戏

一个使用eleeye引擎制作的象棋游戏 游戏介绍 这个游戏是自己使用python3.7配合pygame库写的游戏&#xff0c;由于pygame没有基本的UI组件如&#xff1a;按钮&#xff0c;输入框&#xff0c;文本框之类&#xff0c;这些UI组件都是自己用自己写的类模拟实现。 然后是关于这个象…

表格闪退怎么解决_win10中excel2013闪退怎么修复_win10中excel2013闪退如何解决

win10中excel2013闪退怎么修复_win10中excel2013闪退如何解决我们在使用win10系统办公的过程中&#xff0c;总是需要用到office办公软件中的excel2013表格工具&#xff0c;但是在运行excel2013的时候就闪退了&#xff0c;导致无法正常打开的情况&#xff0c;那么win10中excel20…

Excel2013打开提示 文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?

有的时候打开xls文档时&#xff0c;会提示“文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源&#xff0c;否则请勿打开。是否仍要打开它?” 遇到这种情况&#xff0c;我们需要 1.win键R键&#xff0c;打开“运行“&#xff0c;输入regedit打开“注册表编…

Excel2013-VBA-Validation-Formula1-属性溢出测试记录

Sub test()Dim HelpArr() As StringRunTimes 2 Do While RunTimes < 65536RunTimes RunTimes 1Erase HelpArrReDim HelpArr(1 To RunTimes)For i 1 To UBound(HelpArr)HelpArr(i) iNextTestNumStr Join(HelpArr, ",")Debug.Print "当前时间&#xff1a…