void CheckMatch(Block block, Chain chain) { int x = block.X; int y = block.Y; // look in four directions for matching lines int left = x; while (left > 0) { if (StateAt(left - 1, y) != GridElement.ElementState.Block) { break; } if (!MatchAt(left - 1, y, block)) { break; } left--; } int right = x + 1; while (right < PlayWidth) { if (StateAt(right, y) != GridElement.ElementState.Block) { break; } if (!MatchAt(right, y, block)) { break; } right++; } int bottom = y; while (bottom > 1) { if (StateAt(x, bottom - 1) != GridElement.ElementState.Block) { break; } if (!MatchAt(x, bottom - 1, block)) { break; } bottom--; } int top = y + 1; while (top < PlayHeight) { if (StateAt(x, top) != GridElement.ElementState.Block) { break; } if (!MatchAt(x, top, block)) { break; } top++; } int width = right - left; int height = top - bottom; int magnitude = 0; bool horizontalPattern = false; bool verticalPattern = false; if (width >= MinimumPatternLength) { horizontalPattern = true; magnitude += width; } if (height >= MinimumPatternLength) { verticalPattern = true; magnitude += height; } if (!horizontalPattern && !verticalPattern) { block.EndChainInvolvement(chain); return; } if (chain == null) { chain = ChainManager.CreateChain(); } // if pattern matches both directions if (horizontalPattern && verticalPattern) { magnitude--; } // kill the pattern's blocks and look for touching garbage block.StartDying(chain, magnitude); if (horizontalPattern) { // kill the pattern's blocks for (int killX = left; killX < right; killX++) { if (killX != x) { BlockAt(killX, y).StartDying(chain, magnitude); } } } if (verticalPattern) { // kill the pattern's blocks for (int killY = bottom; killY < top; killY++) { if (killY != y) { BlockAt(x, killY).StartDying(chain, magnitude); } } } chain.ReportMatch(magnitude, block); }