/// <summary> /// 匹配方法 /// </summary> /// <param name="sweet">当前选中甜品</param> /// <param name="newX">位置,x坐标</param> /// <param name="newY">位置,Y坐标</param> /// <returns></returns> public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY) { if (sweet.CanColor()) { ColorSweet.ColorType color = sweet.ColoredComponent.Color; List <GameSweet> matchRowSweets = new List <GameSweet>(); List <GameSweet> matchLineSweets = new List <GameSweet>(); List <GameSweet> finishedMatchingSweets = new List <GameSweet>(); //行匹配 matchRowSweets.Add(sweet); //i=0代表往左,i=1代表往右 for (int i = 0; i <= 1; i++) { for (int xDistance = 0; xDistance < xColumn; xDistance++) { int x; if (i == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xColumn) { break; } if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Color == color) { matchRowSweets.Add(sweets[x, newY]); } else { break; } } } //检查一下当前行遍历列表中的元素数量是否大于3 if (matchRowSweets.Count >= 3) { for (int i = 0; i < matchRowSweets.Count; i++) { finishedMatchingSweets.Add(matchRowSweets[i]); } } if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } } return(null); }
//清除颜色的方法 public void ClearColor(ColorSweet.ColorType color) { for (int x = 0; x < xColumn; x++) { for (int y = 0; y < yRow; y++) { if (sweets[x, y].CanColor() && (sweets[x, y].ColoredComponent.Color == color || color == ColorSweet.ColorType.ANY)) { ClearSweet(x, y); } } } }
/// <summary> /// 清除颜色 /// </summary> /// <param name="color"></param> public void ClearColor(ColorSweet.ColorType color) { for (int x = 0; x < xLie; x++) { for (int y = 0; y < yHang; y++) { if (sweets[x, y].CanColor() && (sweets[x, y].ColorComponet.Color == color || color == ColorSweet.ColorType.ANY)) { ClearSweet(x, y); //清除颜色 } } } }
/// <summary> /// 匹配方法 /// </summary> /// <param name="sweet"></param> /// <param name="newX"></param> /// <param name="newY"></param> /// <returns></returns> public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY) { if (sweet.CanColor()) { ColorSweet.ColorType color = sweet.ColoredComponent.Color; List <GameSweet> matchRowSweets = new List <GameSweet>(); List <GameSweet> matchLineSweets = new List <GameSweet>(); List <GameSweet> finishedMatchingSweets = new List <GameSweet>(); //行匹配 matchRowSweets.Add(sweet); //i=0代表往左,i=1代表往右 for (int i = 0; i <= 1; i++) { for (int xDistance = 1; xDistance < xColumn; xDistance++) { int x; if (i == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xColumn) { break; } if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Color == color) { matchRowSweets.Add(sweets[x, newY]); } else { break; } } } if (matchRowSweets.Count >= 3) { for (int i = 0; i < matchRowSweets.Count; i++) { finishedMatchingSweets.Add(matchRowSweets[i]); } } //L T型匹配 //检查一下当前行遍历列表中的元素数量是否大于3 if (matchRowSweets.Count >= 3) { for (int i = 0; i < matchRowSweets.Count; i++) { //行匹配列表中满足匹配条件的每个元素上下依次进行列遍历 // 0代表上方 1代表下方 for (int j = 0; j <= 1; j++) { for (int yDistance = 1; yDistance < yRow; yDistance++) { int y; if (j == 0) { y = newY - yDistance; } else { y = newY + yDistance; } if (y < 0 || y >= yRow) { break; } if (sweets[matchRowSweets[i].X, y].CanColor() && sweets[matchRowSweets[i].X, y].ColoredComponent.Color == color) { matchLineSweets.Add(sweets[matchRowSweets[i].X, y]); } else { break; } } } if (matchLineSweets.Count < 2) { matchLineSweets.Clear(); } else { for (int j = 0; j < matchLineSweets.Count; j++) { finishedMatchingSweets.Add(matchLineSweets[j]); } break; } } } if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } matchRowSweets.Clear(); matchLineSweets.Clear(); matchLineSweets.Add(sweet); //列匹配 //i=0代表往左,i=1代表往右 for (int i = 0; i <= 1; i++) { for (int yDistance = 1; yDistance < yRow; yDistance++) { int y; if (i == 0) { y = newY - yDistance; } else { y = newY + yDistance; } if (y < 0 || y >= yRow) { break; } if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Color == color) { matchLineSweets.Add(sweets[newX, y]); } else { break; } } } if (matchLineSweets.Count >= 3) { for (int i = 0; i < matchLineSweets.Count; i++) { finishedMatchingSweets.Add(matchLineSweets[i]); } } //L T型匹配 //检查一下当前行遍历列表中的元素数量是否大于3 if (matchLineSweets.Count >= 3) { for (int i = 0; i < matchLineSweets.Count; i++) { //行匹配列表中满足匹配条件的每个元素上下依次进行列遍历 // 0代表上方 1代表下方 for (int j = 0; j <= 1; j++) { for (int xDistance = 1; xDistance < xColumn; xDistance++) { int x; if (j == 0) { x = newY - xDistance; } else { x = newY + xDistance; } if (x < 0 || x >= xColumn) { break; } if (sweets[x, matchLineSweets[i].Y].CanColor() && sweets[x, matchLineSweets[i].Y].ColoredComponent.Color == color) { matchRowSweets.Add(sweets[x, matchLineSweets[i].Y]); } else { break; } } } if (matchRowSweets.Count < 2) { matchRowSweets.Clear(); } else { for (int j = 0; j < matchRowSweets.Count; j++) { finishedMatchingSweets.Add(matchRowSweets[j]); } break; } } } if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } } return(null); }
//匹配方法 public List <GameSweet> MatchSweet(GameSweet sweet, int newX, int newY) { if (sweet.CanColor()) { ColorSweet.ColorType color = sweet.ColorComponent.Color; List <GameSweet> matchRowSweets = new List <GameSweet>(); List <GameSweet> matchColumnSweets = new List <GameSweet>(); List <GameSweet> finishedMatchingSweets = new List <GameSweet>(); //行匹配 matchRowSweets.Add(sweet); matchColumnSweets.Add(sweet); for (int i = 0; i <= 1; i++) { //i=0是左边遍历,i=1是右遍历。左右各遍历一次 for (int xDistance = 1; xDistance < xColumn; xDistance++) { //遍历的新位置 int x; if (i == 0) { x = newX - xDistance; } else { x = newX + xDistance; } //避免越界 if (x < 0 || x >= xColumn) { break; } if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponent.Color == color) { matchRowSweets.Add(sweets[x, newY]); } else { break; } } //i=0是下边遍历,i=1是上边遍历。上下各遍历一次 for (int yDistance = 1; yDistance < yRow; yDistance++) { //遍历的新位置 int y; if (i == 0) { //加是向下遍历 y = newY + yDistance; } else { y = newY - yDistance; } //避免越界 if (y < 0 || y >= yRow) { break; } if (sweets[newX, y].CanColor() && sweets[newX, y].ColorComponent.Color == color) { matchColumnSweets.Add(sweets[newX, y]); } else { break; } } } //如果大于3,就添加到完成的数组里 if (matchColumnSweets.Count >= 3) { matchRowSweets.Clear(); for (int i = 0; i < matchColumnSweets.Count; i++) { finishedMatchingSweets.Add(matchColumnSweets[i]); //和上面一样,两个方向 for (int j = 0; j <= 1; j++) { int x; for (int xDistance = 1; xDistance < xColumn; xDistance++) { if (j == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xColumn) { break; } if (sweets[x, matchColumnSweets[i].Y].CanColor() && sweets[x, matchColumnSweets[i].Y].ColorComponent.Color == color) { matchRowSweets.Add(sweets[x, matchColumnSweets[i].Y]); } else { break; } } } } //L、T型匹配 if (matchRowSweets.Count >= 3) { for (int i = 0; i < matchRowSweets.Count; i++) { finishedMatchingSweets.Add(matchRowSweets[i]); } } //匹配成功 if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } } else if (matchRowSweets.Count >= 3) { matchColumnSweets.Clear(); for (int i = 0; i < matchRowSweets.Count; i++) { finishedMatchingSweets.Add(matchRowSweets[i]); for (int j = 0; j <= 1; j++) { int y; for (int yDistance = 1; yDistance < yRow; yDistance++) { if (j == 0) { y = newX - yDistance; } else { y = newX + yDistance; } if (y < 0 || y >= yDistance) { break; } if (sweets[matchColumnSweets[i].X, y].CanColor() && sweets[matchColumnSweets[i].X, y].ColorComponent.Color == color) { matchRowSweets.Add(sweets[matchColumnSweets[i].X, y]); } else { break; } } } } //L、T型匹配 if (matchColumnSweets.Count >= 3) { for (int i = 0; i < matchColumnSweets.Count; i++) { finishedMatchingSweets.Add(matchColumnSweets[i]); } } //匹配成功 if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } } } return(null); }
//匹配方法 //参数 public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY) { ColorSweet.ColorType type = sweet.ColorComponent.Color; //获取到当前元素的颜色 List <GameSweet> matchRowSweet = new List <GameSweet>(); //横向匹配列表 List <GameSweet> matchLineSweet = new List <GameSweet>(); //纵向匹配列表 List <GameSweet> finishMatchSweet = new List <GameSweet>(); //删除列表 if (sweet.X > newX) //这种情况水平方向只需要向左遍历以及向上下遍历 { for (int i = 1; i < xColumn; i++) { int x = newX - i; //向左前进 if (x < 0) //防止数组越界 { break; } //判断交换元素左边 //如果可以换色并且其颜色和标准一样 if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponent.Color == type) { //将其添加进横向列表当中 matchRowSweet.Add(sweets[x, newY]); } else { break; } } //上下遍历 for (int i = 1; i < yRow; i++) { //向y轴正方向遍历 int y = newY + i; if (y >= yRow) { break; } if (sweets[newX, y].CanColor() && sweets[newX, y].ColorComponent.Color == type) { //将其添加进横向列表当中 matchLineSweet.Add(sweets[newX, y]); } else { break; } } // for (int i = 1; i < yRow; i++) { //向y轴负方向遍历 int y = newY - i; if (y < 0) { break; } if (sweets[newX, y].CanColor() && sweets[newX, y].ColorComponent.Color == type) { //将其添加进横向列表当中 matchLineSweet.Add(sweets[newX, y]); } else { break; } } } else//这种情况水平方向只需要向右遍历以及向上下遍历 { for (int i = 1; i < xColumn; i++) { int x = newX + i; //向右前进 if (x >= xColumn) //防止数组越界 { break; } //判断交换元素 //如果可以换色并且其颜色和标准一样 if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponent.Color == type) { //将其添加进横向列表当中 matchRowSweet.Add(sweets[x, newY]); } else { break; } } //上下遍历 for (int i = 1; i < yRow; i++) { //向y轴正方向遍历 int upY = newY + i; if (upY >= yRow) { break; } if (sweets[newX, upY].CanColor() && sweets[newX, upY].ColorComponent.Color == type) { //将其添加进横向列表当中 matchLineSweet.Add(sweets[newX, upY]); } else { break; } } // for (int i = 1; i < yRow; i++) { //向y轴负方向遍历 int upY = newY - i; if (upY < 0) { break; } if (sweets[newX, upY].CanColor() && sweets[newX, upY].ColorComponent.Color == type) { //将其添加进横向列表当中 matchLineSweet.Add(sweets[newX, upY]); } else { break; } } } //当元素在上方时 if (sweet.Y > newY) //这种情况水平方向需要向下遍历以及左右遍历 { for (int i = 1; i < yRow; i++) { int y = newY - i; //向下方前进 if (y < 0) //防止数组越界 { break; } //判断交换元素左边 //如果可以换色并且其颜色和标准一样 if (sweets[newX, y].CanColor() && sweets[newX, y].ColorComponent.Color == type) { //将其添加进横向列表当中 matchLineSweet.Add(sweets[newX, y]); } else { break; } } //向左右遍历 for (int i = 1; i < xColumn; i++) { //向x轴正方向遍历 int x = newX + i; if (x >= xColumn) { break; } if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponent.Color == type) { //将其添加进横向列表当中 matchRowSweet.Add(sweets[x, newY]); } else { break; } } // for (int i = 1; i < xColumn; i++) { //向y轴负方向遍历 int x = newX - i; if (x < 0) { break; } if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponent.Color == type) { //将其添加进横向列表当中 matchRowSweet.Add(sweets[x, newY]); } else { break; } } } else// { for (int i = 1; i < yRow; i++) { int y = newY + i; //向右前进 if (y >= yRow) //防止数组越界 { break; } //判断交换元素 //如果可以换色并且其颜色和标准一样 if (sweets[newX, y].CanColor() && sweets[newX, y].ColorComponent.Color == type) { //将其添加进横向列表当中 matchRowSweet.Add(sweets[newX, y]); } else { break; } } for (int i = 1; i < xColumn; i++) { //向x轴付方向遍历 int x = newX - i; if (x < 0) { break; } if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponent.Color == type) { //将其添加进横向列表当中 matchLineSweet.Add(sweets[x, newY]); } else { break; } } // for (int i = 1; i < xColumn; i++) { //向y轴正方向遍历 int x = newX + i; if (x >= xColumn) { break; } if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponent.Color == type) { //将其添加进横向列表当中 matchLineSweet.Add(sweets[x, newY]); } else { break; } } } if (matchRowSweet.Count >= 2)//判断如果有一边连续两个 { for (int i = 0; i < matchRowSweet.Count; i++) { finishMatchSweet.Add(matchRowSweet[i]);//加入删除列表 } if (!finishMatchSweet.Contains(sweet)) { finishMatchSweet.Add(sweet);//在将本元素添加进删除列表 } if (finishMatchSweet.Count >= 3) { return(finishMatchSweet); } } else if (matchLineSweet.Count >= 2)// { for (int i = 0; i < matchLineSweet.Count; i++) { finishMatchSweet.Add(matchLineSweet[i]); //加入删除列表 } if (!finishMatchSweet.Contains(sweet)) { finishMatchSweet.Add(sweet);//在将本元素添加进删除列表 } if (finishMatchSweet.Count >= 3) { return(finishMatchSweet); } } matchLineSweet.Clear(); matchRowSweet.Clear(); return(null); }
/// <summary> /// 匹配消除方法 /// </summary> /// <param name="sweet"></param> /// <param name="newX"></param> /// <param name="newY"></param> /// <returns></returns> public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY) { if (sweet.CanColor()) //如果可以着色 { ColorSweet.ColorType color = sweet.ColorComponet.Color; //上色 List <GameSweet> matchHangSweets = new List <GameSweet>(); //物品行 列表 List <GameSweet> matchLieSweets = new List <GameSweet>(); //物品列 列表 List <GameSweet> finishedMatchSweets = new List <GameSweet>(); //完成待删物品 列表 //检查行消除匹配 matchHangSweets.Add(sweet); for (int i = 0; i <= 1; i++) //i等于0,往左,1往右 { for (int xDistance = 1; xDistance < xLie; xDistance++) { int x; //偏移后的 x 坐标 if (i == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xLie) { break; //限定边界 } if (sweets[x, newY].CanColor() && sweets[x, newY].ColorComponet.Color == color) //如果物品颜色一样 { matchHangSweets.Add(sweets[x, newY]); } else { break; } } } if (matchHangSweets.Count >= 3) //行列表元素的添加 { for (int i = 0; i < matchHangSweets.Count; i++) { finishedMatchSweets.Add(matchHangSweets[i]); } } //L T形状匹配 //遍历后,检测当前行遍历元素数量是否大于3 if (matchHangSweets.Count >= 3) { for (int i = 0; i < matchHangSweets.Count; i++) { //行检查后,检测L和T形状。 检查元素上下元素,是否可以消除:0是上,1是下 for (int j = 0; j <= 1; j++) { for (int yDistance = 1; yDistance < yHang; yDistance++) { int y; //被检测物体的,Y轴偏移坐标 if (j == 0) //如果是上方 { y = newY - yDistance; //每次向上递增(物体Y轴坐标,自上而下是0--10) } else { y = newY + yDistance; //每次向下递增() } if (y < 0 || y >= yHang) { break; //限定边界 } if (sweets[matchHangSweets[i].X, y].CanColor() && sweets[matchHangSweets[i].X, y].ColorComponet.Color == color) //如果列方向,颜色一致 { matchLieSweets.Add(sweets[matchHangSweets[i].X, y]); //添加甜品对象到 列表中 } else { break; } } } if (matchLieSweets.Count < 2) //如果在 行列表中的,垂直方向列 数组中,相同元素小于2 { matchLieSweets.Clear(); //清除 } else //满足条件就加到完成列表 { for (int j = 0; j < matchLieSweets.Count; j++) { finishedMatchSweets.Add(matchLieSweets[j]); } break; } } } if (finishedMatchSweets.Count >= 3) { return(finishedMatchSweets); //返回 行LT 列表 } matchHangSweets.Clear(); //开始列检查之前:清除列表 matchLieSweets.Clear(); //列消除匹配 matchLieSweets.Add(sweet); for (int i = 0; i <= 1; i++) //i等于0,往左,1往右 { for (int yDistance = 1; yDistance < yHang; yDistance++) { int y; //偏移后的 y 坐标 if (i == 0) { y = newY - yDistance; } else { y = newY + yDistance; } if (y < 0 || y >= yHang) { break; //限定边界 } if (sweets[newX, y].CanColor() && sweets[newX, y].ColorComponet.Color == color) //如果物品颜色一样 { matchLieSweets.Add(sweets[newX, y]); } else { break; } } } if (matchLieSweets.Count >= 3) //LIE 列表元素的添加 { for (int i = 0; i < matchLieSweets.Count; i++) { finishedMatchSweets.Add(matchLieSweets[i]); } } //垂直列表中,横向L T形状匹配 //遍历后,检测当前行遍历元素数量是否大于3 if (matchLieSweets.Count >= 3) { for (int i = 0; i < matchLieSweets.Count; i++) { //行检查后,检测L和T形状。 检查元素上下元素,是否可以消除:0是上,1是下 for (int j = 0; j <= 1; j++) { for (int xDistance = 1; xDistance < xLie; xDistance++) { int x; //被检测物体的,Y轴偏移坐标 if (j == 0) //如果是上方 { x = newX - xDistance; //每次向上递增(物体Y轴坐标,自上而下是0--10) } else { x = newX + xDistance; //每次向下递增() } if (x < 0 || x >= xLie) { break; //限定边界 } if (sweets[x, matchLieSweets[i].Y].CanColor() && sweets[x, matchLieSweets[i].Y].ColorComponet.Color == color) //如果列方向,颜色一致 { matchHangSweets.Add(sweets[x, matchLieSweets[i].Y]); //添加甜品对象到 列表中 } else { break; } } } if (matchHangSweets.Count < 2) //如果在 列列表中的,左右方向行 数组中,相同元素小于2 { matchHangSweets.Clear(); //清除 } else //满足条件就加到完成列表 { for (int j = 0; j < matchHangSweets.Count; j++) { finishedMatchSweets.Add(matchHangSweets[j]); } break; } } } //这里 if (finishedMatchSweets.Count >= 3) { return(finishedMatchSweets); } } return(null); }
/// <summary> /// 判断是否能进行消除甜品 /// </summary> /// <param name="sweet"></param> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY) { if (sweet.CanMove() && sweet.CanColor())//如果当前甜品是能正常更换精灵的甜品(即不是空甜品或者不是障碍物等) { ColorSweet.ColorType color = sweet.ColorSweetComponent.Color; List <GameSweet> matchRowSweetsList = new List <GameSweet>(); //用来做横向匹配的链表 List <GameSweet> matchColumnSweetList = new List <GameSweet>(); // 用来做纵向匹配的链表 List <GameSweet> finishedMatchList = new List <GameSweet>(); //匹配结束后的链表,同时返回该链表 //行匹配 matchRowSweetsList.Add(sweet); for (int i = 0; i <= 1; i++)//0代表往左匹配,1代表往右匹配 { for (int xDistance = 1; xDistance < xColumn; xDistance++) { int x;//和当前甜品要进行匹配的甜品的x坐标 if (i == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xColumn)//如果匹配完成则直接退出 { break; } if (sweets[x, newY].CanColor() && color == sweets[x, newY].ColorSweetComponent.Color)//如果类型相同,则可以添加进链表 { matchRowSweetsList.Add(sweets[x, newY]); } else { break; } } } if (matchRowSweetsList.Count >= 3) { for (int m = 0; m < matchRowSweetsList.Count; m++) { finishedMatchList.Add(matchRowSweetsList[m]); } } if (matchRowSweetsList.Count >= 3) { for (int i = 0; i < matchRowSweetsList.Count; i++) { // finishedMatchList.Add(matchRowSweetsList[i]); //行匹配完成后,再进行L,T型匹配,即对每一个二元素的列在进行匹配 for (int j = 0; j <= 1; j++)//j=0代表向上匹配,j=1代表向下匹配 { for (int yDistance = 1; yDistance < yRow; yDistance++) { int y; if (j == 0) { y = newY - yDistance; } else { y = newY + yDistance; } if (y < 0 || y >= yRow) { break; } if (sweets[matchRowSweetsList[i].X, y].CanColor() && sweets[matchRowSweetsList[i].X, y].ColorSweetComponent.Color == color) { matchColumnSweetList.Add(sweets[matchRowSweetsList[i].X, y]); } else { break; } } } if (matchColumnSweetList.Count >= 2)//如果又在列方向找到两个或以上甜品,则添加到完成列表中 { for (int k = 0; k < matchColumnSweetList.Count; k++) { finishedMatchList.Add(matchColumnSweetList[k]); } break; } else//否则进行清空 { matchColumnSweetList.Clear(); } } } if (finishedMatchList.Count >= 3) { return(finishedMatchList); } matchRowSweetsList.Clear(); matchColumnSweetList.Clear(); // finishedMatchList.Clear(); //清空后再进行列匹配 //否则进行列匹配 matchColumnSweetList.Add(sweet); for (int i = 0; i <= 1; i++)//0代表往上匹配,1代表往下匹配 { for (int YDistance = 1; YDistance < yRow; YDistance++) { int y;//和当前甜品要进行匹配的甜品的x坐标 if (i == 0) { y = newY - YDistance; } else { y = newY + YDistance; } if (y < 0 || y >= yRow)//如果匹配完成则直接退出 { break; } if (sweets[newX, y].CanColor() && color == sweets[newX, y].ColorSweetComponent.Color)//如果类型相同,则可以添加进链表 { matchColumnSweetList.Add(sweets[newX, y]); } else { break; } } } if (matchColumnSweetList.Count >= 3) { for (int m = 0; m < matchColumnSweetList.Count; m++) { finishedMatchList.Add(matchColumnSweetList[m]); } } if (matchColumnSweetList.Count >= 3)//列匹配完成后再进行L,T,型匹配,即再对每一个已经确定的甜品进行行遍历 { for (int i = 0; i < matchColumnSweetList.Count; i++) { //finishedMatchList.Add(matchColumnSweetList[i]); for (int j = 0; j <= 1; j++)//j=0代表向左匹配,j=1代表向右匹配 { for (int xDistance = 1; xDistance < xColumn; xDistance++) { int x; if (j == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= yRow) { break; } if (sweets[x, matchColumnSweetList[i].Y].CanColor() && sweets[x, matchColumnSweetList[i].Y].ColorSweetComponent.Color == color) { matchRowSweetsList.Add(sweets[x, matchColumnSweetList[i].Y]); } else { break; } } } if (matchRowSweetsList.Count >= 2)//如果又在行方向找到两个或以上甜品,则添加到完成列表中 { for (int k = 0; k < matchRowSweetsList.Count; k++) { finishedMatchList.Add(matchRowSweetsList[k]); } break; } else//否则进行清空 { matchRowSweetsList.Clear(); } } } if (finishedMatchList.Count >= 3) { return(finishedMatchList); } } return(null); }
//匹配方法 public List <GameSweet> MatchSweet(GameSweet sweet, int newX, int newY) { if (sweet.CanColor())//如果可以正常的甜品 { ColorSweet.ColorType color = sweet.ColoredComponent.Clolr; //行列表 List <GameSweet> matchRowSweets = new List <GameSweet>(); //列列表 List <GameSweet> matchLineSweets = new List <GameSweet>(); //完成匹配的列表 List <GameSweet> finishedMatchingSweets = new List <GameSweet>(); #region 行匹配 matchRowSweets.Add(sweet); //0表示往左,1表示往右 for (int i = 0; i <= 1; i++) { for (int xDistance = 1; xDistance < xColumn; xDistance++) { int x = 0; if (i == 0)//往左移动 { x = newX - xDistance; } else//往右移动 { x = newX + xDistance; } if (x < 0 || x >= xColumn)//如果超出范围,退出当前循环 { break; } //如果左边的块是标准快,并且与当前块的类型一样 if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Clolr == color) { matchRowSweets.Add(sweets[x, newY]); } else { break; } } } //检查当前行数列的数值是否大于3 if (matchRowSweets.Count >= 3) { for (int i = 0; i < matchRowSweets.Count; i++) { if (i == 0) { for (int j = 0; j <= 1; j++) { for (int yDistance = 0; yDistance < xColumn; yDistance++) { int y; if (j == 0)//往下移动 { y = newY - yDistance; } else//往下移动 { y = newY + yDistance; } if (y < 0 || y >= yRow)//如果超出范围,推出当前循环 { break; } //如果上下边的块是标准快,并且与当前块的类型一样 if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Clolr == color) { matchLineSweets.Add(sweets[newX, y]); } else { break; } } } } finishedMatchingSweets.Add(matchRowSweets[i]); } if (matchLineSweets.Count >= 3) { foreach (GameSweet item in matchLineSweets) { finishedMatchingSweets.Add(item); } return(finishedMatchingSweets); } foreach (var item in finishedMatchingSweets) { Destroy(item.gameObject); } } //如果匹配的个数大于3,返回完成匹配的集合 if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } #endregion /* #region 列匹配 * matchLineSweets.Add(sweet); * //0表示往上,1表示往下 * for (int i = 0; i <= 1; i++) * { * for (int yDistance = 1; yDistance < yRow; yDistance++) * { * int y ; * if (i == 0)//往下移动 * { * y = newY - yDistance; * } * else//往下移动 * { * y = newY + yDistance; * } * if (y < 0 || y >= yRow)//如果超出范围,推出当前循环 * { * break; * } * //如果上下边的块是标准快,并且与当前块的类型一样 * if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Clolr == color) * { * matchLineSweets.Add(sweets[newX, y]); * } * else * { * break; * } * } * } * //检查当前行数列的数值是否大于3 * if (matchLineSweets.Count >= 3) * { * foreach (GameSweet item in matchLineSweets) * { * finishedMatchingSweets.Add(item); * } * } * //如果匹配的个数大于3,返回完成匹配的集合 * if (finishedMatchingSweets.Count >= 3) * { * return finishedMatchingSweets; * } #endregion */ } return(null); }
public List <GameSweet> MatchSweets(GameSweet sweet, int newX, int newY) { if (sweet.CanColor()) { ColorSweet.ColorType color = sweet.ColoredComponent.Color; List <GameSweet> matchRowSweets = new List <GameSweet>(); List <GameSweet> matchLineSweets = new List <GameSweet>(); List <GameSweet> finishedMatchingSweets = new List <GameSweet>(); matchRowSweets.Add(sweet); for (int i = 0; i <= 1; i++) { for (int xDistance = 1; xDistance < xColumn; xDistance++) { int x; if (i == 0) { x = newX - xDistance; } else { x = newX + xDistance; } if (x < 0 || x >= xColumn) { break; } if (sweets[x, newY].CanColor() && sweets[x, newY].ColoredComponent.Color == color) { matchRowSweets.Add(sweets[x, newY]); } else { break; } } } if (matchRowSweets.Count >= 3) { for (int i = 0; i < matchRowSweets.Count; i++) { finishedMatchingSweets.Add(matchRowSweets[i]); } } if (matchRowSweets.Count >= 3) { for (int i = 0; i < matchRowSweets.Count; i++) { for (int j = 0; j <= 1; j++) { for (int yDistance = 1; yDistance < yRow; yDistance++) { int y; if (j == 0) { y = newY - yDistance; } else { y = newY + yDistance; } if (y < 0 || y >= yRow) { break; } if (sweets[matchRowSweets[i].X, y].CanColor() && sweets[matchRowSweets[i].X, y].ColoredComponent.Color == color) { matchLineSweets.Add(sweets[matchRowSweets[i].X, y]); } else { break; } } } if (matchLineSweets.Count < 2) { matchLineSweets.Clear(); } else { for (int j = 0; j < matchLineSweets.Count; j++) { finishedMatchingSweets.Add(matchLineSweets[j]); } break; } } } if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } matchRowSweets.Clear(); matchLineSweets.Clear(); matchLineSweets.Add(sweet); for (int i = 0; i <= 1; i++) { for (int yDistance = 1; yDistance < yRow; yDistance++) { int y; if (i == 0) { y = newY - yDistance; } else { y = newY + yDistance; } if (y < 0 || y >= yRow) { break; } if (sweets[newX, y].CanColor() && sweets[newX, y].ColoredComponent.Color == color) { matchLineSweets.Add(sweets[newX, y]); } else { break; } } } if (matchLineSweets.Count >= 3) { for (int i = 0; i < matchLineSweets.Count; i++) { finishedMatchingSweets.Add(matchLineSweets[i]); } } if (matchLineSweets.Count >= 3) { for (int i = 0; i < matchLineSweets.Count; i++) { for (int j = 0; j <= 1; j++) { for (int xDistance = 1; xDistance < xColumn; xDistance++) { int x; if (j == 0) { x = newY - xDistance; } else { x = newY + xDistance; } if (x < 0 || x >= xColumn) { break; } if (sweets[x, matchLineSweets[i].Y].CanColor() && sweets[x, matchLineSweets[i].Y].ColoredComponent.Color == color) { matchRowSweets.Add(sweets[x, matchLineSweets[i].Y]); } else { break; } } } if (matchRowSweets.Count < 2) { matchRowSweets.Clear(); } else { for (int j = 0; j < matchRowSweets.Count; j++) { finishedMatchingSweets.Add(matchRowSweets[j]); } break; } } } if (finishedMatchingSweets.Count >= 3) { return(finishedMatchingSweets); } } return(null); }