diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2021-06-25 12:56:04 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2021-06-25 12:56:04 +0200 |
| commit | 6bd8d0345e3ac653c3fad4f1c7a6352e8a4a166e (patch) | |
| tree | f142d3f43add40c2dd56bc496d6b4ab497f31a3e /domain/src/test/java | |
| parent | 38d59e2876b9f4d7c589d58295ef8acdf336a45b (diff) | |
| parent | 102b25f18d9b269c58d15677f10cd71c15003c4b (diff) | |
Merge branch 'mvcFeature' into mainline
Diffstat (limited to 'domain/src/test/java')
| -rw-r--r-- | domain/src/test/java/mancala/domain/BowlTest.java | 508 | ||||
| -rw-r--r-- | domain/src/test/java/mancala/domain/MancalaImplTest.java | 291 |
2 files changed, 541 insertions, 258 deletions
diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java index 530f628..e2eedb9 100644 --- a/domain/src/test/java/mancala/domain/BowlTest.java +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -2,323 +2,315 @@ 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.getMyRocks(), 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); + + currentBowl = currentBowl.getNextBowl(); + assertNotNull(currentBowl); + traversedCount++; } + assertSame(initialBowl, currentBowl); + } - @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()); + SmallBowl referenceSmallBowl; + + { + try { + referenceSmallBowl = new SmallBowl(); + } catch (DomainSmallBowlException e) { + e.printStackTrace(); } } + @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.getMyRocks(); - firstSmallBowlPlayer.play(); - Bowl neighbour = firstSmallBowlPlayer.getNextBowl(); - for (int i = 0; i < initialRocks; i++) { - assertEquals(4, neighbour.getMyRocks()); - neighbour = neighbour.getNextBowl(); + @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()); } - 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); - } + @Nested + class given_a_kalaha { + + Kalaha playerKalaha = referenceSmallBowl.getKalaha(); + Kalaha opponentKalaha = referenceSmallBowl.getKalaha().getNextSmallBowl().getKalaha(); @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()); + public void when_getMyStones_is_called_after_instantiating_then_has_zero_stones() { + assertEquals(0, playerKalaha.getMyStones()); + assertEquals(0, opponentKalaha.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_wonOPPONENTVARIATION() { - Player player = firstSmallBowlPlayer.getMyOwner(); - Player opponent = firstSmallBowlPlayer.getNextSmallBowlTimes(6).getMyOwner(); - goToEndOfGameWhereOpponentWins(); - assertFalse(player.won()); - assertTrue(opponent.won()); - } + @Nested + class instantiatingGameWithStonesList { + SmallBowl referenceSmallBowl; - @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()); + @Test + 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<Integer> stonesList = Arrays.stream(stonesArray).boxed().collect(Collectors.toList()); + try { + referenceSmallBowl = new SmallBowl(stonesList); + } catch (DomainSmallBowlException e) { + fail("Invalid instantiation."); } - - 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()); + 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()); + } } + } - 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(); + @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<Integer> stonesList = Arrays.stream(stonesArray).boxed().collect(Collectors.toList()); + try { + referenceSmallBowl = new SmallBowl(stonesList); + fail(); + } catch (DomainSmallBowlException e) { } + } - 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()); + @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<Integer> 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"); + } + } - // player is stalling until the end - firstSmallBowlPlayer.play(); + void setupGameSituationAndFailIfInvalid(int[] stonesArray) { + try { + referenceSmallBowl = new SmallBowl( + Arrays.stream( + stonesArray + ).boxed().collect(Collectors.toList()) + ); + } catch (DomainSmallBowlException e) { + fail("Invalid instantiation."); + } + } - // 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()); + @Nested + class playBehaviour { + + { + // setup default game in this sub class + // by default + try { + referenceSmallBowl = new SmallBowl(); + } catch (DomainSmallBowlException e) { + e.printStackTrace(); } - } - 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(); + @Test + 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()); + } - firstSmallBowlPlayer.getNextSmallBowlTimes(4).play(); - firstSmallBowlOpponent.getNextSmallBowlTimes(4).play(); + @Test + void given_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()); + } - // Playing this bowl should give a skip! - assertTrue(firstSmallBowlPlayer.getNextSmallBowlTimes(5).getMyRocks() >= 8); - return firstSmallBowlPlayer.getNextSmallBowlTimes(5); + @Test + void given_the_bowl_is_empty_when_play_called_on_the_bowl_then_nothing_happens() { + referenceSmallBowl.play(); + referenceSmallBowl.getMyOwner().switchTurn(); + assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); + referenceSmallBowl.play(); + assertTrue(referenceSmallBowl.getMyOwner().hasTheTurn()); + assertEquals(5, referenceSmallBowl.getNextBowl().getMyStones()); } - } - @Nested - class GIVEN_the_play_ends{ + @Test + 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 in_own_kalaha_WHEN_play_ends_THEN_turn_is_not_switched() { - firstSmallBowlPlayer.getNextSmallBowlTimes(2).play(); - assertTrue(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); + 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 in_own_small_bowl_WHEN_play_ends_THEN_turn_is_switched() { - firstSmallBowlPlayer.play(); - assertFalse(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); + void given_that_play_ends_in_own_small_bowl_when_play_is_called_validly_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() { - firstSmallBowlPlayer.getNextSmallBowlTimes(5).play(); - assertFalse(firstSmallBowlPlayer.getMyOwner().hasTheTurn()); + 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()); } @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); + 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); 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()); + System.out.println(referenceSmallBowl.stateString()); + referenceSmallBowl.play(); + System.out.println(referenceSmallBowl.stateString()); + assertEquals(7, referenceSmallBowl.getKalaha().getMyStones()); + assertEquals(0, referenceSmallBowl.getNextSmallBowlTimes(5).getMyStones()); + assertEquals(0, referenceSmallBowl.getKalaha().getNextBowl().getMyStones()); } - } + @Nested + class endGameBehaviour { - } - - @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 + 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(); + 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(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 exists_in_a_mancala_board() { - traverseAndCheckBoard(kalaha, 14); - } + @Test + 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(); + System.out.println(referenceSmallBowl.stateString()); + assertTrue(referenceSmallBowl.getMyOwner().won() && referenceSmallBowl.getMyOwner().getOpponent().won()); + } - @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); + @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 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..61409ac --- /dev/null +++ b/domain/src/test/java/mancala/domain/MancalaImplTest.java @@ -0,0 +1,291 @@ +package mancala.domain; + +import org.junit.jupiter.api.*; + +import java.util.Arrays; +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!"); + } + + @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 + void given_any_Pit_index_when_getStonesForPit_is_called_then_return_stones_of_pits() { + HashSet<Integer> 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<Integer> 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!"); + } + + } + + /** + * 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_has_not_ended_when_isEndOfGame_is_called_then_return_false() { + assertFalse(mancala.isEndOfGame()); + } + + @Test + 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 + 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() { + 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() { + 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."); + } + + } + + 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!"); + } + + int[] playPitAndFailIfNotValid(int index) { + try { + return mancala.playPit(index); + } catch (MancalaException e) { + fail("Invalid play."); + return new int[0]; + } + } + + 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 |
