[gnome-nibbles/arnaudb/fix-level-25: 5/8] Allow custom Game size.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-nibbles/arnaudb/fix-level-25: 5/8] Allow custom Game size.
- Date: Mon, 29 Jun 2020 17:09:07 +0000 (UTC)
commit 7d49d214bc907f68524379d330d3fd3e98a36546
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Tue Jun 23 15:48:48 2020 +0200
Allow custom Game size.
src/nibbles-game.vala | 32 +++++++++---------
src/nibbles-test.vala | 6 ++--
src/nibbles-view.vala | 25 +++++++-------
src/nibbles-window.vala | 6 ++--
src/worm.vala | 88 ++++++++++++++++++++++++++++++-------------------
5 files changed, 92 insertions(+), 65 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index c00d902..c4b567d 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -34,10 +34,6 @@ private class NibblesGame : Object
internal const int MAX_SPEED = 4;
- internal const int WIDTH = 92;
- internal const int HEIGHT = 66;
- internal const int CAPACITY = WIDTH * HEIGHT;
-
internal const char EMPTYCHAR = 'a';
internal const char WORMCHAR = 'w'; // only used in worm.vala
internal const char WARPCHAR = 'W'; // only used in warp.vala
@@ -49,7 +45,10 @@ private class NibblesGame : Object
public int speed { internal get; internal construct set; }
/* Board data */
- internal int[,] board = new int[WIDTH, HEIGHT];
+ internal int[,] board;
+
+ public int width { internal get; protected construct; }
+ public int height { internal get; protected construct; }
/* Worms data */
internal int numhumans { internal get; internal set; }
@@ -81,20 +80,21 @@ private class NibblesGame : Object
construct
{
+ board = new int [width, height];
warp_manager.warp_added.connect ((warp) => warp_added (warp.x, warp.y));
boni.bonus_removed.connect ((bonus) => bonus_removed (bonus));
}
- internal NibblesGame (int start_level, int speed, bool fakes, bool no_random = false)
+ internal NibblesGame (int start_level, int speed, bool fakes, int width, int height, bool no_random =
false)
{
- Object (start_level: start_level, current_level: start_level, speed: speed, fakes: fakes);
+ Object (start_level: start_level, current_level: start_level, speed: speed, fakes: fakes, width:
width, height: height);
Random.set_seed (no_random ? 42 : (uint32) time_t ());
}
internal bool load_board (string [] future_board)
{
- if (future_board.length != NibblesGame.HEIGHT)
+ if (future_board.length != height)
return false;
boni.reset (numworms);
@@ -102,12 +102,12 @@ private class NibblesGame : Object
string tmpboard;
int count = 0;
- for (int i = 0; i < NibblesGame.HEIGHT; i++)
+ for (int i = 0; i < height; i++)
{
tmpboard = future_board [i];
- if (tmpboard.char_count () != NibblesGame.WIDTH)
+ if (tmpboard.char_count () != width)
return false;
- for (int j = 0; j < NibblesGame.WIDTH; j++)
+ for (int j = 0; j < width; j++)
{
unichar char_value = tmpboard.get_char (tmpboard.index_of_nth_char (j));
switch (char_value)
@@ -351,7 +351,7 @@ private class NibblesGame : Object
numworms = numai + numhumans;
for (int i = 0; i < numworms; i++)
{
- var worm = new Worm (i);
+ var worm = new Worm (i, width, height);
worm.bonus_found.connect (bonus_found_cb);
worm.warp_found.connect (warp_found_cb);
worm.is_human = (i < numhumans);
@@ -458,8 +458,8 @@ private class NibblesGame : Object
do
{
good = true;
- x = Random.int_range (0, WIDTH - 1);
- y = Random.int_range (0, HEIGHT - 1);
+ x = Random.int_range (0, width - 1);
+ y = Random.int_range (0, height - 1);
if (board[x, y] != EMPTYCHAR)
good = false;
@@ -481,8 +481,8 @@ private class NibblesGame : Object
{
good = true;
- x = Random.int_range (0, WIDTH - 1);
- y = Random.int_range (0, HEIGHT - 1);
+ x = Random.int_range (0, width - 1);
+ y = Random.int_range (0, height - 1);
if (board[x, y] != EMPTYCHAR)
good = false;
if (board[x + 1, y] != EMPTYCHAR)
diff --git a/src/nibbles-test.vala b/src/nibbles-test.vala
index f0ffc9a..4fca8a8 100644
--- a/src/nibbles-test.vala
+++ b/src/nibbles-test.vala
@@ -41,7 +41,7 @@ namespace NibblesTest
private static void test_games ()
{
- NibblesGame game = new NibblesGame (/* start level */ 1, /* speed */ 0, /* fakes */ false, /* no
random */ true);
+ NibblesGame game = new NibblesGame (/* start level */ 1, /* speed */ 0, /* fakes */ false,
level_008_width, level_008_height, /* no random */ true);
game.numhumans = 0;
game.numai = 4;
@@ -83,7 +83,9 @@ namespace NibblesTest
assert_true (game.worms.@get (3).score == 16);
}
- private const string [] level_008 = {
+ private const int level_008_width = 92;
+ private const int level_008_height = 66;
+ private const string [] level_008 = {
"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛........┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓",
"┃..........................................................................................┃",
"┃..........................................................................................┃",
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 60c876d..c626395 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -110,6 +110,9 @@ private class WarpTexture: GtkClutter.Texture
private class NibblesView : GtkClutter.Embed
{
+ internal const int WIDTH = 92;
+ internal const int HEIGHT = 66;
+
private const int MINIMUM_TILE_SIZE = 7;
public int tile_size { internal get; protected construct set; }
@@ -157,8 +160,8 @@ private class NibblesView : GtkClutter.Embed
Clutter.Color stage_color = { 0x00, 0x00, 0x00, 0xff };
stage.set_background_color (stage_color);
- set_size_request (MINIMUM_TILE_SIZE * NibblesGame.WIDTH,
- MINIMUM_TILE_SIZE * NibblesGame.HEIGHT);
+ set_size_request (MINIMUM_TILE_SIZE * WIDTH,
+ MINIMUM_TILE_SIZE * HEIGHT);
load_pixmap ();
}
@@ -170,11 +173,11 @@ private class NibblesView : GtkClutter.Embed
/* Compute the new tile size based on the size of the
* drawing area, rounded down.
*/
- ts_x = event.width / NibblesGame.WIDTH;
- ts_y = event.height / NibblesGame.HEIGHT;
- if (ts_x * NibblesGame.WIDTH > event.width)
+ ts_x = event.width / WIDTH;
+ ts_y = event.height / HEIGHT;
+ if (ts_x * WIDTH > event.width)
ts_x--;
- if (ts_y * NibblesGame.HEIGHT > event.height)
+ if (ts_y * HEIGHT > event.height)
ts_y--;
new_tile_size = int.min (ts_x, ts_y);
@@ -183,7 +186,7 @@ private class NibblesView : GtkClutter.Embed
if (tile_size != new_tile_size)
{
- get_stage ().set_size (new_tile_size * NibblesGame.WIDTH, new_tile_size * NibblesGame.HEIGHT);
+ get_stage ().set_size (new_tile_size * WIDTH, new_tile_size * HEIGHT);
board_rescale (new_tile_size);
boni_rescale (new_tile_size);
@@ -246,10 +249,10 @@ private class NibblesView : GtkClutter.Embed
/* Load wall_pixmaps onto the surface */
int x_pos, y_pos;
GtkClutter.Texture? tmp;
- for (int i = 0; i < NibblesGame.HEIGHT; i++)
+ for (int i = 0; i < HEIGHT; i++)
{
y_pos = i * tile_size;
- for (int j = 0; j < NibblesGame.WIDTH; j++)
+ for (int j = 0; j < WIDTH; j++)
{
tmp = null;
try
@@ -470,8 +473,8 @@ private class NibblesView : GtkClutter.Embed
if (level == null)
return;
- board_width = NibblesGame.WIDTH * new_tile_size;
- board_height = NibblesGame.HEIGHT * new_tile_size;
+ board_width = WIDTH * new_tile_size;
+ board_height = HEIGHT * new_tile_size;
foreach (var actor in level.get_children ())
{
diff --git a/src/nibbles-window.vala b/src/nibbles-window.vala
index 521ac68..79e51b9 100644
--- a/src/nibbles-window.vala
+++ b/src/nibbles-window.vala
@@ -119,7 +119,9 @@ private class NibblesWindow : ApplicationWindow
/* Create game */
game = new NibblesGame (settings.get_int ("start-level"),
settings.get_int ("speed"),
- settings.get_boolean ("fakes"));
+ settings.get_boolean ("fakes"),
+ NibblesView.WIDTH,
+ NibblesView.HEIGHT);
game.log_score.connect (log_score_cb);
game.level_completed.connect (level_completed_cb);
game.notify["is-paused"].connect (() => {
@@ -135,7 +137,7 @@ private class NibblesWindow : ApplicationWindow
!settings.get_boolean ("sound"));
view.show ();
- frame = new Games.GridFrame (NibblesGame.WIDTH, NibblesGame.HEIGHT);
+ frame = new Games.GridFrame (NibblesView.WIDTH, NibblesView.HEIGHT);
game_box.pack_start (frame);
/* Create scoreboard */
diff --git a/src/worm.vala b/src/worm.vala
index 584e120..445b160 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -138,9 +138,19 @@ private class Worm : Object
internal signal void bonus_found ();
internal signal void warp_found ();
- internal Worm (int id)
+ public int width { private get; protected construct; }
+ public int height { private get; protected construct; }
+ public int capacity { private get; protected construct; }
+
+ construct
+ {
+ deadend_board = new uint [width, height];
+ }
+
+ internal Worm (int id, int width, int height)
{
- Object (id: id);
+ int capacity = width * height;
+ Object (id: id, width: width, height: height, capacity: capacity);
}
internal void set_start (int xhead, int yhead, WormDirection direction)
@@ -171,24 +181,24 @@ private class Worm : Object
case WormDirection.UP:
position.y = --head.y;
if (position.y < 0)
- position.y = NibblesGame.HEIGHT - 1;
+ position.y = height - 1;
break;
case WormDirection.DOWN:
position.y = ++head.y;
- if (position.y >= NibblesGame.HEIGHT)
+ if (position.y >= height)
position.y = 0;
break;
case WormDirection.LEFT:
position.x = --head.x;
if (position.x < 0)
- position.x = NibblesGame.WIDTH - 1;
+ position.x = width - 1;
break;
case WormDirection.RIGHT:
position.x = ++head.x;
- if (position.x >= NibblesGame.WIDTH)
+ if (position.x >= width)
position.x = 0;
break;
@@ -376,24 +386,24 @@ private class Worm : Object
case WormDirection.UP:
position.y = --head.y;
if (position.y < 0)
- position.y = NibblesGame.HEIGHT - 1;
+ position.y = height - 1;
break;
case WormDirection.DOWN:
position.y = ++head.y;
- if (position.y >= NibblesGame.HEIGHT)
+ if (position.y >= height)
position.y = 0;
break;
case WormDirection.LEFT:
position.x = --head.x;
if (position.x < 0)
- position.x = NibblesGame.WIDTH - 1;
+ position.x = width - 1;
break;
case WormDirection.RIGHT:
position.x = ++head.x;
- if (position.x >= NibblesGame.WIDTH)
+ if (position.x >= width)
position.x = 0;
break;
@@ -504,19 +514,22 @@ private class Worm : Object
* after 4 billion steps the entire board is likely to have been
* overwritten anyway.
*/
- private static uint[,] deadend_board = new uint[NibblesGame.WIDTH, NibblesGame.HEIGHT];
+ private static uint[,] deadend_board;
private static uint deadend_runnumber = 0;
private static int ai_deadend (int[,] board, int numworms, int x, int y, int length_left)
{
- if (x >= NibblesGame.WIDTH)
+ uint8 width = (uint8) /* int */ board.length [0];
+ uint8 height = (uint8) /* int */ board.length [1];
+
+ if (x >= width)
x = 0;
else if (x < 0)
- x = NibblesGame.WIDTH - 1;
- if (y >= NibblesGame.HEIGHT)
+ x = width - 1;
+ if (y >= height)
y = 0;
else if (y < 0)
- y = NibblesGame.HEIGHT - 1;
+ y = height - 1;
if (length_left <= 0)
return 0;
@@ -534,14 +547,14 @@ private class Worm : Object
default: assert_not_reached ();
}
- if (cx >= NibblesGame.WIDTH)
+ if (cx >= width)
cx = 0;
else if (cx < 0)
- cx = NibblesGame.WIDTH - 1;
- if (cy >= NibblesGame.HEIGHT)
+ cx = width - 1;
+ if (cy >= height)
cy = 0;
else if (cy < 0)
- cy = NibblesGame.HEIGHT - 1;
+ cy = height - 1;
if ((board[cx, cy] <= NibblesGame.EMPTYCHAR
|| board[x, y] >= 'z' + numworms)
@@ -570,7 +583,11 @@ private class Worm : Object
{
int cx, cy, cl, i;
- if (x < 0 || x >= NibblesGame.WIDTH || y < 0 || y >= NibblesGame.HEIGHT)
+ uint8 width = (uint8) /* int */ board.length [0];
+ uint8 height = (uint8) /* int */ board.length [1];
+
+ if (x < 0 || x >= width
+ || y < 0 || y >= height)
return 0;
++deadend_runnumber;
@@ -585,9 +602,9 @@ private class Worm : Object
deadend_board[cx - 1, cy] = deadend_runnumber;
if (cy > 0)
deadend_board[cx, cy - 1] = deadend_runnumber;
- if (cx < NibblesGame.WIDTH - 1)
+ if (cx < width - 1)
deadend_board[cx + 1, cy] = deadend_runnumber;
- if (cy < NibblesGame.HEIGHT - 1)
+ if (cy < height - 1)
deadend_board[cx, cy + 1] = deadend_runnumber;
}
}
@@ -612,21 +629,21 @@ private class Worm : Object
assert_not_reached ();
}
- if (cx >= NibblesGame.WIDTH)
+ if (cx >= width)
cx = 0;
else if (cx < 0)
- cx = NibblesGame.WIDTH - 1;
- if (cy >= NibblesGame.HEIGHT)
+ cx = width - 1;
+ if (cy >= height)
cy = 0;
else if (cy < 0)
- cy = NibblesGame.HEIGHT - 1;
+ cy = height - 1;
deadend_board[x, y] = deadend_runnumber;
deadend_board[cx, cy] = deadend_runnumber;
cl = (length * length) / 16;
- if (cl < NibblesGame.WIDTH)
- cl = NibblesGame.WIDTH;
+ if (cl < width)
+ cl = width;
return Worm.ai_deadend (board, numworms, cx, cy, cl);
}
@@ -671,6 +688,9 @@ private class Worm : Object
private static bool ai_wander (int[,] board, int numworms, int x, int y, WormDirection direction, int
ox, int oy)
{
+ uint8 width = (uint8) /* int */ board.length [0];
+ uint8 height = (uint8) /* int */ board.length [1];
+
switch (direction)
{
case WormDirection.UP:
@@ -689,14 +709,14 @@ private class Worm : Object
assert_not_reached ();
}
- if (x >= NibblesGame.WIDTH)
+ if (x >= width)
x = 0;
else if (x < 0)
- x = NibblesGame.WIDTH - 1;
- if (y >= NibblesGame.HEIGHT)
+ x = width - 1;
+ if (y >= height)
y = 0;
else if (y < 0)
- y = NibblesGame.HEIGHT - 1;
+ y = height - 1;
switch (board[x, y] - 'A')
{
@@ -762,7 +782,7 @@ private class Worm : Object
*/
WormDirection prev_dir = direction;
WormDirection best_dir = NONE;
- int best_yet = NibblesGame.CAPACITY * 2;
+ int best_yet = capacity * 2;
int this_len;
for (int dir = 1; dir <= 4; dir++)
@@ -775,7 +795,7 @@ private class Worm : Object
this_len = 0;
if (!can_move_to (board, numworms))
- this_len += NibblesGame.CAPACITY;
+ this_len += capacity;
if (ai_too_close (worms, numworms))
this_len += 4;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]