Đệ quy, yo. Đó là khi bạn không biết ranh giới của mình.
public int getHMatchSize(int row, int column)
{
int returnMe = getMatchValue(row, 0, column, 1);
if (returnMe < 3)
{
return 0;
}
else return returnMe;
}
public int getVMatchSize(int row, int column)
{
int returnMe = getMatchValue(row, 1, column, 0);
if (returnMe < 3)
{
return 0;
}
else return returnMe;
}
/// <summary>
/// I return the match size.
/// </summary>
/// <param name="row"></param>
/// <param name="rowDelta">1 means look vertically. Dont set both deltas to 1.</param>
/// <param name="column"></param>
/// <param name="columnDelta">1 means look horizontally. Dont set both deltas to 1.</param>
/// <returns>The number of contiguous matching things</returns>
public int getMatchValue(int row, int rowDelta, int column, int columnDelta)
{
int[] start = getEndItem(row, -1 * rowDelta, column, -1 * columnDelta);
int[] end = getEndItem(row, rowDelta, column, columnDelta);
int returnMe = 0;
returnMe += end[0] - start[0];
returnMe += end[1] - start[1];
return returnMe;
}
/// <summary>
/// I will return the end of a sequence of matching items.
/// </summary>
/// <param name="row">start here</param>
/// <param name="column">start here</param>
private int[] getEndItem(int row, int rowDelta, int column, int columnDelta)
{
Gem matchGem = new Gem(-1);
int[] returnMe = new int[2];
if (boardSpace[row + rowDelta][column + columnDelta] == boardSpace[row][column])
{
return getEndItem(row + rowDelta, rowDelta, column + columnDelta, columnDelta);
}
else
{
returnMe[0] = row;
returnMe[1] = column;
return returnMe;
}
}