|
Sudoku Showcase
|
Represents the Sudoku game logic and acts as a sub-controller within the application's Model-View-Controller architecture. More...
Public Member Functions | |
| def | __init__ (self, bool verbose=True) |
| When the App starts or user creates a new Game Mistakes will be set to zero, time to zero, and grids will be cleared. More... | |
| str | difficulty (self) |
| Properties. More... | |
| int | mistakes (self) |
| (readonly property) The mistakes made in the game NOTE: A mistake is a violation of the sudoku rules. More... | |
| None | startNewGame (self, str difficulty="") |
| Entry Point to start a new Game with described Difficulty. More... | |
| None | restart (self) |
| Restarts the game. More... | |
| Field|None | getField (self, int row, int col) |
| getter More... | |
| list[Field]|list | getFields (self) |
| Returns a list of all Fields. More... | |
| dict | getDigitCount (self) |
| Returns the count of digits in the grid as dict NOTE: If current Grid is not set, an empty dict is returned. More... | |
| list[Field] | getFieldsCausingMistake (self, int row, int col, int value) |
| Returns the Fields causing a mistake. More... | |
| str | getPuzzle (self) |
| Returns the str-repr of the current puzzle. More... | |
| bool | setValue (self, int row, int col, int value) |
| Make Moves. More... | |
| bool | clearValue (self, int row, int col) |
| Clears a value (and Notes) from the grid. More... | |
| bool | addNote (self, int row, int col, int value) |
| Adds a Note to a Field. More... | |
| None | clearNotes (self, int row, int col) |
| Removes all Notes from a Field. More... | |
| None | autoNotes (self) |
| Adds Notes in the puzzle automatically. More... | |
| None | clearAllNotes (self) |
| Removes all the notes. More... | |
| int | getElapsedTime (self) |
| Gamification. More... | |
| bool | isGameOver (self) |
| Wether the game is over due to mistakes. More... | |
| bool | isWon (self) |
| The game can be won or its not. More... | |
| bool | hasEnded (self) |
| If game has ended. More... | |
Static Public Attributes | |
| dictionary | DIFFICULTY = {'Nearly Full': 25, 'Easy': 40, 'Medium': 48, 'Hard': 51, 'Extreme': 55} |
| string | DEFAULT_DIFFICULTY = 'Easy' |
| int | MAX_MISTAKES = 3 |
Represents the Sudoku game logic and acts as a sub-controller within the application's Model-View-Controller architecture.
This class manages multiple puzzle states:
The initial puzzle is generated with a unique solution. Non-empty fields from the initial puzzle are treated as fixed and cannot be edited. The current puzzle derives from the initial state and stores all user input.
The Game is responsible for validating moves, tracking mistakes, and handling overall gameplay state. Invalid moves violating Sudoku rules count as mistakes, while incorrect but rule-compliant digits do not. To prevent brute forcing, the game ends once MAX_MISTAKES is reached.
Most low-level puzzle operations, such as row or column access, are implemented in Puzzle.
The puzzle generation algorithm prioritizes readability and maintainability over heavy optimization, which fits the educational showcase nature of this project. Despite this, puzzle and solution generation remains fast, even for extreme difficulty level (around 0.5 seconds). The time consumed generating the puzzle can be printed on console with verbose.
| def sudoku.game.Game.__init__ | ( | self, | |
| bool | verbose = True |
||
| ) |
When the App starts or user creates a new Game Mistakes will be set to zero, time to zero, and grids will be cleared.
| bool sudoku.game.Game.addNote | ( | self, | |
| int | row, | ||
| int | col, | ||
| int | value | ||
| ) |
Adds a Note to a Field.
| row | int - the row of the field |
| col | int - the column of the field |
| value | int - the value to add as note |
| None sudoku.game.Game.autoNotes | ( | self | ) |
Adds Notes in the puzzle automatically.
| None sudoku.game.Game.clearAllNotes | ( | self | ) |
Removes all the notes.
This reverses self.autoNotes()
| None sudoku.game.Game.clearNotes | ( | self, | |
| int | row, | ||
| int | col | ||
| ) |
Removes all Notes from a Field.
| row | int - the row of the field |
| col | int - the column of the field |
| bool sudoku.game.Game.clearValue | ( | self, | |
| int | row, | ||
| int | col | ||
| ) |
Clears a value (and Notes) from the grid.
@param row: int - the row of the field @param col: int - the column of the field @return bool
| str sudoku.game.Game.difficulty | ( | self | ) |
Properties.
(readonly property) The difficulty level of a game
| dict sudoku.game.Game.getDigitCount | ( | self | ) |
Returns the count of digits in the grid as dict NOTE: If current Grid is not set, an empty dict is returned.
| int sudoku.game.Game.getElapsedTime | ( | self | ) |
Gamification.
Returns the elapsed time in seconds
| Field | None sudoku.game.Game.getField | ( | self, | |
| int | row, | ||
| int | col | ||
| ) |
getter
Returns a Field from the current Game NOTE: If the current game doesnt exist, return None.
| row | int - the row of the Field in question |
| col | int - the column of the Field in question |
| list[Field] | list sudoku.game.Game.getFields | ( | self | ) |
Returns a list of all Fields.
NOTE: If current Grid is not set, an empty list is returned.
| list[Field] sudoku.game.Game.getFieldsCausingMistake | ( | self, | |
| int | row, | ||
| int | col, | ||
| int | value | ||
| ) |
Returns the Fields causing a mistake.
NOTE: If this theoretical move does not result in a mistake, the returned list will be empty.
| row | int |
| col | int |
| value | int |
| str sudoku.game.Game.getPuzzle | ( | self | ) |
Returns the str-repr of the current puzzle.
| bool sudoku.game.Game.hasEnded | ( | self | ) |
If game has ended.
A game has ended, when its either won or gameOver.
| bool sudoku.game.Game.isGameOver | ( | self | ) |
Wether the game is over due to mistakes.
| bool sudoku.game.Game.isWon | ( | self | ) |
The game can be won or its not.
| int sudoku.game.Game.mistakes | ( | self | ) |
(readonly property) The mistakes made in the game NOTE: A mistake is a violation of the sudoku rules.
| None sudoku.game.Game.restart | ( | self | ) |
Restarts the game.
| bool sudoku.game.Game.setValue | ( | self, | |
| int | row, | ||
| int | col, | ||
| int | value | ||
| ) |
Make Moves.
Sets a Value and handles violated rules. Returns False if rule is violated, else True. NOTE: A valid move is not necessarily a correct solution move!!
| row | int - the row of the field |
| col | int - the column of the field |
| value | int - the value to set |
| None sudoku.game.Game.startNewGame | ( | self, | |
| str | difficulty = "" |
||
| ) |
Entry Point to start a new Game with described Difficulty.
The game attributes will be reset and the solution and initial grid are calculated. NOTE: Does not create a new Game-Object.
| difficulty | str - on empty, the default is used |
| KeyError | - when difficulty is unknown |
|
static |
|
static |
|
static |