[gnome-mahjongg] Added shuffle callback and methods



commit e56e2fa2e437ff075737e485897171d60f809052
Author: Mario Wenzel <maweki gmail com>
Date:   Tue Feb 4 00:08:36 2014 +0100

    Added shuffle callback and methods

 src/game.vala           |   38 ++++++++++++++++++++++++++++++++++++++
 src/gnome-mahjongg.vala |    6 ++++++
 2 files changed, 44 insertions(+), 0 deletions(-)
---
diff --git a/src/game.vala b/src/game.vala
index f403d64..f01bba0 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -36,6 +36,18 @@ private static int compare_tiles (Tile a, Tile b)
     return compare_slots (a.slot, b.slot);
 }
 
+private static bool switch_tiles (Tile a, Tile b)
+{
+    if (a.visible && b.visible)
+    {
+        Slot h = a.slot;
+        a.slot = b.slot;
+        b.slot = h;
+        return true;
+    }
+    return false;
+}
+
 public class Match
 {
     public Tile tile0;
@@ -177,6 +189,32 @@ public class Game
         reset ();
     }
 
+    public void shuffle_remaining (bool redraw = true) {
+        // Fisher Yates Shuffle
+        var n = tiles.length();
+        for (var i = n-1; i > 0; i--) {
+            int j = Random.int_range(0,(int)i+1);
+            // switch internal positions
+            switch_tiles (tiles.nth_data(j), tiles.nth_data(i));
+        }
+        // resort for drawing order
+        tiles.sort(compare_tiles);
+        // reset moves and move numbers
+        move_number = 1;
+        foreach (var tile in tiles)
+            tile.move_number = 0;
+        find_matches ();
+        moved ();
+        if (redraw)
+            redraw_all_tiles ();
+    }
+
+    public void redraw_all_tiles () {
+        foreach (var tile in tiles)
+            if (tile.visible)
+                redraw_tile (tile);
+    }
+
     private bool shuffle (int[] numbers, int depth = 0)
     {
         /* All shuffled */
diff --git a/src/gnome-mahjongg.vala b/src/gnome-mahjongg.vala
index 0964357..4f465a5 100644
--- a/src/gnome-mahjongg.vala
+++ b/src/gnome-mahjongg.vala
@@ -495,6 +495,11 @@ public class Mahjongg : Gtk.Application
         update_ui ();
     }
 
+    private void shuffle_cb ()
+    {
+        game_view.game.shuffle_remaining();
+    }
+
     private void about_cb ()
     {
         string[] authors =
@@ -668,6 +673,7 @@ public class Mahjongg : Gtk.Application
         { "undo",          undo_cb         },
         { "redo",          redo_cb         },
         { "hint",          hint_cb         },
+        { "shuffle",       shuffle_cb      },
         { "pause",         pause_cb        },
         { "restart-game",  restart_game_cb },
         { "scores",        scores_cb       },


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