[gnome-games/applygsoc2009: 52/76] Extract the methods to calc various coordinates



commit 106ec1e53f48ab82ca016569c590bccfc15ae951
Author: Pablo Castellano <pablog src gnome org>
Date:   Wed Sep 1 04:16:04 2010 +0200

    Extract the methods to calc various coordinates
    
    XXX: And a little fix on networking.py

 gnome-sudoku/src/lib/sudoku.py |   80 ++++++++++++++++++++++++++++-----------
 1 files changed, 57 insertions(+), 23 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/sudoku.py b/gnome-sudoku/src/lib/sudoku.py
index cb1c5e6..ba57831 100644
--- a/gnome-sudoku/src/lib/sudoku.py
+++ b/gnome-sudoku/src/lib/sudoku.py
@@ -60,6 +60,60 @@ 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 ParallelDict (dict):
     """A handy new sort of dictionary for tracking conflicts.
 
@@ -120,15 +174,9 @@ class SudokuGrid(object):
             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)
@@ -140,20 +188,6 @@ class SudokuGrid(object):
             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]