[gnome-nibbles/arnaudb/modernize-code: 39/58] Populate game from NibblesGame.



commit 94ca5242f831f9d7b50c82c709943ce0698e1919
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri May 29 15:32:43 2020 +0200

    Populate game from NibblesGame.

 src/nibbles-game.vala |  91 ++++++++++++++++++++
 src/nibbles-test.vala | 228 +++++++++++++++-----------------------------------
 src/nibbles-view.vala | 147 +++++++++-----------------------
 3 files changed, 198 insertions(+), 268 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 06c204e..ff24e05 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -88,6 +88,97 @@ private class NibblesGame : Object
         Random.set_seed (no_random ? 42 : (uint32) time_t ());
     }
 
+    internal bool load_board (string [] future_board)
+    {
+        if (future_board.length != NibblesGame.HEIGHT)
+            return false;
+
+        boni.reset (numworms);
+        warp_manager.warps.clear ();
+
+        string tmpboard;
+        int count = 0;
+        for (int i = 0; i < NibblesGame.HEIGHT; i++)
+        {
+            tmpboard = future_board [i];
+            if (tmpboard.length != NibblesGame.WIDTH)
+                return false;
+            for (int j = 0; j < NibblesGame.WIDTH; j++)
+            {
+                board[j, i] = tmpboard.@get(j);
+                switch (board[j, i])
+                {
+                    case '.': // readable empty space, but the game internals uses 'a'
+                        board[j, i] = 'a';
+                        break;
+
+                    case 'm':
+                        board[j, i] = NibblesGame.EMPTYCHAR;
+                        if (count < numworms)
+                        {
+                            worms[count].set_start (j, i, WormDirection.UP);
+                            count++;
+                        }
+                        break;
+                    case 'n':
+                        board[j, i] = NibblesGame.EMPTYCHAR;
+                        if (count < numworms)
+                        {
+                            worms[count].set_start (j, i, WormDirection.LEFT);
+                            count++;
+                        }
+                        break;
+                    case 'o':
+                        board[j, i] = NibblesGame.EMPTYCHAR;
+                        if (count < numworms)
+                        {
+                            worms[count].set_start (j, i, WormDirection.DOWN);
+                            count++;
+                        }
+                        break;
+                    case 'p':
+                        board[j, i] = NibblesGame.EMPTYCHAR;
+                        if (count < numworms)
+                        {
+                            worms[count].set_start (j, i, WormDirection.RIGHT);
+                            count++;
+                        }
+                        break;
+
+                    case 'Q':
+                    case 'R':
+                    case 'S':
+                    case 'T':
+                    case 'U':
+                    case 'V':
+                    case 'W':
+                    case 'X':
+                    case 'Y':
+                    case 'Z':
+                        warp_manager.add_warp (board, j - 1, i - 1, -(board[j, i]), 0);
+                        break;
+
+                    case 'r':
+                    case 's':
+                    case 't':
+                    case 'u':
+                    case 'v':
+                    case 'w':
+                    case 'x':
+                    case 'y':
+                    case 'z':
+                        warp_manager.add_warp (board, -(board[j, i] - 'a' + 'A'), 0, j, i);
+                        board[j, i] = NibblesGame.EMPTYCHAR;
+                        break;
+
+                    default:
+                        break;  // return false?
+                }
+            }
+        }
+        return true;
+    }
+
     /*\
     * * Game controls
     \*/
diff --git a/src/nibbles-test.vala b/src/nibbles-test.vala
index e1c148c..bbe0e72 100644
--- a/src/nibbles-test.vala
+++ b/src/nibbles-test.vala
@@ -47,99 +47,7 @@ namespace NibblesTest
         game.numai = 4;
         game.create_worms ();
 
-        // FIXME adapted from nibbles-view.vala; should be in game.vala
-
-        game.boni.reset (game.numworms);
-        game.warp_manager.warps.clear ();
-
-        string tmpboard;
-        int count = 0;
-        string [] level = level_008.split ("\n");
-        for (int i = 0; i < NibblesGame.HEIGHT; i++)
-        {
-            tmpboard = level [i];
-            for (int j = 0; j < NibblesGame.WIDTH; j++)
-            {
-                game.board[j, i] = tmpboard.@get(j);
-                switch (game.board[j, i])
-                {
-                    case 'm':
-                        game.board[j, i] = NibblesGame.EMPTYCHAR;
-                        if (count < game.numworms)
-                        {
-                            game.worms[count].set_start (j, i, WormDirection.UP);
-                            count++;
-                        }
-                        break;
-                    case 'n':
-                        game.board[j, i] = NibblesGame.EMPTYCHAR;
-                        if (count < game.numworms)
-                        {
-                            game.worms[count].set_start (j, i, WormDirection.LEFT);
-                            count++;
-                        }
-                        break;
-                    case 'o':
-                        game.board[j, i] = NibblesGame.EMPTYCHAR;
-                        if (count < game.numworms)
-                        {
-                            game.worms[count].set_start (j, i, WormDirection.DOWN);
-                            count++;
-                        }
-                        break;
-                    case 'p':
-                        game.board[j, i] = NibblesGame.EMPTYCHAR;
-                        if (count < game.numworms)
-                        {
-                            game.worms[count].set_start (j, i, WormDirection.RIGHT);
-                            count++;
-                        }
-                        break;
-                    default:
-                        break;
-                }
-            }
-        }
-
-        for (int i = 0; i < NibblesGame.HEIGHT; i++)
-        {
-            for (int j = 0; j < NibblesGame.WIDTH; j++)
-            {
-                switch (game.board[j, i])
-                {
-                    case '.': // empty space
-                        game.board[j, i] = 'a';
-                        break;
-                    case 'Q':
-                    case 'R':
-                    case 'S':
-                    case 'T':
-                    case 'U':
-                    case 'V':
-                    case 'W':
-                    case 'X':
-                    case 'Y':
-                    case 'Z':
-                        game.warp_manager.add_warp (game.board, j - 1, i - 1, -(game.board[j, i]), 0);
-                        break;
-                    case 'r':
-                    case 's':
-                    case 't':
-                    case 'u':
-                    case 'v':
-                    case 'w':
-                    case 'x':
-                    case 'y':
-                    case 'z':
-                        game.warp_manager.add_warp (game.board, -(game.board[j, i] - 'a' + 'A'), 0, j, i);
-                        game.board[j, i] = NibblesGame.EMPTYCHAR;
-                        break;
-                    default:
-                        break;
-                }
-            }
-        }
-        // END FIXME
+        game.load_board (level_008);
 
         assert_true (game.numworms == 4);
         assert_true (game.worms.size == 4);
@@ -175,70 +83,72 @@ namespace NibblesTest
         assert_true (game.worms.@get (3).score == 16);
     }
 
-    private const string level_008 = 
"fccccccccccccccccccccccccccccccccccccccce........dcccccccccccccccccccccccccccccccccccccccccg"
-                            + "\n" + 
"b..........................................................................................b"
-                            + "\n" + 
"b..........................................................................................b"
-                            + "\n" + 
"b..........................................................................................b"
-                            + "\n" + 
"b...R...............................fg...........................fg.....................S..b"
-                            + "\n" + 
"b...u...............................dlg..........................bb....................t...b"
-                            + "\n" + 
"b....................................dlg.........................bb........................b"
-                            + "\n" + 
"b...........fg........................dlg........................bb........................b"
-                            + "\n" + 
"b...........bb.........................dlg.......................bb........................b"
-                            + "\n" + 
"b...o.......bb..........................dlg......................bb........................b"
-                            + "\n" + 
"b...........bb...........................dlg.....................bb...fccccccccg...........b"
-                            + "\n" + 
"b...........bb............................dlg....................bb...dcccccccclg..........b"
-                            + "\n" + 
"b...........bb.............................dlg...................bb............dlg.........b"
-                            + "\n" + 
"b...........bb..............................dlg..................bb.............dlg........b"
-                            + "\n" + 
"b...........bb............fg.................dlg.................bb..............dlccg.....b"
-                            + "\n" + 
"b...........bb...........fle..................dlg................bb...............dcce.....b"
-                            + "\n" + 
"b...........bb..........fle....................dlg...............bb........................b"
-                            + "\n" + 
"e...........de.........fle......................dlg..............bb........................d"
-                            + "\n" + 
"......................fle........................dlg.............bb........................."
-                            + "\n" + 
".....................fle.........................fle.............bb........................."
-                            + "\n" + 
"....................fle.........................fle..............bb........................."
-                            + "\n" + 
"...................fle.........................fle...............bb........................."
-                            + "\n" + 
"..................fle.........................fle................bb......fg................."
-                            + "\n" + 
"..................de.........................fle.................bb......dlg................"
-                            + "\n" + 
"............................................fle..................bb.......dlg..............."
-                            + "\n" + 
"............................................dlg..................bb........dlg.............."
-                            + "\n" + 
"g.................o..........................dlg.................bb.........dlg............f"
-                            + "\n" + 
"b.............................................dlg................bb..........dlg...........b"
-                            + "\n" + 
"b..............................................dlg...............bb...........dlg..........b"
-                            + "\n" + 
"b........fg.....................fg..............dlg..............bb............de..........b"
-                            + "\n" + 
"b........bb.....................dlg..............dlg.............bb........................b"
-                            + "\n" + 
"b........bb......................dlg..............dlg............bb........................b"
-                            + "\n" + 
"b........bb.......................dlg..............dlg...........bb........................b"
-                            + "\n" + 
"b........bb........................dlg..............de...........bb........................b"
-                            + "\n" + 
"b........bb.........................dlg..........................bb........................b"
-                            + "\n" + 
"b........bdcccccccg..................dlg.........................bb........................b"
-                            + "\n" + 
"b........dcccccccce...................dlg........................bb........................b"
-                            + "\n" + 
"b......................................dlg.......................bb........................b"
-                            + "\n" + 
"b.......................................dlg......................bb....fcccccccccccccg.....b"
-                            + "\n" + 
"b...p....................................dlg.....................bb....dccccccccccccce.....b"
-                            + "\n" + 
"b........................................fle.....................bb........................b"
-                            + "\n" + 
"b.......................................fle......................bb........................b"
-                            + "\n" + 
"b.................fg...................fle.......................bb........................b"
-                            + "\n" + 
"b.................bb..................fle........................bb........................b"
-                            + "\n" + 
"b...........fg....bb.................fle.........................bb........................b"
-                            + "\n" + 
"b..........fle....bb................fle..........................bb........................b"
-                            + "\n" + 
"b.........fle.....bb...............fle...........................bb........................b"
-                            + "\n" + 
"b........fle......bb...............de............................bb........................b"
-                            + "\n" + 
"b.......fle.......bb.............................................bb........................b"
-                            + "\n" + 
"b......fle........bb.............................................bb........................b"
-                            + "\n" + 
"b......de.........bb...............................m.............bb........................b"
-                            + "\n" + 
"b.................bb.............................................bb........................b"
-                            + "\n" + 
"b.................bb.............................................bb........................b"
-                            + "\n" + 
"b.................bb.............................................bdcccccccccccccg..........b"
-                            + "\n" + 
"b.................bb............fccccccccccccccccccccccg.........dcccccccccccccce..........b"
-                            + "\n" + 
"b.................bb............dcccccccccccccccccccccce...................................b"
-                            + "\n" + 
"b.................bb.......................................................................b"
-                            + "\n" + 
"b.................bb...................................................................n...b"
-                            + "\n" + 
"b.................bb.......................................................................b"
-                            + "\n" + 
"b.................bb......................n................................................b"
-                            + "\n" + 
"b...s.............bb...................................................................r...b"
-                            + "\n" + 
"b.................de.......................................................................b"
-                            + "\n" + 
"b...T...................................................................................U..b"
-                            + "\n" + 
"b..........................................................................................b"
-                            + "\n" + 
"b..........................................................................................b"
-                            + "\n" + 
"dcccccccccccccccccccccccccccccccccccccccg........fccccccccccccccccccccccccccccccccccccccccce";
+    private const string [] level_008 = {
+            "fccccccccccccccccccccccccccccccccccccccce........dcccccccccccccccccccccccccccccccccccccccccg",
+            "b..........................................................................................b",
+            "b..........................................................................................b",
+            "b..........................................................................................b",
+            "b...R...............................fg...........................fg.....................S..b",
+            "b...u...............................dlg..........................bb....................t...b",
+            "b....................................dlg.........................bb........................b",
+            "b...........fg........................dlg........................bb........................b",
+            "b...........bb.........................dlg.......................bb........................b",
+            "b...o.......bb..........................dlg......................bb........................b",
+            "b...........bb...........................dlg.....................bb...fccccccccg...........b",
+            "b...........bb............................dlg....................bb...dcccccccclg..........b",
+            "b...........bb.............................dlg...................bb............dlg.........b",
+            "b...........bb..............................dlg..................bb.............dlg........b",
+            "b...........bb............fg.................dlg.................bb..............dlccg.....b",
+            "b...........bb...........fle..................dlg................bb...............dcce.....b",
+            "b...........bb..........fle....................dlg...............bb........................b",
+            "e...........de.........fle......................dlg..............bb........................d",
+            "......................fle........................dlg.............bb.........................",
+            ".....................fle.........................fle.............bb.........................",
+            "....................fle.........................fle..............bb.........................",
+            "...................fle.........................fle...............bb.........................",
+            "..................fle.........................fle................bb......fg.................",
+            "..................de.........................fle.................bb......dlg................",
+            "............................................fle..................bb.......dlg...............",
+            "............................................dlg..................bb........dlg..............",
+            "g.................o..........................dlg.................bb.........dlg............f",
+            "b.............................................dlg................bb..........dlg...........b",
+            "b..............................................dlg...............bb...........dlg..........b",
+            "b........fg.....................fg..............dlg..............bb............de..........b",
+            "b........bb.....................dlg..............dlg.............bb........................b",
+            "b........bb......................dlg..............dlg............bb........................b",
+            "b........bb.......................dlg..............dlg...........bb........................b",
+            "b........bb........................dlg..............de...........bb........................b",
+            "b........bb.........................dlg..........................bb........................b",
+            "b........bdcccccccg..................dlg.........................bb........................b",
+            "b........dcccccccce...................dlg........................bb........................b",
+            "b......................................dlg.......................bb........................b",
+            "b.......................................dlg......................bb....fcccccccccccccg.....b",
+            "b...p....................................dlg.....................bb....dccccccccccccce.....b",
+            "b........................................fle.....................bb........................b",
+            "b.......................................fle......................bb........................b",
+            "b.................fg...................fle.......................bb........................b",
+            "b.................bb..................fle........................bb........................b",
+            "b...........fg....bb.................fle.........................bb........................b",
+            "b..........fle....bb................fle..........................bb........................b",
+            "b.........fle.....bb...............fle...........................bb........................b",
+            "b........fle......bb...............de............................bb........................b",
+            "b.......fle.......bb.............................................bb........................b",
+            "b......fle........bb.............................................bb........................b",
+            "b......de.........bb...............................m.............bb........................b",
+            "b.................bb.............................................bb........................b",
+            "b.................bb.............................................bb........................b",
+            "b.................bb.............................................bdcccccccccccccg..........b",
+            "b.................bb............fccccccccccccccccccccccg.........dcccccccccccccce..........b",
+            "b.................bb............dcccccccccccccccccccccce...................................b",
+            "b.................bb.......................................................................b",
+            "b.................bb...................................................................n...b",
+            "b.................bb.......................................................................b",
+            "b.................bb......................n................................................b",
+            "b...s.............bb...................................................................r...b",
+            "b.................de.......................................................................b",
+            "b...T...................................................................................U..b",
+            "b..........................................................................................b",
+            "b..........................................................................................b",
+            "dcccccccccccccccccccccccccccccccccccccccg........fccccccccccccccccccccccccccccccccccccccccce"
+        };
 }
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 355ab8d..bfbbca9 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -198,9 +198,9 @@ private class NibblesView : GtkClutter.Embed
     * * Level creationg and loading
     \*/
 
-    internal void new_level (int level)
+    internal void new_level (int level_id)
     {
-        string level_name = "level%03d.gnl".printf (level);
+        string level_name = "level%03d.gnl".printf (level_id);
         string filename = Path.build_filename (PKGDATADIR, "levels", level_name, null);
 
         FileStream file;
@@ -219,110 +219,43 @@ private class NibblesView : GtkClutter.Embed
             actor.destroy ();
         warp_actors.clear ();
 
-        game.boni.reset (game.numworms);
-        game.warp_manager.warps.clear ();
-
-        string tmpboard;
-        int count = 0;
-        for (int i = 0; i < NibblesGame.HEIGHT; i++)
-        {
-            if ((tmpboard = file.read_line ()) == null)
-                error ("Level file appears to be damaged: %s", filename);
-
-            for (int j = 0; j < NibblesGame.WIDTH; j++)
-            {
-                game.board[j, i] = tmpboard.@get(j);
-                switch (game.board[j, i])
-                {
-                    case 'm':
-                        game.board[j, i] = NibblesGame.EMPTYCHAR;
-                        if (count < game.numworms)
-                        {
-                            game.worms[count].set_start (j, i, WormDirection.UP);
-
-                            var actors = new WormActor ();
-                            stage.add_child (actors);
-                            worm_actors.@set (game.worms[count], actors);
-                            count++;
-                        }
-                        break;
-                    case 'n':
-                        game.board[j, i] = NibblesGame.EMPTYCHAR;
-                        if (count < game.numworms)
-                        {
-                            game.worms[count].set_start (j, i, WormDirection.LEFT);
-
-                            var actors = new WormActor ();
-                            stage.add_child (actors);
-                            worm_actors.@set (game.worms[count], actors);
-                            count++;
-                        }
-                        break;
-                    case 'o':
-                        game.board[j, i] = NibblesGame.EMPTYCHAR;
-                        if (count < game.numworms)
-                        {
-                            game.worms[count].set_start (j, i, WormDirection.DOWN);
-
-                            var actors = new WormActor ();
-                            stage.add_child (actors);
-                            worm_actors.@set (game.worms[count], actors);
-                            count++;
-                        }
-                        break;
-                    case 'p':
-                        game.board[j, i] = NibblesGame.EMPTYCHAR;
-                        if (count < game.numworms)
-                        {
-                            game.worms[count].set_start (j, i, WormDirection.RIGHT);
-
-                            var actors = new WormActor ();
-                            stage.add_child (actors);
-                            worm_actors.@set (game.worms[count], actors);
-                            count++;
-                        }
-                        break;
-                    default:
-                        break;
-                }
-            }
-        }
-
-        load_level ();
-    }
-
-    private void load_level ()
-    {
-        int x_pos, y_pos;
-        GtkClutter.Texture tmp = null;
-        bool is_wall = true;
-
         if (level != null)
         {
             level.remove_all_children ();
             stage.remove_child (level);
         }
-
         level = new Clutter.Actor ();
 
+        string? line;
+        string [] board = {};
+        while ((line = file.read_line ()) != null)
+            board += (!) line;
+        if (!game.load_board (board))
+            error ("Level file appears to be damaged: %s", filename);
+
+        foreach (Worm worm in game.worms)
+        {
+            var actors = new WormActor ();
+            stage.add_child (actors);
+            worm_actors.@set (worm, actors);
+        }
+
         /* Load wall_pixmaps onto the surface */
+        int x_pos, y_pos;
+        GtkClutter.Texture? tmp;
         for (int i = 0; i < NibblesGame.HEIGHT; i++)
         {
             y_pos = i * game.tile_size;
             for (int j = 0; j < NibblesGame.WIDTH; j++)
             {
-                is_wall = true;
+                tmp = null;
                 try
                 {
                     switch (game.board[j, i])
                     {
-                        case '.': // empty space
-                            game.board[j, i] = 'a';
-                            is_wall = false;
-                            break;
-                        case 'a': // kept for compatibility
-                            is_wall = false;
+                        case 'a':   // the most common thing on top in the switch
                             break;
+
                         case 'b': // straight up
                             tmp = new GtkClutter.Texture ();
                             tmp.set_from_pixbuf (wall_pixmaps[0]);
@@ -367,6 +300,19 @@ private class NibblesView : GtkClutter.Embed
                             tmp = new GtkClutter.Texture ();
                             tmp.set_from_pixbuf (wall_pixmaps[10]);
                             break;
+
+                        case 'r': // should have been repleced by NibblesGame.EMPTYCHAR
+                        case 's':
+                        case 't':
+                        case 'u':
+                        case 'v':
+                        case 'w':
+                        case 'x':
+                        case 'y':
+                        case 'z':
+                        case '.': // empty space in files, replaced by an 'a'
+                            assert_not_reached ();
+
                         case 'Q':
                         case 'R':
                         case 'S':
@@ -377,24 +323,7 @@ private class NibblesView : GtkClutter.Embed
                         case 'X':
                         case 'Y':
                         case 'Z':
-                            is_wall = false;
-                            game.warp_manager.add_warp (game.board, j - 1, i - 1, -(game.board[j, i]), 0);
-                            break;
-                        case 'r':
-                        case 's':
-                        case 't':
-                        case 'u':
-                        case 'v':
-                        case 'w':
-                        case 'x':
-                        case 'y':
-                        case 'z':
-                            is_wall = false;
-                            game.warp_manager.add_warp (game.board, -(game.board[j, i] - 'a' + 'A'), 0, j, 
i);
-                            game.board[j, i] = NibblesGame.EMPTYCHAR;
-                            break;
                         default:
-                            is_wall = false;
                             break;
                     }
                 }
@@ -403,13 +332,13 @@ private class NibblesView : GtkClutter.Embed
                     error ("Error loading level: %s", e.message);
                 }
 
-                if (is_wall)
+                if (tmp != null)
                 {
                     x_pos = j * game.tile_size;
 
-                    tmp.set_size (game.tile_size, game.tile_size);
-                    tmp.set_position (x_pos, y_pos);
-                    level.add_child (tmp);
+                    ((!) tmp).set_size (game.tile_size, game.tile_size);
+                    ((!) tmp).set_position (x_pos, y_pos);
+                    level.add_child ((!) tmp);
                 }
             }
         }


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