public void ClearColor(ColorPieces.ColorType color) { for (int x = 0; x < xDim; x++) { for (int y = 0; y < yDim; y++) { if (pieces[x, y].IsColorpiece() && (pieces[x, y].Colorpieces.Color == color || color == ColorPieces.ColorType.任意)) { ClearPiece(x, y); } } } }
/// <summary> /// 检查相邻的sprite是否为同一颜色,为同一颜色就加入匹配列表。否则匹配列表返回NUll。 /// </summary> /// <param name="piece">当前鼠标点击选择的对象</param> /// <param name="newX">鼠标进入对象的X</param> /// <param name="newY">鼠标进入对象的Y</param> /// <returns></returns> public List <GamePiece> GetSameColorPiece(GamePiece piece, int newX, int newY) { if (piece.IsColorpiece()) { ColorPieces.ColorType Color = piece.Colorpieces.Color; List <GamePiece> horizontalList = new List <GamePiece>(); List <GamePiece> verticaList = new List <GamePiece>(); List <GamePiece> matching = new List <GamePiece>(); horizontalList.Add(piece); for (int i = 0; i <= 1; i++) { for (int Xindex = 1; Xindex < xDim; Xindex++) { int x; if (i == 0) { x = newX - Xindex; } else { x = newX + Xindex; } if (x < 0 || x >= xDim) { break; } if (pieces[x, newY].IsColorpiece() && pieces[x, newY].Colorpieces.Color == Color) { horizontalList.Add(pieces[x, newY]); } else { break; } } } if (horizontalList.Count >= 3) { for (int x = 0; x < horizontalList.Count; x++) { matching.Add(horizontalList[x]); } } if (horizontalList.Count >= 3) { for (int i = 0; i < horizontalList.Count; i++) { for (int dir = 0; dir <= 1; dir++) { for (int yOffset = 1; yOffset < yDim; yOffset++) { int y; if (dir == 0) // Up { y = newY - yOffset; } else // Down { y = newY + yOffset; } if (y < 0 || y >= yDim) { break; } if (pieces [horizontalList [i].X, y].IsColorpiece() && pieces [horizontalList [i].X, y].Colorpieces.Color == Color) { verticaList.Add(pieces [horizontalList [i].X, y]); } else { break; } } } if (verticaList.Count < 2) //至少2个相连 { verticaList.Clear(); } else { for (int j = 0; j < verticaList.Count; j++) { matching.Add(verticaList [j]); } break; } } } if (matching.Count >= 3) { return(matching); } horizontalList.Clear(); verticaList.Clear(); verticaList.Add(piece); for (int i = 0; i <= 1; i++) { for (int Yindex = 1; Yindex < yDim; Yindex++) { int y; if (i == 0) { y = newY - Yindex; } else { y = newY + Yindex; } if (y < 0 || y >= yDim) { break; } if (pieces[newX, y].IsColorpiece() && pieces[newX, y].Colorpieces.Color == Color) { verticaList.Add(pieces[newX, y]); } else { break; } } } if (verticaList.Count >= 3) { for (int x = 0; x < verticaList.Count; x++) { matching.Add(verticaList[x]); } } if (verticaList.Count >= 3) { for (int i = 0; i < verticaList.Count; i++) { for (int dir = 0; dir <= 1; dir++) { for (int xOffset = 1; xOffset < xDim; xOffset++) { int x; if (dir == 0) // Left { x = newX - xOffset; } else // Right { x = newX + xOffset; } if (x < 0 || x >= xDim) { break; } if (pieces [x, verticaList[i].Y].IsColorpiece() && pieces [x, verticaList[i].Y].Colorpieces.Color == Color) { horizontalList.Add(pieces [x, verticaList[i].Y]); } else { break; } } } if (horizontalList.Count < 2) //至少2个相连 { horizontalList.Clear(); } else { for (int j = 0; j < horizontalList.Count; j++) { matching.Add(horizontalList [j]); } break; } } } if (matching.Count >= 3) { return(matching); } } return(null); }