Commits

Josh VanderLinden committed 99e958c

Rearranged some methods. Added a new decorator.

Comments (0)

Files changed (1)

sudokulib/sudoku.py

             raise ValueError('Invalid values.  Please specify a list of %i values.' % self.side_length)
     return wrapped
 
+def handle_negative(func):
+    """Handles negative values for get_row and get_col"""
+
+    def wrapped(self, num):
+        if num < 0:
+            num = self.side_length + num
+        return func(self, num)
+    return wrapped
+
 # ------------------- Sudoku --------------------
 
 class Sudoku(object):
 
         self.clear()
 
+    def clear(self):
+        """Cleans up the Sudoku solution"""
+
+        self.solution = [None for i in range(self.square)]
+
+    def __row_col_to_index(self, row, col):
+        """Translates a (row, col) into an index in our 1-dimensional list"""
+
+        return (row * self.side_length) + col
+
+    def __index_to_row_col(self, index):
+        """Translates an index in our 1-dimensional list to a (row, col)"""
+
+        return divmod(index, self.side_length)
+
     def set_grid_size(self, grid_size):
         self.grid_size = grid_size
 
     def set_difficulty(self, difficulty):
         self.difficulty = difficulty
 
+    @handle_negative
     def get_row(self, row):
         """Returns all values for the specified row"""
 
         end = start + self.side_length
         self.solution[start:end] = values
 
+    @handle_negative
     def get_col(self, col):
         """Returns all values for the specified column"""
 
         row, col = self.__index_to_row_col(index)
         return self.get_region(row, col)
 
-    def clear(self):
-        """Cleans up the Sudoku solution"""
-
-        self.solution = [None for i in range(self.square)]
-
-    def __row_col_to_index(self, row, col):
-        """Translates a (row, col) into an index in our 1-dimensional list"""
-
-        return (row * self.side_length) + col
-
-    def __index_to_row_col(self, index):
-        """Translates an index in our 1-dimensional list to a (row, col)"""
-
-        return divmod(index, self.side_length)
-
     def get_used(self, row, col):
         """Returns a list of all used values for a row, column, and region"""
 
 
         return value not in self.get_used(row, col)
 
+    def is_valid_value_for_index(self, index, value):
+        """Validates a value for the given index"""
+
+        row, col = self.__index_to_row_col(index)
+        return self.is_valid_value(row, col, value)
+
     def fill_square(self, index=0):
         """
         Recursively populates each square on the Sudoku grid until a solution
         return self._masked
 
     def print_solution(self):
-        """
-        Prints the generated solution nicely formatted
-        """
+        """Prints the generated solution nicely formatted"""
 
         print 'Required %i iterations' % self.iterations
         return self.__print_grid(self.solution)
 
     def print_masked(self):
-        """
-        Prints a masked version of the grid
-        """
+        """Prints a masked version of the grid"""
 
         return self.__print_grid(self.masked_grid)
 
     def __print_grid(self, grid):
-        """
-        Prints a nicely formatted version of the Sudoku grid
-        """
+        """Prints a nicely formatted version of the Sudoku grid"""
 
         field_width = len(str(self.side_length)) + 2
         format = ''.join(['%s' for i in range(self.grid_size)])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.