// Private methods private void FillMatrix() { MatrixPosition position = new MatrixPosition(0, 0); int currentRow = 1; int currentColumn = 1; int number = 1; while (true) { this.Matrix[position.Row, position.Column] = number; if (!this.IsCellVacant(position.Row, position.Column)) { this.FindVacantCell(position); if (this.IsCellVacant(position.Row, position.Column)) { number++; this.Matrix[position.Row, position.Column] = number; currentRow = 1; currentColumn = 1; } else { break; } } int nextRow = position.Row + currentRow; int nextColumn = position.Column + currentColumn; while (!this.IsInMatrix(nextRow) || !this.IsInMatrix(nextColumn) || this.Matrix[nextRow, nextColumn] != 0) { this.GetDirection(ref currentRow, ref currentColumn); nextRow = position.Row + currentRow; nextColumn = position.Column + currentColumn; } position.Row = nextRow; position.Column = nextColumn; number++; } }
private void FindVacantCell(MatrixPosition position) { for (int currRow = 0; currRow < this.MatrixSize; currRow++) { for (int currColumn = 0; currColumn < this.MatrixSize; currColumn++) { if (this.Matrix[currRow, currColumn] == 0) { position.Row = currRow; position.Column = currColumn; return; } } } }