[gnome-games/sudoku-tube] Extract methods to make set_value shorter
- From: Zhang Sen <zhangsen src gnome org>
- To: svn-commits-list gnome org
- Subject: [gnome-games/sudoku-tube] Extract methods to make set_value shorter
- Date: Tue, 14 Jul 2009 01:25:57 +0000 (UTC)
commit 7a1dbf46c2f21958800ba6f6e12e9aa1750fb28c
Author: Zhang Sen <zh jesse gmail com>
Date: Tue Jul 14 09:05:45 2009 +0800
Extract methods to make set_value shorter
gnome-sudoku/src/lib/gsudoku.py | 60 ++++++++++++++++++++++++--------------
1 files changed, 38 insertions(+), 22 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/gsudoku.py b/gnome-sudoku/src/lib/gsudoku.py
index 584fb64..d8fb506 100644
--- a/gnome-sudoku/src/lib/gsudoku.py
+++ b/gnome-sudoku/src/lib/gsudoku.py
@@ -493,32 +493,28 @@ class SudokuModel:
def set_value(self, x, y, value):
result = []
- # first remove conflicts; grid.add should return conflicts together
- # with ConflictError
- old_conflicts = self._remove_related_conflicts(x, y)
+ old_conflicts = self._remove_old_conficts(x, y)
new_conflicts = []
- for entry in old_conflicts:
- change = NumberBoxModel(x=entry[0], y=entry[1], conflict=False)
- result.append(change)
- if not value: # should use unified way to update the grid
- self.grid.remove(x, y)
+ # Some value won't go into the grid, e.g. conflicts values?
+ # This makes things complicated.
+ if not value:
+ if self.grid._get_(x, y):
+ self.grid.remove(x, y)
else:
try:
- self.grid.add(x, y, value) # force=True???
+ self.grid.add(x, y, value, force=True)
except sudoku.ConflictError, err:
- new_conflicts = self.grid.find_conflicts(x, y, value)
- has_conflict = True if new_conflicts else False
- result.append(NumberBoxModel(x, y, value, conflict=has_conflict))
- if new_conflicts:
- self._record_conflicts(x, y, new_conflicts)
- for entry in new_conflicts:
- change = NumberBoxModel(x=entry[0], y=entry[1], conflict=True)
- result.append(change)
- self._signal_observers(result)
+ new_conflicts = self._find_new_conflicts(x, y, value)
- if self.grid.check_for_completeness():
- self._signal_completeness()
+ flag = True if new_conflicts else False
+ result.append(NumberBoxModel(x, y, value, conflict=flag))
+ result.extend(old_conflicts)
+ result.extend(new_conflicts)
+
+ print self.grid
+ self._signal_observers(result)
+ self._check_for_completeness()
def get_value(self, x, y):
return self.grid._get_(x, y)
@@ -529,6 +525,25 @@ class SudokuModel:
top_note=top_note, bottom_note=bottom_note)
self._signal_observers([change])
+ def _remove_old_conficts(self, x, y):
+ result = []
+ old_conflicts = self._remove_related_conflicts(x, y)
+ for entry in old_conflicts:
+ change = NumberBoxModel(x=entry[0], y=entry[1], conflict=False)
+ result.append(change)
+ return result
+
+ def _find_new_conflicts(self, x, y, value):
+ result = []
+ new_conflicts = self.grid.find_conflicts(x, y, value)
+ print new_conflicts
+ if new_conflicts:
+ self.__error_pairs__[(x, y)] = new_conflicts
+ for entry in new_conflicts:
+ change = NumberBoxModel(x=entry[0], y=entry[1], conflict=True)
+ result.append(change)
+ return result
+
def _remove_related_conflicts(self, x, y):
result = []
if self.__error_pairs__.has_key((x, y)):
@@ -540,8 +555,9 @@ class SudokuModel:
result.append(coord)
return result
- def _record_conflicts(self, x, y, new_conflicts):
- self.__error_pairs__[(x, y)] = new_conflicts
+ def _check_for_completeness(self):
+ if self.grid.check_for_completeness():
+ self._signal_completeness()
def remove (self, x, y, do_removal = False):
"""Remove x, y from our visible grid.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]