[gnome-nibbles/arnaudb/rework-warps: 18/18] Add warps tests.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-nibbles/arnaudb/rework-warps: 18/18] Add warps tests.
- Date: Mon, 29 Jun 2020 17:18:29 +0000 (UTC)
commit 04d033e559ca05d45755607efd83a1f26692b489
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Fri Jun 26 18:37:48 2020 +0200
Add warps tests.
And make them work.
src/nibbles-game.vala | 13 +++++-
src/nibbles-test.vala | 112 ++++++++++++++++++++++++++++++++++++++++++++++++--
src/worm.vala | 9 ++--
3 files changed, 123 insertions(+), 11 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 84a1793..fedb955 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -416,7 +416,15 @@ private class NibblesGame : Object
|| worm.list.is_empty)
continue;
- if (!worm.can_move_to (board, numworms))
+ Position position = worm.position_move ();
+ int target_x;
+ int target_y;
+ if (warp_manager.get_warp_target (position.x, position.y,
+ /* horizontal */ worm.direction == WormDirection.LEFT || worm.direction ==
WormDirection.RIGHT,
+ out target_x, out target_y))
+ position = Position () { x = target_x, y = target_y };
+
+ if (!worm.can_move_to (board, numworms, position))
dead_worms.add (worm);
}
@@ -424,7 +432,8 @@ private class NibblesGame : Object
foreach (var worm in worms)
{
if (worm.is_stopped
- || worm.list.is_empty)
+ || worm.list.is_empty
+ || worm in dead_worms)
continue;
worm.move_part_1 ();
diff --git a/src/nibbles-test.vala b/src/nibbles-test.vala
index 1b0ec08..d20fe85 100644
--- a/src/nibbles-test.vala
+++ b/src/nibbles-test.vala
@@ -29,6 +29,8 @@ namespace NibblesTest
test_games);
Test.add_func ("/Nibbles/test heads",
test_heads);
+ Test.add_func ("/Nibbles/test warps",
+ test_warps);
return Test.run ();
}
@@ -53,7 +55,7 @@ namespace NibblesTest
ulong [] worms_handlers = new ulong [game.worms.size];
foreach (Worm worm in game.worms)
- // FIXME we should not have to connect to anything 1/2
+ // FIXME we should not have to connect to anything 1/3
worms_handlers [worm.id] = worm.finish_added.connect (() => { worm.dematerialize (game.board,
3); worm.is_stopped = false; });
assert_true (game.numworms == 4);
@@ -176,7 +178,7 @@ namespace NibblesTest
_test_heads (test_heads_1, /* worm 0 */ 6, 4, /* worm 1 */ 11, 4, /* lives */ 6, 6);
Test.message ("test heads 2");
- _test_heads (test_heads_2, /* worm 0 */ 6, 4, /* worm 1 */ 11, 4, /* lives */ 6, 5);
+ _test_heads (test_heads_2, /* worm 0 */ 6, 4, /* worm 1 */ 11, 4, /* lives */ 6, 4);
Test.message ("test heads 3");
_test_heads (test_heads_3, /* worm 0 */ 6, 4, /* worm 1 */ 10, 4, /* lives */ 6, 6);
@@ -191,7 +193,7 @@ namespace NibblesTest
_test_heads (test_heads_6, /* worm 0 */ 6, 1, /* worm 1 */ 6, 4, /* lives */ 4, 6);
Test.message ("test heads 9");
- _test_heads (test_heads_9, /* worm 0 */ 6, 1, /* worm 1 */ 6, 4, /* lives */ 6, 5);
+ _test_heads (test_heads_9, /* worm 0 */ 6, 1, /* worm 1 */ 6, 4, /* lives */ 6, 4);
Test.message ("test heads 7");
_test_heads (test_heads_7, /* worm 0 */ 6, 2, /* worm 1 */ 6, 4, /* lives */ 6, 6);
@@ -221,7 +223,7 @@ namespace NibblesTest
ulong [] worms_handlers = new ulong [game.worms.size];
foreach (Worm worm in game.worms)
- // FIXME we should not have to connect to anything 2/2
+ // FIXME we should not have to connect to anything 2/3
worms_handlers [worm.id] = worm.finish_added.connect (() => { worm.dematerialize (game.board,
3); worm.is_stopped = false; });
assert_true (game.numworms == 2);
@@ -343,4 +345,106 @@ namespace NibblesTest
"┃▶...............┃",
"┗━━━━━━━━━━━━━━━━┛"
}; /* expected: 6, 6 */
+
+ /*\
+ * * test warps
+ \*/
+
+ private static void test_warps ()
+ {
+ Test.message ("test warps 1");
+ _test_warps (test_warps_1, /* worm 0 */ 6, 4, /* lives */ 6);
+
+ Test.message ("test warps 2");
+ _test_warps (test_warps_2, /* worm 0 */ 6, 4, /* lives */ 0);
+
+ Test.message ("test warps 3");
+ _test_warps (test_warps_3, /* worm 0 */ 9, 4, /* lives */ 0);
+
+ Test.message ("test warps 4");
+ _test_warps (test_warps_4, /* worm 0 */ 9, 4, /* lives */ 6);
+ }
+
+ private static void _test_warps (string [] board,
+ int worm_0_x,
+ int worm_0_y,
+ int expected)
+ {
+ NibblesGame game = new NibblesGame (/* start level */ 0, /* speed */ 0, /* fakes */ false,
test_warps_width, test_warps_height, /* no random */ true);
+
+ game.numhumans = 0;
+ game.numai = 1;
+ game.create_worms ();
+
+ game.load_board (board, /* regular bonus */ 1);
+
+ ulong [] worms_handlers = new ulong [game.worms.size];
+ foreach (Worm worm in game.worms)
+ // FIXME we should not have to connect to anything 3/3
+ worms_handlers [worm.id] = worm.finish_added.connect (() => { worm.dematerialize (game.board,
3); worm.is_stopped = false; });
+
+ assert_true (game.numworms == 1);
+ assert_true (game.worms.size == 1);
+
+ ulong game_handler_1 = game.bonus_applied.connect ((bonus, worm) => { Test.message (@"worm
$(worm.id) took bonus at [$(bonus.x), $(bonus.y)]"); });
+
+ game.add_worms ();
+ game.start (/* add initial bonus */ true);
+
+ assert_true (game.worms.@get (0).lives == 6);
+ assert_true (game.worms.@get (0).score == 0);
+ assert_true (game.worms.@get (0).head.x == worm_0_x && game.worms.@get (0).head.y == worm_0_y);
+
+ // run until game is finished
+ bool completed = false;
+ ulong game_handler_2 = game.level_completed.connect (() => { completed = true; });
+ MainContext context = MainContext.@default ();
+ do context.iteration (/* may block */ false);
+ while (!completed && (game.get_game_status () != GameStatus.GAMEOVER));
+
+ assert_true (game.worms.@get (0).lives == expected);
+
+ // FIXME looks like last bonus is not counted...
+ assert_true (game.worms.@get (0).score == 0);
+
+ foreach (Worm worm in game.worms)
+ worm.disconnect (worms_handlers [worm.id]);
+ game.disconnect (game_handler_1);
+ game.disconnect (game_handler_2);
+ }
+
+ private const int test_warps_width = 11;
+ private const int test_warps_height = 6;
+ private const string [] test_warps_1 = {
+ "┏━━┓┏━━┓┏━┓",
+ "┃++┗┛..┗┛.┃",
+ "┃+S....┏━━┫",
+ "┣━━━━━━┛++┃",
+ "┃▶......+S┃",
+ "┗━━━━━━━━━┛"
+ }; /* expected: 6 */
+ private const string [] test_warps_2 = {
+ "┏━━━━━━┓┏━┓",
+ "┃++....┗┛.┃",
+ "┃+S┏┓..┏━━┫",
+ "┣━━┛┗━━┛++┃",
+ "┃▶......+S┃",
+ "┗━━━━━━━━━┛"
+ }; /* expected: 0 */
+ private const string [] test_warps_3 = {
+ "┏━━━━━━┳━━┓",
+ "┃......┃++┃",
+ "┃.━┓...┃+S┃",
+ "┃++┣━━━┻━.┃",
+ "┃+S┃▶.....┃",
+ "┗━━┻━━━━━━┛"
+ }; /* expected: 0 */
+ private const string [] test_warps_4 = {
+ "┏━━━━━━┳━━┓",
+ "┃......┃++┃",
+ "┣━.┃...┃+S┃",
+ "┃++┣━━━┻━.┃",
+ "┃+S┃▶.....┃",
+ "┗━━┻━━━━━━┛"
+ }; /* expected: 6 */
}
diff --git a/src/worm.vala b/src/worm.vala
index a86519d..b8d2ebc 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -272,9 +272,8 @@ private class Worm : Object
direction = (list[0].y > list[1].y) ? WormDirection.DOWN : WormDirection.UP;
}
- internal bool can_move_to (int[,] board, int numworms)
+ internal bool can_move_to (int[,] board, int numworms, Position position)
{
- var position = position_move ();
int next_position = board[position.x, position.y];
if (next_position > NibblesGame.EMPTYCHAR
@@ -363,7 +362,7 @@ private class Worm : Object
finish_added ();
}
- private Position position_move ()
+ internal Position position_move ()
{
Position position = head;
@@ -780,7 +779,7 @@ private class Worm : Object
continue;
this_len = 0;
- if (!can_move_to (board, numworms))
+ if (!can_move_to (board, numworms, position_move ()))
this_len += capacity;
if (ai_too_close (worms, numworms))
@@ -818,7 +817,7 @@ private class Worm : Object
if (opposite == (WormDirection) dir)
continue;
- if (!can_move_to (board, numworms))
+ if (!can_move_to (board, numworms, position_move ()))
direction = (WormDirection) dir;
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]