From 653764b4fcd3fd951cb1ca7b7993a1736fd9dda6 Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Mon, 21 Jun 2021 18:13:31 +0200 Subject: merge mainline domain with mvcFeature domain --- domain/src/test/java/mancala/domain/BowlTest.java | 324 +++++++++++++++++++++ .../src/test/java/mancala/domain/PlayerTest.java | 46 +++ 2 files changed, 370 insertions(+) create mode 100644 domain/src/test/java/mancala/domain/BowlTest.java create mode 100644 domain/src/test/java/mancala/domain/PlayerTest.java (limited to 'domain/src/test/java') diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java new file mode 100644 index 0000000..530f628 --- /dev/null +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -0,0 +1,324 @@ +package mancala.domain; + +import org.junit.jupiter.api.*; + +import static org.junit.jupiter.api.Assertions.*; + + +class BowlTest { + + protected void traverseAndCheckBoard(Bowl currentBowl, int position) { + Bowl initialBowl = currentBowl; + int traversedCount = 0; + int currentPosition = 0; + for (int i = 0; i < 14; i++) { + if ((position + traversedCount) > 14) { + // if looping around the board, position = ((start+traversed) - total) + // in other words the amount of bowls that the absolute position is greater than the board's total bowls + // + // Only relevant to check construction btw, also only checking in the case where there are 14 bowls + currentPosition = ((traversedCount + position) - 14); + } else + // Or just use normal position + currentPosition = position + traversedCount; + + // check for kalaha's, and check for smallbowl otherwise + if (currentPosition == 7 || currentPosition == 14) + assertEquals(currentBowl.getClass(), Kalaha.class); + else + assertEquals(currentBowl.getClass(), SmallBowl.class); + + currentBowl = currentBowl.getNextBowl(); + assertNotNull(currentBowl); + traversedCount++; + } + assertSame(initialBowl, currentBowl); + } + + @Nested + @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) + class For_a_normal_mancala_bowl{ + SmallBowl firstSmallBowlPlayer; + + @BeforeEach + public void makeASmallBowlInMancala() { + firstSmallBowlPlayer = new SmallBowl(); + traverseAndCheckBoard(firstSmallBowlPlayer, 1); + } + + @Nested + class GIVEN_its_the_start_of_the_game { + @Test + public void WHEN_before_any_small_bowls_are_played_THEN_has_four_rocks() { + Bowl current = firstSmallBowlPlayer; + for (int i = 0; i < 14; i++) { + current = current.getNextBowl(); + if (current.getClass() == SmallBowl.class) + assertEquals(current.getMyRocks(), 4); + } + assertSame(current, firstSmallBowlPlayer); + } + + @Test + public void WHEN_chosen_by_the_player_that_has_the_turn_THEN_distribute_its_rocks_anti_clockwise() { + int initialRocks = firstSmallBowlPlayer.getMyRocks(); + firstSmallBowlPlayer.play(); + Bowl neighbour = firstSmallBowlPlayer.getNextBowl(); + for (int i = 0; i < initialRocks; i++) { + assertEquals(5, neighbour.getMyRocks()); + neighbour = neighbour.getNextBowl(); + } + assertEquals(4, neighbour.getMyRocks()); + } + } + + @Nested + class GIVEN_the_game_is_in_a_state_where { + + @Test + public void its_not_the_players_turn_WHEN_played_by_the_player_THEN_nothing_happens() { + firstSmallBowlPlayer.getMyOwner().switchTurn(); + int initialRocks = firstSmallBowlPlayer.getMyRocks(); + firstSmallBowlPlayer.play(); + Bowl neighbour = firstSmallBowlPlayer.getNextBowl(); + for (int i = 0; i < initialRocks; i++) { + assertEquals(4, neighbour.getMyRocks()); + neighbour = neighbour.getNextBowl(); + } + assertEquals(4, neighbour.getMyRocks()); + } + + @Test + public void play_can_reach_opponents_kalaha_WHEN_played_by_the_player_THEN_opponents_kalaha_is_skipped() { + SmallBowl playWillSkipFromThisBowl = goToSkippableState(); + int opponentKalahaRocksBefore = firstSmallBowlPlayer.getNextSmallBowlTimes(11).getNextBowl().getMyRocks(); + playWillSkipFromThisBowl.play(); + int opponentKalahaRocksAfter = firstSmallBowlPlayer.getNextSmallBowlTimes(11).getNextBowl().getMyRocks(); + assertEquals(opponentKalahaRocksBefore, opponentKalahaRocksAfter); + } + + @Test + public void the_bowl_is_empty_WHEN_the_player_plays_the_empty_bowl_THEN_nothing_happens() { + firstSmallBowlPlayer.play(); + firstSmallBowlPlayer.getMyOwner().switchTurn(); + assertTrue(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); + firstSmallBowlPlayer.play(); + assertTrue(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); + assertEquals(5, firstSmallBowlPlayer.getNextBowl().getMyRocks()); + } + + @Test + public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_won() { + Player player = firstSmallBowlPlayer.getMyOwner(); + Player opponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6).getMyOwner(); + assertFalse(player.won()); + assertFalse(opponent.won()); + goToEndOfSillyGame(); + assertTrue(player.won()); + assertFalse(opponent.won()); + + } + + @Test + public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_wonOPPONENTVARIATION() { + Player player = firstSmallBowlPlayer.getMyOwner(); + Player opponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6).getMyOwner(); + goToEndOfGameWhereOpponentWins(); + assertFalse(player.won()); + assertTrue(opponent.won()); + } + + @Test + public void the_play_would_skip_past_opponent_kalaha_at_the_last_rock_and_steal_WHEN_played_THEN_should_skip_and_steal_correctly() { + goToSkipAndStealOnLast(); + SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); + assertEquals(3, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + assertEquals(19, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + } + + private void goToSkipAndStealOnLast() { + SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); + firstSmallBowlPlayer.getNextSmallBowlTimes(1).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + firstSmallBowlPlayer.getNextSmallBowlTimes(1).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); + firstSmallBowlOpponent.play(); + firstSmallBowlPlayer.getNextSmallBowlTimes(3).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); + firstSmallBowlOpponent.play(); + // Cheating here, let player go again >:), i'm too dumb too make a loop/skip and steal play happen in fair game + firstSmallBowlOpponent.getMyOwner().switchTurn(); + // Should skip and steal + // this bowls rocks + assertEquals(10, firstSmallBowlOpponent.getNextSmallBowlTimes(3).getMyRocks()); + // End up here by looping around the board, thus skipping + assertEquals(0, firstSmallBowlOpponent.getMyRocks()); + // Thus steal from last bowl on players side + assertEquals(8, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getMyRocks()); + // Result is big kalaha booty + assertEquals(8, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + } + + private void goToEndOfGameWhereOpponentWins() { + goToSkipAndStealOnLast(); + SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + firstSmallBowlPlayer.getNextSmallBowlTimes(1).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + firstSmallBowlPlayer.play(); + firstSmallBowlPlayer.getMyOwner().switchTurn(); + firstSmallBowlPlayer.getNextSmallBowlTimes(3).play(); + firstSmallBowlPlayer.getMyOwner().switchTurn(); + firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); + firstSmallBowlPlayer.getNextSmallBowlTimes(5).play(); + } + + private void goToEndOfSillyGame() { + SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); + + // player + // Best opening + firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); + // Set up for steal move + firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); + assertEquals(2, firstSmallBowlPlayer.getKalaha().getMyRocks()); + + // opponent + // ... worst opening? + firstSmallBowlOpponent.play(); + + // player + assertSame(firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite(), firstSmallBowlPlayer.getKalaha().getNextBowl().getNextBowl()); + firstSmallBowlPlayer.play(); + // Check if i did it properly on paper + assertEquals(9, firstSmallBowlPlayer.getKalaha().getMyRocks()); + assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getMyRocks()); + // assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite().getMyRocks()); + + // opponent + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + + //Player + firstSmallBowlPlayer.getNextSmallBowlTimes(3).play(); + assertEquals(10, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + + // opponent makes stupid move again + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + + // player makes big steal + //assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(10, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); + assertEquals(19, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + + // opponent steals tiny booty + firstSmallBowlOpponent.play(); + assertEquals(3, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + + // player is stalling until the end + firstSmallBowlPlayer.play(); + + // opponent is heading for disaster + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + firstSmallBowlPlayer.play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); + firstSmallBowlPlayer.play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + // everything empty! + for (int i = 0; i < 6; i++) { + assertEquals(0, firstSmallBowlOpponent.getNextSmallBowlTimes(i).getMyRocks()); + } + + } + + private SmallBowl goToSkippableState() { + SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); + + firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); + firstSmallBowlPlayer.getNextSmallBowlTimes(3).play(); + + firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + + firstSmallBowlPlayer.play(); + firstSmallBowlOpponent.play(); + + firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); + + // Playing this bowl should give a skip! + assertTrue(firstSmallBowlPlayer.getNextSmallBowlTimes(5).getMyRocks() >= 8); + return firstSmallBowlPlayer.getNextSmallBowlTimes(5); + } + } + + @Nested + class GIVEN_the_play_ends{ + + @Test + public void in_own_kalaha_WHEN_play_ends_THEN_turn_is_not_switched() { + firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); + assertTrue(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); + } + + @Test + public void in_own_small_bowl_WHEN_play_ends_THEN_turn_is_switched() { + firstSmallBowlPlayer.play(); + assertFalse(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); + } + + @Test + public void in_opponents_small_bowl_WHEN_player_plays_this_bowl_THEN_turn_is_switched() { + firstSmallBowlPlayer.getNextSmallBowlTimes(5).play(); + assertFalse(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); + } + + @Test + public void in_own_empty_small_bowl_and_opposite_has_rocks_WHEN_play_ends_THEN_rocks_of_opposite_plus_last_rock_of_play_are_added_to_kalaha() { + firstSmallBowlPlayer.getNextSmallBowlTimes(5).play(); + SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + assertSame(firstSmallBowlPlayer.getNextSmallBowlTimes(1).getOpposite(), firstSmallBowlPlayer.getKalaha().getSmallBowl().getNextSmallBowlTimes(4)); + // assertSame(firstSmallBowlPlayer.getOpposite(), firstSmallBowlPlayer.getKalaha().getNextSmallBowlTimes(5)); + firstSmallBowlPlayer.play(); + assertEquals(7, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + } + + } + + + } + + @Nested + @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) + class a_kalaha { + + SmallBowl smallBowl; + Kalaha kalaha; + + @BeforeEach + public void makeKalahaInBoard() { + smallBowl = new SmallBowl(); + kalaha = smallBowl.getNextSmallBowlTimes(6).getKalaha(); + } + + @Test + public void exists_in_a_mancala_board() { + traverseAndCheckBoard(kalaha, 14); + } + + @Test + public void has_zero_rocks_when_created() { + Bowl current = kalaha; + for (int i = 0; i < 14; i++) { + current = current.getNextBowl(); + if (current.getClass() == Kalaha.class) + assertEquals(current.getMyRocks(), 0); + } + } + } +} \ No newline at end of file diff --git a/domain/src/test/java/mancala/domain/PlayerTest.java b/domain/src/test/java/mancala/domain/PlayerTest.java new file mode 100644 index 0000000..4de7837 --- /dev/null +++ b/domain/src/test/java/mancala/domain/PlayerTest.java @@ -0,0 +1,46 @@ +package mancala.domain; + +import org.junit.jupiter.api.*; + +import static org.junit.jupiter.api.Assertions.*; + +@DisplayName("A player model ") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +class PlayerTest { + Player player; + Player opponent; + + @BeforeEach + public void let_player_and_oppenent_exist() { + player = new Player(); + opponent = player.getOpponent(); + } + + @Test + public void has_turn_when_created() { + assertTrue(player.hasTheTurn()); + } + + @Test + public void opponent_does_not_have_turn_when_player_models_are_created() { + assertFalse(player.getOpponent().hasTheTurn()); + } + + @Test + public void can_change_turn_when_necessary() { + player.switchTurn(); + assertFalse(player.hasTheTurn()); + assertTrue(player.getOpponent().hasTheTurn()); + } + + @Test + public void knows_when_it_is_the_winner() { + assertFalse(player.won()); + player.isTheWinner(); + assertTrue(player.won()); + assertFalse(opponent.won()); + opponent.isTheWinner(); + assertTrue(opponent.won()); + assertFalse(player.won()); + } +} \ No newline at end of file -- cgit v1.2.3 From 431af32d73a37887fa7d35f371a21260f5e93949 Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Tue, 22 Jun 2021 15:33:33 +0200 Subject: refactoring endgame --- domain/src/test/java/mancala/domain/BowlTest.java | 54 +++--- domain/src/test/java/mancala/domain/FooTest.java | 23 --- .../test/java/mancala/domain/MancalaImplTest.java | 214 +++++++++++++++++++++ 3 files changed, 241 insertions(+), 50 deletions(-) delete mode 100644 domain/src/test/java/mancala/domain/FooTest.java create mode 100644 domain/src/test/java/mancala/domain/MancalaImplTest.java (limited to 'domain/src/test/java') diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java index 530f628..34ca42d 100644 --- a/domain/src/test/java/mancala/domain/BowlTest.java +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -54,21 +54,21 @@ class BowlTest { for (int i = 0; i < 14; i++) { current = current.getNextBowl(); if (current.getClass() == SmallBowl.class) - assertEquals(current.getMyRocks(), 4); + assertEquals(current.getMyStones(), 4); } assertSame(current, firstSmallBowlPlayer); } @Test public void WHEN_chosen_by_the_player_that_has_the_turn_THEN_distribute_its_rocks_anti_clockwise() { - int initialRocks = firstSmallBowlPlayer.getMyRocks(); + int initialRocks = firstSmallBowlPlayer.getMyStones(); firstSmallBowlPlayer.play(); Bowl neighbour = firstSmallBowlPlayer.getNextBowl(); for (int i = 0; i < initialRocks; i++) { - assertEquals(5, neighbour.getMyRocks()); + assertEquals(5, neighbour.getMyStones()); neighbour = neighbour.getNextBowl(); } - assertEquals(4, neighbour.getMyRocks()); + assertEquals(4, neighbour.getMyStones()); } } @@ -78,22 +78,22 @@ class BowlTest { @Test public void its_not_the_players_turn_WHEN_played_by_the_player_THEN_nothing_happens() { firstSmallBowlPlayer.getMyOwner().switchTurn(); - int initialRocks = firstSmallBowlPlayer.getMyRocks(); + int initialRocks = firstSmallBowlPlayer.getMyStones(); firstSmallBowlPlayer.play(); Bowl neighbour = firstSmallBowlPlayer.getNextBowl(); for (int i = 0; i < initialRocks; i++) { - assertEquals(4, neighbour.getMyRocks()); + assertEquals(4, neighbour.getMyStones()); neighbour = neighbour.getNextBowl(); } - assertEquals(4, neighbour.getMyRocks()); + assertEquals(4, neighbour.getMyStones()); } @Test public void play_can_reach_opponents_kalaha_WHEN_played_by_the_player_THEN_opponents_kalaha_is_skipped() { SmallBowl playWillSkipFromThisBowl = goToSkippableState(); - int opponentKalahaRocksBefore = firstSmallBowlPlayer.getNextSmallBowlTimes(11).getNextBowl().getMyRocks(); + int opponentKalahaRocksBefore = firstSmallBowlPlayer.getNextSmallBowlTimes(11).getNextBowl().getMyStones(); playWillSkipFromThisBowl.play(); - int opponentKalahaRocksAfter = firstSmallBowlPlayer.getNextSmallBowlTimes(11).getNextBowl().getMyRocks(); + int opponentKalahaRocksAfter = firstSmallBowlPlayer.getNextSmallBowlTimes(11).getNextBowl().getMyStones(); assertEquals(opponentKalahaRocksBefore, opponentKalahaRocksAfter); } @@ -104,7 +104,7 @@ class BowlTest { assertTrue(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); firstSmallBowlPlayer.play(); assertTrue(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); - assertEquals(5, firstSmallBowlPlayer.getNextBowl().getMyRocks()); + assertEquals(5, firstSmallBowlPlayer.getNextBowl().getMyStones()); } @Test @@ -132,9 +132,9 @@ class BowlTest { public void the_play_would_skip_past_opponent_kalaha_at_the_last_rock_and_steal_WHEN_played_THEN_should_skip_and_steal_correctly() { goToSkipAndStealOnLast(); SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); - assertEquals(3, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(3, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - assertEquals(19, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(19, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); } private void goToSkipAndStealOnLast() { @@ -154,13 +154,13 @@ class BowlTest { firstSmallBowlOpponent.getMyOwner().switchTurn(); // Should skip and steal // this bowls rocks - assertEquals(10, firstSmallBowlOpponent.getNextSmallBowlTimes(3).getMyRocks()); + assertEquals(10, firstSmallBowlOpponent.getNextSmallBowlTimes(3).getMyStones()); // End up here by looping around the board, thus skipping - assertEquals(0, firstSmallBowlOpponent.getMyRocks()); + assertEquals(0, firstSmallBowlOpponent.getMyStones()); // Thus steal from last bowl on players side - assertEquals(8, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getMyRocks()); + assertEquals(8, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getMyStones()); // Result is big kalaha booty - assertEquals(8, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(8, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); } private void goToEndOfGameWhereOpponentWins() { @@ -185,7 +185,7 @@ class BowlTest { firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); // Set up for steal move firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); - assertEquals(2, firstSmallBowlPlayer.getKalaha().getMyRocks()); + assertEquals(2, firstSmallBowlPlayer.getKalaha().getMyStones()); // opponent // ... worst opening? @@ -195,8 +195,8 @@ class BowlTest { assertSame(firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite(), firstSmallBowlPlayer.getKalaha().getNextBowl().getNextBowl()); firstSmallBowlPlayer.play(); // Check if i did it properly on paper - assertEquals(9, firstSmallBowlPlayer.getKalaha().getMyRocks()); - assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getMyRocks()); + assertEquals(9, firstSmallBowlPlayer.getKalaha().getMyStones()); + assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getMyStones()); // assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite().getMyRocks()); // opponent @@ -204,20 +204,20 @@ class BowlTest { //Player firstSmallBowlPlayer.getNextSmallBowlTimes(3).play(); - assertEquals(10, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(10, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); // opponent makes stupid move again firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); // player makes big steal //assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); - assertEquals(10, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(10, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); - assertEquals(19, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(19, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); // opponent steals tiny booty firstSmallBowlOpponent.play(); - assertEquals(3, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(3, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); // player is stalling until the end firstSmallBowlPlayer.play(); @@ -230,7 +230,7 @@ class BowlTest { firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); // everything empty! for (int i = 0; i < 6; i++) { - assertEquals(0, firstSmallBowlOpponent.getNextSmallBowlTimes(i).getMyRocks()); + assertEquals(0, firstSmallBowlOpponent.getNextSmallBowlTimes(i).getMyStones()); } } @@ -251,7 +251,7 @@ class BowlTest { firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); // Playing this bowl should give a skip! - assertTrue(firstSmallBowlPlayer.getNextSmallBowlTimes(5).getMyRocks() >= 8); + assertTrue(firstSmallBowlPlayer.getNextSmallBowlTimes(5).getMyStones() >= 8); return firstSmallBowlPlayer.getNextSmallBowlTimes(5); } } @@ -285,7 +285,7 @@ class BowlTest { assertSame(firstSmallBowlPlayer.getNextSmallBowlTimes(1).getOpposite(), firstSmallBowlPlayer.getKalaha().getSmallBowl().getNextSmallBowlTimes(4)); // assertSame(firstSmallBowlPlayer.getOpposite(), firstSmallBowlPlayer.getKalaha().getNextSmallBowlTimes(5)); firstSmallBowlPlayer.play(); - assertEquals(7, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(7, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); } } @@ -317,7 +317,7 @@ class BowlTest { for (int i = 0; i < 14; i++) { current = current.getNextBowl(); if (current.getClass() == Kalaha.class) - assertEquals(current.getMyRocks(), 0); + assertEquals(current.getMyStones(), 0); } } } diff --git a/domain/src/test/java/mancala/domain/FooTest.java b/domain/src/test/java/mancala/domain/FooTest.java deleted file mode 100644 index fa784d3..0000000 --- a/domain/src/test/java/mancala/domain/FooTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package mancala.domain; - -// Your test class should be in the same -// package as the class you're testing. -// Usually the test directory mirrors the -// main directory 1:1. So for each class in src/main, -// there is a class in src/test. - -// Import our test dependencies. We import the Test-attribute -// and a set of assertions. -import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -public class FooTest { - // Define a test starting with @Test. The test is like - // a small main method - you need to setup everything - // and you can write any arbitrary Java code in it. - @Test - public void aNormalBorlStartsWith4Stones() { - Foo foo = new Foo(); - assertEquals(42, foo.theAnswerToLifeTheUniverseAndEverything()); - } -} \ No newline at end of file diff --git a/domain/src/test/java/mancala/domain/MancalaImplTest.java b/domain/src/test/java/mancala/domain/MancalaImplTest.java new file mode 100644 index 0000000..b040b02 --- /dev/null +++ b/domain/src/test/java/mancala/domain/MancalaImplTest.java @@ -0,0 +1,214 @@ +package mancala.domain; + +import org.junit.jupiter.api.*; + +import java.util.HashSet; + +import static org.junit.jupiter.api.Assertions.*; + +@IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class) +class MancalaImplTest { + + Mancala mancala; + + @BeforeEach + void setUp() { + mancala = new MancalaImpl(); + } + + /** + * Method indicating if the first player has the next turn or not. + * If player 1 is not in turn, then player 2 is in turn. + * + * @param The player which you want to know the turn for. + * @return True if the first player has the next turn, false if it's the turn of the other player. + */ + @Nested + class isPlayersTurn { + @Test + void given_mancala_PLAYER_ONE_when_first_player_has_next_turn_then_return_true() { + assertTrue(mancala.isPlayersTurn(Mancala.PLAYER_ONE)); + } + + @Test + void given_mancala_PLAYER_ONE_when_second_player_has_next_turn_then_return_false() { + playPitAndFailIfNotValid(0); + assertFalse(mancala.isPlayersTurn(Mancala.PLAYER_ONE)); + } + + + @Test + void given_mancala_PLAYER_TWO_when_second_player_has_next_turn_then_return_true() { + playPitAndFailIfNotValid(0); + assertTrue(mancala.isPlayersTurn(Mancala.PLAYER_TWO)); + } + + @Test + void given_mancala_PLAYER_TWO_when_second_player_has_turn_then_return_false() { + assertFalse(mancala.isPlayersTurn(Mancala.PLAYER_TWO)); + } + } + + /** + * Method for playing the specified recess. Index is as specified below: + * + * 12 11 10 9 8 7 + * 13 6 + * 0 1 2 3 4 5 + * + * @param index Index of the recess to be played. + * @return 15 item long Array with the current state of the game. The 15th item indicates which player has the next turn (possible values are 1 or 2). + */ + @Nested + class playPit { + @Test + void given_a_pit_index_from_PLAYER_one_choice_when_pit_doesnt_belong_to_player_one_then_throw_MancalaException() { + for (int index: MancalaImpl.PLAYER_TWO_PITS) { + playPitAndFailIfNoException(index, "Player one could play a pit that was not his without an exception!"); + } + } + + @Test + void given_a_pit_index_from_PLAYER_two_choice_when_pit_doesnt_belong_to_player_two_then_throw_MancalaException() { + assumeTurn(Mancala.PLAYER_ONE); + playPitAndFailIfNotValid(0); + + assumeTurn(Mancala.PLAYER_TWO); + for (int index: MancalaImpl.PLAYER_ONE_PITS) { + playPitAndFailIfNoException(index, "Player two could play a pit that was not his without an exception!"); + } + } + + @Test + void given_a_Kalaha_index_when_playPit_is_called_then_always_throw_MancalaException() { + assumeTurn(Mancala.PLAYER_ONE); + playPitAndFailIfNoException(MancalaImpl.PLAYER_ONE_KALAHA, "Kalaha of player one was played without throwing exception!"); + playPitAndFailIfNoException(MancalaImpl.PLAYER_TWO_KALAHA, "Kalaha of player two was played without throwing exception!"); + + playPitAndFailIfNotValid(0); + + assumeTurn(Mancala.PLAYER_TWO); + playPitAndFailIfNoException(MancalaImpl.PLAYER_ONE_KALAHA, "Kalaha of player one was played without throwing exception!"); + playPitAndFailIfNoException(MancalaImpl.PLAYER_TWO_KALAHA, "Kalaha of player two was played without throwing exception!"); + } + + @Test + void given_that_valid_play_is_made_when_play_is_done_then_players_switch_turns() { + assumeTurn(Mancala.PLAYER_ONE); + playPitAndFailIfNotValid(0); + + assumeNotTurn(Mancala.PLAYER_ONE); + assumeTurn(Mancala.PLAYER_TWO); + } + + @Test + void given_that_pit_has_no_stones_when_play_is_made_by_player_one_then_throw_MancalaException() { + assumeTurn(Mancala.PLAYER_ONE); + playPitAndFailIfNotValid(0); + + assumeTurn(Mancala.PLAYER_TWO); + playPitAndFailIfNotValid(7); + + assumeTurn(Mancala.PLAYER_ONE); + playPitAndFailIfNoException(0, "Didn't throw exception when Pit was empty when played!"); + } + + @Test + void given_that_pit_has_no_stones_when_play_is_made_by_player_two_then_throw_MancalaException() { + assumeTurn(Mancala.PLAYER_ONE); + playPitAndFailIfNotValid(5); + + assumeTurn(Mancala.PLAYER_TWO); + playPitAndFailIfNotValid(7); + + assumeTurn(Mancala.PLAYER_ONE); + playPitAndFailIfNotValid(0); + + assumeTurn(Mancala.PLAYER_TWO); + playPitAndFailIfNoException(7, "Didn't throw exception when Pit was empty when played!"); + } + + } + + @Nested + class getStonesForPit { + @Test + void given_any_Pit_index_when_getStonesForPit_is_called_then_return_stones_of_pits() { + HashSet allPitIndexes = new HashSet<>(MancalaImpl.PLAYER_ONE_PITS); + allPitIndexes.addAll(MancalaImpl.PLAYER_TWO_PITS); + + for (int pitIndex: allPitIndexes) { + assertEquals(4, mancala.getStonesForPit(pitIndex), + "pit should have four stones when created in the domain model."); + } + } + + @Test + void given_any_Kalaha_index_when_getStonesForPit_is_called_then_return_stones_of_Kalaha() { + HashSet allKalahaIndexes = new HashSet<>(); + allKalahaIndexes.add(MancalaImpl.PLAYER_ONE_KALAHA); + allKalahaIndexes.add(MancalaImpl.PLAYER_TWO_KALAHA); + for (int kalahaIndex: allKalahaIndexes) { + assertEquals(0, mancala.getStonesForPit(kalahaIndex), + "Kalaha should have zero stones when created in the domain model."); + } + } + + @Test + void given_that_a_play_has_been_made_and_not_looped_around_board_when_getting_stones_then_should_give_zero() { + assumeTurn(Mancala.PLAYER_ONE); + playPitAndFailIfNotValid(5); + + assertEquals(0, mancala.getStonesForPit(5), + "In this situation play should leave zero rocks!"); + + assumeTurn(Mancala.PLAYER_TWO); + playPitAndFailIfNotValid(7); + + assertEquals(0, mancala.getStonesForPit(7), + "In this situation play should leave zero rocks!"); + } + + } + + @Nested + class isEndOfGame { + void given_the_game_is_not_ended_when_isEndOfGame_is_called_then_return_false() { + assertFalse(mancala.isEndOfGame()); + } + } + + @Nested + class getWinner { + } + + void assumeTurn(int player) { + assertTrue(mancala.isPlayersTurn(player), + "It's not PLAYER " + (player == 1 ? "ONE's" : "TWO's") + " turn!"); + } + + void assumeNotTurn(int player) { + assertFalse(mancala.isPlayersTurn(player), + "It's PLAYER " + (player == 1 ? "ONE's" : "TWO's") + " turn!"); + } + + void playPitAndFailIfNotValid(int index) { + try { + mancala.playPit(index); + } catch (MancalaException e) { + fail("Invalid play."); + } + } + + void playPitAndFailIfNoException(int index, String message) { + try { + mancala.playPit(index); + fail(message); + } catch (MancalaException e) { + } + } + + void notImplementedYet() { + fail("Not implemented yet."); + } +} \ No newline at end of file -- cgit v1.2.3 From 92c61668daefe1486d8ce6aefcf4c60c2eee0c3b Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Tue, 22 Jun 2021 17:11:42 +0200 Subject: refactor(domain tests) <- configurable state --- domain/src/test/java/mancala/domain/BowlTest.java | 547 +++++++++++---------- .../test/java/mancala/domain/MancalaImplTest.java | 23 + 2 files changed, 309 insertions(+), 261 deletions(-) (limited to 'domain/src/test/java') diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java index 34ca42d..7e1ec01 100644 --- a/domain/src/test/java/mancala/domain/BowlTest.java +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -2,323 +2,348 @@ package mancala.domain; import org.junit.jupiter.api.*; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + import static org.junit.jupiter.api.Assertions.*; +@IndicativeSentencesGeneration(separator = " -> ", generator = DisplayNameGenerator.ReplaceUnderscores.class) class BowlTest { - protected void traverseAndCheckBoard(Bowl currentBowl, int position) { - Bowl initialBowl = currentBowl; - int traversedCount = 0; - int currentPosition = 0; - for (int i = 0; i < 14; i++) { - if ((position + traversedCount) > 14) { - // if looping around the board, position = ((start+traversed) - total) - // in other words the amount of bowls that the absolute position is greater than the board's total bowls - // - // Only relevant to check construction btw, also only checking in the case where there are 14 bowls - currentPosition = ((traversedCount + position) - 14); - } else - // Or just use normal position - currentPosition = position + traversedCount; - - // check for kalaha's, and check for smallbowl otherwise - if (currentPosition == 7 || currentPosition == 14) - assertEquals(currentBowl.getClass(), Kalaha.class); - else - assertEquals(currentBowl.getClass(), SmallBowl.class); - - currentBowl = currentBowl.getNextBowl(); - assertNotNull(currentBowl); - traversedCount++; - } - assertSame(initialBowl, currentBowl); - } @Nested - @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) - class For_a_normal_mancala_bowl{ - SmallBowl firstSmallBowlPlayer; - - @BeforeEach - public void makeASmallBowlInMancala() { - firstSmallBowlPlayer = new SmallBowl(); - traverseAndCheckBoard(firstSmallBowlPlayer, 1); - } + class instantiatingDefaultGame { - @Nested - class GIVEN_its_the_start_of_the_game { - @Test - public void WHEN_before_any_small_bowls_are_played_THEN_has_four_rocks() { - Bowl current = firstSmallBowlPlayer; - for (int i = 0; i < 14; i++) { - current = current.getNextBowl(); - if (current.getClass() == SmallBowl.class) - assertEquals(current.getMyStones(), 4); + private void checkIfDefaultState(Bowl currentBowl, int position) { + Bowl initialBowl = currentBowl; + int traversedCount = 0; + int currentPosition = 0; + for (int i = 0; i < 14; i++) { + if ((position + traversedCount) > 14) { + // if looping around the board, position = ((start+traversed) - total) + // in other words the amount of bowls that the absolute position is greater than the board's total bowls + // + // Only relevant to check construction btw, also only checking in the case where there are 14 bowls + currentPosition = ((traversedCount + position) - 14); + } else + // Or just use normal position + currentPosition = position + traversedCount; + + // check for kalaha's, and check for smallbowl otherwise + if (currentPosition == 7 || currentPosition == 14) { + assertEquals(currentBowl.getClass(), Kalaha.class); + assertEquals(0, currentBowl.getMyStones(), + "In a 'normal' game the kalaha should have 0 rocks when created."); + } else { + assertEquals(currentBowl.getClass(), SmallBowl.class); + assertEquals(4, currentBowl.getMyStones(), + "In a 'normal' game the smallbowl should have 4 rocks when created."); } - assertSame(current, firstSmallBowlPlayer); - } - @Test - public void WHEN_chosen_by_the_player_that_has_the_turn_THEN_distribute_its_rocks_anti_clockwise() { - int initialRocks = firstSmallBowlPlayer.getMyStones(); - firstSmallBowlPlayer.play(); - Bowl neighbour = firstSmallBowlPlayer.getNextBowl(); - for (int i = 0; i < initialRocks; i++) { - assertEquals(5, neighbour.getMyStones()); - neighbour = neighbour.getNextBowl(); - } - assertEquals(4, neighbour.getMyStones()); + currentBowl = currentBowl.getNextBowl(); + assertNotNull(currentBowl); + traversedCount++; } + assertSame(initialBowl, currentBowl); + } + + SmallBowl referenceSmallBowl = new SmallBowl(); + + @Test + public void default_stones_amount_check() { + checkIfDefaultState(referenceSmallBowl, 1); } @Nested - class GIVEN_the_game_is_in_a_state_where { + class given_a_small_bowl { + + @Nested + class and_its_the_start_of_the_game { + @Test + public void when_getMyStones_is_called_then_it_returns_four() { + SmallBowl next = referenceSmallBowl.getNextSmallBowl(); + while (!next.equals(referenceSmallBowl)) { + assertEquals(4, next.getMyStones()); + next = next.getNextSmallBowl(); + } + } - @Test - public void its_not_the_players_turn_WHEN_played_by_the_player_THEN_nothing_happens() { - firstSmallBowlPlayer.getMyOwner().switchTurn(); - int initialRocks = firstSmallBowlPlayer.getMyStones(); - firstSmallBowlPlayer.play(); - Bowl neighbour = firstSmallBowlPlayer.getNextBowl(); - for (int i = 0; i < initialRocks; i++) { + @Test + public void when_play_is_called_and_owner_has_turn_then_distribute_its_rocks_anti_clockwise() { + int initialRocks = referenceSmallBowl.getMyStones(); + referenceSmallBowl.play(); + Bowl neighbour = referenceSmallBowl.getNextBowl(); + for (int i = 0; i < initialRocks; i++) { + assertEquals(5, neighbour.getMyStones()); + neighbour = neighbour.getNextBowl(); + } assertEquals(4, neighbour.getMyStones()); - neighbour = neighbour.getNextBowl(); } - assertEquals(4, neighbour.getMyStones()); } - @Test - public void play_can_reach_opponents_kalaha_WHEN_played_by_the_player_THEN_opponents_kalaha_is_skipped() { - SmallBowl playWillSkipFromThisBowl = goToSkippableState(); - int opponentKalahaRocksBefore = firstSmallBowlPlayer.getNextSmallBowlTimes(11).getNextBowl().getMyStones(); - playWillSkipFromThisBowl.play(); - int opponentKalahaRocksAfter = firstSmallBowlPlayer.getNextSmallBowlTimes(11).getNextBowl().getMyStones(); - assertEquals(opponentKalahaRocksBefore, opponentKalahaRocksAfter); - } + @Nested + class and_the_game_is_in_a_state_where { + + @Test + public void its_not_the_players_turn_when_play_is_called_then_nothing_happens() { + referenceSmallBowl.getMyOwner().switchTurn(); + int initialRocks = referenceSmallBowl.getMyStones(); + referenceSmallBowl.play(); + Bowl neighbour = referenceSmallBowl.getNextBowl(); + for (int i = 0; i < initialRocks; i++) { + assertEquals(4, neighbour.getMyStones()); + neighbour = neighbour.getNextBowl(); + } + assertEquals(4, neighbour.getMyStones()); + } - @Test - public void the_bowl_is_empty_WHEN_the_player_plays_the_empty_bowl_THEN_nothing_happens() { - firstSmallBowlPlayer.play(); - firstSmallBowlPlayer.getMyOwner().switchTurn(); - assertTrue(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); - firstSmallBowlPlayer.play(); - assertTrue(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); - assertEquals(5, firstSmallBowlPlayer.getNextBowl().getMyStones()); - } + @Test + public void the_bowl_is_empty_WHEN_the_player_plays_the_empty_bowl_THEN_nothing_happens() { + referenceSmallBowl.play(); + referenceSmallBowl.getMyOwner().switchTurn(); + assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); + referenceSmallBowl.play(); + assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); + assertEquals(5, referenceSmallBowl.getNextBowl().getMyStones()); + } - @Test - public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_won() { - Player player = firstSmallBowlPlayer.getMyOwner(); - Player opponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6).getMyOwner(); - assertFalse(player.won()); - assertFalse(opponent.won()); - goToEndOfSillyGame(); - assertTrue(player.won()); - assertFalse(opponent.won()); + @Test + public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_won() { + Player player = referenceSmallBowl.getMyOwner(); + Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); + assertFalse(player.won()); + assertFalse(opponent.won()); + goToEndOfSillyGame(); + assertTrue(player.won()); + assertFalse(opponent.won()); - } + } - @Test - public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_wonOPPONENTVARIATION() { - Player player = firstSmallBowlPlayer.getMyOwner(); - Player opponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6).getMyOwner(); - goToEndOfGameWhereOpponentWins(); - assertFalse(player.won()); - assertTrue(opponent.won()); - } + @Test + public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_wonOPPONENTVARIATION() { + Player player = referenceSmallBowl.getMyOwner(); + Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); + goToEndOfGameWhereOpponentWins(); + assertFalse(player.won()); + assertTrue(opponent.won()); + } - @Test - public void the_play_would_skip_past_opponent_kalaha_at_the_last_rock_and_steal_WHEN_played_THEN_should_skip_and_steal_correctly() { - goToSkipAndStealOnLast(); - SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); - assertEquals(3, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - assertEquals(19, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - } + @Test + public void the_play_would_skip_past_opponent_kalaha_at_the_last_rock_and_steal_WHEN_played_THEN_should_skip_and_steal_correctly() { + goToSkipAndStealOnLast(); + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + assertEquals(3, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + assertEquals(19, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + } - private void goToSkipAndStealOnLast() { - SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); - firstSmallBowlPlayer.getNextSmallBowlTimes(1).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - firstSmallBowlPlayer.getNextSmallBowlTimes(1).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); - firstSmallBowlOpponent.play(); - firstSmallBowlPlayer.getNextSmallBowlTimes(3).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); - firstSmallBowlOpponent.play(); - // Cheating here, let player go again >:), i'm too dumb too make a loop/skip and steal play happen in fair game - firstSmallBowlOpponent.getMyOwner().switchTurn(); - // Should skip and steal - // this bowls rocks - assertEquals(10, firstSmallBowlOpponent.getNextSmallBowlTimes(3).getMyStones()); - // End up here by looping around the board, thus skipping - assertEquals(0, firstSmallBowlOpponent.getMyStones()); - // Thus steal from last bowl on players side - assertEquals(8, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getMyStones()); - // Result is big kalaha booty - assertEquals(8, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - } + private void goToSkipAndStealOnLast() { + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + referenceSmallBowl.getNextSmallBowlTimes(1).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + referenceSmallBowl.getNextSmallBowlTimes(1).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + firstSmallBowlOpponent.play(); + referenceSmallBowl.getNextSmallBowlTimes(3).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + referenceSmallBowl.getNextSmallBowlTimes(4).play(); + firstSmallBowlOpponent.play(); + // Cheating here, let player go again >:), i'm too dumb too make a loop/skip and steal play happen in fair game + firstSmallBowlOpponent.getMyOwner().switchTurn(); + // Should skip and steal + // this bowls rocks + assertEquals(10, firstSmallBowlOpponent.getNextSmallBowlTimes(3).getMyStones()); + // End up here by looping around the board, thus skipping + assertEquals(0, firstSmallBowlOpponent.getMyStones()); + // Thus steal from last bowl on players side + assertEquals(8, referenceSmallBowl.getNextSmallBowlTimes(5).getMyStones()); + // Result is big kalaha booty + assertEquals(8, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + } - private void goToEndOfGameWhereOpponentWins() { - goToSkipAndStealOnLast(); - SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - firstSmallBowlPlayer.getNextSmallBowlTimes(1).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - firstSmallBowlPlayer.play(); - firstSmallBowlPlayer.getMyOwner().switchTurn(); - firstSmallBowlPlayer.getNextSmallBowlTimes(3).play(); - firstSmallBowlPlayer.getMyOwner().switchTurn(); - firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); - firstSmallBowlPlayer.getNextSmallBowlTimes(5).play(); - } + private void goToEndOfGameWhereOpponentWins() { + goToSkipAndStealOnLast(); + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + referenceSmallBowl.getNextSmallBowlTimes(1).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + referenceSmallBowl.play(); + referenceSmallBowl.getMyOwner().switchTurn(); + referenceSmallBowl.getNextSmallBowlTimes(3).play(); + referenceSmallBowl.getMyOwner().switchTurn(); + referenceSmallBowl.getNextSmallBowlTimes(4).play(); + referenceSmallBowl.getNextSmallBowlTimes(5).play(); + } + + private void goToEndOfSillyGame() { + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + + // player + // Best opening + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + // Set up for steal move + referenceSmallBowl.getNextSmallBowlTimes(4).play(); + assertEquals(2, referenceSmallBowl.getKalaha().getMyStones()); + + // opponent + // ... worst opening? + firstSmallBowlOpponent.play(); + + // player + assertSame(referenceSmallBowl.getNextSmallBowlTimes(4).getOpposite(), referenceSmallBowl.getKalaha().getNextBowl().getNextBowl()); + referenceSmallBowl.play(); + // Check if i did it properly on paper + assertEquals(9, referenceSmallBowl.getKalaha().getMyStones()); + assertEquals(0, referenceSmallBowl.getNextSmallBowlTimes(4).getMyStones()); + // assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite().getMyRocks()); + + // opponent + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + + //Player + referenceSmallBowl.getNextSmallBowlTimes(3).play(); + assertEquals(10, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + + // opponent makes stupid move again + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + + // player makes big steal + //assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(10, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + assertEquals(19, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + + // opponent steals tiny booty + firstSmallBowlOpponent.play(); + assertEquals(3, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + + // player is stalling until the end + referenceSmallBowl.play(); + + // opponent is heading for disaster + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + referenceSmallBowl.play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); + referenceSmallBowl.play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + // everything empty! + for (int i = 0; i < 6; i++) { + assertEquals(0, firstSmallBowlOpponent.getNextSmallBowlTimes(i).getMyStones()); + } - private void goToEndOfSillyGame() { - SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); - - // player - // Best opening - firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); - // Set up for steal move - firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); - assertEquals(2, firstSmallBowlPlayer.getKalaha().getMyStones()); - - // opponent - // ... worst opening? - firstSmallBowlOpponent.play(); - - // player - assertSame(firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite(), firstSmallBowlPlayer.getKalaha().getNextBowl().getNextBowl()); - firstSmallBowlPlayer.play(); - // Check if i did it properly on paper - assertEquals(9, firstSmallBowlPlayer.getKalaha().getMyStones()); - assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getMyStones()); - // assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite().getMyRocks()); - - // opponent - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - - //Player - firstSmallBowlPlayer.getNextSmallBowlTimes(3).play(); - assertEquals(10, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - - // opponent makes stupid move again - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - - // player makes big steal - //assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); - assertEquals(10, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); - assertEquals(19, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - - // opponent steals tiny booty - firstSmallBowlOpponent.play(); - assertEquals(3, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - - // player is stalling until the end - firstSmallBowlPlayer.play(); - - // opponent is heading for disaster - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - firstSmallBowlPlayer.play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); - firstSmallBowlPlayer.play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - // everything empty! - for (int i = 0; i < 6; i++) { - assertEquals(0, firstSmallBowlOpponent.getNextSmallBowlTimes(i).getMyStones()); } + private SmallBowl goToSkippableState() { + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + referenceSmallBowl.getNextSmallBowlTimes(3).play(); + + firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + + referenceSmallBowl.play(); + firstSmallBowlOpponent.play(); + + referenceSmallBowl.getNextSmallBowlTimes(4).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); + + // Playing this bowl should give a skip! + assertTrue(referenceSmallBowl.getNextSmallBowlTimes(5).getMyStones() >= 8); + return referenceSmallBowl.getNextSmallBowlTimes(5); + } } - private SmallBowl goToSkippableState() { - SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); + @Nested + class GIVEN_the_play_ends{ - firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); - firstSmallBowlPlayer.getNextSmallBowlTimes(3).play(); + @Test + public void in_own_kalaha_WHEN_play_ends_THEN_turn_is_not_switched() { + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); + } - firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + @Test + public void in_own_small_bowl_WHEN_play_ends_THEN_turn_is_switched() { + referenceSmallBowl.play(); + assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); + } - firstSmallBowlPlayer.play(); - firstSmallBowlOpponent.play(); + @Test + public void in_opponents_small_bowl_WHEN_player_plays_this_bowl_THEN_turn_is_switched() { + referenceSmallBowl.getNextSmallBowlTimes(5).play(); + assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); + } - firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); + @Test + public void in_own_empty_small_bowl_and_opposite_has_rocks_WHEN_play_ends_THEN_rocks_of_opposite_plus_last_rock_of_play_are_added_to_kalaha() { + referenceSmallBowl.getNextSmallBowlTimes(5).play(); + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + assertSame(referenceSmallBowl.getNextSmallBowlTimes(1).getOpposite(), referenceSmallBowl.getKalaha().getNextSmallBowl().getNextSmallBowlTimes(4)); + // assertSame(firstSmallBowlPlayer.getOpposite(), firstSmallBowlPlayer.getKalaha().getNextSmallBowlTimes(5)); + referenceSmallBowl.play(); + assertEquals(7, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + } - // Playing this bowl should give a skip! - assertTrue(firstSmallBowlPlayer.getNextSmallBowlTimes(5).getMyStones() >= 8); - return firstSmallBowlPlayer.getNextSmallBowlTimes(5); } + + } @Nested - class GIVEN_the_play_ends{ + class given_a_kalaha { - @Test - public void in_own_kalaha_WHEN_play_ends_THEN_turn_is_not_switched() { - firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); - assertTrue(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); - } - - @Test - public void in_own_small_bowl_WHEN_play_ends_THEN_turn_is_switched() { - firstSmallBowlPlayer.play(); - assertFalse(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); - } + Kalaha playerKalaha; + Kalaha opponentKalaha; - @Test - public void in_opponents_small_bowl_WHEN_player_plays_this_bowl_THEN_turn_is_switched() { - firstSmallBowlPlayer.getNextSmallBowlTimes(5).play(); - assertFalse(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); + @BeforeEach + public void makeKalahaInBoard() { + playerKalaha = referenceSmallBowl.getKalaha(); + opponentKalaha = referenceSmallBowl.getKalaha().getNextSmallBowl().getKalaha(); } @Test - public void in_own_empty_small_bowl_and_opposite_has_rocks_WHEN_play_ends_THEN_rocks_of_opposite_plus_last_rock_of_play_are_added_to_kalaha() { - firstSmallBowlPlayer.getNextSmallBowlTimes(5).play(); - SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6); - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - assertSame(firstSmallBowlPlayer.getNextSmallBowlTimes(1).getOpposite(), firstSmallBowlPlayer.getKalaha().getSmallBowl().getNextSmallBowlTimes(4)); - // assertSame(firstSmallBowlPlayer.getOpposite(), firstSmallBowlPlayer.getKalaha().getNextSmallBowlTimes(5)); - firstSmallBowlPlayer.play(); - assertEquals(7, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + public void when_getMyStones_is_called_after_instantiating_then_has_zero_stones() { + assertEquals(0, playerKalaha.getMyStones()); + assertEquals(0, opponentKalaha.getMyStones()); } - } - } - @Nested - @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) - class a_kalaha { - - SmallBowl smallBowl; - Kalaha kalaha; - @BeforeEach - public void makeKalahaInBoard() { - smallBowl = new SmallBowl(); - kalaha = smallBowl.getNextSmallBowlTimes(6).getKalaha(); - } + @Nested + class instantiatingGameWithStonesList { + SmallBowl referenceSmallBowl; @Test - public void exists_in_a_mancala_board() { - traverseAndCheckBoard(kalaha, 14); + void given_a_stones_list_when_instantiating_a_small_bowl_then_configure_stones_as_in_list() { + int[] stonesArray = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14}; + List stonesList = Arrays.stream(stonesArray).boxed().collect(Collectors.toList()); + referenceSmallBowl = new SmallBowl(stonesList); + for (int i = 0; i < stonesArray.length; i++) { + if (i < 6) assertEquals(stonesArray[i], referenceSmallBowl.getNextSmallBowlTimes(i).getMyStones()); + else if (i == 6) assertEquals(stonesArray[i], referenceSmallBowl.getKalaha().getMyStones()); + else if (i == 13) assertEquals(stonesArray[i], referenceSmallBowl.getKalaha().getNextBowl().getKalaha().getMyStones()); + else { + int index = i - 1; + assertEquals(stonesArray[i], referenceSmallBowl.getNextSmallBowlTimes(index).getMyStones()); + } + } } @Test - public void has_zero_rocks_when_created() { - Bowl current = kalaha; - for (int i = 0; i < 14; i++) { - current = current.getNextBowl(); - if (current.getClass() == Kalaha.class) - assertEquals(current.getMyStones(), 0); - } + public void given_stones_can_reach_oppenent_kalaha_when_played_validly_then_opponents_kalaha_is_skipped() { + SmallBowl playWillSkipFromThisBowl = goToSkippableState(); + int opponentKalahaRocksBefore = referenceSmallBowl.getNextSmallBowlTimes(11).getNextBowl().getMyStones(); + playWillSkipFromThisBowl.play(); + int opponentKalahaRocksAfter = referenceSmallBowl.getNextSmallBowlTimes(11).getNextBowl().getMyStones(); + assertEquals(opponentKalahaRocksBefore, opponentKalahaRocksAfter); } } } \ No newline at end of file diff --git a/domain/src/test/java/mancala/domain/MancalaImplTest.java b/domain/src/test/java/mancala/domain/MancalaImplTest.java index b040b02..6f0ce2a 100644 --- a/domain/src/test/java/mancala/domain/MancalaImplTest.java +++ b/domain/src/test/java/mancala/domain/MancalaImplTest.java @@ -173,13 +173,36 @@ class MancalaImplTest { @Nested class isEndOfGame { + @Test void given_the_game_is_not_ended_when_isEndOfGame_is_called_then_return_false() { assertFalse(mancala.isEndOfGame()); } + + @Test + void given_the_game_is_ended_when_isEndOfGame_is_called_then_return_true() { + assertTrue(mancala.isEndOfGame()); + } + + } @Nested class getWinner { + @Test + void given_the_game_has_not_ended_when_getWinner_is_called_then_immediately_return_Mancala_NO_PLAYERS() { + assertEquals(Mancala.NO_PLAYERS, mancala.getWinner()); + } + + @Test + void given_PLAYER_ONE_has_won_in_the_domain_model_when_getWinner_is_called_then_return_Mancala_PLAYER_ONE() { + assertEquals(Mancala.PLAYER_ONE, mancala.getWinner()); + } + + @Test + void given_PLAYER_TWO_has_won_in_the_domain_model_when_getWinner_is_called_then_return_Mancala_PLAYER_TWO() { + assertEquals(Mancala.PLAYER_TWO, mancala.getWinner()); + } + } void assumeTurn(int player) { -- cgit v1.2.3 From 0f4370cee071ada7bf1974a3ae2eeb14c4d00aab Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Tue, 22 Jun 2021 17:28:10 +0200 Subject: refactor(domain tests) <- reorganising --- domain/src/test/java/mancala/domain/BowlTest.java | 439 +++++++++++----------- 1 file changed, 227 insertions(+), 212 deletions(-) (limited to 'domain/src/test/java') diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java index 7e1ec01..cd9f405 100644 --- a/domain/src/test/java/mancala/domain/BowlTest.java +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -49,7 +49,15 @@ class BowlTest { assertSame(initialBowl, currentBowl); } - SmallBowl referenceSmallBowl = new SmallBowl(); + SmallBowl referenceSmallBowl; + + { + try { + referenceSmallBowl = new SmallBowl(); + } catch (DomainSmallBowlException e) { + e.printStackTrace(); + } + } @Test public void default_stones_amount_check() { @@ -83,214 +91,9 @@ class BowlTest { } } - @Nested - class and_the_game_is_in_a_state_where { - - @Test - public void its_not_the_players_turn_when_play_is_called_then_nothing_happens() { - referenceSmallBowl.getMyOwner().switchTurn(); - int initialRocks = referenceSmallBowl.getMyStones(); - referenceSmallBowl.play(); - Bowl neighbour = referenceSmallBowl.getNextBowl(); - for (int i = 0; i < initialRocks; i++) { - assertEquals(4, neighbour.getMyStones()); - neighbour = neighbour.getNextBowl(); - } - assertEquals(4, neighbour.getMyStones()); - } - - @Test - public void the_bowl_is_empty_WHEN_the_player_plays_the_empty_bowl_THEN_nothing_happens() { - referenceSmallBowl.play(); - referenceSmallBowl.getMyOwner().switchTurn(); - assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); - referenceSmallBowl.play(); - assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); - assertEquals(5, referenceSmallBowl.getNextBowl().getMyStones()); - } - - @Test - public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_won() { - Player player = referenceSmallBowl.getMyOwner(); - Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); - assertFalse(player.won()); - assertFalse(opponent.won()); - goToEndOfSillyGame(); - assertTrue(player.won()); - assertFalse(opponent.won()); - - } - - @Test - public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_wonOPPONENTVARIATION() { - Player player = referenceSmallBowl.getMyOwner(); - Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); - goToEndOfGameWhereOpponentWins(); - assertFalse(player.won()); - assertTrue(opponent.won()); - } - - @Test - public void the_play_would_skip_past_opponent_kalaha_at_the_last_rock_and_steal_WHEN_played_THEN_should_skip_and_steal_correctly() { - goToSkipAndStealOnLast(); - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - assertEquals(3, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - assertEquals(19, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - } - - private void goToSkipAndStealOnLast() { - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - referenceSmallBowl.getNextSmallBowlTimes(1).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - referenceSmallBowl.getNextSmallBowlTimes(1).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - referenceSmallBowl.getNextSmallBowlTimes(2).play(); - firstSmallBowlOpponent.play(); - referenceSmallBowl.getNextSmallBowlTimes(3).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - referenceSmallBowl.getNextSmallBowlTimes(4).play(); - firstSmallBowlOpponent.play(); - // Cheating here, let player go again >:), i'm too dumb too make a loop/skip and steal play happen in fair game - firstSmallBowlOpponent.getMyOwner().switchTurn(); - // Should skip and steal - // this bowls rocks - assertEquals(10, firstSmallBowlOpponent.getNextSmallBowlTimes(3).getMyStones()); - // End up here by looping around the board, thus skipping - assertEquals(0, firstSmallBowlOpponent.getMyStones()); - // Thus steal from last bowl on players side - assertEquals(8, referenceSmallBowl.getNextSmallBowlTimes(5).getMyStones()); - // Result is big kalaha booty - assertEquals(8, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - } - - private void goToEndOfGameWhereOpponentWins() { - goToSkipAndStealOnLast(); - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - referenceSmallBowl.getNextSmallBowlTimes(1).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - referenceSmallBowl.play(); - referenceSmallBowl.getMyOwner().switchTurn(); - referenceSmallBowl.getNextSmallBowlTimes(3).play(); - referenceSmallBowl.getMyOwner().switchTurn(); - referenceSmallBowl.getNextSmallBowlTimes(4).play(); - referenceSmallBowl.getNextSmallBowlTimes(5).play(); - } - - private void goToEndOfSillyGame() { - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - - // player - // Best opening - referenceSmallBowl.getNextSmallBowlTimes(2).play(); - // Set up for steal move - referenceSmallBowl.getNextSmallBowlTimes(4).play(); - assertEquals(2, referenceSmallBowl.getKalaha().getMyStones()); - - // opponent - // ... worst opening? - firstSmallBowlOpponent.play(); - - // player - assertSame(referenceSmallBowl.getNextSmallBowlTimes(4).getOpposite(), referenceSmallBowl.getKalaha().getNextBowl().getNextBowl()); - referenceSmallBowl.play(); - // Check if i did it properly on paper - assertEquals(9, referenceSmallBowl.getKalaha().getMyStones()); - assertEquals(0, referenceSmallBowl.getNextSmallBowlTimes(4).getMyStones()); - // assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite().getMyRocks()); - - // opponent - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - - //Player - referenceSmallBowl.getNextSmallBowlTimes(3).play(); - assertEquals(10, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - // opponent makes stupid move again - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - // player makes big steal - //assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); - assertEquals(10, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - referenceSmallBowl.getNextSmallBowlTimes(2).play(); - assertEquals(19, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - // opponent steals tiny booty - firstSmallBowlOpponent.play(); - assertEquals(3, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - - // player is stalling until the end - referenceSmallBowl.play(); - - // opponent is heading for disaster - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - referenceSmallBowl.play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); - referenceSmallBowl.play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - // everything empty! - for (int i = 0; i < 6; i++) { - assertEquals(0, firstSmallBowlOpponent.getNextSmallBowlTimes(i).getMyStones()); - } - - } - - private SmallBowl goToSkippableState() { - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - - referenceSmallBowl.getNextSmallBowlTimes(2).play(); - referenceSmallBowl.getNextSmallBowlTimes(3).play(); - - firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - - referenceSmallBowl.play(); - firstSmallBowlOpponent.play(); - - referenceSmallBowl.getNextSmallBowlTimes(4).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); - - // Playing this bowl should give a skip! - assertTrue(referenceSmallBowl.getNextSmallBowlTimes(5).getMyStones() >= 8); - return referenceSmallBowl.getNextSmallBowlTimes(5); - } - } - - @Nested - class GIVEN_the_play_ends{ - - @Test - public void in_own_kalaha_WHEN_play_ends_THEN_turn_is_not_switched() { - referenceSmallBowl.getNextSmallBowlTimes(2).play(); - assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); - } - - @Test - public void in_own_small_bowl_WHEN_play_ends_THEN_turn_is_switched() { - referenceSmallBowl.play(); - assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); - } - - @Test - public void in_opponents_small_bowl_WHEN_player_plays_this_bowl_THEN_turn_is_switched() { - referenceSmallBowl.getNextSmallBowlTimes(5).play(); - assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); - } - - @Test - public void in_own_empty_small_bowl_and_opposite_has_rocks_WHEN_play_ends_THEN_rocks_of_opposite_plus_last_rock_of_play_are_added_to_kalaha() { - referenceSmallBowl.getNextSmallBowlTimes(5).play(); - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - assertSame(referenceSmallBowl.getNextSmallBowlTimes(1).getOpposite(), referenceSmallBowl.getKalaha().getNextSmallBowl().getNextSmallBowlTimes(4)); - // assertSame(firstSmallBowlPlayer.getOpposite(), firstSmallBowlPlayer.getKalaha().getNextSmallBowlTimes(5)); - referenceSmallBowl.play(); - assertEquals(7, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - } - - } } @@ -325,7 +128,11 @@ class BowlTest { void given_a_stones_list_when_instantiating_a_small_bowl_then_configure_stones_as_in_list() { int[] stonesArray = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14}; List stonesList = Arrays.stream(stonesArray).boxed().collect(Collectors.toList()); - referenceSmallBowl = new SmallBowl(stonesList); + try { + referenceSmallBowl = new SmallBowl(stonesList); + } catch (DomainSmallBowlException e) { + fail("Invalid instantiation."); + } for (int i = 0; i < stonesArray.length; i++) { if (i < 6) assertEquals(stonesArray[i], referenceSmallBowl.getNextSmallBowlTimes(i).getMyStones()); else if (i == 6) assertEquals(stonesArray[i], referenceSmallBowl.getKalaha().getMyStones()); @@ -337,13 +144,221 @@ class BowlTest { } } + @Test + void given_a_stones_list_with_odd_number_of_elements_when_instantiating_small_bowl_then_throw_DomainSmallBowlException() { + int[] stonesArray = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13}; + List stonesList = Arrays.stream(stonesArray).boxed().collect(Collectors.toList()); + try { + referenceSmallBowl = new SmallBowl(stonesList); + fail(); + } catch (DomainSmallBowlException e) { + } + } + @Test public void given_stones_can_reach_oppenent_kalaha_when_played_validly_then_opponents_kalaha_is_skipped() { - SmallBowl playWillSkipFromThisBowl = goToSkippableState(); - int opponentKalahaRocksBefore = referenceSmallBowl.getNextSmallBowlTimes(11).getNextBowl().getMyStones(); - playWillSkipFromThisBowl.play(); - int opponentKalahaRocksAfter = referenceSmallBowl.getNextSmallBowlTimes(11).getNextBowl().getMyStones(); - assertEquals(opponentKalahaRocksBefore, opponentKalahaRocksAfter); + try { + referenceSmallBowl = new SmallBowl( + Arrays.stream( + new int[] {0,0,0,0,0,100,0,0,0,0,0,0,0,0} + ).boxed().collect(Collectors.toList()) + ); + } catch (DomainSmallBowlException e) { + fail("Invalid instantiation."); + } + referenceSmallBowl.getNextSmallBowlTimes(5).play(); + assertEquals(0, referenceSmallBowl.getKalaha().getNextBowl().getKalaha().getMyStones()); } + + @Nested + class and_the_game_is_in_a_state_where { + + @Test + public void its_not_the_players_turn_when_play_is_called_then_nothing_happens() { + referenceSmallBowl.getMyOwner().switchTurn(); + int initialRocks = referenceSmallBowl.getMyStones(); + referenceSmallBowl.play(); + Bowl neighbour = referenceSmallBowl.getNextBowl(); + for (int i = 0; i < initialRocks; i++) { + assertEquals(4, neighbour.getMyStones()); + neighbour = neighbour.getNextBowl(); + } + assertEquals(4, neighbour.getMyStones()); + } + + @Test + public void the_bowl_is_empty_WHEN_the_player_plays_the_empty_bowl_THEN_nothing_happens() { + referenceSmallBowl.play(); + referenceSmallBowl.getMyOwner().switchTurn(); + assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); + referenceSmallBowl.play(); + assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); + assertEquals(5, referenceSmallBowl.getNextBowl().getMyStones()); + } + + @Test + public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_won() { + Player player = referenceSmallBowl.getMyOwner(); + Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); + assertFalse(player.won()); + assertFalse(opponent.won()); + goToEndOfSillyGame(); + assertTrue(player.won()); + assertFalse(opponent.won()); + + } + + @Test + public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_wonOPPONENTVARIATION() { + Player player = referenceSmallBowl.getMyOwner(); + Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); + goToEndOfGameWhereOpponentWins(); + assertFalse(player.won()); + assertTrue(opponent.won()); + } + + @Test + public void the_play_would_skip_past_opponent_kalaha_at_the_last_rock_and_steal_WHEN_played_THEN_should_skip_and_steal_correctly() { + goToSkipAndStealOnLast(); + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + assertEquals(3, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + assertEquals(19, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + } + + private void goToSkipAndStealOnLast() { + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + referenceSmallBowl.getNextSmallBowlTimes(1).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + referenceSmallBowl.getNextSmallBowlTimes(1).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + firstSmallBowlOpponent.play(); + referenceSmallBowl.getNextSmallBowlTimes(3).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + referenceSmallBowl.getNextSmallBowlTimes(4).play(); + firstSmallBowlOpponent.play(); + // Cheating here, let player go again >:), i'm too dumb too make a loop/skip and steal play happen in fair game + firstSmallBowlOpponent.getMyOwner().switchTurn(); + // Should skip and steal + // this bowls rocks + assertEquals(10, firstSmallBowlOpponent.getNextSmallBowlTimes(3).getMyStones()); + // End up here by looping around the board, thus skipping + assertEquals(0, firstSmallBowlOpponent.getMyStones()); + // Thus steal from last bowl on players side + assertEquals(8, referenceSmallBowl.getNextSmallBowlTimes(5).getMyStones()); + // Result is big kalaha booty + assertEquals(8, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + } + + private void goToEndOfGameWhereOpponentWins() { + goToSkipAndStealOnLast(); + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + referenceSmallBowl.getNextSmallBowlTimes(1).play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + referenceSmallBowl.play(); + referenceSmallBowl.getMyOwner().switchTurn(); + referenceSmallBowl.getNextSmallBowlTimes(3).play(); + referenceSmallBowl.getMyOwner().switchTurn(); + referenceSmallBowl.getNextSmallBowlTimes(4).play(); + referenceSmallBowl.getNextSmallBowlTimes(5).play(); + } + + private void goToEndOfSillyGame() { + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + + // player + // Best opening + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + // Set up for steal move + referenceSmallBowl.getNextSmallBowlTimes(4).play(); + assertEquals(2, referenceSmallBowl.getKalaha().getMyStones()); + + // opponent + // ... worst opening? + firstSmallBowlOpponent.play(); + + // player + assertSame(referenceSmallBowl.getNextSmallBowlTimes(4).getOpposite(), referenceSmallBowl.getKalaha().getNextBowl().getNextBowl()); + referenceSmallBowl.play(); + // Check if i did it properly on paper + assertEquals(9, referenceSmallBowl.getKalaha().getMyStones()); + assertEquals(0, referenceSmallBowl.getNextSmallBowlTimes(4).getMyStones()); + // assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite().getMyRocks()); + + // opponent + firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); + + //Player + referenceSmallBowl.getNextSmallBowlTimes(3).play(); + assertEquals(10, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + + // opponent makes stupid move again + firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); + + // player makes big steal + //assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); + assertEquals(10, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + assertEquals(19, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + + // opponent steals tiny booty + firstSmallBowlOpponent.play(); + assertEquals(3, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + + // player is stalling until the end + referenceSmallBowl.play(); + + // opponent is heading for disaster + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + referenceSmallBowl.play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); + referenceSmallBowl.play(); + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + // everything empty! + for (int i = 0; i < 6; i++) { + assertEquals(0, firstSmallBowlOpponent.getNextSmallBowlTimes(i).getMyStones()); + } + + } + } + + + @Nested + class GIVEN_the_play_ends{ + + @Test + public void in_own_kalaha_WHEN_play_ends_THEN_turn_is_not_switched() { + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); + } + + @Test + public void in_own_small_bowl_WHEN_play_ends_THEN_turn_is_switched() { + referenceSmallBowl.play(); + assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); + } + + @Test + public void in_opponents_small_bowl_WHEN_player_plays_this_bowl_THEN_turn_is_switched() { + referenceSmallBowl.getNextSmallBowlTimes(5).play(); + assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); + } + + @Test + public void in_own_empty_small_bowl_and_opposite_has_rocks_WHEN_play_ends_THEN_rocks_of_opposite_plus_last_rock_of_play_are_added_to_kalaha() { + referenceSmallBowl.getNextSmallBowlTimes(5).play(); + SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); + firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + assertSame(referenceSmallBowl.getNextSmallBowlTimes(1).getOpposite(), referenceSmallBowl.getKalaha().getNextSmallBowl().getNextSmallBowlTimes(4)); + // assertSame(firstSmallBowlPlayer.getOpposite(), firstSmallBowlPlayer.getKalaha().getNextSmallBowlTimes(5)); + referenceSmallBowl.play(); + assertEquals(7, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + } + + } + } } \ No newline at end of file -- cgit v1.2.3 From ba2ec6bc470399eb8ed906691626d62ef11d3244 Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Tue, 22 Jun 2021 17:47:56 +0200 Subject: before dinner --- domain/src/test/java/mancala/domain/BowlTest.java | 60 ++++++++++++----------- 1 file changed, 31 insertions(+), 29 deletions(-) (limited to 'domain/src/test/java') diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java index cd9f405..6d5a12f 100644 --- a/domain/src/test/java/mancala/domain/BowlTest.java +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -90,25 +90,13 @@ class BowlTest { assertEquals(4, neighbour.getMyStones()); } } - - - - - - } @Nested class given_a_kalaha { - Kalaha playerKalaha; - Kalaha opponentKalaha; - - @BeforeEach - public void makeKalahaInBoard() { - playerKalaha = referenceSmallBowl.getKalaha(); - opponentKalaha = referenceSmallBowl.getKalaha().getNextSmallBowl().getKalaha(); - } + Kalaha playerKalaha = referenceSmallBowl.getKalaha(); + Kalaha opponentKalaha = referenceSmallBowl.getKalaha().getNextSmallBowl().getKalaha(); @Test public void when_getMyStones_is_called_after_instantiating_then_has_zero_stones() { @@ -155,23 +143,31 @@ class BowlTest { } } - @Test - public void given_stones_can_reach_oppenent_kalaha_when_played_validly_then_opponents_kalaha_is_skipped() { - try { - referenceSmallBowl = new SmallBowl( - Arrays.stream( - new int[] {0,0,0,0,0,100,0,0,0,0,0,0,0,0} - ).boxed().collect(Collectors.toList()) - ); - } catch (DomainSmallBowlException e) { - fail("Invalid instantiation."); + @Nested + class playBehaviour { + + { + try { + referenceSmallBowl = new SmallBowl(); + } catch (DomainSmallBowlException e) { + e.printStackTrace(); + } } - referenceSmallBowl.getNextSmallBowlTimes(5).play(); - assertEquals(0, referenceSmallBowl.getKalaha().getNextBowl().getKalaha().getMyStones()); - } - @Nested - class and_the_game_is_in_a_state_where { + @Test + public void given_stones_can_reach_oppenent_kalaha_when_played_validly_then_opponents_kalaha_is_skipped() { + try { + referenceSmallBowl = new SmallBowl( + Arrays.stream( + new int[] {0,0,0,0,0,100,0,0,0,0,0,0,0,0} + ).boxed().collect(Collectors.toList()) + ); + } catch (DomainSmallBowlException e) { + fail("Invalid instantiation."); + } + referenceSmallBowl.getNextSmallBowlTimes(5).play(); + assertEquals(0, referenceSmallBowl.getKalaha().getNextBowl().getKalaha().getMyStones()); + } @Test public void its_not_the_players_turn_when_play_is_called_then_nothing_happens() { @@ -186,6 +182,12 @@ class BowlTest { assertEquals(4, neighbour.getMyStones()); } + } + + + @Nested + class and_the_game_is_in_a_state_where { + @Test public void the_bowl_is_empty_WHEN_the_player_plays_the_empty_bowl_THEN_nothing_happens() { referenceSmallBowl.play(); -- cgit v1.2.3 From 2e1a7cbcd36296c5abb1bb6d12fc155e3e3913fb Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Tue, 22 Jun 2021 20:14:22 +0200 Subject: finished making my life easier (domain tests) --- domain/src/test/java/mancala/domain/BowlTest.java | 227 +++++++--------------- 1 file changed, 75 insertions(+), 152 deletions(-) (limited to 'domain/src/test/java') diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java index 6d5a12f..0f2ea4a 100644 --- a/domain/src/test/java/mancala/domain/BowlTest.java +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -143,10 +143,24 @@ class BowlTest { } } + void setupGameSituationAndFailIfInvalid(int[] stonesArray) { + try { + referenceSmallBowl = new SmallBowl( + Arrays.stream( + stonesArray + ).boxed().collect(Collectors.toList()) + ); + } catch (DomainSmallBowlException e) { + fail("Invalid instantiation."); + } + } + @Nested class playBehaviour { { + // setup default game in this sub class + // by default try { referenceSmallBowl = new SmallBowl(); } catch (DomainSmallBowlException e) { @@ -155,22 +169,14 @@ class BowlTest { } @Test - public void given_stones_can_reach_oppenent_kalaha_when_played_validly_then_opponents_kalaha_is_skipped() { - try { - referenceSmallBowl = new SmallBowl( - Arrays.stream( - new int[] {0,0,0,0,0,100,0,0,0,0,0,0,0,0} - ).boxed().collect(Collectors.toList()) - ); - } catch (DomainSmallBowlException e) { - fail("Invalid instantiation."); - } + void given_stones_can_reach_oppenent_kalaha_when_played_validly_then_opponents_kalaha_is_skipped() { + setupGameSituationAndFailIfInvalid(new int[] {0,0,0,0,0,100,0,0,0,0,0,0,0,0}); referenceSmallBowl.getNextSmallBowlTimes(5).play(); assertEquals(0, referenceSmallBowl.getKalaha().getNextBowl().getKalaha().getMyStones()); } @Test - public void its_not_the_players_turn_when_play_is_called_then_nothing_happens() { + void given_its_not_the_players_turn_when_play_is_called_then_nothing_happens() { referenceSmallBowl.getMyOwner().switchTurn(); int initialRocks = referenceSmallBowl.getMyStones(); referenceSmallBowl.play(); @@ -182,14 +188,8 @@ class BowlTest { assertEquals(4, neighbour.getMyStones()); } - } - - - @Nested - class and_the_game_is_in_a_state_where { - @Test - public void the_bowl_is_empty_WHEN_the_player_plays_the_empty_bowl_THEN_nothing_happens() { + void given_the_bowl_is_empty_when_play_called_on_the_bowl_then_nothing_happens() { referenceSmallBowl.play(); referenceSmallBowl.getMyOwner().switchTurn(); assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); @@ -199,168 +199,91 @@ class BowlTest { } @Test - public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_won() { - Player player = referenceSmallBowl.getMyOwner(); - Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); - assertFalse(player.won()); - assertFalse(opponent.won()); - goToEndOfSillyGame(); - assertTrue(player.won()); - assertFalse(opponent.won()); + void given_stones_would_skip_opponent_kalaha_at_the_last_rock_and_steal_when_played_then_should_skip_and_steal_correctly() { + setupGameSituationAndFailIfInvalid(new int[] {13,0,0,0,0,0, + 0, + 0,0,0,0,0,8, + 0}); + System.out.println(referenceSmallBowl.getMyStones()); + System.out.println(referenceSmallBowl.stateString()); + referenceSmallBowl.play(); + System.out.println(referenceSmallBowl.stateString()); + assertEquals(11, referenceSmallBowl.getKalaha().getMyStones(), + "resulting kalaha stones after stealing should be 11."); + assertEquals(0, referenceSmallBowl.getMyStones(), + "played bowl should be zero, since steal happened"); + assertEquals(0, referenceSmallBowl.getNextSmallBowlTimes(12).getMyStones()); } @Test - public void all_small_bowls_of_the_player_are_empty_WHEN_a_play_ends_THEN_tell_players_who_wonOPPONENTVARIATION() { - Player player = referenceSmallBowl.getMyOwner(); - Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); - goToEndOfGameWhereOpponentWins(); - assertFalse(player.won()); - assertTrue(opponent.won()); + void given_that_play_ends_in_own_kalaha_when_play_is_called_validly_then_turn_is_not_switched() { + referenceSmallBowl.getNextSmallBowlTimes(2).play(); + assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); } @Test - public void the_play_would_skip_past_opponent_kalaha_at_the_last_rock_and_steal_WHEN_played_THEN_should_skip_and_steal_correctly() { - goToSkipAndStealOnLast(); - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - assertEquals(3, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - assertEquals(19, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - } - - private void goToSkipAndStealOnLast() { - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - referenceSmallBowl.getNextSmallBowlTimes(1).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(2).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - referenceSmallBowl.getNextSmallBowlTimes(1).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - referenceSmallBowl.getNextSmallBowlTimes(2).play(); - firstSmallBowlOpponent.play(); - referenceSmallBowl.getNextSmallBowlTimes(3).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - referenceSmallBowl.getNextSmallBowlTimes(4).play(); - firstSmallBowlOpponent.play(); - // Cheating here, let player go again >:), i'm too dumb too make a loop/skip and steal play happen in fair game - firstSmallBowlOpponent.getMyOwner().switchTurn(); - // Should skip and steal - // this bowls rocks - assertEquals(10, firstSmallBowlOpponent.getNextSmallBowlTimes(3).getMyStones()); - // End up here by looping around the board, thus skipping - assertEquals(0, firstSmallBowlOpponent.getMyStones()); - // Thus steal from last bowl on players side - assertEquals(8, referenceSmallBowl.getNextSmallBowlTimes(5).getMyStones()); - // Result is big kalaha booty - assertEquals(8, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + void given_that_play_ends_in_own_small_bowl_when_play_is_called_validly_then_turn_is_switched() { + referenceSmallBowl.play(); + assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); } - private void goToEndOfGameWhereOpponentWins() { - goToSkipAndStealOnLast(); - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - referenceSmallBowl.getNextSmallBowlTimes(1).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - referenceSmallBowl.play(); - referenceSmallBowl.getMyOwner().switchTurn(); - referenceSmallBowl.getNextSmallBowlTimes(3).play(); - referenceSmallBowl.getMyOwner().switchTurn(); - referenceSmallBowl.getNextSmallBowlTimes(4).play(); + @Test + void given_that_play_ends_in_opponents_small_bowl_when_play_is_called_validly_then_turn_is_switched() { referenceSmallBowl.getNextSmallBowlTimes(5).play(); + assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); } - private void goToEndOfSillyGame() { + @Test + void given_that_play_ends_in_own_empty_small_bowl_and_opposite_has_rocks_when_play_is_called_validly_then_rocks_of_opposite_plus_last_rock_of_play_are_added_to_next_kalaha() { + System.out.println(referenceSmallBowl.stateString()); + referenceSmallBowl.getNextSmallBowlTimes(5).play(); + System.out.println(referenceSmallBowl.stateString()); SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - - // player - // Best opening - referenceSmallBowl.getNextSmallBowlTimes(2).play(); - // Set up for steal move - referenceSmallBowl.getNextSmallBowlTimes(4).play(); - assertEquals(2, referenceSmallBowl.getKalaha().getMyStones()); - - // opponent - // ... worst opening? - firstSmallBowlOpponent.play(); - - // player - assertSame(referenceSmallBowl.getNextSmallBowlTimes(4).getOpposite(), referenceSmallBowl.getKalaha().getNextBowl().getNextBowl()); - referenceSmallBowl.play(); - // Check if i did it properly on paper - assertEquals(9, referenceSmallBowl.getKalaha().getMyStones()); - assertEquals(0, referenceSmallBowl.getNextSmallBowlTimes(4).getMyStones()); - // assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(4).getOpposite().getMyRocks()); - - // opponent - firstSmallBowlOpponent.getNextSmallBowlTimes(3).play(); - - //Player - referenceSmallBowl.getNextSmallBowlTimes(3).play(); - assertEquals(10, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - - // opponent makes stupid move again - firstSmallBowlOpponent.getNextSmallBowlTimes(1).play(); - - // player makes big steal - //assertEquals(0, firstSmallBowlPlayer.getNextSmallBowlTimes(5).getNextBowl().getMyRocks()); - assertEquals(10, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - referenceSmallBowl.getNextSmallBowlTimes(2).play(); - assertEquals(19, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - - // opponent steals tiny booty - firstSmallBowlOpponent.play(); - assertEquals(3, firstSmallBowlOpponent.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); - - // player is stalling until the end - referenceSmallBowl.play(); - - // opponent is heading for disaster firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); + System.out.println(referenceSmallBowl.stateString()); referenceSmallBowl.play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); - referenceSmallBowl.play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - // everything empty! - for (int i = 0; i < 6; i++) { - assertEquals(0, firstSmallBowlOpponent.getNextSmallBowlTimes(i).getMyStones()); - } - + System.out.println(referenceSmallBowl.stateString()); + assertEquals(7, referenceSmallBowl.getKalaha().getMyStones()); + assertEquals(0, referenceSmallBowl.getNextSmallBowlTimes(5).getMyStones()); + assertEquals(0, referenceSmallBowl.getKalaha().getNextBowl().getMyStones()); } } - @Nested - class GIVEN_the_play_ends{ - - @Test - public void in_own_kalaha_WHEN_play_ends_THEN_turn_is_not_switched() { - referenceSmallBowl.getNextSmallBowlTimes(2).play(); - assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); - } - - @Test - public void in_own_small_bowl_WHEN_play_ends_THEN_turn_is_switched() { - referenceSmallBowl.play(); - assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); - } + class endGameBehaviour { @Test - public void in_opponents_small_bowl_WHEN_player_plays_this_bowl_THEN_turn_is_switched() { + void given_all_small_bowls_of_the_player_are_empty_when_a_play_ends_then_tell_players_who_won() { + setupGameSituationAndFailIfInvalid(new int[] {0,0,0,0,0,1,0,4,4,4,4,4,4,0}); + Player player = referenceSmallBowl.getMyOwner(); + Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); + assertFalse(player.won(), "players haven't won at start of game"); + assertFalse(opponent.won(), "players haven't won at start of game"); referenceSmallBowl.getNextSmallBowlTimes(5).play(); - assertFalse(referenceSmallBowl.getMyOwner().hasTheTurn()); + assertFalse(player.won(), "player should lose here."); + assertTrue(opponent.won(), "opponent should win here."); + + setupGameSituationAndFailIfInvalid(new int[] {4,4,4,4,4,4,0,0,0,0,0,0,1,0}); + player = referenceSmallBowl.getMyOwner(); + opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); + assertFalse(player.won(), "players haven't won at start of game"); + assertFalse(opponent.won(), "players haven't won at start of game"); + player.switchTurn(); + referenceSmallBowl.getNextSmallBowlTimes(6 + 5).play(); + assertTrue(player.won(), "player should win here."); + assertFalse(opponent.won(), "opponent should lose here."); } @Test - public void in_own_empty_small_bowl_and_opposite_has_rocks_WHEN_play_ends_THEN_rocks_of_opposite_plus_last_rock_of_play_are_added_to_kalaha() { + void given_all_small_bowls_of_the_player_are_empty_and_score_is_tied_when_a_play_ends_then_tell_both_player_they_won() { + setupGameSituationAndFailIfInvalid(new int[] {0,0,0,0,0,1,0,0,0,0,0,0,1,0}); + System.out.println(referenceSmallBowl.stateString()); referenceSmallBowl.getNextSmallBowlTimes(5).play(); - SmallBowl firstSmallBowlOpponent = referenceSmallBowl.getNextSmallBowlTimes(6); - firstSmallBowlOpponent.getNextSmallBowlTimes(5).play(); - assertSame(referenceSmallBowl.getNextSmallBowlTimes(1).getOpposite(), referenceSmallBowl.getKalaha().getNextSmallBowl().getNextSmallBowlTimes(4)); - // assertSame(firstSmallBowlPlayer.getOpposite(), firstSmallBowlPlayer.getKalaha().getNextSmallBowlTimes(5)); - referenceSmallBowl.play(); - assertEquals(7, referenceSmallBowl.getNextSmallBowlTimes(5).getNextBowl().getMyStones()); + System.out.println(referenceSmallBowl.stateString()); + assertTrue(referenceSmallBowl.getMyOwner().won() && referenceSmallBowl.getMyOwner().getOpponent().won()); } } - } } \ No newline at end of file -- cgit v1.2.3 From 3906fcf3d702f13da79c797c91a2dd32d874af49 Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Tue, 22 Jun 2021 21:12:52 +0200 Subject: update(MancalaImpl) <- playPit returns stateArray --- domain/src/test/java/mancala/domain/BowlTest.java | 18 ++++++ .../test/java/mancala/domain/MancalaImplTest.java | 64 ++++++++++++++++++++-- 2 files changed, 77 insertions(+), 5 deletions(-) (limited to 'domain/src/test/java') diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java index 0f2ea4a..0614201 100644 --- a/domain/src/test/java/mancala/domain/BowlTest.java +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -143,6 +143,24 @@ class BowlTest { } } + @Test + void given_a_stones_list_with_less_than_four_elements_when_instantiating_small_bowl_then_throw_DomainSmallBowlException() { + int[] stonesArray = new int[] {1,2}; + List stonesList = Arrays.stream(stonesArray).boxed().collect(Collectors.toList()); + try { + referenceSmallBowl = new SmallBowl(stonesList); + fail("No exception when stones list is too small"); + } catch (DomainSmallBowlException e) { + } + stonesArray = new int[] {1,2,3,4}; + stonesList = Arrays.stream(stonesArray).boxed().collect(Collectors.toList()); + try { + referenceSmallBowl = new SmallBowl(stonesList); + } catch (DomainSmallBowlException e) { + fail("Should work fine"); + } + } + void setupGameSituationAndFailIfInvalid(int[] stonesArray) { try { referenceSmallBowl = new SmallBowl( diff --git a/domain/src/test/java/mancala/domain/MancalaImplTest.java b/domain/src/test/java/mancala/domain/MancalaImplTest.java index 6f0ce2a..61409ac 100644 --- a/domain/src/test/java/mancala/domain/MancalaImplTest.java +++ b/domain/src/test/java/mancala/domain/MancalaImplTest.java @@ -2,6 +2,7 @@ package mancala.domain; import org.junit.jupiter.api.*; +import java.util.Arrays; import java.util.HashSet; import static org.junit.jupiter.api.Assertions.*; @@ -128,8 +129,33 @@ class MancalaImplTest { playPitAndFailIfNoException(7, "Didn't throw exception when Pit was empty when played!"); } + @Test + void given_that_pit_has_stones_and_player_has_turn_when_playPit_is_called_then_return_array_representing_state() { + mancala = new MancalaImpl(new int[] {1,0,0,0,0,0,0,1,0,0,0,0,0,0}); + assumeTurn(Mancala.PLAYER_ONE); + int[] stateArray = playPitAndFailIfNotValid(0); + assertEquals(15, stateArray.length, + "length of the state array should be 15."); + assertTrue(Arrays.equals(new int[] {0,1,0,0,0,0,0,1,0,0,0,0,0,0,2}, stateArray)); + + stateArray = playPitAndFailIfNotValid(7); + assertEquals(15, stateArray.length, + "length of the state array should be 15."); + assertTrue(Arrays.equals(new int[] {0,1,0,0,0,0,0,0,1,0,0,0,0,0,1}, stateArray)); + } + } + /** + * Method for returning the amount of stones in de specified pit. Index is as specified below: + * + * 12 11 10 9 8 7 + * 13 6 + * 0 1 2 3 4 5 + * + * @param index Index of the pit. + * @return Amount of stone. + */ @Nested class getStonesForPit { @Test @@ -171,21 +197,33 @@ class MancalaImplTest { } + /** + * Method for retrieving whether the game has ended or not. + * + * @return True is the game has ended otherwise False. + */ @Nested class isEndOfGame { @Test - void given_the_game_is_not_ended_when_isEndOfGame_is_called_then_return_false() { + void given_the_game_has_not_ended_when_isEndOfGame_is_called_then_return_false() { assertFalse(mancala.isEndOfGame()); } @Test - void given_the_game_is_ended_when_isEndOfGame_is_called_then_return_true() { + void given_the_game_has_ended_when_isEndOfGame_is_called_then_return_true() { + mancala = new MancalaImpl(new int[] {0,0,0,0,0,1,0,0,0,0,0,0,0,0}); + playPitAndFailIfNotValid(5); assertTrue(mancala.isEndOfGame()); } } + /** + * Method for retrieving the player that has won the game. + * + * @return Integer value representing which player(s) (if any) won the game. + */ @Nested class getWinner { @Test @@ -195,12 +233,27 @@ class MancalaImplTest { @Test void given_PLAYER_ONE_has_won_in_the_domain_model_when_getWinner_is_called_then_return_Mancala_PLAYER_ONE() { + mancala = new MancalaImpl(new int[] {0,0,0,0,0,1,0,0,0,0,0,0,0,0}); + playPitAndFailIfNotValid(5); assertEquals(Mancala.PLAYER_ONE, mancala.getWinner()); } @Test void given_PLAYER_TWO_has_won_in_the_domain_model_when_getWinner_is_called_then_return_Mancala_PLAYER_TWO() { - assertEquals(Mancala.PLAYER_TWO, mancala.getWinner()); + mancala = new MancalaImpl(new int[] {0,1,0,0,0,0,0,0,0,0,0,2,0,0}); + playPitAndFailIfNotValid(1); + playPitAndFailIfNotValid(7 + 4); + playPitAndFailIfNotValid(7 + 5); + assertEquals(Mancala.PLAYER_TWO, mancala.getWinner(), + "PLAYER TWO should win here."); + } + + @Test + void given_BOTH_PLAYER_have_won_in_the_domain_model_when_getWinner_is_called_then_return_Mancala_BOTH_PLAYERS() { + mancala = new MancalaImpl(new int[] {0,0,0,0,0,1,0,0,0,0,0,0,1,0}); + playPitAndFailIfNotValid(5); + assertEquals(Mancala.BOTH_PLAYERS, mancala.getWinner(), + "PLAYER TWO should win here."); } } @@ -215,11 +268,12 @@ class MancalaImplTest { "It's PLAYER " + (player == 1 ? "ONE's" : "TWO's") + " turn!"); } - void playPitAndFailIfNotValid(int index) { + int[] playPitAndFailIfNotValid(int index) { try { - mancala.playPit(index); + return mancala.playPit(index); } catch (MancalaException e) { fail("Invalid play."); + return new int[0]; } } -- cgit v1.2.3 From 102b25f18d9b269c58d15677f10cd71c15003c4b Mon Sep 17 00:00:00 2001 From: Mike Vink Date: Fri, 25 Jun 2021 11:49:50 +0200 Subject: ending game from a steal should work now --- domain/src/test/java/mancala/domain/BowlTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'domain/src/test/java') diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java index 0614201..e2eedb9 100644 --- a/domain/src/test/java/mancala/domain/BowlTest.java +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -272,7 +272,7 @@ class BowlTest { class endGameBehaviour { @Test - void given_all_small_bowls_of_the_player_are_empty_when_a_play_ends_then_tell_players_who_won() { + void given_all_small_bowls_of_the_player_that_did_the_turn_are_empty_when_a_play_ends_then_tell_players_who_won() { setupGameSituationAndFailIfInvalid(new int[] {0,0,0,0,0,1,0,4,4,4,4,4,4,0}); Player player = referenceSmallBowl.getMyOwner(); Player opponent = referenceSmallBowl.getNextSmallBowlTimes(6).getMyOwner(); @@ -302,6 +302,15 @@ class BowlTest { assertTrue(referenceSmallBowl.getMyOwner().won() && referenceSmallBowl.getMyOwner().getOpponent().won()); } + @Test + void given_that_the_opponents_board_is_emptied_by_stealing_when_player_made_a_play_then_tell_players_who_won() { + setupGameSituationAndFailIfInvalid(new int[] {1,0,0,0,1,0,0,1,0,0,0,0,0,0}); + System.out.println(referenceSmallBowl.stateString()); + referenceSmallBowl.getNextSmallBowlTimes(4).play(); + System.out.println(referenceSmallBowl.stateString()); + assertTrue(referenceSmallBowl.getMyOwner().won() || referenceSmallBowl.getMyOwner().getOpponent().won()); + } + } } } \ No newline at end of file -- cgit v1.2.3