在第一章我們已經(jīng)說(shuō)了怎么才能“夾一個(gè)”以及怎樣才能挑一對(duì),但那畢竟只是書(shū)面上的,對(duì)碼農(nóng)來(lái)講,我們還是用代碼講解起來(lái)會(huì)更容易了解。
為了更容易對(duì)照分析,我們先把路線再次貼出來(lái): // 可走的路線 this.lines = [ [ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24], [ 0, 5, 10, 15, 20], [ 1, 6, 11, 16, 21], [ 2, 7, 12, 17, 22], [ 3, 8, 13, 18, 23], [ 4, 9, 14, 19, 24], [ 0, 6, 12, 18, 24], [ 4, 8, 12, 16, 20], [ 2, 6, 10], [ 2, 8, 14], [10, 16, 22], [14, 18, 22] ];
一、夾一個(gè): 根據(jù)上面給出的有限路線中,要實(shí)現(xiàn)“夾一個(gè)”,首頁(yè)這顆棋子的index得在[0,1,2...24]之中,我們循環(huán)搜索每條路線,只要找出符合條件的路線和位置就可把對(duì)方的棋子給吃掉。 首先我們找出棋子的目標(biāo)位置是在哪條路線中: int index = $.inArray(chess.point.index, this.lines[i]);//chess被移動(dòng)的棋子,下同 然后再找出該條線上能被吃掉的棋子是哪一個(gè)。如果按照水平方向來(lái)看,被吃掉的棋子有可能在左邊,也有可能在右邊,如果在左邊,那么該方還有一個(gè)棋子應(yīng)該在被吃掉的棋子的左邊: var p1 = index > 1 ? this.chesses[this.lines[i][index - 1]].player : Player.None; if (p1 != Player.None && p1 != chess.player) { if (this.chesses[this.lines[i][index - 2]].player == chess.player) { //...找到符合條件的路線 同理,如果被吃掉的棋子在右邊,那么該方還有一個(gè)棋子應(yīng)該在被吃掉的棋子的右邊: var p2 = index < this.lines[i].length - 2 ? this.chesses[this.lines[i][index + 1]].player : Player.None; if (p2 != Player.None && p2 != chess.player) { if (this.chesses[this.lines[i][index + 2]].player == chess.player) { //...找到符合條件的路線 不過(guò),因?yàn)?span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">按照規(guī)則,能夾對(duì)方棋子的同時(shí),該條路徑上僅且只能有三顆棋子,已方兩顆,對(duì)方一顆,其他位置上是不能有棋子存在的: 對(duì)于在左邊的情況: var bfind = true;// 是否找到能被吃的棋子 for (j = 0; j < index - 2; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) return; bfind = true; for (j = index + 1; j < this.lines[i].length; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) return; chessArray.push([this.chesses[this.lines[i][index - 1]]]);// 找到了 對(duì)于在右邊的情況: var bfind = true; for (j = 0; j < index; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) return; bfind = true; for (j = index + 3; j < this.lines[i].length; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) return; chessArray.push([this.chesses[this.lines[i][index + 1]]]);// 找到了 對(duì)于找到可以被夾掉的棋子我們記錄下來(lái),存到 chessArray 里面,以便進(jìn)行其他操作。 二、挑一對(duì): 同樣,我們先找出棋子在哪條路徑中: index = $.inArray(chess.point.index, this.lines[i]); if (index > 0 && index < this.lines[i].length - 1) { 然后相對(duì)于夾一個(gè)來(lái)說(shuō)簡(jiǎn)單很多,我們只要找出該棋子左右相鄰的兩個(gè)棋子是對(duì)方的棋子,且該條直線上其他位置都是空位就行了。 先找出左右相鄰的兩顆棋子: var p1 = this.chesses[this.lines[i][index - 1]].player; var p2 = this.chesses[this.lines[i][index + 1]].player; if (p1 != chess.player && p2 != chess.player && p1 != Player.None && p2 != Player.None) { 再判斷其他位置是空位: var bfind = true; for (j = 0; j < index - 1; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) return; bfind = true; for (j = this.lines[i].length - 1; j > index + 1; j--) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) return; chessArray.push([this.chesses[this.lines[i][index - 1]], this.chesses[this.lines[i][index + 1]]]);// 找到了
現(xiàn)在實(shí)現(xiàn)了兩個(gè)基本的函數(shù),下一章里沃特再跟大家分析移動(dòng)棋子。本章實(shí)現(xiàn)的兩個(gè)函數(shù)歸納如下: ![]() // 是否可“挑一對(duì)” this.canCarry = function (chess) { var p1, p2, j, index, bfind, chessArray = []; for (var i = 0; i < this.lines.length; i++) { index = $.inArray(chess.point.index, this.lines[i]); if (index > 0 && index < this.lines[i].length - 1) { p1 = this.chesses[this.lines[i][index - 1]].player; p2 = this.chesses[this.lines[i][index + 1]].player; if (p1 != chess.player && p2 != chess.player && p1 != Player.None && p2 != Player.None) { bfind = true; for (j = 0; j < index - 1; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; bfind = true; for (j = this.lines[i].length - 1; j > index + 1; j--) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; chessArray.push([this.chesses[this.lines[i][index - 1]], this.chesses[this.lines[i][index + 1]]]); } } } return chessArray.length == 0 ? false : chessArray; }; // 是否可“夾一個(gè)” this.canClip = function (chess) { var p1, p2, j, index, bfind, chessArray = []; for (var i = 0; i < this.lines.length; i++) { index = $.inArray(chess.point.index, this.lines[i]); if (index != -1) { p1 = index > 1 ? this.chesses[this.lines[i][index - 1]].player : Player.None; p2 = index < this.lines[i].length - 2 ? this.chesses[this.lines[i][index + 1]].player : Player.None; if (p1 != Player.None && p1 != chess.player) { if (this.chesses[this.lines[i][index - 2]].player == chess.player) { bfind = true; for (j = 0; j < index - 2; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; bfind = true; for (j = index + 1; j < this.lines[i].length; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; chessArray.push([this.chesses[this.lines[i][index - 1]]]); } } else if (p2 != Player.None && p2 != chess.player) { if (this.chesses[this.lines[i][index + 2]].player == chess.player) { bfind = true; for (j = 0; j < index; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; bfind = true; for (j = index + 3; j < this.lines[i].length; j++) { if (this.chesses[this.lines[i][j]].player != Player.None) { bfind = false; break; } } if (!bfind) continue; chessArray.push([this.chesses[this.lines[i][index + 1]]]); } } } } return chessArray.length == 0 ? false : chessArray; };
HTML5+JS 《五子飛》游戲?qū)崿F(xiàn)(一)規(guī)則 |
|
來(lái)自: 昵稱10504424 > 《工作》