[gnome-nibbles/arnaudb/issue-16-suggestion] Allow from 2 to 6 worms.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-nibbles/arnaudb/issue-16-suggestion] Allow from 2 to 6 worms.
- Date: Sat, 20 Jun 2020 14:14:06 +0000 (UTC)
commit 22d7a4e55a7c325c3467433fdb83fb38c87b9c90
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Sat Jun 20 15:55:39 2020 +0200
Allow from 2 to 6 worms.
Fixes #16.
data/ui/players.ui | 16 +++---
src/nibbles-window.vala | 7 +--
src/players.vala | 143 +++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 133 insertions(+), 33 deletions(-)
---
diff --git a/data/ui/players.ui b/data/ui/players.ui
index 46c49a0..9e41f20 100644
--- a/data/ui/players.ui
+++ b/data/ui/players.ui
@@ -145,36 +145,36 @@
<property name="spacing">10</property>
<property name="vexpand">True</property>
<child>
- <object class="GtkToggleButton" id="worms4">
+ <object class="GtkToggleButton" id="worms_min">
<property name="visible">True</property>
<property name="use-underline">True</property>
<property name="action-name">players.change-nibbles-number</property>
- <property name="action-target">@i 4</property>
- <property name="sensitive" bind-source="worms4" bind-property="active"
bind-flags="invert-boolean"/>
+ <property name="action-target">@i 2</property>
+ <property name="sensitive" bind-source="worms_min" bind-property="active"
bind-flags="invert-boolean"/>
<style>
<class name="number-box"/>
</style>
</object>
</child>
<child>
- <object class="GtkToggleButton" id="worms5">
+ <object class="GtkToggleButton" id="worms_mid">
<property name="visible">True</property>
<property name="use-underline">True</property>
<property name="action-name">players.change-nibbles-number</property>
- <property name="action-target">@i 5</property>
- <property name="sensitive" bind-source="worms5" bind-property="active"
bind-flags="invert-boolean"/>
+ <property name="action-target">@i 4</property>
+ <property name="sensitive" bind-source="worms_mid" bind-property="active"
bind-flags="invert-boolean"/>
<style>
<class name="number-box"/>
</style>
</object>
</child>
<child>
- <object class="GtkToggleButton" id="worms6">
+ <object class="GtkToggleButton" id="worms_max">
<property name="visible">True</property>
<property name="use-underline">True</property>
<property name="action-name">players.change-nibbles-number</property>
<property name="action-target">@i 6</property>
- <property name="sensitive" bind-source="worms6" bind-property="active"
bind-flags="invert-boolean"/>
+ <property name="sensitive" bind-source="worms_max" bind-property="active"
bind-flags="invert-boolean"/>
<style>
<class name="number-box"/>
</style>
diff --git a/src/nibbles-window.vala b/src/nibbles-window.vala
index 521ac68..1587e07 100644
--- a/src/nibbles-window.vala
+++ b/src/nibbles-window.vala
@@ -147,12 +147,7 @@ private class NibblesWindow : ApplicationWindow
/* Number of worms */
game.numhumans = settings.get_int ("players");
int numai = settings.get_int ("ai");
- if (numai + game.numhumans > NibblesGame.MAX_WORMS
- || numai + game.numhumans < 4)
- {
- numai = 4 - game.numhumans;
- settings.set_int ("ai", numai);
- }
+ Players.sanitize_ai_number (game.numhumans, ref numai);
game.numai = numai;
players.set_values (game.numhumans, numai);
diff --git a/src/players.vala b/src/players.vala
index b81e6ba..1c98d44 100644
--- a/src/players.vala
+++ b/src/players.vala
@@ -21,9 +21,9 @@ using Gtk;
[GtkTemplate (ui = "/org/gnome/nibbles/ui/players.ui")]
private class Players : Box
{
- [GtkChild] private ToggleButton worms4;
- [GtkChild] private ToggleButton worms5;
- [GtkChild] private ToggleButton worms6;
+ [GtkChild] private ToggleButton worms_min;
+ [GtkChild] private ToggleButton worms_mid;
+ [GtkChild] private ToggleButton worms_max;
private SimpleAction nibbles_number_action;
private SimpleAction players_number_action;
@@ -66,7 +66,7 @@ private class Players : Box
{
nibbles_number_action.set_state (players_number + number_of_ais);
players_number_action.set_state (players_number);
- update_buttons_labels ();
+ update_ai_buttons ();
}
internal void get_values (out int players_number, out int number_of_ais)
@@ -82,35 +82,64 @@ private class Players : Box
assert_not_reached ();
_players_number_action.set_state (players_number);
- update_buttons_labels ();
+ update_ai_buttons ();
}
- private void update_buttons_labels ()
+ private void update_ai_buttons ()
{
+ int nibbles_number = nibbles_number_action.get_state ().get_int32 ();
switch (players_number_action.get_state ().get_int32 ())
{
case 1:
- worms4.set_label (_(ai3_label));
- worms5.set_label (_(ai4_label));
- worms6.set_label (_(ai5_label));
+ worms_min.set_label (_(ai1_label));
+ worms_mid.set_label (_(ai3_label));
+ worms_max.set_label (_(ai5_label));
+ if (nibbles_number == 3)
+ nibbles_number_action.set_state (2);
+ else if (nibbles_number == 5)
+ nibbles_number_action.set_state (4);
+ worms_min.set_detailed_action_name ("players.change-nibbles-number(@i 2)");
+ worms_mid.set_detailed_action_name ("players.change-nibbles-number(@i 4)");
+ worms_max.set_detailed_action_name ("players.change-nibbles-number(@i 6)");
break;
case 2:
- worms4.set_label (_(ai2_label));
- worms5.set_label (_(ai3_label));
- worms6.set_label (_(ai4_label));
+ worms_min.set_label (_(ai0_label));
+ worms_mid.set_label (_(ai2_label));
+ worms_max.set_label (_(ai4_label));
+ if (nibbles_number == 3)
+ nibbles_number_action.set_state (2);
+ else if (nibbles_number == 5)
+ nibbles_number_action.set_state (4);
+ worms_min.set_detailed_action_name ("players.change-nibbles-number(@i 2)");
+ worms_mid.set_detailed_action_name ("players.change-nibbles-number(@i 4)");
+ worms_max.set_detailed_action_name ("players.change-nibbles-number(@i 6)");
break;
case 3:
- worms4.set_label (_(ai1_label));
- worms5.set_label (_(ai2_label));
- worms6.set_label (_(ai3_label));
+ worms_min.set_label (_(ai0_label));
+ worms_mid.set_label (_(ai1_label));
+ worms_max.set_label (_(ai3_label));
+ if (nibbles_number == 5)
+ nibbles_number_action.set_state (4);
+ else if (nibbles_number < 3)
+ nibbles_number_action.set_state (3);
+ worms_min.set_detailed_action_name ("players.change-nibbles-number(@i 3)");
+ worms_mid.set_detailed_action_name ("players.change-nibbles-number(@i 4)");
+ worms_max.set_detailed_action_name ("players.change-nibbles-number(@i 6)");
break;
case 4:
- worms4.set_label (_(ai0_label));
- worms5.set_label (_(ai1_label));
- worms6.set_label (_(ai2_label));
+ worms_min.set_label (_(ai0_label));
+ worms_mid.set_label (_(ai1_label));
+ worms_max.set_label (_(ai2_label));
+ if (nibbles_number == 3)
+ nibbles_number_action.set_state (4);
+ else if (nibbles_number < 4)
+ nibbles_number_action.set_state (4);
+ worms_min.set_detailed_action_name ("players.change-nibbles-number(@i 4)");
+ worms_mid.set_detailed_action_name ("players.change-nibbles-number(@i 5)");
+ worms_max.set_detailed_action_name ("players.change-nibbles-number(@i 6)");
break;
default:
@@ -121,8 +150,84 @@ private class Players : Box
private inline void change_nibbles_number (SimpleAction _nibbles_number_action, Variant variant)
{
int nibbles_number = variant.get_int32 ();
- if (nibbles_number < 4 || nibbles_number > 6)
+ if (nibbles_number < 2 || nibbles_number > 6)
assert_not_reached ();
+
+ int players_number = players_number_action.get_state ().get_int32 ();
+ if (nibbles_number == 3 && players_number != 3)
+ assert_not_reached ();
+ if (nibbles_number == 5 && players_number != 4)
+ assert_not_reached ();
+ if (nibbles_number < players_number)
+ assert_not_reached ();
+
_nibbles_number_action.set_state (nibbles_number);
}
+
+ internal static void sanitize_ai_number (int players_number, ref int ai_number)
+ {
+ switch (players_number)
+ {
+ case 1:
+ if (ai_number == 1
+ || ai_number == 3
+ || ai_number == 5)
+ return;
+
+ if (ai_number < 1)
+ ai_number = 1;
+ else if (ai_number == 2)
+ ai_number = 3;
+ else if (ai_number == 4)
+ ai_number = 3;
+ else if (ai_number > 5)
+ ai_number = 5;
+ return;
+
+ case 2:
+ if (ai_number == 0
+ || ai_number == 2
+ || ai_number == 4)
+ return;
+
+ if (ai_number < 0)
+ ai_number = 0;
+ else if (ai_number == 1)
+ ai_number = 2;
+ else if (ai_number == 3)
+ ai_number = 2;
+ else if (ai_number > 4)
+ ai_number = 4;
+ return;
+
+ case 3:
+ if (ai_number == 0
+ || ai_number == 1
+ || ai_number == 3)
+ return;
+
+ if (ai_number < 0)
+ ai_number = 0;
+ else if (ai_number == 2)
+ ai_number = 1;
+ else if (ai_number > 3)
+ ai_number = 3;
+ return;
+
+ case 4:
+ if (ai_number == 0
+ || ai_number == 1
+ || ai_number == 2)
+ return;
+
+ if (ai_number < 0)
+ ai_number = 0;
+ else if (ai_number > 2)
+ ai_number = 2;
+ return;
+
+ default:
+ assert_not_reached ();
+ }
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]