[gnome-games] sudoku: Make hint suggest a square: Bug #616351



commit ee3e6a1d3c185f6912d00d890d5bfa5be82eaec3
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon Jul 12 12:14:19 2010 +1000

    sudoku: Make hint suggest a square: Bug #616351

 gnome-sudoku/src/lib/gsudoku.py    |   53 ++++++++++++++++++++++++++++++-----
 gnome-sudoku/src/lib/number_box.py |   12 +++++++-
 2 files changed, 56 insertions(+), 9 deletions(-)
---
diff --git a/gnome-sudoku/src/lib/gsudoku.py b/gnome-sudoku/src/lib/gsudoku.py
index ba8f1ec..2202352 100644
--- a/gnome-sudoku/src/lib/gsudoku.py
+++ b/gnome-sudoku/src/lib/gsudoku.py
@@ -2,6 +2,7 @@
 import gtk, gobject
 import colors
 import math
+import random
 from simple_debug import simple_debug
 import sudoku
 import number_box
@@ -81,6 +82,7 @@ class SudokuGameDisplay (SudokuNumberGrid, gobject.GObject):
                   show_impossible_implications = False):
         group_size = int(group_size)
         self.hints = 0
+        self.hint_square = None
         self.always_show_hints = False
         self.show_impossible_implications = show_impossible_implications
         self.impossible_hints = 0
@@ -183,15 +185,52 @@ class SudokuGameDisplay (SudokuNumberGrid, gobject.GObject):
                 else:
                     e.set_background_color(self.box_color)
 
+    def animate_hint (self):
+        if self.hint_animate_count % 2 == 0:
+            color = (1.0, 0.0, 0.0)
+        else:
+            color = None
+        self.hint_square.set_border_color(color)
+        self.hint_animate_count += 1
+
+        if self.hint_animate_count == 4:
+            self.hint_square = None
+            return False
+
+        return True;
+
+    def set_hint_square (self, square):
+        if self.hint_square is not None:
+            self.hint_square.set_border_color(None)
+            gobject.source_remove(self.hint_timer)
+            self.hint_timer = None
+
+        if square is None:
+            self.hint_square = None
+        else:
+            self.hint_square = self.__entries__[square]
+            self.hint_animate_count = 0
+            self.animate_hint()
+            self.hint_timer = gobject.timeout_add(150, self.animate_hint)
 
     @simple_debug
     def show_hint (self):
-        if hasattr(self, 'focused'):
-            entry = self.focused
-            if entry.read_only or entry.get_text():
-                pass
-            else:
-                self.show_hint_for_entry(entry, interactive = True)
+        min_options = 10;
+        squares = []
+        for x in xrange(9):
+            for y in xrange(9):
+                if self.grid._get_(x, y) != 0:
+                    continue
+                n_options = len(self.grid.possible_values(x, y))
+                if n_options < min_options:
+                    squares = [(x, y)]
+                    min_options = n_options
+                elif n_options == min_options:
+                    squares.append((x, y))
+
+        if len(squares) != 0:
+            self.set_hint_square(random.choice(squares))
+            self.hints += 1
 
     def show_hint_for_entry (self, entry, interactive = False):
         if interactive:
@@ -206,10 +245,8 @@ class SudokuGameDisplay (SudokuNumberGrid, gobject.GObject):
             txt = ''.join([str(v) for v in vals])
             if txt != entry.get_text():
                 set_method(bottom_text = txt, for_hint = True)
-                self.hints += 1
         elif not entry.get_text():
             if entry.get_text() != 'X':
-                self.hints += 1
                 set_method(bottom_text = 'X', for_hint = True)
         else:
             set_method(bottom_text = "", for_hint = True)
diff --git a/gnome-sudoku/src/lib/number_box.py b/gnome-sudoku/src/lib/number_box.py
index dca23e6..dc38dc3 100644
--- a/gnome-sudoku/src/lib/number_box.py
+++ b/gnome-sudoku/src/lib/number_box.py
@@ -81,6 +81,7 @@ class NumberBox (gtk.Widget):
     highlight_color = None
     shadow_color = None
     custom_background_color = None
+    border_color = None
 
     __gsignals__ = {
         'value-about-to-change':(gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
@@ -577,6 +578,12 @@ class NumberBox (gtk.Widget):
         self.draw_background_color(cr, w, h)
         if self.is_focus():
             self.draw_highlight_box(cr, w, h)
+        if self.border_color is not None:
+            border_width = 3.0
+            cr.set_source_rgb(*self.border_color)
+            cr.rectangle(border_width*0.5, border_width*0.5, w-border_width, h-border_width)
+            cr.set_line_width(border_width)
+            cr.stroke()
         if h < w:
             scale = h/float(BASE_SIZE)
         else:
@@ -586,7 +593,6 @@ class NumberBox (gtk.Widget):
         if self.draw_boxes and self.is_focus():
             self.draw_note_area_highlight_box(cr)
 
-
     def draw_background_color (self, cr, w, h):
         if self.read_only:
             if self.custom_background_color:
@@ -696,6 +702,10 @@ class NumberBox (gtk.Widget):
         self.custom_background_color = color
         self.queue_draw()
 
+    def set_border_color (self, color):
+        self.border_color = color
+        self.queue_draw()
+
     def hide_notes (self):
         pass
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]