[gnome-games/sudoku-tube] Extract the methods to calc various coordinates
- From: Zhang Sen <zhangsen src gnome org>
- To: svn-commits-list gnome org
- Subject: [gnome-games/sudoku-tube] Extract the methods to calc various coordinates
- Date: Thu, 23 Jul 2009 10:09:16 +0000 (UTC)
commit 9f66ca06383a840dbf53e1b3104a7a6e848f7871
Author: Zhang Sen <zh jesse gmail com>
Date: Thu Jul 23 17:28:51 2009 +0800
Extract the methods to calc various coordinates
And a little fix on networking.py
gnome-sudoku/src/lib/networking.py | 2 +-
gnome-sudoku/src/lib/sudoku.py | 78 +++++++++++++++++++++++++-----------
2 files changed, 56 insertions(+), 24 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/networking.py b/gnome-sudoku/src/lib/networking.py
index a6fb882..c45ba4c 100644
--- a/gnome-sudoku/src/lib/networking.py
+++ b/gnome-sudoku/src/lib/networking.py
@@ -31,7 +31,7 @@ class RemoteViewProxy(dbus.service.Object):
@dbus.service.method(dbus_interface=view_interface,
in_signature="", out_signature="")
def puzzle_finished_cb(self):
- self.puzzle_finished_cb()
+ print "the other user has finished"
class LocalViewProxy:
diff --git a/gnome-sudoku/src/lib/sudoku.py b/gnome-sudoku/src/lib/sudoku.py
index cbbcc39..8ce7b6e 100644
--- a/gnome-sudoku/src/lib/sudoku.py
+++ b/gnome-sudoku/src/lib/sudoku.py
@@ -60,6 +60,58 @@ class ConflictError (ValueError):
class AlreadySetError (ValueError):
pass
+def _calc_row_coords(group_size):
+ """A map from i => [all coordinates with x==i]
+
+ 0: [(0, 0), (0, 1), (0, 2), (0, 3)]
+ 1: [(1, 0), (1, 1), (1, 2), (1, 3)]
+ """
+ row_coords = {}
+ for n, row in enumerate([[(x, y) for x in range(group_size)]
+ for y in range(group_size)]):
+ row_coords[n] = row
+ return row_coords
+
+def _calc_col_coords(group_size):
+ """A map from i => [all coordinates with y==i]
+
+ 0: [(0, 0), (1, 0), (2, 0), (3, 0)]
+ 1: [(0, 1), (1, 1), (2, 1), (3, 1)]
+ """
+ col_coords = {}
+ for n, col in enumerate([[(x, y) for y in range(group_size)]
+ for x in range(group_size)]):
+ col_coords[n] = col
+ return col_coords
+
+def _calc_box_coords(group_size):
+ """group_size should be a square number.
+
+ box_coords: A map from i => [all coordinates in box i]
+
+ 0: [(0, 0), (0, 1), (0, 2),
+ (1, 0), (1, 1), (1, 2),
+ (2, 0), (2, 1), (2, 2)]
+
+ box_by_coords: reverse of box_coords
+ """
+ box_coords = {}
+ box_by_coords = {}
+ width = int(math.sqrt(group_size))
+ box_coordinates = [[n * width,
+ (n + 1) * width] for n in range(width)]
+ box_num = 0
+ for xx in box_coordinates:
+ for yy in box_coordinates:
+ box_coords[box_num] = []
+ for x in range(*xx):
+ for y in range(*yy):
+ box_coords[box_num].append((x, y))
+ box_by_coords[(x, y)] = box_num
+ box_num += 1
+
+ return box_coords, box_by_coords
+
class SudokuGrid:
def __init__ (self, grid = False, verbose = False, group_size = 9):
self.grid = []
@@ -74,15 +126,9 @@ class SudokuGrid:
self.rows.append(set())
self.boxes.append(set())
self.grid.append([0] * self.group_size)
- self.box_by_coords = {}
- self.box_coords = {}
- self.calculate_box_coords() # sets box_coords and box_by_coords
- self.row_coords = {}
- for n, row in enumerate([[(x, y) for x in range(self.group_size)] for y in range(self.group_size)]):
- self.row_coords[n] = row
- self.col_coords = {}
- for n, col in enumerate([[(x, y) for y in range(self.group_size)] for x in range(self.group_size)]):
- self.col_coords[n] = col
+ self.box_coords, self.box_by_coords = _calc_box_coords(group_size)
+ self.row_coords = _calc_row_coords(group_size)
+ self.col_coords = _calc_col_coords(group_size)
if grid:
if type(grid) == str:
g = re.split("\s+", grid)
@@ -94,20 +140,6 @@ class SudokuGrid:
self.populate_from_grid(grid)
self.verbose = verbose
- def calculate_box_coords (self):
- width = int(math.sqrt(self.group_size))
- box_coordinates = [[n * width,
- (n + 1) * width] for n in range(width)]
- box_num = 0
- for xx in box_coordinates:
- for yy in box_coordinates:
- self.box_coords[box_num] = []
- for x in range(*xx):
- for y in range(*yy):
- self.box_by_coords[(x, y)] = box_num
- self.box_coords[box_num].append((x, y))
- box_num += 1
-
def add (self, x, y, val, force = False):
if not val:
pass
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]