public void ClearColorType(SweetsColorType.ColorType color) { for (int x = 0; x < columns; x++) { for (int y = 0; y < rows; y++) { if (sweets[x, y].ColorAble() && (sweets[x, y].ColoredComponent.ThisType == color || color == SweetsColorType.ColorType.UNIVERSAL)) { EliminateSweet(x, y); } } } }
public List <SweetsController> MatchSweets(SweetsController sweet, int newX, int newY) { if (sweet.ColorAble()) { SweetsColorType.ColorType color = sweet.ColoredComponent.ThisType; List <SweetsController> matchSweetsInSameRow = new List <SweetsController>(); List <SweetsController> matchSweetsInSameCol = new List <SweetsController>(); List <SweetsController> matchingResult = new List <SweetsController>(); //row maching matchSweetsInSameRow.Add(sweet); //match the current row.(0->left, 1->right) for (int i = 0; i <= 1; i++) { for (int xDis = 1; xDis < columns; xDis++) { int x = newX; if (i == 0) //traverse the left side. { x -= xDis; } if (i == 1) //traverse the right side. { x += xDis; } if (x < 0 || x >= columns) //encounter the border. { break; } if (sweets[x, newY].ColorAble() && sweets[x, newY].ColoredComponent.ThisType == color) { matchSweetsInSameRow.Add(sweets[x, newY]); } else { break; } } } //"L","T" shape matching. if (matchSweetsInSameRow.Count >= 3) { for (int i = 0; i < matchSweetsInSameRow.Count; i++) { matchingResult.Add(matchSweetsInSameRow[i]); } for (int j = 0; j <= 1; j++) //只需扫描被调换元素的列而无需所有行匹配成功元素? { //apply col-matching to the first element. for (int yDis = 1; yDis <= rows; yDis++) { int y = newY; if (j == 0) { y -= yDis; } if (j == 1) { y += yDis; } if (y < 0 || y >= rows) { break; } if (sweets[matchSweetsInSameRow[0].X, y].ColorAble() && sweets[matchSweetsInSameRow[0].X, y].ColoredComponent.ThisType == color) { matchSweetsInSameCol.Add(sweets[matchSweetsInSameRow[0].X, y]); } else { break; } } } if (matchSweetsInSameCol.Count < 2) { matchSweetsInSameCol.Clear(); } else { for (int k = 0; k < matchSweetsInSameCol.Count; k++) { matchingResult.Add(matchSweetsInSameCol[k]); } } } if (matchingResult.Count >= 3) { //If successed in matching sweets in one row, then return. return(matchingResult); } else { matchSweetsInSameRow.Clear(); matchSweetsInSameCol.Clear(); } //columns matching matchSweetsInSameCol.Add(sweet); //basic element //match the current row to the left and to the right. for (int i = 0; i <= 1; i++) { for (int yDis = 1; yDis < rows; yDis++) { int y = newY; if (i == 0) //traverse the up side. { y -= yDis; } if (i == 1) //traverse the down side. { y += yDis; } if (y < 0 || y >= rows) //encounter the border. { break; } if (sweets[newX, y].ColorAble() && sweets[newX, y].ColoredComponent.ThisType == color) { matchSweetsInSameCol.Add(sweets[newX, y]); } else { break; } } } if (matchSweetsInSameCol.Count >= 3) { for (int i = 0; i < matchSweetsInSameCol.Count; i++) { matchingResult.Add(matchSweetsInSameCol[i]); } for (int j = 0; j <= 1; j++) { for (int xDis = 1; xDis < columns; xDis++) { int x = newX; if (j == 0) { x -= xDis; } if (j == 1) { x += xDis; } if (x < 0 || x >= columns) { break; } if (sweets[x, matchSweetsInSameCol[0].Y].ColorAble() && sweets[x, matchSweetsInSameCol[0].Y].ColoredComponent.ThisType == color) { matchSweetsInSameRow.Add(sweets[x, matchSweetsInSameCol[0].Y]); } else { break; } } } if (matchSweetsInSameRow.Count < 2) { matchSweetsInSameRow.Clear(); } else { for (int k = 0; k < matchSweetsInSameRow.Count; k++) { matchingResult.Add(matchSweetsInSameRow[k]); } } } if (matchingResult.Count >= 3) { //If successed in matching sweets in one column. return(matchingResult); } } return(null); }