diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2021-06-22 17:11:42 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2021-06-22 17:11:42 +0200 |
| commit | 92c61668daefe1486d8ce6aefcf4c60c2eee0c3b (patch) | |
| tree | f88110b3ec6d9f65bbe3b4183286b276b38fb50e /domain | |
| parent | 17b594e943eaccc69b6036e258308d1a1ddad53f (diff) | |
refactor(domain tests) <- configurable state
Diffstat (limited to 'domain')
| -rw-r--r-- | domain/src/main/java/mancala/domain/Bowl.java | 19 | ||||
| -rw-r--r-- | domain/src/main/java/mancala/domain/Kalaha.java | 22 | ||||
| -rw-r--r-- | domain/src/main/java/mancala/domain/MancalaImpl.java | 25 | ||||
| -rw-r--r-- | domain/src/main/java/mancala/domain/SmallBowl.java | 63 | ||||
| -rw-r--r-- | domain/src/test/java/mancala/domain/BowlTest.java | 547 | ||||
| -rw-r--r-- | domain/src/test/java/mancala/domain/MancalaImplTest.java | 23 |
6 files changed, 378 insertions, 321 deletions
diff --git a/domain/src/main/java/mancala/domain/Bowl.java b/domain/src/main/java/mancala/domain/Bowl.java index fece393..8a214da 100644 --- a/domain/src/main/java/mancala/domain/Bowl.java +++ b/domain/src/main/java/mancala/domain/Bowl.java @@ -1,12 +1,12 @@ package mancala.domain; abstract class Bowl { - protected int myRocks; + protected int myStones; protected Player myOwner; protected Bowl nextBowl; public int getMyStones() { - return myRocks; + return myStones; } public Bowl getNextBowl() { @@ -25,19 +25,19 @@ abstract class Bowl { abstract Kalaha getKalaha(); - abstract SmallBowl getSmallBowl(); + abstract SmallBowl getNextSmallBowl(); abstract SmallBowl goToFirstBowlOfPlayerWithTurn(); abstract boolean isEmpty(); // abstract SmallBowl getNextSmallBowl(); - boolean endTheGame() { - return goToFirstBowlOfPlayerWithTurn().getNextBowl().endTheGame(goToFirstBowlOfPlayerWithTurn(), 0, 0); + void endTheGame() { + goToFirstBowlOfPlayerWithTurn().getNextBowl().endTheGame(goToFirstBowlOfPlayerWithTurn(), 0, 0); } - protected boolean endTheGame(Bowl startOfLoop, int scorePlayer, int scoreOpponent) { - if (isEmpty() == false && getMyOwner().equals(startOfLoop.getMyOwner())) return false; + protected void endTheGame(Bowl startOfLoop, int scorePlayer, int scoreOpponent) { + if (isEmpty() == false && getMyOwner().equals(startOfLoop.getMyOwner())) return; if (this.equals(startOfLoop)) { @@ -48,8 +48,6 @@ abstract class Bowl { if (scorePlayer == scoreOpponent) getMyOwner().gotADraw(); else if (scorePlayer > scoreOpponent) getMyOwner().isTheWinner(); else getMyOwner().getOpponent().isTheWinner(); - - return true; } @@ -58,9 +56,8 @@ abstract class Bowl { scorePlayer = scorePlayer + getMyStones(); } else scoreOpponent = scoreOpponent + getMyStones(); - return getNextBowl().endTheGame(startOfLoop, scorePlayer, scoreOpponent); + getNextBowl().endTheGame(startOfLoop, scorePlayer, scoreOpponent); } - return false; } diff --git a/domain/src/main/java/mancala/domain/Kalaha.java b/domain/src/main/java/mancala/domain/Kalaha.java index 7ab954c..a13ebd8 100644 --- a/domain/src/main/java/mancala/domain/Kalaha.java +++ b/domain/src/main/java/mancala/domain/Kalaha.java @@ -1,31 +1,33 @@ package mancala.domain; +import java.util.List; + class Kalaha extends Bowl { - Kalaha(int boardSize, int bowlsToAdd, Bowl startBowl, Player playerOwningThisSide) { + Kalaha(int boardSize, int bowlsToAdd, List<Integer> stonesList, Bowl startBowl, Player playerOwningThisSide) { bowlsToAdd = bowlsToAdd - 1; - this.myRocks = 0; + this.myStones = stonesList.remove(0); this.myOwner = playerOwningThisSide; if (bowlsToAdd == 0) this.nextBowl = startBowl; - else this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, startBowl, playerOwningThisSide.getOpponent()); + else this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, stonesList, startBowl, playerOwningThisSide.getOpponent()); } Kalaha getKalaha() { return this; } - SmallBowl getSmallBowl() { - return getNextBowl().getSmallBowl(); + SmallBowl getNextSmallBowl() { + return getNextBowl().getNextSmallBowl(); } @Override SmallBowl goToFirstBowlOfPlayerWithTurn() { if (getMyOwner().hasTheTurn()) { - return getNextBowl().getKalaha().getSmallBowl(); + return getNextBowl().getKalaha().getNextSmallBowl(); } else { - return getSmallBowl(); + return getNextSmallBowl(); } } @@ -38,10 +40,10 @@ class Kalaha extends Bowl { } void distribute(int remainingRocks) { - myRocks++; + myStones++; // Skip? if (getMyOwner().hasTheTurn() == false) { - myRocks--; + myStones--; getNextBowl().distribute(remainingRocks); } else if (remainingRocks == 1) { endTheGame(); @@ -55,6 +57,6 @@ class Kalaha extends Bowl { void claimStolenBooty(int booty) { - myRocks = myRocks + booty; + myStones = myStones + booty; } } diff --git a/domain/src/main/java/mancala/domain/MancalaImpl.java b/domain/src/main/java/mancala/domain/MancalaImpl.java index 82f914f..29048ff 100644 --- a/domain/src/main/java/mancala/domain/MancalaImpl.java +++ b/domain/src/main/java/mancala/domain/MancalaImpl.java @@ -6,19 +6,21 @@ import java.util.stream.IntStream; public class MancalaImpl implements Mancala { - public static HashSet<Integer> PLAYER_ONE_PITS = new HashSet<>( + public static final HashSet<Integer> PLAYER_ONE_PITS = new HashSet<>( IntStream.rangeClosed(0, 5).boxed().collect(Collectors.toList()) ); - public static HashSet<Integer> PLAYER_TWO_PITS = new HashSet<>( + public static final HashSet<Integer> PLAYER_TWO_PITS = new HashSet<>( IntStream.rangeClosed(7, 12).boxed().collect(Collectors.toList()) ); - public static int PLAYER_ONE_KALAHA = 6; - public static int PLAYER_TWO_KALAHA = 13; + public static final int PLAYER_ONE_KALAHA = 6; + public static final int PLAYER_TWO_KALAHA = 13; private SmallBowl domainReference; public MancalaImpl() { domainReference = new SmallBowl(); + domainPlayer = domainReference.getMyOwner(); + domainOpponent = domainPlayer.getOpponent(); } @Override @@ -75,13 +77,22 @@ public class MancalaImpl implements Mancala { return -1; } - @Override + private final mancala.domain.Player domainPlayer; + private final mancala.domain.Player domainOpponent; + + @Override public boolean isEndOfGame() { - return domainReference.endTheGame(); + return domainPlayer.won() || domainOpponent.won(); } @Override public int getWinner() { - return Mancala.NO_PLAYERS; + if (!isEndOfGame()) return Mancala.NO_PLAYERS; + + if (domainPlayer.won()) return Mancala.PLAYER_ONE; + else if (domainOpponent.won()) return Mancala.PLAYER_TWO; + else if (domainPlayer.won() && domainOpponent.won()) return Mancala.BOTH_PLAYERS; + else return Mancala.NO_PLAYERS; } + }
\ No newline at end of file diff --git a/domain/src/main/java/mancala/domain/SmallBowl.java b/domain/src/main/java/mancala/domain/SmallBowl.java index ce4c0ce..07fa1e0 100644 --- a/domain/src/main/java/mancala/domain/SmallBowl.java +++ b/domain/src/main/java/mancala/domain/SmallBowl.java @@ -1,45 +1,44 @@ package mancala.domain; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + public class SmallBowl extends Bowl { public SmallBowl() { - this.myRocks = 4; + this( + Arrays.stream(new int[] {4,4,4,4,4,4,0,4,4,4,4,4,4,0}).boxed().collect(Collectors.toList()) + ); + } + + public SmallBowl(List<Integer> stonesList) { this.myOwner = new Player(); - int boardSize = 14; + int boardSize = stonesList.size(); int bowlsToAdd = boardSize - 1; - this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, this, this.getMyOwner()); - } + this.myStones = stonesList.remove(0); + + this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, stonesList, this, this.getMyOwner()); - public SmallBowl(int boardSize) { - try { - if (boardSize < 4) { - throw new Exception("Can't have a board smaller than four bowls."); - } - } catch (Exception e) { - e.printStackTrace(); - } - this.myRocks = 4; - this.myOwner = new Player(); - int bowlsToAdd = boardSize - 1; - this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, this, this.getMyOwner()); } - SmallBowl(int boardSize, int bowlsToAdd, Bowl startBowl, Player playerOwningThisSide) { + SmallBowl(int boardSize, int bowlsToAdd, List<Integer> stonesList, Bowl startBowl, Player playerOwningThisSide) { bowlsToAdd = bowlsToAdd - 1; this.myOwner = playerOwningThisSide; - this.myRocks = 4; + this.myStones = stonesList.remove(0); if (bowlsToAdd == 0) nextBowl = startBowl; - else if (bowlsToAdd == (boardSize / 2) + 1) nextBowl = new Kalaha(boardSize, bowlsToAdd, startBowl, playerOwningThisSide); + else if (bowlsToAdd == (boardSize / 2) + 1) nextBowl = new Kalaha(boardSize, bowlsToAdd, stonesList, startBowl, playerOwningThisSide); - else if (bowlsToAdd == 1) nextBowl = new Kalaha(boardSize, bowlsToAdd, startBowl, playerOwningThisSide); + else if (bowlsToAdd == 1) nextBowl = new Kalaha(boardSize, bowlsToAdd, stonesList, startBowl, playerOwningThisSide); - else nextBowl = new SmallBowl(boardSize, bowlsToAdd, startBowl, playerOwningThisSide); + else nextBowl = new SmallBowl(boardSize, bowlsToAdd, stonesList, startBowl, playerOwningThisSide); } + public SmallBowl getNextSmallBowlTimes(int remainingTimes) { if (remainingTimes == 0) return this; @@ -52,18 +51,18 @@ public class SmallBowl extends Bowl { if (myOwner.hasTheTurn() == false) return; if (isEmpty()) return; - int passThese = myRocks; - myRocks = 0; + int passThese = myStones; + myStones = 0; getNextBowl().distribute(passThese); } @Override boolean isEmpty() { - return this.myRocks == 0; + return this.myStones == 0; } void distribute(int remainingRocks) { - this.myRocks++; + this.myStones++; // last? if (remainingRocks == 1) lastSmallBowl(); @@ -81,16 +80,16 @@ public class SmallBowl extends Bowl { endTheGame(); } - SmallBowl getSmallBowl() { + SmallBowl getNextSmallBowl() { return this; } @Override SmallBowl goToFirstBowlOfPlayerWithTurn() { if (getMyOwner().hasTheTurn()) { - return getKalaha().getNextBowl().getKalaha().getSmallBowl(); + return getKalaha().getNextBowl().getKalaha().getNextSmallBowl(); } else { - return getKalaha().getSmallBowl(); + return getKalaha().getNextSmallBowl(); } } @@ -100,14 +99,14 @@ public class SmallBowl extends Bowl { private void stealTheBooty(boolean victim) { if (victim){ - getOpposite().getKalaha().claimStolenBooty(myRocks); - myRocks = 0; + getOpposite().getKalaha().claimStolenBooty(myStones); + myStones = 0; } else if (getMyStones() == 1 && getOpposite().getMyStones() != 0) { - getKalaha().claimStolenBooty(myRocks); - myRocks = 0; + getKalaha().claimStolenBooty(myStones); + myStones = 0; getOpposite().stealTheBooty(true); } } 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<Integer> 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) { |
