[gnome-sudoku/qqwing] Create SudokuGenerator class



commit fb2f72c4f33d20e38144d0dcca673eef406d26fe
Author: Parin Porecha <parinporecha gmail com>
Date:   Wed Aug 6 13:46:57 2014 +0200

    Create SudokuGenerator class

 lib/sudoku-generator.vala |   44 +++++++++++++++++++++++++++++++++-----------
 src/gnome-sudoku.vala     |   20 +++++++++++++++++++-
 2 files changed, 52 insertions(+), 12 deletions(-)
---
diff --git a/lib/sudoku-generator.vala b/lib/sudoku-generator.vala
index e9d9f68..10f5778 100644
--- a/lib/sudoku-generator.vala
+++ b/lib/sudoku-generator.vala
@@ -1,26 +1,48 @@
 /* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
-public void generate_puzzle()
+public class SudokuGenerator : Object
 {
-    stdout.printf ("Testing qqwing, generating 4 puzzles of different difficulties ...\n\n");
+    public SudokuGenerator () {
+    }
 
-    int number_to_generate = 4;
+    public SudokuBoard generate (DifficultyCategory category)
+    {
+        stdout.printf ("Generating a puzzle of difficulty %s ...\n\n", category.to_string ());
 
-    // 1 corresponds to SIMPLE difficulty
-    // 2 corresponds to EASY difficulty
-    // 3 corresponds to INTERMEDIATE difficulty
-    // 4 corresponds to EXPERT difficulty
-    int difficulty = 1;
+        var board = new SudokuBoard ();
+        int[] puzzle = QQwing.generate_puzzle ((int) category + 1);
+        stdout.printf ("puzzle length = %d\n", puzzle.length);
 
-    for (var i = 0; i < number_to_generate; i++)
-    {
-        int[] puzzle = QQwing.generate_puzzle (difficulty++);
+//        assert (puzzle.length >= board.rows * board.cols);
 
         stdout.printf ("\n");
         for (var j = 0; j < 81; j++)
             stdout.printf ("%d", puzzle[j]);
         stdout.printf ("\n");
 
+        for (var row = 0; row < board.rows; row++)
+        {
+            for (var col = 0; col < board.cols; col++)
+            {
+                var val = puzzle[(row * board.cols) + col];
+                if (val != 0)
+                    board.insert (row, col, val, true);
+            }
+        }
+        board.difficulty_rating = 0;
+
+        return board;
+    }
+
+    public void print_stats (SudokuBoard board)
+    {
+        var cells = board.get_cells ();
+        var puzzle = new int[board.rows * board.cols];
+
+        for (var row = 0; row < board.rows; row++)
+            for (var col = 0; col < board.cols; col++)
+                puzzle[(row * board.cols) + col] = cells[row, col];
+
         QQwing.print_stats (puzzle);
     }
 }
diff --git a/src/gnome-sudoku.vala b/src/gnome-sudoku.vala
index 6c53004..a676b5e 100644
--- a/src/gnome-sudoku.vala
+++ b/src/gnome-sudoku.vala
@@ -24,6 +24,7 @@ public class Sudoku : Gtk.Application
     private Button back_button;
 
     private SudokuStore sudoku_store;
+    private SudokuGenerator generator;
     private SudokuSaver saver;
 
     private SimpleAction undo_action;
@@ -141,6 +142,7 @@ public class Sudoku : Gtk.Application
         print_action = (SimpleAction) lookup_action ("print");
 
         sudoku_store = new SudokuStore ();
+        generator = new SudokuGenerator ();
         saver = new SudokuSaver ();
 
         var savegame = saver.get_savedgame ();
@@ -164,7 +166,23 @@ public class Sudoku : Gtk.Application
 
     private void start_game (SudokuBoard board)
     {
-        generate_puzzle ();
+        var gen_board = generator.generate (board.get_difficulty_category ());
+        var gen_cells = gen_board.get_cells ();
+
+        for (var i = 0; i < board.rows; i++)
+        {
+            for (var j = 0; j < board.cols; j++)
+            {
+                if (gen_cells[i, j] == 0)
+                    stdout.printf (". ");
+                else
+                    stdout.printf ("%d ", gen_cells[i, j]);
+            }
+            stdout.printf ("\n");
+        }
+
+        generator.print_stats (gen_board);
+
         var difficulty_category = board.get_difficulty_category ();
         undo_action.set_enabled (false);
         redo_action.set_enabled (false);


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