2019-11-30 13:55:22 +01:00
|
|
|
"""This module contains the Square class."""
|
|
|
|
|
|
|
|
|
|
|
|
class Square:
|
|
|
|
"""A Square object represents a square on the board.
|
|
|
|
|
2019-11-30 17:04:43 +01:00
|
|
|
The square's values will consist of a set of possible values for that
|
2019-11-30 13:55:22 +01:00
|
|
|
square.
|
2019-11-30 17:04:43 +01:00
|
|
|
The grid_id attribute identifies in which 9x9 grid the square belongs to.
|
2019-11-30 13:55:22 +01:00
|
|
|
"""
|
|
|
|
|
2019-11-30 17:04:43 +01:00
|
|
|
def __init__(self, line, column, values):
|
2019-11-30 13:55:22 +01:00
|
|
|
self.line = line
|
|
|
|
self.column = column
|
2019-11-30 17:04:43 +01:00
|
|
|
self.grid_id = self.compute_grid()
|
2019-11-30 13:55:22 +01:00
|
|
|
self.coordinates = (self.line, self.column)
|
2019-11-30 17:04:43 +01:00
|
|
|
self.values = values
|
2019-11-30 13:55:22 +01:00
|
|
|
|
|
|
|
def compute_grid(self):
|
|
|
|
"""Given the coordinates, determine which grid the Square belongs to.
|
|
|
|
|
|
|
|
The grids are numbered from 0 to 8, in the following configuration:
|
|
|
|
0 1 2
|
|
|
|
3 4 5
|
|
|
|
6 7 8
|
|
|
|
"""
|
2019-11-30 15:21:33 +01:00
|
|
|
|
|
|
|
grids = [
|
|
|
|
(0, 0), # grid 0
|
|
|
|
(0, 1), # grid 1
|
|
|
|
(0, 2), # grid 2
|
|
|
|
(1, 0), # grid 3
|
|
|
|
(1, 1), # grid 4
|
|
|
|
(1, 2), # grid 5
|
|
|
|
(2, 0), # grid 6
|
|
|
|
(2, 1), # grid 7
|
|
|
|
(2, 2), # grid 8
|
|
|
|
]
|
|
|
|
|
|
|
|
grid_coordinates = (self.line // 3, self.column // 3)
|
|
|
|
|
|
|
|
grid_number = 0
|
|
|
|
while grids[grid_number] != grid_coordinates:
|
|
|
|
grid_number += 1
|
|
|
|
|
|
|
|
if grid_number == 9:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return grid_number
|