Sudoku Showcase
Public Member Functions | Static Public Attributes | List of all members
sudoku.game.Game Class Reference

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
 

Detailed Description

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.

See also
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.

Constructor & Destructor Documentation

◆ __init__()

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.

Member Function Documentation

◆ addNote()

bool sudoku.game.Game.addNote (   self,
int  row,
int  col,
int  value 
)

Adds a Note to a Field.

Parameters
rowint - the row of the field
colint - the column of the field
valueint - the value to add as note
Returns
bool

◆ autoNotes()

None sudoku.game.Game.autoNotes (   self)

Adds Notes in the puzzle automatically.

Returns
None

◆ clearAllNotes()

None sudoku.game.Game.clearAllNotes (   self)

Removes all the notes.

This reverses self.autoNotes()

Returns
None

◆ clearNotes()

None sudoku.game.Game.clearNotes (   self,
int  row,
int  col 
)

Removes all Notes from a Field.

Parameters
rowint - the row of the field
colint - the column of the field
Returns
None

◆ clearValue()

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 

◆ difficulty()

str sudoku.game.Game.difficulty (   self)

Properties.

(readonly property) The difficulty level of a game

Returns
str

◆ getDigitCount()

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.

Returns
dict

◆ getElapsedTime()

int sudoku.game.Game.getElapsedTime (   self)

Gamification.

Returns the elapsed time in seconds

Returns
int

◆ getField()

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.

Parameters
rowint - the row of the Field in question
colint - the column of the Field in question
Returns
Field | None

◆ getFields()

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.

Returns
list[Field] | list

◆ getFieldsCausingMistake()

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.

Parameters
rowint
colint
valueint
Returns
list[Field]

◆ getPuzzle()

str sudoku.game.Game.getPuzzle (   self)

Returns the str-repr of the current puzzle.

Returns
str

◆ hasEnded()

bool sudoku.game.Game.hasEnded (   self)

If game has ended.

A game has ended, when its either won or gameOver.

Returns
bool

◆ isGameOver()

bool sudoku.game.Game.isGameOver (   self)

Wether the game is over due to mistakes.

Returns
bool

◆ isWon()

bool sudoku.game.Game.isWon (   self)

The game can be won or its not.

Returns
bool

◆ mistakes()

int sudoku.game.Game.mistakes (   self)

(readonly property) The mistakes made in the game NOTE: A mistake is a violation of the sudoku rules.

Returns
int

◆ restart()

None sudoku.game.Game.restart (   self)

Restarts the game.

Returns
None

◆ setValue()

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!!

Parameters
rowint - the row of the field
colint - the column of the field
valueint - the value to set
Returns
bool - if rules violated or not

◆ startNewGame()

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.

Parameters
difficultystr - on empty, the default is used
Exceptions
KeyError- when difficulty is unknown
Returns
None

Member Data Documentation

◆ DEFAULT_DIFFICULTY

string sudoku.game.Game.DEFAULT_DIFFICULTY = 'Easy'
static

◆ DIFFICULTY

dictionary sudoku.game.Game.DIFFICULTY = {'Nearly Full': 25, 'Easy': 40, 'Medium': 48, 'Hard': 51, 'Extreme': 55}
static

◆ MAX_MISTAKES

int sudoku.game.Game.MAX_MISTAKES = 3
static

The documentation for this class was generated from the following file: