[lightsoff/wip/gtkview] Refactored common board-view code
- From: Robert Roth <robertroth src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [lightsoff/wip/gtkview] Refactored common board-view code
- Date: Mon, 16 Jul 2018 22:17:54 +0000 (UTC)
commit 63a5f3f077c5f94f148a5965b274de024971fec4
Author: Robert Roth <robert roth off gmail com>
Date: Tue Jul 17 01:16:09 2018 +0300
Refactored common board-view code
src/board-view-clutter.vala | 42 +++++++++++------------------------------
src/board-view-gtk.vala | 46 ++++++++++++---------------------------------
src/board-view.vala | 40 +++++++++++++++++++++++++++++++++++++++
3 files changed, 63 insertions(+), 65 deletions(-)
---
diff --git a/src/board-view-clutter.vala b/src/board-view-clutter.vala
index 5f62fbe..788d65e 100644
--- a/src/board-view-clutter.vala
+++ b/src/board-view-clutter.vala
@@ -150,7 +150,7 @@ public class BoardViewClutter : Clutter.Group, BoardView
}
// Toggle a light and those in each cardinal direction around it.
- private void toggle_light (int x, int y, bool animate = true)
+ public void toggle_light (int x, int y, bool animate = true)
{
if (!playable)
return;
@@ -176,53 +176,33 @@ public class BoardViewClutter : Clutter.Group, BoardView
timeline.start ();
}
- // Pseudorandomly generates and sets the state of each light based on
- // a level number; hopefully this is stable between machines, but that
- // depends on GLib's PRNG stability. Also, provides some semblance of
- // symmetry for some levels.
- public void load_level (int level)
+ public PuzzleGenerator get_puzzle_generator ()
{
- /* We *must* not have level < 1, as the following assumes a nonzero, nonnegative number */
- if (level < 1)
- level = 1;
+ return puzzle_generator;
+ }
+ public void clear_level ()
+ {
/* Clear level */
for (var x = 0; x < size; x++)
for (var y = 0; y < size; y++)
lights[x, y].is_lit = false;
-
- /* Use the same pseudo-random levels */
- Random.set_seed (level);
-
- /* Levels require more and more clicks to make */
- var solution_length = (int) Math.floor (2 * Math.log (level) + 1);
-
- /* Do the moves the player needs to */
- var sol = puzzle_generator.minimal_solution (solution_length);
- for (var x = 0; x < size; x++)
- for (var y = 0; y < size; y++)
- if (sol[x, y])
- toggle_light (x, y, false);
}
- private void find_light (GLib.Object light, out int x, out int y)
+
+ public GLib.Object get_light_at (int x, int y)
{
- x = y = 0;
- for (x = 0; x < size; x++)
- for (y = 0; y < size; y++)
- if (lights[x, y] == light)
- return;
+ return lights[x, y];
}
private bool is_completed ()
{
- var cleared = true;
for (var x = 0; x < size; x++)
for (var y = 0; y < size; y++)
if (lights[x, y].is_lit)
- cleared = false;
+ return false;
- return cleared;
+ return true;
}
public void move_to (int x, int y)
diff --git a/src/board-view-gtk.vala b/src/board-view-gtk.vala
index 2c93700..d741f0c 100644
--- a/src/board-view-gtk.vala
+++ b/src/board-view-gtk.vala
@@ -61,7 +61,7 @@ public class BoardViewGtk : Gtk.Grid, BoardView
// symmetry for some levels.
// Toggle a light and those in each cardinal direction around it.
- private void toggle_light (int x, int y, bool clicked = true)
+ public void toggle_light (int x, int y, bool clicked = true)
{
@foreach((light) => (light as Gtk.ToggleButton).toggled.disconnect (light_toggled_cb));
@@ -82,55 +82,33 @@ public class BoardViewGtk : Gtk.Grid, BoardView
@foreach((light) => (light as Gtk.ToggleButton).toggled.connect (light_toggled_cb));
}
- // Pseudorandomly generates and sets the state of each light based on
- // a level number; hopefully this is stable between machines, but that
- // depends on GLib's PRNG stability. Also, provides some semblance of
- // symmetry for some levels.
- public void load_level (int level)
- {
- /* We *must* not have level < 1, as the following assumes a nonzero, nonnegative number */
- if (level < 1)
- level = 1;
-
- @foreach((light) => (light as Gtk.ToggleButton).toggled.disconnect (light_toggled_cb));
+ public void clear_level ()
+ {
/* Clear level */
for (var x = 0; x < size; x++)
for (var y = 0; y < size; y++)
lights[x, y].active = false;
-
- /* Use the same pseudo-random levels */
- Random.set_seed (level);
-
- /* Levels require more and more clicks to make */
- var solution_length = (int) Math.floor (2 * Math.log (level) + 1);
-
- /* Do the moves the player needs to */
- var sol = puzzle_generator.minimal_solution (solution_length);
- for (var x = 0; x < size; x++)
- for (var y = 0; y < size; y++)
- if (sol[x, y])
- toggle_light (x, y, false);
}
- private void find_light (GLib.Object light, out int x, out int y)
+ public PuzzleGenerator get_puzzle_generator ()
{
- x = y = 0;
- for (x = 0; x < size; x++)
- for (y = 0; y < size; y++)
- if (lights[x, y] == light)
- return;
+ return puzzle_generator;
}
private bool is_completed ()
{
- var cleared = true;
for (var x = 0; x < size; x++)
for (var y = 0; y < size; y++)
if (lights[x, y].active)
- cleared = false;
+ return false;
+
+ return true;
+ }
- return cleared;
+ public GLib.Object get_light_at (int x, int y)
+ {
+ return lights[x, y];
}
public void move_to (int x, int y)
diff --git a/src/board-view.vala b/src/board-view.vala
index a6fce0e..ed62510 100644
--- a/src/board-view.vala
+++ b/src/board-view.vala
@@ -12,6 +12,46 @@ public interface BoardView: GLib.Object {
protected new const int size = 5;
public abstract int get_moves ();
+ public abstract PuzzleGenerator get_puzzle_generator ();
+ public abstract void clear_level ();
+ public abstract void toggle_light (int x, int y, bool user_initiated = true);
+
+ public abstract GLib.Object get_light_at (int x, int y);
+
+ // Pseudorandomly generates and sets the state of each light based on
+ // a level number; hopefully this is stable between machines, but that
+ // depends on GLib's PRNG stability. Also, provides some semblance of
+ // symmetry for some levels.
+ public void load_level (int level)
+ {
+ /* We *must* not have level < 1, as the following assumes a nonzero, nonnegative number */
+ if (level < 1)
+ level = 1;
+
+ clear_level ();
+ /* Use the same pseudo-random levels */
+ Random.set_seed (level);
+
+ /* Levels require more and more clicks to make */
+ var solution_length = (int) Math.floor (2 * Math.log (level) + 1);
+
+ /* Do the moves the player needs to */
+ var sol = get_puzzle_generator ().minimal_solution (solution_length);
+ for (var x = 0; x < size; x++)
+ for (var y = 0; y < size; y++)
+ if (sol[x, y])
+ toggle_light (x, y, false);
+ }
+
+ public void find_light (GLib.Object light, out int x, out int y)
+ {
+ x = y = 0;
+ for (x = 0; x < size; x++)
+ for (y = 0; y < size; y++)
+ if (get_light_at (x, y) == light)
+ return;
+ }
+
public signal void game_won ();
public signal void light_toggled ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]