標籤:
要想實現人機對戰,就必須讓電腦自動下棋,而且要知道自動去尋找對方的棋子,看看有沒有可以挑一對的,有沒有可以夾一個的,這樣下起來才有意思。
當電腦使用者下完棋後,電腦應立即搜尋使用者的棋子,然後如果沒有被吃的,就再隨機走一個棋子(要想讓電腦成為下棋高手就不能隨機下棋,要考慮下棋後會不會再被對方吃掉等等,這裡只簡單做隨機下子了)。
完整《五子飛》遊戲人機對戰:http://www.lyout.com/projects/Fiveflychess/FiveflyChess8.htm
// 尋找是否有可挑的棋子for (i = 0; i < this.lines.length; i++) { for (j = 0; j < this.lines[i].length; j++) { if (this.chesses[this.lines[i][j]].player == p1 && j < this.lines[i].length - 2) { if ((j + 2) < this.lines[i].length && this.chesses[this.lines[i][j + 1]].player == Player.None) { if (this.chesses[this.lines[i][j + 2]].player == p1) {
找到可以挑的兩個棋子後,還要判斷這條線上不能有其他的棋子:
isfind = true;for (k = j + 3; k < this.lines[i].length; k++) { if (this.chesses[this.lines[i][k]].player != Player.None) { isfind = false; break; }}
確定這條線上只有對方兩個棋子並且能被挑之後,再尋找電腦方有沒有可以去挑這兩個棋子的棋子:
var result;for (k = 0; k < this.lines.length; k++) { pos = $.inArray(this.lines[i][j + 1], this.lines[k]); if (pos != -1) { for (n = pos - 1; n >= 0; n--) { p2 = this.chesses[this.lines[k][n]].player; if (p2 == Player.None) continue; if (p2 == p1) break; // 被挑的棋子1,被挑的棋子2,挑棋子的位置,用來挑的棋子 result = [this.lines[i][j], this.lines[i][j + 2], this.lines[i][j + 1], this.lines[k][n]]; break; } for (n = pos + 1; n < this.lines[k].length; n++) { p2 = this.chesses[this.lines[k][n]].player; if (p2 == Player.None) continue; if (p2 == p1) break; // 被挑的棋子1,被挑的棋子2,挑棋子的位置,用來挑的棋子 result = [this.lines[i][j], this.lines[i][j + 2], this.lines[i][j + 1], this.lines[k][n]]; break; } }}
如果沒有找到可挑的棋子,再尋找可夾的棋子(夾棋子的時候有左右兩方可能用來夾,這裡只分析左方):
// 尋找是否有可夾的棋子for (i = 0; i < this.lines.length; i++) { for (j = 0; j < this.lines[i].length; j++) { if (this.chesses[this.lines[i][j]].player == p1 && j > 0 && j < (this.lines[i].length - 1)) {// 找到了 if (this.chesses[this.lines[i][j - 1]].player == player) {// 左邊是否有對方棋子
找到被夾的後,要判斷一下這條線上只能有已方的一個棋子和對方的一個棋子並且挨著:
isfind = true;// 其他的位置只能是空for (k = 0; k < j - 1; k++) { if (this.chesses[this.lines[i][k]].player != Player.None) { isfind = false; break; }}if (isfind) { for (k = j + 1; k < this.lines[i].length; k++) { if (this.chesses[this.lines[i][k]].player != Player.None) { isfind = false; break; } }}
最後再找已方可以用來執行“夾”的棋子:
var result;for (k = 0; k < this.lines.length; k++) { pos = $.inArray(this.lines[i][j + 1], this.lines[k]); if (pos != -1) { for (n = pos - 1; n >= 0; n--) { p2 = this.chesses[this.lines[k][n]].player; if (p2 == Player.None) continue; if (p2 == p1) break; // 被夾的棋子,可用來夾的位置,夾棋子的棋子 result = [this.lines[i][j], this.lines[i][j + 1], this.lines[k][n]]; break; } for (n = pos + 1; n < this.lines[k].length; n++) { p2 = this.chesses[this.lines[k][n]].player; if (p2 == Player.None) continue; if (p2 == p1) break; // 被夾的棋子,可用來夾的位置,夾棋子的棋子 result = [this.lines[i][j], this.lines[i][j + 1], this.lines[k][n]]; break; } }}
找了可以被挑或被夾的棋子後,就直接執行 moveChess 函數,然後再判斷一下執行完後遊戲是否結束就可以了。
如果沒有找到合適的棋子,就需要自動下棋。我們暫時做最初層級的,隨機下一個棋子:
var i, j, k, index, moved, mover;var items = [];// 可以下棋的棋子for(i=0; i<this.chesses.length; i++){ if(this.chesses[i].player==this.currentPlayer) items.push(i);}if(items.length==0) return;k = 0;moved = 0;while(k<20){ // 隨機得到一個棋子用於移動 var movec = items.length==1?0:Math.floor(Math.random() * ( items.length-1 + 1)); var dirs = []; for(i=0; i<this.lines.length; i++){ index = $.inArray(items[movec], this.lines[i]); if(index!=-1) dirs.push(i); } // 隨機得到一條可移動的路線 var linec = Math.floor(Math.random() * ( dirs.length-1 + 1)); // 移動棋子 index = $.inArray(items[movec], this.lines[dirs[linec]]); mover = 0; if(index==0||index==(this.lines[dirs[linec]].length-1)){ if(index==0) mover = 1; }else{// 隨機下棋方向 var dirc = Math.floor(Math.random() * ( 1 + 1)); if(dirc==0) mover = 1; } if(mover){ if(this.moveChess(this.lines[dirs[linec]][index+1], items[movec])){ this.currentIndex = this.lines[dirs[linec]][index+1]; console.log(this.currentIndex); moved = 1; break; } }else{ if(this.moveChess(this.lines[dirs[linec]][index-1], items[movec])){ this.currentIndex = this.lines[dirs[linec]][index-1]; console.log(this.currentIndex); moved = 1; break; } } k++;}if(moved){ if (!this.chessarray) { var player = this.currentPlayer; this.currentPlayer = this.getAnotherPlayer(player); this.changePlayer(); if (this.isGameOver(this.currentPlayer)) { this.winner = player; this.isover = true; } }}else{ this.winner = player; this.isover = true;}
好的,《五子飛》遊戲就到此結束了。
代碼中都有相關注釋了,裡沃特就簡單跟小夥伴們說下,不清楚的可以留言。要源碼的可以直接查看網頁原始碼。
還有一點點小BUG,遊戲結束時也沒有提示,要想重新開始遊戲,需要重新整理頁面。
裡沃特旨在跟大家分享怎麼從寫個小遊戲入手,代碼考慮得不周全,夥伴們互相交流。高手們也請指教下:)
HTML5+JS 《五子飛》遊戲實現(一)規則
HTML5+JS 《五子飛》遊戲實現(二)路線分析和資源準備
HTML5+JS 《五子飛》遊戲實現(三)頁面和棋盤棋子
HTML5+JS 《五子飛》遊戲實現(四)夾一個和挑一對
HTML5+JS 《五子飛》遊戲實現(五)移動棋子
HTML5+JS 《五子飛》遊戲實現(六)滑鼠響應與多重選取
HTML5+JS 《五子飛》遊戲實現(七)遊戲試玩
HTML5+JS 《五子飛》遊戲實現(八)人機對戰