Package sudoku :: Module chart :: Class Chart
[hide private]
[frames] | no frames]

Class Chart

object --+
         |
        Chart

Sudoku is a popular puzzle game of Japanese origin. It involves placing integer numbers in a chart according to a certain set of rules. The class Chart represents Sudoku game charts. Each chart consists of a number of cells. In addition to being arranged in rows and columns, the cells form equally-sized and -shaped groups called "regions". A typical Sudoku chart consists of 9 rows or nine columns each, arranged in 9 regions of 3 by 3 cells each. A simple diagram of such a Sudoku chart is shown below (# denotes a cell, lines mark region borders).:

   ###|###|###
   ###|###|###
   ###|###|###
   ---+---+---       
   ###|###|###
   ###|###|###
   ###|###|###
   ---+---+---       
   ###|###|###
   ###|###|###
   ###|###|###

Below, the term "chart size" is used to mean the width and height (which are equal) of a Sudoku chart. For instance, the chart size of the above chart is 9.

Placing numbers

During a game of Sudoku, the player places numbers in the cells forming the chart. A few simple rules govern the placement of numbers:

The player must always follow these rules if they wish to form a valid solution to a Sudoku puzzle. Below, the diagram on the left shows a partial but valid Sudoku chart (# denotes an empty cell where no number has been placed yet). The diagram on the right is invalid in several ways (note the conflicting numbers marked in bold).:

   14#|86#|#23        ###|5##|9##
   3#5|#42|#71        6##|2##|48#
   #29|1#3|#85        #9#|#1#|B57#
   ---+---+---        ---+---+---
   2#8|#1#|3##        #B4#|6B4#|127
   6#4|53#|21#        #28|#7#|##B3
   713|2##|96#        #57|9##|B3##
   ---+---+---        ---+---+---
   #32|#54|1#6        ##3|8##|6##
   #8#|321|4##        #6#|##9|B548
   5#1|7#9|#32        ##4|##5|##9

Please see e.g. Wikipedia for a more detailed description of Sudoku charts and the rules of the game! Note that the Wikipedia page also contains some additional information on some more complex Sudoku variants which are not relevant for this class.

Chart and region sizes

This class supports charts of different sizes and regions of different shapes, given the following simplifying assumptions:

Each chart's dimensions can be defined in terms of its region width and region height. The chart size (i.e., width * height of the whole square) is the product of region width and region height. For instance:

The class Chart supports placing numbers onto charts and is capable of determining whether such placements violate the basic Sudoku rules of "only one per row/column/region". The class does not support canceling number placements. Once a cell is filled, there's no way to go back.

Instance Methods [hide private]
 
__init__(self, region_width, region_height)
Creates a new chart whose dimensions are determined based on the given region width and region height.
int
get_size(self)
Returns the size of the chart.
int
get_region_width(self)
Returns the region width of the chart, i.e., the number of cells that each region has on a single row.
int
get_region_height(self)
Returns the region height of the chart, i.e., the number of cells that each region has in a single column.
boolean
is_empty(self, row, column)
Determines if the cell at the given position of the chart is empty.
int
get_number(self, row, column)
Returns the number in the given position of the chart, if there is one.
boolean
set_number(self, row, column, number)
Places a given number in the indicated position in the chart.
 
update_row_options(self, row, number)
Removes the given number as a viable number to place in any cells of the given row (because the number already exists somewhere in the row).
 
update_column_options(self, column, number)
Removes the given number as a viable number to place in any cells in the given column (because the number already exists somewhere in the column).
 
update_region_options(self, row, column, number)
Removes the given number as a viable number to place in any cells in the same region as the cell indicated by the parameters (because the number already exists somewhere in the region).

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, region_width, region_height)
(Constructor)

 

Creates a new chart whose dimensions are determined based on the given region width and region height. The new chart has no numbers in it yet.

Parameters:
  • region_width (int) - the height, in cells, of each region in the chart
  • region_height (int) - the width, in cells, of each region in the chart
Overrides: object.__init__

get_size(self)

 

Returns the size of the chart. The chart size is the width (and height, since charts are square) of the chart.

Returns: int
chart size

get_region_width(self)

 

Returns the region width of the chart, i.e., the number of cells that each region has on a single row.

Returns: int
region width

get_region_height(self)

 

Returns the region height of the chart, i.e., the number of cells that each region has in a single column.

Returns: int
region height

is_empty(self, row, column)

 

Determines if the cell at the given position of the chart is empty. A cell is empty if no number has been placed in it yet.

Parameters:
  • row (int) - a row index (between 0 and chart size-1)
  • column (int) - a column index (between 0 and chart size-1)
Returns: boolean
a boolean value indicating if the indicated cell is empty

get_number(self, row, column)

 

Returns the number in the given position of the chart, if there is one.

Parameters:
  • row (int) - a row index (between 0 and chart size-1)
  • column (int) - a column index (between 0 and chart size-1)
Returns: int
the number at the indicated cell or a non-positive value if the cell is empty

set_number(self, row, column, number)

 

Places a given number in the indicated position in the chart. This can only be done if the indicated cell is empty and if the placement would not cause the number to appear more than once in any row, column, or region.

Parameters:
  • row (int) - a row index (between 0 and chart size-1)
  • column (int) - a column index (between 0 and chart size-1)
  • number (int) - the number to place in the indicated cell (assumed to be between 1 and chart size)
Returns: boolean
a boolean value indicating if the operation was successful

update_row_options(self, row, number)

 

Removes the given number as a viable number to place in any cells of the given row (because the number already exists somewhere in the row).

Parameters:
  • row (int) - a row index (between 0 and chart size-1)
  • number (int) - a number to mark as already existing on the row

update_column_options(self, column, number)

 

Removes the given number as a viable number to place in any cells in the given column (because the number already exists somewhere in the column).

Parameters:
  • column (int) - a column index (between 0 and chart size-1)
  • number (int) - a number to mark as already existing in the column

update_region_options(self, row, column, number)

 

Removes the given number as a viable number to place in any cells in the same region as the cell indicated by the parameters (because the number already exists somewhere in the region).

Parameters:
  • row (int) - a row index (between 0 and chart size-1)
  • column (int) - a column index (between 0 and chart size-1)
  • number (int) - a number to mark as already existing in the region