[four-in-a-row] Reorder main file.



commit 7ce4eb77b72e8228ef7d606f213cbedd8713bc0d
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Dec 21 14:43:52 2019 +0100

    Reorder main file.

 src/four-in-a-row.vala | 641 +++++++++++++++++++++++++------------------------
 1 file changed, 327 insertions(+), 314 deletions(-)
---
diff --git a/src/four-in-a-row.vala b/src/four-in-a-row.vala
index 32d4e0e..6b05699 100644
--- a/src/four-in-a-row.vala
+++ b/src/four-in-a-row.vala
@@ -31,19 +31,6 @@ private class FourInARow : Gtk.Application
     private const int SPEED_DROP = 20;
     private const char vlevel [] = { '0','a','b','c' };
 
-    private static int main (string [] args)
-    {
-        Intl.setlocale ();
-        Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
-        Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
-        Intl.textdomain (GETTEXT_PACKAGE);
-
-        Environment.set_application_name (PROGRAM_NAME);
-        Window.set_default_icon_name ("org.gnome.Four-in-a-row");
-
-        return new FourInARow ().run (args);
-    }
-
     private enum AnimID {
         NONE,
         MOVE,
@@ -103,80 +90,96 @@ private class FourInARow : Gtk.Application
         { "about",          on_help_about           }
     };
 
-    internal void game_reset ()
+    private static int main (string [] args)
     {
-        stop_anim ();
+        Intl.setlocale ();
+        Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+        Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+        Intl.textdomain (GETTEXT_PACKAGE);
 
-        window.allow_undo (false);
-        window.allow_hint (false);
+        Environment.set_application_name (PROGRAM_NAME);
+        Window.set_default_icon_name ("org.gnome.Four-in-a-row");
 
-        one_player_game = Prefs.instance.settings.get_int ("num-players") == 1;
-        if (reset_score)
-        {
-            score = { 0, 0, 0 };
-            scorebox.update (score, one_player_game);
-            reset_score = false;
-        }
-        if (one_player_game)
-        {
-            player = Prefs.instance.settings.get_string ("first-player") == "computer" ? PlayerID.PLAYER2 : 
PlayerID.PLAYER1;
-            Prefs.instance.settings.set_string ("first-player", player == PlayerID.PLAYER1 ? "computer" : 
"human");
-            ai_level = Prefs.instance.settings.get_int ("opponent");
-        }
-        else
-        {
-            switch (last_first_player)
-            {
-                case PlayerID.PLAYER1: player = PlayerID.PLAYER2; break;
-                case PlayerID.PLAYER2:
-                case PlayerID.NOBODY : player = PlayerID.PLAYER1; break;
-            }
-            last_first_player = player;
-        }
+        return new FourInARow ().run (args);
+    }
 
-        gameover = true;
-        player_active = false;
-        winner = NOBODY;
-        column = 3;
-        column_moveto = 3;
-        row = 0;
-        row_dropto = 0;
+    private FourInARow ()
+    {
+        Object (application_id: "org.gnome.Four-in-a-row", flags: ApplicationFlags.FLAGS_NONE);
 
         clear_board ();
-        set_status_message (null);
-        game_board_view.queue_draw ();
-
-        move_cursor (column);
-        gameover = false;
-        prompt_player ();
-        if (!is_player_human ())
-        {
-            vstr [0] = vlevel [ai_level];
-            process_move (playgame ((string) vstr) - 1);
-        }
     }
 
-    private void blink_winner (int n)   /* blink the winner's line(s) n times */
+    protected override void startup ()
     {
-        if (winner == NOBODY)
-            return;
+        base.startup ();
 
-        blink_t = winner;
+        /* UI parts */
+        new_game_screen = new NewGameScreen ();
+        new_game_screen.show ();
 
-        if (game_board.is_line_at ((Tile) winner, row, column,
-                               out blink_r1, out blink_c1,
-                               out blink_r2, out blink_c2))
-        {
-            anim = AnimID.BLINK;
-            blink_on = false;
-            blink_n = n;
-            var temp = new Animate (0, this);
-            timeout = Timeout.add (SPEED_BLINK, temp.exec);
-            while (timeout != 0)
-                main_iteration ();
-        }
-    }
+        game_board_view = new GameBoardView (game_board);
+        game_board_view.show ();
+
+        GLib.Menu app_menu = new GLib.Menu ();
+
+        GLib.Menu appearance_menu = new GLib.Menu ();
+        for (uint8 i = 0; i < theme.length; i++)     // TODO default theme
+            appearance_menu.append (theme_get_title (i), @"app.theme-id($i)");
+        appearance_menu.freeze ();
+
+        GLib.Menu section = new GLib.Menu ();
+        /* Translators: hamburger menu entry; "Appearance" submenu (with a mnemonic that appears pressing 
Alt) */
+        section.append_submenu (_("A_ppearance"), (!) appearance_menu);
+
+        section.append (_("Sound"), "app.sound");
+        section.freeze ();
+        app_menu.append_section (null, section);
+
+        section = new GLib.Menu ();
+        section.append (_("_Scores"), "app.scores");
+        section.freeze ();
+        app_menu.append_section (null, section);
+
+        section = new GLib.Menu ();
+        section.append (_("_Help"), "app.help");
+        section.append (_("_About Four-in-a-row"), "app.about");
+        section.freeze ();
+        app_menu.append_section (null, section);
+
+        app_menu.freeze ();
+
+        MenuButton history_button_1 = new HistoryButton (/* direction: down */ false);
+        MenuButton history_button_2 = new HistoryButton (/* direction: up   */ true);
+
+        /* Window */
+        window = new GameWindow ("/org/gnome/Four-in-a-row/ui/four-in-a-row.css",
+                                 PROGRAM_NAME,
+                                 /* start_now */ true,
+                                 GameWindowFlags.SHOW_START_BUTTON,
+                                 (Box) new_game_screen,
+                                 game_board_view,
+                                 app_menu,
+                                 history_button_1,
+                                 history_button_2);
+
+        scorebox = new Scorebox (window, this);
+
+        add_actions ();
+
+        /* various */
+        game_board_view.column_clicked.connect (column_clicked_cb);
+        window.key_press_event.connect (on_key_press);
+
+        window.play.connect (on_game_new);
+        window.undo.connect (on_game_undo);
+        window.hint.connect (on_game_hint);
 
+        window.allow_hint (false);
+        window.allow_undo (false);
+
+        add_window (window);
+    }
     private inline void add_actions ()
     {
         GLib.Settings settings = Prefs.instance.settings;
@@ -241,39 +244,94 @@ private class FourInARow : Gtk.Application
             });
     }
 
-    private SimpleAction game_type_action;
-    private void change_game_type (SimpleAction action, Variant? gvariant)
-        requires (gvariant != null)
+    protected override void activate ()
     {
-        string type = ((!) gvariant).get_string ();
-//        game_type_action.set_state ((!) gvariant);
-        switch (type)
-        {
-            case "human"    : Prefs.instance.settings.set_int    ("num-players", 1); 
new_game_screen.update_sensitivity (true);
-                              Prefs.instance.settings.set_string ("first-player", "human");                  
                    return;
-            case "computer" : Prefs.instance.settings.set_int    ("num-players", 1); 
new_game_screen.update_sensitivity (true);
-                              Prefs.instance.settings.set_string ("first-player", "computer");               
                    return;
-            case "two"      : Prefs.instance.settings.set_int    ("num-players", 2); 
new_game_screen.update_sensitivity (false); return;
-            default: assert_not_reached ();
-        }
+        if (window.is_visible ())
+            return;
+
+        window.show ();
+        game_board_view.queue_draw ();
+        scorebox.update (score, one_player_game);    /* update visible player descriptions */
+        prompt_player ();
+        game_reset ();
     }
 
-    private inline bool column_clicked_cb (int column)
+    /*\
+    * * various
+    \*/
+
+    internal void game_reset ()
     {
-        if (player_active)
-            return false;
+        stop_anim ();
 
-        if (gameover && timeout == 0)
-            blink_winner (2);
-        else if (is_player_human () && timeout == 0)
-            process_move (column);
-        return true;
+        window.allow_undo (false);
+        window.allow_hint (false);
+
+        one_player_game = Prefs.instance.settings.get_int ("num-players") == 1;
+        if (reset_score)
+        {
+            score = { 0, 0, 0 };
+            scorebox.update (score, one_player_game);
+            reset_score = false;
+        }
+        if (one_player_game)
+        {
+            player = Prefs.instance.settings.get_string ("first-player") == "computer" ? PlayerID.PLAYER2 : 
PlayerID.PLAYER1;
+            Prefs.instance.settings.set_string ("first-player", player == PlayerID.PLAYER1 ? "computer" : 
"human");
+            ai_level = Prefs.instance.settings.get_int ("opponent");
+        }
+        else
+        {
+            switch (last_first_player)
+            {
+                case PlayerID.PLAYER1: player = PlayerID.PLAYER2; break;
+                case PlayerID.PLAYER2:
+                case PlayerID.NOBODY : player = PlayerID.PLAYER1; break;
+            }
+            last_first_player = player;
+        }
+
+        gameover = true;
+        player_active = false;
+        winner = NOBODY;
+        column = 3;
+        column_moveto = 3;
+        row = 0;
+        row_dropto = 0;
+
+        clear_board ();
+        set_status_message (null);
+        game_board_view.queue_draw ();
+
+        move_cursor (column);
+        gameover = false;
+        prompt_player ();
+        if (!is_player_human ())
+        {
+            vstr [0] = vlevel [ai_level];
+            process_move (playgame ((string) vstr) - 1);
+        }
     }
 
-    private inline void on_game_new (/* SimpleAction action, Variant? variant */)
+    private void blink_winner (int n)   /* blink the winner's line(s) n times */
     {
-        stop_anim ();
-        game_reset ();
+        if (winner == NOBODY)
+            return;
+
+        blink_t = winner;
+
+        if (game_board.is_line_at ((Tile) winner, row, column,
+                               out blink_r1, out blink_c1,
+                               out blink_r2, out blink_c2))
+        {
+            anim = AnimID.BLINK;
+            blink_on = false;
+            blink_n = n;
+            var temp = new Animate (0, this);
+            timeout = Timeout.add (SPEED_BLINK, temp.exec);
+            while (timeout != 0)
+                main_iteration ();
+        }
     }
 
     private inline void draw_line (int r1, int c1, int r2, int c2, int tile)
@@ -307,25 +365,6 @@ private class FourInARow : Gtk.Application
         while (!done);
     }
 
-    private FourInARow ()
-    {
-        Object (application_id: "org.gnome.Four-in-a-row", flags: ApplicationFlags.FLAGS_NONE);
-
-        clear_board ();
-    }
-
-    protected override void activate ()
-    {
-        if (window.is_visible ())
-            return;
-
-        window.show ();
-        game_board_view.queue_draw ();
-        scorebox.update (score, one_player_game);    /* update visible player descriptions */
-        prompt_player ();
-        game_reset ();
-    }
-
     private void prompt_player ()
     {
         bool human = is_player_human ();
@@ -425,6 +464,26 @@ private class FourInARow : Gtk.Application
             }
         }
     }
+    private inline void check_game_state ()
+    {
+        if (game_board.is_line_at ((Tile) player, row, column))
+        {
+            gameover = true;
+            winner = player;
+            if (one_player_game)
+                play_sound (is_player_human () ? SoundID.YOU_WIN : SoundID.I_WIN);
+            else
+                play_sound (SoundID.PLAYER_WIN);
+            window.allow_hint (false);
+            blink_winner (6);
+        }
+        else if (moves == 42)
+        {
+            gameover = true;
+            winner = NOBODY;
+            play_sound (SoundID.DRAWN_GAME);
+        }
+    }
 
     private bool is_player_human ()
     {
@@ -533,73 +592,25 @@ private class FourInARow : Gtk.Application
         for (var i = 0; i < SIZE_VSTR; i++)
             vstr [i] = '\0';
 
-        vstr [0] = vlevel [Level.WEAK];
-        vstr [1] = '0';
-        moves = 0;
-    }
-
-    private inline void blink_tile (int r, int c, int t, int n)
-    {
-        if (timeout != 0)
-            return;
-        blink_r1 = r;
-        blink_c1 = c;
-        blink_r2 = r;
-        blink_c2 = c;
-        blink_t = t;
-        blink_n = n;
-        blink_on = false;
-        anim = AnimID.BLINK;
-        var temp = new Animate (0, this);
-        timeout = Timeout.add (SPEED_BLINK, temp.exec);
-    }
-
-    private inline void on_game_hint (/* SimpleAction action, Variant? parameter */)
-    {
-        string s;
-        int c;
-
-        if (timeout != 0)
-            return;
-        if (gameover)
-            return;
-
-        window.allow_hint (false);
-        window.allow_undo (false);
-
-        set_status_message (_("I’m Thinking…"));
-
-        vstr [0] = vlevel [Level.STRONG];
-        c = playgame ((string) vstr) - 1;
-
-        column_moveto = c;
-        while (timeout != 0)
-            main_iteration ();
-        anim = AnimID.HINT;
-        var temp = new Animate (0, this);
-        timeout = Timeout.add (SPEED_MOVE, temp.exec);
-
-        blink_tile (0, c, game_board [0, c], 6);
-
-        s = _("Hint: Column %d").printf (c + 1);
-        set_status_message (s);
-
-        if (moves <= 0 || (moves == 1 && is_player_human ()))
-            window.allow_undo (false);
-        else
-            window.allow_undo (true);
-    }
-
-    private inline void on_game_scores (/* SimpleAction action, Variant? parameter */)
-    {
-        scorebox.present ();
-        return;
+        vstr [0] = vlevel [/* weak */ 1];
+        vstr [1] = '0';
+        moves = 0;
     }
 
-    private inline void on_game_exit (/* SimpleAction action, Variant? parameter */)
+    private inline void blink_tile (int r, int c, int t, int n)
     {
-        stop_anim ();
-        quit ();
+        if (timeout != 0)
+            return;
+        blink_r1 = r;
+        blink_c1 = c;
+        blink_r2 = r;
+        blink_c2 = c;
+        blink_t = t;
+        blink_n = n;
+        blink_on = false;
+        anim = AnimID.BLINK;
+        var temp = new Animate (0, this);
+        timeout = Timeout.add (SPEED_BLINK, temp.exec);
     }
 
     private class Animate
@@ -670,6 +681,52 @@ private class FourInARow : Gtk.Application
         }
     }
 
+    /*\
+    * * game window callbacks
+    \*/
+
+    private inline void on_game_new ()
+    {
+        stop_anim ();
+        game_reset ();
+    }
+
+    private inline void on_game_hint ()
+    {
+        string s;
+        int c;
+
+        if (timeout != 0)
+            return;
+        if (gameover)
+            return;
+
+        window.allow_hint (false);
+        window.allow_undo (false);
+
+        set_status_message (_("I’m Thinking…"));
+
+        vstr [0] = vlevel [/* strong */ 3];
+        c = playgame ((string) vstr) - 1;
+
+        column_moveto = c;
+        while (timeout != 0)
+            main_iteration ();
+        anim = AnimID.HINT;
+        var temp = new Animate (0, this);
+        timeout = Timeout.add (SPEED_MOVE, temp.exec);
+
+        blink_tile (0, c, game_board [0, c], 6);
+
+        s = _("Hint: Column %d").printf (c + 1);
+        set_status_message (s);
+
+        if (moves <= 0 || (moves == 1 && is_player_human ()))
+            window.allow_undo (false);
+        else
+            window.allow_undo (true);
+    }
+
     private inline void on_game_undo ()
     {
         if (timeout != 0)
@@ -711,140 +768,43 @@ private class FourInARow : Gtk.Application
         }
     }
 
-    private inline void on_help_about (SimpleAction action, Variant? parameter)
-    {
-        const string authors [] = {
-            "Tim Musson <trmusson ihug co nz>",
-            "David Neary <bolsh gimp org>",
-            "Nikhar Agrawal <nikharagrawal2006 gmail com>",
-            "Jacob Humphrey <jacob ryan humphrey gmail com>"
-        };
-
-        const string artists [] = {
-            "Alan Horkan",
-            "Anatol Drlicek",
-            "Based on the Faenza icon theme by Matthieu James"
-        };
-
-        const string documenters[] = { "Timothy Musson" };
-
-        show_about_dialog (window,
-            name: PROGRAM_NAME,
-            version: VERSION,
-            copyright: "Copyright © 1999–2008 Tim Musson and David Neary\n" +
-                       "Copyright © 2014 Michael Catanzaro\n" +
-                       "Copyright © 2018 Jacob Humphrey",
-            license_type: License.GPL_3_0,
-            comments: _("Connect four in a row to win"),
-            authors: authors,
-            documenters: documenters,
-            artists: artists,
-            translator_credits: _("translator-credits"),
-            logo_icon_name: "org.gnome.Four-in-a-row",
-            website: "https://wiki.gnome.org/Apps/Four-in-a-row";);
-    }
+    /*\
+    * * actions
+    \*/
 
-    private inline void on_help_contents (/* SimpleAction action, Variant? parameter */)
+    private SimpleAction game_type_action;
+    private void change_game_type (SimpleAction action, Variant? gvariant)
+        requires (gvariant != null)
     {
-        try {
-            show_uri_on_window (window, "help:four-in-a-row", get_current_event_time ());
-        } catch (Error error) {
-            warning ("Failed to show help: %s", error.message);
+        string type = ((!) gvariant).get_string ();
+//        game_type_action.set_state ((!) gvariant);
+        switch (type)
+        {
+            case "human"    : Prefs.instance.settings.set_int    ("num-players", 1); 
new_game_screen.update_sensitivity (true);
+                              Prefs.instance.settings.set_string ("first-player", "human");                  
                    return;
+            case "computer" : Prefs.instance.settings.set_int    ("num-players", 1); 
new_game_screen.update_sensitivity (true);
+                              Prefs.instance.settings.set_string ("first-player", "computer");               
                    return;
+            case "two"      : Prefs.instance.settings.set_int    ("num-players", 2); 
new_game_screen.update_sensitivity (false); return;
+            default: assert_not_reached ();
         }
     }
 
-    private inline void check_game_state ()
+    private inline void on_game_scores (/* SimpleAction action, Variant? parameter */)
     {
-        if (game_board.is_line_at ((Tile) player, row, column))
-        {
-            gameover = true;
-            winner = player;
-            if (one_player_game)
-                play_sound (is_player_human () ? SoundID.YOU_WIN : SoundID.I_WIN);
-            else
-                play_sound (SoundID.PLAYER_WIN);
-            window.allow_hint (false);
-            blink_winner (6);
-        }
-        else if (moves == 42)
-        {
-            gameover = true;
-            winner = NOBODY;
-            play_sound (SoundID.DRAWN_GAME);
-        }
+        scorebox.present ();
+        return;
     }
 
-    protected override void startup ()
+    private inline void on_game_exit (/* SimpleAction action, Variant? parameter */)
     {
-        base.startup ();
-
-        /* UI parts */
-        new_game_screen = new NewGameScreen ();
-        new_game_screen.show ();
-
-        game_board_view = new GameBoardView (game_board);
-        game_board_view.show ();
-
-        GLib.Menu app_menu = new GLib.Menu ();
-
-        GLib.Menu appearance_menu = new GLib.Menu ();
-        for (uint8 i = 0; i < theme.length; i++)     // TODO default theme
-            appearance_menu.append (theme_get_title (i), @"app.theme-id($i)");
-        appearance_menu.freeze ();
-
-        GLib.Menu section = new GLib.Menu ();
-        /* Translators: hamburger menu entry; "Appearance" submenu (with a mnemonic that appears pressing 
Alt) */
-        section.append_submenu (_("A_ppearance"), (!) appearance_menu);
-
-        section.append (_("Sound"), "app.sound");
-        section.freeze ();
-        app_menu.append_section (null, section);
-
-        section = new GLib.Menu ();
-        section.append (_("_Scores"), "app.scores");
-        section.freeze ();
-        app_menu.append_section (null, section);
-
-        section = new GLib.Menu ();
-        section.append (_("_Help"), "app.help");
-        section.append (_("_About Four-in-a-row"), "app.about");
-        section.freeze ();
-        app_menu.append_section (null, section);
-
-        app_menu.freeze ();
-
-        MenuButton history_button_1 = new HistoryButton (/* direction: down */ false);
-        MenuButton history_button_2 = new HistoryButton (/* direction: up   */ true);
-
-        /* Window */
-        window = new GameWindow ("/org/gnome/Four-in-a-row/ui/four-in-a-row.css",
-                                 PROGRAM_NAME,
-                                 /* start_now */ true,
-                                 GameWindowFlags.SHOW_START_BUTTON,
-                                 (Box) new_game_screen,
-                                 game_board_view,
-                                 app_menu,
-                                 history_button_1,
-                                 history_button_2);
-
-        scorebox = new Scorebox (window, this);
-
-        add_actions ();
-
-        /* various */
-        game_board_view.column_clicked.connect (column_clicked_cb);
-        window.key_press_event.connect (on_key_press);
-
-        window.play.connect (on_game_new);
-        window.undo.connect (on_game_undo);
-        window.hint.connect (on_game_hint);
-
-        window.allow_hint (false);
-        window.allow_undo (false);
-
-        add_window (window);
+        stop_anim ();
+        quit ();
     }
 
+    /*\
+    * * game interaction
+    \*/
+
     private inline bool on_key_press (Gdk.EventKey e)
     {
         if (player_active
@@ -876,7 +836,67 @@ private class FourInARow : Gtk.Application
         return true;
     }
 
-    /* Sound-related methods */
+    private inline bool column_clicked_cb (int column)
+    {
+        if (player_active)
+            return false;
+
+        if (gameover && timeout == 0)
+            blink_winner (2);
+        else if (is_player_human () && timeout == 0)
+            process_move (column);
+        return true;
+    }
+
+    /*\
+    * * help and about
+    \*/
+
+    private inline void on_help_about (/* SimpleAction action, Variant? parameter */)
+    {
+        const string authors [] = {
+            "Tim Musson <trmusson ihug co nz>",
+            "David Neary <bolsh gimp org>",
+            "Nikhar Agrawal <nikharagrawal2006 gmail com>",
+            "Jacob Humphrey <jacob ryan humphrey gmail com>"
+        };
+
+        const string artists [] = {
+            "Alan Horkan",
+            "Anatol Drlicek",
+            "Based on the Faenza icon theme by Matthieu James"
+        };
+
+        const string documenters[] = { "Timothy Musson" };
+
+        show_about_dialog (window,
+            name: PROGRAM_NAME,
+            version: VERSION,
+            copyright: "Copyright © 1999–2008 Tim Musson and David Neary\n" +
+                       "Copyright © 2014 Michael Catanzaro\n" +
+                       "Copyright © 2018 Jacob Humphrey",
+            license_type: License.GPL_3_0,
+            comments: _("Connect four in a row to win"),
+            authors: authors,
+            documenters: documenters,
+            artists: artists,
+            translator_credits: _("translator-credits"),
+            logo_icon_name: "org.gnome.Four-in-a-row",
+            website: "https://wiki.gnome.org/Apps/Four-in-a-row";);
+    }
+
+    private inline void on_help_contents (/* SimpleAction action, Variant? parameter */)
+    {
+        try {
+            show_uri_on_window (window, "help:four-in-a-row", get_current_event_time ());
+        } catch (Error error) {
+            warning ("Failed to show help: %s", error.message);
+        }
+    }
+
+    /*\
+    * * sound
+    \*/
 
     private GSound.Context sound_context;
     private SoundContextState sound_context_state = SoundContextState.INITIAL;
@@ -963,13 +983,6 @@ private enum PlayerID {
     NOBODY;
 }
 
-private enum Level {
-    HUMAN,
-    WEAK,
-    MEDIUM,
-    STRONG;
-}
-
 private enum Tile {
     PLAYER1 = 0,
     PLAYER2,


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