您好,登錄后才能下訂單哦!
這篇文章主要講解了“基于HTML+JavaScript如何實現(xiàn)中國象棋”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“基于HTML+JavaScript如何實現(xiàn)中國象棋”吧!
在線演示地址
目錄結構如下:
HTML 主要代碼:
<div class="box" id="box"> <div class="chess_left"> <canvas id="chess">對不起,您的瀏覽器不支持HTML5,請升級瀏覽器至IE9、firefox或者谷歌瀏覽器!</canvas> <audio src="audio/click.wav" id="clickAudio" preload="auto"></audio> <!--<audio src="audio/check.wav" id="checkAudio" preload="auto"></audio>--> <audio src="audio/select.wav" id="selectAudio" preload="auto"></audio> <link rel="stylesheet" type="text/css" href="https://www.bootcss.com/p/buttons/css/buttons.css" rel="external nofollow" > <div> <div class="bn_box" id="bnBox"> <input type="button" name="offensivePlay" id="tyroPlay" class="button button-caution button-pill button-jumbo" value="新手水平" /> <input type="button" name="offensivePlay" id="superPlay" class="button button-caution button-pill button-jumbo" value="中級水平" /> <input type="button" name="button" id="" value="大師水平" class="button button-caution button-pill button-jumbo" disabled /><br> <!-- <input type="button" name="offensivePlay" id="offensivePlay" value="先手開始" /> <input type="button" name="defensivePlay" id="defensivePlay" value="后手開始" /> --> <input type="button" name="regret" id="regretBn" class="button button-raised button-pill button-inverse" value="悔棋" /> <input type="button" name="billBn" id="billBn" value="重新開始" class="button button-glow button-rounded button-royal" class="bn_box" /> <input type="button" name="stypeBn" id="stypeBn" class="button button-raised button-primary button-pill" value="放大棋局" /> </div> </div> </div> <div class="chess_right" id="chessRight"> <select name="billList" id="billList"> </select> <ol id="billBox" class="bill_box"> </ol> </div> <div id="moveInfo" class="move_info"> </div> </div>
CSS主要代碼:
@charset "gb2312"; body { font-size: 12px; line-height: 150%; } .box { margin:0 auto; width:460px; position: relative; } .chess_left { float:left; text-align:center } .chess_right { float:left; display:none } .move_info { float:left; margin-top:20px } .bill_box { height: 320px; width: 80px; overflow:auto; } .bill_box li { cursor:pointer; text-align:left } .bill_box li:hover { cursor:pointer; background: #C6A577; } .bill_box li:active { cursor:pointer; background: #fff; } #billList { margin-top:20px } .bn_box { display:none }
JS代碼較多這里只展示部分JS代碼
AI.init = function(pace){ var bill = AI.historyBill || com.gambit; //開局庫 if (bill.length){ var len=pace.length; var arr=[]; //先搜索棋譜 for (var i=0;i< bill.length;i++){ if (bill[i].slice(0,len)==pace) { arr.push(bill[i]); } } if (arr.length){ var inx=Math.floor( Math.random() * arr.length ); AI.historyBill = arr ; return arr[inx].slice(len,len+4).split(""); }else{ AI.historyBill = [] ; } } //如果棋譜里面沒有,人工智能開始運作 var initTime = new Date().getTime(); AI.treeDepth=play.depth; //AI.treeDepth=4; AI.number=0; AI.setHistoryTable.lenght = 0 var val=AI.getAlphaBeta(-99999 ,99999, AI.treeDepth, com.arr2Clone(play.map),play.my); //var val = AI.iterativeSearch(com.arr2Clone(play.map),play.my) if (!val||val.value==-8888) { AI.treeDepth=2; val=AI.getAlphaBeta(-99999 ,99999, AI.treeDepth, com.arr2Clone(play.map),play.my); } //var val = AI.iterativeSearch(com.arr2Clone(play.map),play.my); if (val&&val.value!=-8888) { var man = play.mans[val.key]; var nowTime= new Date().getTime(); com.get("moveInfo").innerHTML='<h4>AI搜索結果:</h4>最佳著法:'+ com.createMove(com.arr2Clone(play.map),man.x,man.y,val.x,val.y)+ '<br />搜索深度:'+AI.treeDepth+'<br />搜索分支:'+ AI.number+'個 <br />最佳著法評估:'+ val.value+'分'+ ' <br />搜索用時:'+ (nowTime-initTime)+'毫秒' return [man.x,man.y,val.x,val.y] }else { return false; } }
AI.iterativeSearch = function (map, my){ var timeOut=100; var initDepth = 1; var maxDepth = 8; AI.treeDepth=0; var initTime = new Date().getTime(); var val = {}; for (var i=initDepth; i<=maxDepth; i++){ var nowTime= new Date().getTime(); AI.treeDepth=i; AI.aotuDepth=i; var val = AI.getAlphaBeta(-99999, 99999, AI.treeDepth , map ,my) if (nowTime-initTime > timeOut){ return val; } } return false; }
AI.getMapAllMan = function (map, my){ var mans=[]; for (var i=0; i<map.length; i++){ for (var n=0; n<map[i].length; n++){ var key = map[i][n]; if (key && play.mans[key].my == my){ play.mans[key].x = n; play.mans[key].y = i; mans.push(play.mans[key]) } } } return mans; }
AI.getMoves = function (map, my){ var manArr = AI.getMapAllMan (map, my); var moves = []; var foul=play.isFoul; for (var i=0; i<manArr.length; i++){ var man = manArr[i]; var val=man.bl(map); for (var n=0; n<val.length; n++){ var x=man.x; var y=man.y; var newX=val[n][0]; var newY=val[n][1]; //如果不是長將著法 if (foul[0]!=x || foul[1]!=y || foul[2]!=newX || foul[3]!=newY ){ moves.push([x,y,newX,newY,man.key]) } } } return moves; }
AI.getAlphaBeta = function (A, B, depth, map ,my) { //var txtMap= map.join(); //var history=AI.historyTable[txtMap]; // if (history && history.depth >= AI.treeDepth-depth+1){ // return history.value*my; //} if (depth == 0) { return {"value":AI.evaluate(map , my)}; //局面評價函數(shù); } var moves = AI.getMoves(map , my ); //生成全部走法; //這里排序以后會增加效率 for (var i=0; i < moves.length; i++) { //走這個走法; var move= moves[i]; var key = move[4]; var oldX= move[0]; var oldY= move[1]; var newX= move[2]; var newY= move[3]; var clearKey = map[ newY ][ newX ]||""; map[ newY ][ newX ] = key; delete map[ oldY ][ oldX ]; play.mans[key].x = newX; play.mans[key].y = newY; if (clearKey=="j0"||clearKey=="J0") {//被吃老將,撤消這個走法; play.mans[key] .x = oldX; play.mans[key] .y = oldY; map[ oldY ][ oldX ] = key; delete map[ newY ][ newX ]; if (clearKey){ map[ newY ][ newX ] = clearKey; // play.mans[ clearKey ].isShow = false; } return {"key":key,"x":newX,"y":newY,"value":8888}; //return rootKey; }else { var val = -AI.getAlphaBeta(-B, -A, depth - 1, map , -my).value; //val = val || val.value; //撤消這個走法; play.mans[key] .x = oldX; play.mans[key] .y = oldY; map[ oldY ][ oldX ] = key; delete map[ newY ][ newX ]; if (clearKey){ map[ newY ][ newX ] = clearKey; //play.mans[ clearKey ].isShow = true; } if (val >= B) { //將這個走法記錄到歷史表中; //AI.setHistoryTable(txtMap,AI.treeDepth-depth+1,B,my); return {"key":key,"x":newX,"y":newY,"value":B}; } if (val > A) { A = val; //設置最佳走法; if (AI.treeDepth == depth) var rootKey={"key":key,"x":newX,"y":newY,"value":A}; } } } //將這個走法記錄到歷史表中; //AI.setHistoryTable(txtMap,AI.treeDepth-depth+1,A,my); if (AI.treeDepth == depth) {//已經遞歸回根了 if (!rootKey){ //AI沒有最佳走法,說明AI被將死了,返回false return false; }else{ //這個就是最佳走法; return rootKey; } } return {"key":key,"x":newX,"y":newY,"value":A}; }
AI.setHistoryTable = function (txtMap,depth,value,my){ AI.setHistoryTable.lenght ++; AI.historyTable[txtMap] = {depth:depth,value:value} }
AI.evaluate = function (map,my){ var val=0; for (var i=0; i<map.length; i++){ for (var n=0; n<map[i].length; n++){ var key = map[i][n]; if (key){ val += play.mans[key].value[i][n] * play.mans[key].my; } } } //val+=Math.floor( Math.random() * 10); //讓AI走棋增加隨機元素 //com.show() //z(val*my) AI.number++; return val*my; } AI.evaluate1 = function (map,my){ var val=0; for (var i in play.mans){ var man=play.mans[i]; if (man.isShow){ val += man.value[man.y][man.x] * man.my; } } //val+=Math.floor( Math.random() * 10); //讓AI走棋增加隨機元素 //com.show() //z(val*my) AI.number++; return val*my; }
感謝各位的閱讀,以上就是“基于HTML+JavaScript如何實現(xiàn)中國象棋”的內容了,經過本文的學習后,相信大家對基于HTML+JavaScript如何實現(xiàn)中國象棋這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。