public class LineCore { //整体宽度 public int width { get; set; } //整体高度 public int height { get; set; } //前次被点击的图片 public Position lastPicture { get; set; } //当前被电击的图片 public Position currentPicture { get; set; } // 图片矩阵,-1表示没有图片,初始化时,任意matrix[x,y]当x==0或y==0时,matrix[x,y]=-1 public int[,] matrix { get; set; } public bool testLine(Position current) { //上次连接成功后,第一次点击, if (currentPicture == null) { currentPicture = current; return false; } //连续点击了同一幅图片 if (currentPicture.x==current.x&¤tPicture.y==current.y) { return false; } //不是相同的图像,不能消除 if (current.picture != currentPicture.picture) { lastPicture = null; currentPicture = current; return false; } lastPicture = currentPicture; currentPicture = current; //记录连接路径 List paths = new List(); paths.Add(currentPicture); bool finded=false; //向东搜索 finded = _find(paths, 2, Direct.East, lastPicture); if (finded) { return true; } //向南搜索 finded = _find(paths, 2, Direct.South, lastPicture); if (finded) { return true; } //向西搜索 finded = _find(paths, 2, Direct.West, lastPicture); if (finded) { return true; } //向北搜索 finded = _find(paths, 2, Direct.North, lastPicture); if (finded) { return true; } //搜索失败 return false; } /*** * results-> 搜索路径 * chance-> 还剩有转弯的机会,最多2次 * direct-> 当前前进的方向 * target-> 搜索的目标 * **/ protected bool _find(List results, int chance, Direct direct, Position target) { //路径中不能为空 if (results.Count <= 0) { return false; } //取路径中最后一个 Position currentPos = results.ElementAt(results.Count - 1); //构造下一个将要检测的路径 Position nextPos = new Position(); switch(direct){ case Direct.East: nextPos.x=currentPos.x+1; nextPos.y=currentPos.y; break; case Direct.West: nextPos.x=currentPos.x-1; nextPos.y=currentPos.y; break; case Direct.South: nextPos.x=currentPos.x; nextPos.y=currentPos.y+1; break; case Direct.North: nextPos.x=currentPos.x; nextPos.y=currentPos.y-1; break; } bool overFlow = false; //检测是否已经走出边界 if (nextPos.x < 0 || nextPos.x >= width || nextPos.y < 0 || nextPos.y>=height) { overFlow = true; } if (overFlow) { return false; } //检测是否是目标 if (target.x == nextPos.x&&target.y==nextPos.y) { results.Add(nextPos); return true; } //不是-1 说明该位置有图片,不能通过 if (matrix[nextPos.x, nextPos.y]!=-1) { return false; } //加入路径 results.Add(nextPos); bool find = false; //按照当前方向继续试探 find = _find(results, chance, direct, target); if (find) { return true; } //转弯机会已经耗尽 if (chance <= 0) { //此路不通,将路径最后一个位置删除 results.RemoveAt(results.Count - 1); return false; } //当前方向向东,还可以转弯向南,向北继续试探 if (Direct.East == direct) { find = _find(results, chance-1, Direct.North, target); if (find) { return true; } find = _find(results, chance-1, Direct.South, target); if (find) { return true; } results.RemoveAt(results.Count - 1); return false; } //当前方向向南,还可以转弯向东,向西继续试探 else if (Direct.South == direct) { find = _find(results, chance-1, Direct.East, target); if (find) { return true; } find = _find(results, chance-1, Direct.West, target); if (find) { return true; } results.RemoveAt(results.Count - 1); return false; } //当前方向向西,还可以转弯向南,向北继续试探 else if (Direct.West == direct) { find = _find(results, chance-1, Direct.North, target); if (find) { return true; } find = _find(results, chance-1, Direct.South, target); if (find) { return true; } results.RemoveAt(results.Count - 1); return false; } //当前方向向北,还可以转弯向东,向西继续试探 else { find = _find(results, chance-1, Direct.East, target); if (find) { return true; } find = _find(results, chance-1, Direct.West, target); if (find) { return true; } results.RemoveAt(results.Count - 1); return false; } } //联通成功后,将两个位置设置成空 public void onSuccess() { this.matrix[this.lastPicture.x, this.lastPicture.y] = -1; this.matrix[this.currentPicture.x, this.currentPicture.y] = -1; this.currentPicture = null; this.lastPicture = null; } } public class Position { public int x { get; set; } public int y { get; set; } //picture 表示不同图像,如果是-1,表示为空 public int picture { get; set; } } public enum Direct : int { East = 0, South = 1, West = 2, North = 3 } //csharp/1148