[gnome-sudoku/qqwing: 1/2] Start merging QQwing wrapper with libsudoku



commit dd775fdf33bc48437a30e9d730bf3edd70cb5431
Author: Parin Porecha <parinporecha gmail com>
Date:   Tue Aug 5 23:00:05 2014 +0200

    Start merging QQwing wrapper with libsudoku

 configure.ac              |    6 ++++
 lib/Makefile.am           |    5 ++-
 lib/QQwing-client.vapi    |    6 ++++
 lib/QQwing-wrapper.cpp    |   71 +++++++++++++++++++++++++++++++++++++++++++++
 lib/QQwing-wrapper.h      |   19 ++++++++++++
 lib/sudoku-generator.vala |   26 ++++++++++++++++
 src/gnome-sudoku.vala     |    1 +
 7 files changed, 133 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 90681e2..1478e35 100644
--- a/configure.ac
+++ b/configure.ac
@@ -22,6 +22,7 @@ dnl ###########################################################################
 
 GLIB_REQUIRED=2.40.0
 GTK_REQUIRED=3.13.0
+QQWING_REQUIRED=1.1.2
 
 PKG_CHECK_MODULES(GNOME_SUDOKU, [
   glib-2.0 >= $GLIB_REQUIRED
@@ -33,6 +34,7 @@ PKG_CHECK_MODULES(GNOME_SUDOKU, [
 PKG_CHECK_MODULES(LIBSUDOKU, [
   glib-2.0 >= $GLIB_REQUIRED
   gio-2.0 >= $GLIB_REQUIRED
+  qqwing >= $QQWING_REQUIRED
   gee-0.8
   json-glib-1.0
 ])
@@ -42,6 +44,10 @@ AC_PATH_PROG([DESKTOP_FILE_VALIDATE], [desktop-file-validate], [/bin/true])
 
 AC_CHECK_LIB([m],[floor])
 
+# Checks for programs.
+AC_PROG_CXX
+AC_PROG_CC
+
 dnl ###########################################################################
 dnl GResources
 dnl ###########################################################################
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 94808e1..fe05bee 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -3,7 +3,10 @@ noinst_LTLIBRARIES = libsudoku.la
 libsudoku_la_SOURCES = \
        sudoku-board.vala \
        sudoku-game.vala \
-       sudoku-saver.vala
+       sudoku-saver.vala       \
+       sudoku-generator.vala   \
+       QQwing-client.vapi      \
+       QQwing-wrapper.cpp
 
 libsudoku_la_CFLAGS = -w
 
diff --git a/lib/QQwing-client.vapi b/lib/QQwing-client.vapi
new file mode 100644
index 0000000..8ae1e8b
--- /dev/null
+++ b/lib/QQwing-client.vapi
@@ -0,0 +1,6 @@
+[CCode (cheader_filename = "QQwing-wrapper.h")]
+namespace QQwing {
+    [CCode (array_length = false)]
+    int[] generate_puzzle (int difficulty);
+    void print_stats ([CCode (array_length = false)] int[] initPuzzle);
+}
diff --git a/lib/QQwing-wrapper.cpp b/lib/QQwing-wrapper.cpp
new file mode 100644
index 0000000..07dfd64
--- /dev/null
+++ b/lib/QQwing-wrapper.cpp
@@ -0,0 +1,71 @@
+/* -*- Mode: CPP; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ *
+ * Copyright © 2014 Parin Porecha
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+
+#include <iostream>
+
+#include "qqwing.hpp"
+#include "QQwing-wrapper.h"
+
+using namespace qqwing;
+
+/*
+ * Generate a symmetric puzzle of specified difficulty.
+ */
+const int* qqwing_generate_puzzle(int difficulty)
+{
+    int i = 0;
+    bool havePuzzle = false;
+    const int *puzzle_array;
+    const int MAX_ITERATION_COUNT = 200;
+    SudokuBoard *board = new SudokuBoard();
+
+    board->setRecordHistory(true);
+    board->setLogHistory(false);
+    board->setPrintStyle(SudokuBoard::ONE_LINE);
+
+    for (i = 0; i < MAX_ITERATION_COUNT; i++)
+    {
+        havePuzzle = board->generatePuzzleSymmetry(SudokuBoard::RANDOM);
+        board->solve();
+        if (havePuzzle && (SudokuBoard::Difficulty) difficulty == board->getDifficulty())
+            break;
+    }
+
+    if (i == MAX_ITERATION_COUNT)
+        g_warning ("Could not generate puzzle of specified difficulty");
+
+    puzzle_array = board->getPuzzle();
+    return puzzle_array;
+}
+
+/*
+ * Print the stats gathered while solving the puzzle given as input.
+ */
+void qqwing_print_stats(int *initPuzzle)
+{
+    SudokuBoard *board = new SudokuBoard();
+    board->setRecordHistory(true);
+    board->setLogHistory(false);
+    board->setPuzzle(initPuzzle);
+    board->solve();
+
+    cout << "Number of Givens: " << board->getGivenCount()  << endl;
+    cout << "Number of Singles: " << board->getSingleCount() << endl;
+    cout << "Number of Hidden Singles: " << board->getHiddenSingleCount()  << endl;
+    cout << "Number of Naked Pairs: " << board->getNakedPairCount()  << endl;
+    cout << "Number of Hidden Pairs: " << board->getHiddenPairCount()  << endl;
+    cout << "Number of Pointing Pairs/Triples: " << board->getPointingPairTripleCount()  << endl;
+    cout << "Number of Box/Line Intersections: " << board->getBoxLineReductionCount()  << endl;
+    cout << "Number of Guesses: " << board->getGuessCount()  << endl;
+    cout << "Number of Backtracks: " << board->getBacktrackCount()  << endl;
+    cout << "Difficulty: " << board->getDifficultyAsString()  << endl;
+}
+
diff --git a/lib/QQwing-wrapper.h b/lib/QQwing-wrapper.h
new file mode 100644
index 0000000..2ef7185
--- /dev/null
+++ b/lib/QQwing-wrapper.h
@@ -0,0 +1,19 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+ *
+ * Copyright © 2014 Parin Porecha
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+const int* qqwing_generate_puzzle(int difficulty);
+void qqwing_print_stats(int *initPuzzle);
+
+G_END_DECLS
diff --git a/lib/sudoku-generator.vala b/lib/sudoku-generator.vala
new file mode 100644
index 0000000..e9d9f68
--- /dev/null
+++ b/lib/sudoku-generator.vala
@@ -0,0 +1,26 @@
+/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+
+public void generate_puzzle()
+{
+    stdout.printf ("Testing qqwing, generating 4 puzzles of different difficulties ...\n\n");
+
+    int number_to_generate = 4;
+
+    // 1 corresponds to SIMPLE difficulty
+    // 2 corresponds to EASY difficulty
+    // 3 corresponds to INTERMEDIATE difficulty
+    // 4 corresponds to EXPERT difficulty
+    int difficulty = 1;
+
+    for (var i = 0; i < number_to_generate; i++)
+    {
+        int[] puzzle = QQwing.generate_puzzle (difficulty++);
+
+        stdout.printf ("\n");
+        for (var j = 0; j < 81; j++)
+            stdout.printf ("%d", puzzle[j]);
+        stdout.printf ("\n");
+
+        QQwing.print_stats (puzzle);
+    }
+}
diff --git a/src/gnome-sudoku.vala b/src/gnome-sudoku.vala
index 5b485ad..6c53004 100644
--- a/src/gnome-sudoku.vala
+++ b/src/gnome-sudoku.vala
@@ -164,6 +164,7 @@ public class Sudoku : Gtk.Application
 
     private void start_game (SudokuBoard board)
     {
+        generate_puzzle ();
         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]