博主玩了這麼久的連連看,竟然是第一次發現,連連看最多隻能有2個轉彎。orz…javascript
在網上搜索連連看的連線算法判斷,並無找到很全面的,通過本身摸索以後,作了一些小動畫,但願你們能夠看一遍都懂啦~)java
一. 2個物體在同一直線上,能夠直接連通 (這個不須要解釋啦)算法
二. 2個對象不在同一直線上,一個轉彎數組
【2個物體分別在所在位置進行x,y軸的延伸,以下圖則交點爲A,B。 只需判斷2個交點到2個物體直接是否有障礙物,若沒有,則能夠連通】框架
看動畫容易理解一點~iphone
三. 2個物體不在同一直線上,連線有2個轉彎 ide
【以下圖,因爲有2個轉彎,不一樣於1個轉彎的狀況,咱們須要有2個拐點。這2個拐點必須在同一個軸上(x軸 或者 y軸),那咱們只要分別作這2個點的x軸的延伸,或者y軸的延伸,一個一個取點遍歷掃描,若均可以連線,則能夠連線~】動畫
以上就是四種連線算法判斷,動畫只列舉部分狀況,每一種按照一樣的原理掃描。可覆蓋全部連線判斷~this
說完連線判斷的邏輯以後,寫一下總體的遊戲框架,遊戲基本使用原生javascript,使用createjs遊戲引擎進行開發。spa
代碼思路:
1. 繪製遊戲畫圖,肯定爲多少宮圖,因爲是在移動端的小遊戲,根據最小屏幕尺寸(iphone4 320*480),肯定爲7*9的宮圖。
1. 建立一個二維數組,若是某個座標上有物體,則設爲1,不然爲0
2.判斷該位置是否有物體,只須要判斷對應的二維數組上值是否爲1,若爲1,則有物體,不然沒有。
至於畫線,消除相同物體,只要會連線邏輯,確定就會本身繪製線條,消除物體了,因此本篇文章就只講連線判斷啦~
在判斷可否連線的時候,確定是從最簡單的方法開始判斷,以下:
同一直線可否直線連通--->如何一點被包圍,則不通--->兩點在一條直線上,不能直線鏈接可是能夠連通---> 不在同一直線可是能夠連通
getPath: function (p1, p2) { //開始搜索前對p1,p2排序,使p2儘量的在p1的右下方。 if (p1.x > p2.x) { var t = p1; p1 = p2; p2 = t; } else if (p1.x == p2.x) { if (p1.y > p2.y) { var t = p1; p1 = p2; p2 = t; } } //2點在同一直線上,能夠直線連通 if (this.hasLine(p1, p2).status) { return true; } //若是兩點中任何一個點被全包圍,則不通。 else if (this.isWrap(p1, p2)) { return false; } //兩點在一條直線上,不能直線鏈接可是能夠連通 else if (this.LineLink(p1, p2)) { return true; } //不在同一直線可是能夠連通 else if (this.curveLink(p1, p2)) { return true; } }
//判斷同一條線可否連通,x軸相同或者y軸相同 hasLine: function (p1, p2) { this.path = []; //同一點 if (p1.x == p2.x && p1.y == p2.y) { return { status: false }; } if (this.onlineY(p1, p2)) { var min = p1.y > p2.y ? p2.y : p1.y; min = min + 1; var max = p1.y > p2.y ? p1.y : p2.y; for (min; min < max; min++) { var p = {x: p1.x, y: min}; if (!this.isEmpty(p)) { console.log('有障礙物p點………………'); console.log(p); this.path = []; break; } this.path.push(p); } if (min == max) { return { status: true, data: this.path, dir: 'y' //y軸 }; } this.path = []; return { status: false }; } else if (this.onlineX(p1, p2)) { var j = p1.x > p2.x ? p2.x : p1.x; j = j + 1; var max = p1.x > p2.x ? p1.x : p2.x; for (j; j < max; j++) { var p = {x: j, y: p1.y}; if (!this.isEmpty(p)) { console.log('有障礙物p點………………'); console.log(p); this.path = []; break; } this.path.push(p); } if (j == max) { return { status: true, data: this.path, dir: 'x' //x軸 }; } this.path = []; return { status: false }; } return { status: false }; //2點是否有其中一點被全包圍,如有,則返回true isWrap: function (p1, p2) { //有一點爲空,則條件不成立 if (!this.isEmpty({x: p1.x, y: p1.y + 1}) && !this.isEmpty({ x: p1.x, y: p1.y - 1 }) && !this.isEmpty({ x: p1.x - 1, y: p1.y }) && !this.isEmpty({x: p1.x + 1, y: p1.y})) { return true; } if (!this.isEmpty({x: p2.x, y: p2.y + 1}) && !this.isEmpty({ x: p2.x, y: p2.y - 1 }) && !this.isEmpty({ x: p2.x - 1, y: p2.y }) && !this.isEmpty({x: p2.x + 1, y: p2.y})) { return true; } return false; } //兩點在一條直線上,不能直線鏈接可是能夠連通 LineLink: function (p1, p2) { var pt0, pt1, pt2, pt3; //若是都在x軸,則自左至右掃描可能的路徑, //每次構造4個頂點pt0, pt1, pt2, pt3,而後看他們兩兩之間是否連通 if (this.onlineX(p1, p2)) { for (var i = 0; i < this.H; i++) { if (i == p1.y) { continue; } pt0 = p1; pt1 = {x: p1.x, y: i}; pt2 = {x: p2.x, y: i}; pt3 = p2; //若是頂點不爲空,則該路不通。 if (!this.isEmpty(pt1) || !this.isEmpty(pt2)) { continue; } if (this.hasLine(pt0, pt1).status && this.hasLine(pt1, pt2).status && this.hasLine(pt2, pt3).status) { this.drawLine(2, [pt0, pt3, pt1, pt2]); return [pt0, pt1, pt2, pt3]; } } } //若是都在y軸,則自上至下掃描可能的路徑, //每次構造4個頂點pt0, pt1, pt2, pt3,而後看他們兩兩之間是否連通 if (this.onlineY(p1, p2)) { for (var j = 0; j < this.W; j++) { if (j == p1.x) { continue; } pt0 = p1; pt1 = {x: j, y: p1.y}; pt2 = {x: j, y: p2.y}; pt3 = p2; //若是頂點不爲空,則該路不通。 if (!this.isEmpty(pt1) || !this.isEmpty(pt2)) { continue; } if (this.hasLine(pt0, pt1).status && this.hasLine(pt1, pt2).status && this.hasLine(pt2, pt3).status) { this.drawLine(2, [pt0, pt3, pt1, pt2]); return [pt0, pt1, pt2, pt3]; } } } }, //兩點不在一條直線上,看是否可通 curveLink: function (p1, p2) { var pt0, pt1, pt2, pt3; //特殊狀況,先判斷是不是一個轉彎 var spec1 = {x: p1.x, y: p2.y}, spec2 = {x: p2.x, y: p1.y}; if (this.isEmpty(spec1)) { if (this.hasLine(p1, spec1).status && this.hasLine(p2, spec1).status) { console.log('1個轉彎'); this.drawLine(1, [p1, p2, spec1]); return [p1, p2, spec1]; } } if (this.isEmpty(spec2)) { if (this.hasLine(p1, spec2).status && this.hasLine(p2, spec2).status) { console.log('1個轉彎'); // console.table([pt0, spec2, pt3]); this.drawLine(1, [p1, p2, spec2]); return [p1, spec2, p2]; } } //先縱向掃描可能的路徑 //一樣,每次構造4個頂點,看是否可通 for (var k = 0; k <= this.H; k++) { pt0 = p1; pt1 = {x: p1.x, y: k}; pt2 = {x: p2.x, y: k}; pt3 = p2; //2個交點都爲空 if (this.isEmpty(pt1) && this.isEmpty(pt2)) { //2個轉彎 if (this.hasLine(pt0, pt1).status && this.hasLine(pt1, pt2).status && this.hasLine(pt2, pt3).status) { console.log('2個轉彎'); this.drawLine(2, [pt0, pt3, pt1, pt2]); return [pt0, pt3, pt1, pt2]; } } } //橫向掃描全部可能的路徑 for (var k = 0; k <= this.W; k++) { pt0 = p1; pt1 = {x: k, y: p1.y}; pt2 = {x: k, y: p2.y}; pt3 = p2; //2個交點都爲空 if (this.isEmpty(pt1) && this.isEmpty(pt2)) { //2個轉彎 if (this.hasLine(pt0, pt1).status && this.hasLine(pt1, pt2).status && this.hasLine(pt2, pt3).status) { console.log('2個轉彎'); this.drawLine(2, [pt0, pt3, pt1, pt2]); return [pt0, pt3, pt1, pt2]; } } } return false; }