-
Notifications
You must be signed in to change notification settings - Fork 20.3k
Update Sudoku.java #6513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Update Sudoku.java #6513
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3aaf7fd
Update Sudoku.java
KaranMishra3610 f201c31
Update Sudoku.java
KaranMishra3610 98c9423
Refactor: Apply Iterator pattern to SudokuBoard
4a04e9d
Refactor: Apply Iterator pattern to SudokuBoard
6ec70a5
Refactor: Apply Iterator pattern to SudokuBoard
2d32eae
Refactor: Apply Iterator pattern to SudokuBoard
edf6c98
Refactor: Apply Iterator pattern to SudokuBoard
020605b
Refactor: Apply Iterator pattern to SudokuBoard
31dbf72
Refactor: Apply Iterator pattern to SudokuBoard
c136733
Refactor: Apply Iterator pattern to SudokuBoard
84cb0c8
Refactor: Apply Iterator pattern to SudokuBoard
8d8aa19
Refactor: Apply Iterator pattern to SudokuBoard
1b999cf
Refactor: Apply Iterator pattern to SudokuBoard
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
package com.thealgorithms.puzzlesandgames; | ||
|
||
/** | ||
|
223 changes: 223 additions & 0 deletions
src/main/java/com/thealgorithms/puzzlesandgames/SudokuBoard.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
package com.thealgorithms.puzzlesandgames; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Represents a Sudoku board with validation and iteration support. | ||
* The board is always a square grid of size n x n, | ||
* where n must be a perfect square (e.g., 4, 9, 16). | ||
*/ | ||
public class SudokuBoard implements Iterable<SudokuBoard.Cell> { | ||
|
||
private final int size; | ||
private final int boxSize; | ||
private final int[][] board; | ||
|
||
/** | ||
* Constructs a SudokuBoard of the given size. | ||
* | ||
* @param size the dimension of the Sudoku board (must be a perfect square) | ||
* @throws IllegalArgumentException if size is not a positive perfect square | ||
*/ | ||
public SudokuBoard(int size) { | ||
if (size <= 0 || Math.sqrt(size) % 1 != 0) { | ||
throw new IllegalArgumentException("Size must be a perfect square (e.g., 4, 9, 16)"); | ||
} | ||
this.size = size; | ||
this.boxSize = (int) Math.sqrt(size); | ||
this.board = new int[size][size]; | ||
} | ||
|
||
/** | ||
* Returns the size of the board. | ||
* | ||
* @return the board size | ||
*/ | ||
public int getSize() { | ||
return size; | ||
} | ||
|
||
/** | ||
* Returns the box (subgrid) size. | ||
* | ||
* @return the size of a subgrid | ||
*/ | ||
public int getBoxSize() { | ||
return boxSize; | ||
} | ||
|
||
/** | ||
* Gets the value at the given cell. | ||
* | ||
* @param row the row index | ||
* @param col the column index | ||
* @return the value at the specified cell | ||
* @throws IndexOutOfBoundsException if indices are invalid | ||
*/ | ||
public int get(int row, int col) { | ||
validateCell(row, col); | ||
return board[row][col]; | ||
} | ||
|
||
/** | ||
* Sets the value at the given cell. | ||
* | ||
* @param row the row index | ||
* @param col the column index | ||
* @param value the value to set (0 means empty) | ||
* @throws IndexOutOfBoundsException if indices are invalid | ||
* @throws IllegalArgumentException if value is out of range | ||
*/ | ||
public void set(int row, int col, int value) { | ||
validateCell(row, col); | ||
if (value < 0 || value > size) { | ||
throw new IllegalArgumentException("Value must be between 0 and " + size); | ||
} | ||
board[row][col] = value; | ||
} | ||
|
||
/** | ||
* Checks whether placing a value at the given cell is valid | ||
* according to Sudoku rules. | ||
* | ||
* @param row the row index | ||
* @param col the column index | ||
* @param value the value to check | ||
* @return true if placement is valid, false otherwise | ||
*/ | ||
public boolean isValid(int row, int col, int value) { | ||
validateCell(row, col); | ||
if (value <= 0 || value > size) { | ||
return false; | ||
} | ||
|
||
// check row | ||
for (int c = 0; c < size; c++) { | ||
if (board[row][c] == value) { | ||
return false; | ||
} | ||
} | ||
|
||
// check column | ||
for (int r = 0; r < size; r++) { | ||
if (board[r][col] == value) { | ||
return false; | ||
} | ||
} | ||
|
||
// check box | ||
int boxRowStart = (row / boxSize) * boxSize; | ||
int boxColStart = (col / boxSize) * boxSize; | ||
|
||
for (int r = 0; r < boxSize; r++) { | ||
for (int c = 0; c < boxSize; c++) { | ||
if (board[boxRowStart + r][boxColStart + c] == value) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Ensures that the given cell indices are valid. | ||
* | ||
* @param row the row index | ||
* @param col the column index | ||
* @throws IndexOutOfBoundsException if indices are outside the board | ||
*/ | ||
private void validateCell(int row, int col) { | ||
if (row < 0 || row >= size || col < 0 || col >= size) { | ||
throw new IndexOutOfBoundsException("Cell position out of bounds"); | ||
} | ||
} | ||
|
||
/** | ||
* Represents a single cell on the Sudoku board. | ||
*/ | ||
public class Cell { | ||
private final int row; | ||
private final int col; | ||
|
||
/** | ||
* Constructs a Cell with the given row and column. | ||
* | ||
* @param row the row index | ||
* @param col the column index | ||
*/ | ||
public Cell(int row, int col) { | ||
this.row = row; | ||
this.col = col; | ||
} | ||
|
||
/** | ||
* Returns the row index of this cell. | ||
* | ||
* @return the row index | ||
*/ | ||
public int getRow() { | ||
return row; | ||
} | ||
|
||
/** | ||
* Returns the column index of this cell. | ||
* | ||
* @return the column index | ||
*/ | ||
public int getCol() { | ||
return col; | ||
} | ||
|
||
/** | ||
* Gets the current value stored in this cell. | ||
* | ||
* @return the cell value | ||
*/ | ||
public int getValue() { | ||
return board[row][col]; | ||
} | ||
|
||
/** | ||
* Sets a value in this cell. | ||
* | ||
* @param value the value to set | ||
*/ | ||
public void setValue(int value) { | ||
SudokuBoard.this.set(row, col, value); | ||
} | ||
} | ||
|
||
/** | ||
* Iterator for traversing all cells in the board. | ||
*/ | ||
private class CellIterator implements Iterator<Cell> { | ||
private int row = 0; | ||
private int col = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return row < size; | ||
} | ||
|
||
@Override | ||
public Cell next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
Cell cell = new Cell(row, col); | ||
col++; | ||
if (col == size) { | ||
col = 0; | ||
row++; | ||
} | ||
return cell; | ||
} | ||
} | ||
|
||
@Override | ||
public Iterator<Cell> iterator() { | ||
return new CellIterator(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.