public static int[] TakeAnyFreeCellInRect( Board board, int cellAtTopLeft, int cellAtBottomRight) { Contract.Requires(board != null); var coordinateTopLeft = CellCoordinates.FromCellAt(cellAtTopLeft, board.Dimensions); var coordinateBottomRight = CellCoordinates.FromCellAt(cellAtBottomRight, board.Dimensions); var cellsAt = new List <int>(); for (int y = coordinateTopLeft.Y; y <= coordinateBottomRight.Y; y++) { for (int x = coordinateTopLeft.X; x <= coordinateBottomRight.X; x++) { if (!CellCoordinates.IsInside(x, y, board.Dimensions)) { continue; } var cellAt = CellCoordinates.ToCellAt(x, y, board.Dimensions); if (board.Cells[cellAt] == CellOwner.None) { cellsAt.Add(cellAt); } } } return(TakeAnyCell(cellsAt)); }
private static void FindInCellOwnerSpans( Action <Consecutiveness> consecutivenessConsumer, Board board, int lineDimension, int minimumSpan, Func <int, CellCoordinates> iteratorToCoordinates) { // there are consecutiveness only if line dimension is big enough if (lineDimension < minimumSpan) { return; } // first cell is our pivot cell for the first span var pivotCellAt = CellCoordinates.ToCellAt(iteratorToCoordinates(0), board.Dimensions); // track spans of same CellOwners var cellsAt = new List <int>() { pivotCellAt }; var ownerOfSpan = board.Cells[pivotCellAt]; foreach (var index in Range(1, lineDimension - 1)) { var iAsCellAt = CellCoordinates.ToCellAt(iteratorToCoordinates(index), board.Dimensions); var ownerAtCell = board.Cells[iAsCellAt]; if (ownerOfSpan == ownerAtCell) { cellsAt.Add(iAsCellAt); continue; } // CellOwner changed, so span ended // add span as consecutiveness if it exceeds the minimum span length if (cellsAt.Count >= minimumSpan && ownerOfSpan != CellOwner.None) { consecutivenessConsumer(new Consecutiveness(cellsAt)); } // start next span cellsAt = new List <int>() { iAsCellAt }; ownerOfSpan = ownerAtCell; } // don't forget to check the last started span if (cellsAt.Count >= minimumSpan && ownerOfSpan != CellOwner.None) { consecutivenessConsumer(new Consecutiveness(cellsAt)); } }