diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2021-05-20 23:28:10 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2021-05-20 23:28:10 +0200 |
| commit | dd2f424ab25c73cc762ed203fccae2e481a622f4 (patch) | |
| tree | 9a5426be66c714c26c160f848521435da8fd504d | |
| parent | 37bcb87e7c23a859f34c608ac54d7a1f1dfe956a (diff) | |
betere encapsulatie?
| -rw-r--r-- | domain/src/main/java/mancala/domain/Bowl.java | 29 | ||||
| -rw-r--r-- | domain/src/main/java/mancala/domain/Kalaha.java | 57 | ||||
| -rw-r--r-- | domain/src/main/java/mancala/domain/SmallBowl.java | 133 | ||||
| -rw-r--r-- | domain/src/test/java/mancala/domain/BowlTest.java | 9 |
4 files changed, 150 insertions, 78 deletions
diff --git a/domain/src/main/java/mancala/domain/Bowl.java b/domain/src/main/java/mancala/domain/Bowl.java index 9da4f3b..8c83396 100644 --- a/domain/src/main/java/mancala/domain/Bowl.java +++ b/domain/src/main/java/mancala/domain/Bowl.java @@ -1,29 +1,20 @@ package mancala.domain; -abstract class Bowl { - protected int myRocks; - protected Player playerThatOwnsMe; - protected Bowl nextBowl; +interface Bowl { - protected int calculateBoardPosition(int startPosition, int addedBowlsCount) { + int getMyRocks(); + + Bowl getNextBowl(); + + Player getPlayerThatOwnsMe(); + + Bowl takeOneAndContinue(int remainingRocks); + + static int calculateBoardPosition(int startPosition, int addedBowlsCount) { if ((startPosition + addedBowlsCount) > 14) { return ((addedBowlsCount + startPosition) - 14); } return startPosition + addedBowlsCount; } - protected void printStateDuringConstruction(int startPosition, int addedBowlsCount) { - System.out.println("Board position: " + calculateBoardPosition(startPosition, addedBowlsCount)+ " type of bowl: " + this.getClass().toString() + ", Player pointer:" + this.playerThatOwnsMe); - } - - Bowl getNextBowl() { - return nextBowl; - } - Player getPlayerThatOwnsMe() { - return playerThatOwnsMe; - } - - int getMyRocks() { - return myRocks; - } } diff --git a/domain/src/main/java/mancala/domain/Kalaha.java b/domain/src/main/java/mancala/domain/Kalaha.java index 13e8273..c61dc34 100644 --- a/domain/src/main/java/mancala/domain/Kalaha.java +++ b/domain/src/main/java/mancala/domain/Kalaha.java @@ -1,6 +1,11 @@ package mancala.domain; -public class Kalaha extends Bowl { +import java.beans.Expression; + +public class Kalaha implements Bowl { + private int myRocks; + private final Player playerThatOwnsMe; + private final Bowl nextBowl; public Kalaha() { this.myRocks = 0; @@ -12,18 +17,44 @@ public class Kalaha extends Bowl { this.myRocks = 0; this.playerThatOwnsMe = playerOwningThisSide; - int boardPosition = calculateBoardPosition(startPosition, addedBowlsCount); - - try { - if (!(boardPosition == 7 || boardPosition == 14)) - throw new Exception("Kalaha in wrong position"); - else if (addedBowlsCount == 13) - this.nextBowl = startBowl; - else { - this.nextBowl = new SmallBowl(startPosition, ++addedBowlsCount, startBowl, playerOwningThisSide.getOpponent()); - } - } catch (Exception e) { - e.printStackTrace(); + int boardPosition = Bowl.calculateBoardPosition(startPosition, addedBowlsCount); + + if (!(boardPosition == 7 || boardPosition == 14)) { + this.nextBowl = null; + } else if (addedBowlsCount == 13) + this.nextBowl = startBowl; + else { + this.nextBowl = new SmallBowl(startPosition, ++addedBowlsCount, startBowl, playerOwningThisSide.getOpponent()); } + + + } + + public void acceptBooty(int booty) { + this.myRocks = this.myRocks + booty; + } + + @Override + public int getMyRocks() { + return this.myRocks; + } + + @Override + public Bowl getNextBowl() { + return this.nextBowl; + } + + @Override + public Player getPlayerThatOwnsMe() { + return this.playerThatOwnsMe; + } + + @Override + public Bowl takeOneAndContinue(int remainingRocks) { + this.myRocks++; + if (remainingRocks == 1) + return SmallBowl.Recursive.distributeAntiClockWise(--remainingRocks, this); + else + return SmallBowl.Recursive.distributeAntiClockWise(--remainingRocks, this.getNextBowl()); } } diff --git a/domain/src/main/java/mancala/domain/SmallBowl.java b/domain/src/main/java/mancala/domain/SmallBowl.java index 097b9c7..1e703d8 100644 --- a/domain/src/main/java/mancala/domain/SmallBowl.java +++ b/domain/src/main/java/mancala/domain/SmallBowl.java @@ -1,32 +1,74 @@ package mancala.domain; // Make your own mancala implementation using your design. -// You can take this stub as an example how to make a +// You can take this stub as an example how to make a // class inside a package and how to test it. -public class SmallBowl extends Bowl { +public class SmallBowl implements Bowl { + private int myRocks; + private final Player playerThatOwnsMe; + private final Bowl nextBowl; public SmallBowl() { - constructorHelper(1); + this(1); } public SmallBowl(int position) { // constructs board by recursively calling either kalaha or smallbowl constructors - constructorHelper(position); + this.myRocks = 4; + this.playerThatOwnsMe = new Player(); + if ((0 < position&&position < 6) || (7 < position && position < 13)) + this.nextBowl = new SmallBowl(position, 1, this, this.playerThatOwnsMe); + else if (position == 6 || position == 13) + this.nextBowl = new Kalaha(position, 1, this, this.playerThatOwnsMe); + // Special cases where smallbowl is on "wrong position" + else if (position == 7) { + this.nextBowl = new SmallBowl(1, 1, this, this.playerThatOwnsMe); + } else if (position == 14) { + this.nextBowl = new SmallBowl(8, 1, this, this.playerThatOwnsMe); + } else { + this.nextBowl = null; + } + } + + public void play() { + if ((!this.playerThatOwnsMe.hasTheTurn()) || (this.myRocks == 0)); + else { + Bowl playEndedInThisBowl = Recursive.distributeAntiClockWise(this.myRocks, this.getNextBowl()); + this.myRocks = 0; + + if (!(playEndedInThisBowl.getClass() == Kalaha.class&&playEndedInThisBowl.getPlayerThatOwnsMe().equals(this.getPlayerThatOwnsMe()))) { + this.playerThatOwnsMe.switchTurn(); + } + + if (playEndedInThisBowl.getClass() == SmallBowl.class) { + + SmallBowl playEndSmallBowl = (SmallBowl) playEndedInThisBowl; + SmallBowl opposite = playEndSmallBowl.getOpposite(); + int booty; + if (playEndSmallBowl.getMyRocks() == 1&&opposite.getMyRocks() != 0) { + booty = opposite.getMyRocks(); + booty++; + this.myRocks = 0; + opposite.myRocks = 0; + this.getKalaha().acceptBooty(booty); + } + + } + } } SmallBowl getNextSmallBowlRepeat(int i) { Recursive recursive = new Recursive(); - SmallBowl next = recursive.getNextSmallBowl(i, this); - return next; + return recursive.getNextSmallBowl(i, this); } - SmallBowl getOpposite() { + public SmallBowl getOpposite() { Bowl kalaha = this.getNextBowl(); Recursive recursive = new Recursive(); int i = 0; while (kalaha.getClass() != Kalaha.class) { i++; - kalaha.getNextBowl(); + kalaha = kalaha.getNextBowl(); } return (SmallBowl) recursive.getNextSmallBowl(i-1, (SmallBowl) kalaha.getNextBowl()); } @@ -38,29 +80,42 @@ public class SmallBowl extends Bowl { return (Kalaha) kalaha; } - public void play() { - if ((!this.playerThatOwnsMe.hasTheTurn()) || (this.myRocks == 0)); - else { - Recursive recursive = new Recursive(); - Bowl playEndedInThisBowl = recursive.distributeAntiClockWise(this.myRocks, this.getNextBowl()); - this.myRocks = 0; - if (!(playEndedInThisBowl.getClass() == Kalaha.class&&playEndedInThisBowl.getPlayerThatOwnsMe().equals(this.getPlayerThatOwnsMe()))) { - this.playerThatOwnsMe.switchTurn(); - } - } + public int getMyRocks() { + return this.myRocks; + } + + @Override + public Bowl getNextBowl() { + return nextBowl; } - private class Recursive { - Bowl distributeAntiClockWise(int remainingRocks, Bowl currentBowl) { - Boolean opponentKalahaCondition = (currentBowl.getClass() == Kalaha.class)&&!(currentBowl.getPlayerThatOwnsMe().equals(SmallBowl.this.getPlayerThatOwnsMe())); - if (remainingRocks > 1&&!opponentKalahaCondition) { - currentBowl.myRocks++; - return distributeAntiClockWise(--remainingRocks, currentBowl.getNextBowl()); - } else if (opponentKalahaCondition) { + @Override + public Player getPlayerThatOwnsMe() { + return playerThatOwnsMe; + } + + @Override + public Bowl takeOneAndContinue(int remainingRocks) { + this.myRocks++; + if (remainingRocks == 1) + return Recursive.distributeAntiClockWise(--remainingRocks, this); + else + return Recursive.distributeAntiClockWise(--remainingRocks, this.getNextBowl()); + } + + static class Recursive { + static Bowl distributeAntiClockWise(int remainingRocks, Bowl currentBowl) { + Boolean opponentKalahaCondition = (currentBowl.getClass() == Kalaha.class)&&!(currentBowl.getPlayerThatOwnsMe().hasTheTurn()); + if (remainingRocks == 0) { + return currentBowl; + } else if (opponentKalahaCondition) return distributeAntiClockWise(remainingRocks, currentBowl.getNextBowl()); + else if (currentBowl.getClass() == Kalaha.class) { + Kalaha tmpKalaha = (Kalaha) currentBowl; + return tmpKalaha.takeOneAndContinue(remainingRocks); } else { - currentBowl.myRocks++; - return currentBowl; + SmallBowl tmpSmallBowl = (SmallBowl) currentBowl; + return tmpSmallBowl.takeOneAndContinue(remainingRocks); } } @@ -75,38 +130,24 @@ public class SmallBowl extends Bowl { } } + // Recurses through board positions until connected again to startBowl SmallBowl(int startPosition, int addedBowlsCount, Bowl startBowl, Player playerOwningThisSide) { this.myRocks = 4; this.playerThatOwnsMe = playerOwningThisSide; - int boardPosition = calculateBoardPosition(startPosition, addedBowlsCount); + int boardPosition = Bowl.calculateBoardPosition(startPosition, addedBowlsCount); if (addedBowlsCount == 13) this.nextBowl = startBowl; - else if (boardPosition < 6 || (7 < boardPosition && boardPosition < 13)) + else if ((0 < boardPosition && boardPosition < 6) || (7 < boardPosition && boardPosition < 13)) this.nextBowl = new SmallBowl(startPosition, ++addedBowlsCount, startBowl, playerOwningThisSide); else if (boardPosition == 6 || boardPosition == 13) this.nextBowl = new Kalaha(startPosition, ++addedBowlsCount, startBowl, playerOwningThisSide); - } - - private void constructorHelper(int position) { - this.myRocks = 4; - this.playerThatOwnsMe = new Player(); - if (position < 6 || (7 < position && position < 13)) - this.nextBowl = new SmallBowl(position, 1, this, this.playerThatOwnsMe); - else if (position == 6 || position == 13) - this.nextBowl = new Kalaha(position, 1, this, this.playerThatOwnsMe); - else if (position == 7) { - this.nextBowl = new SmallBowl(1, 1, this, this.playerThatOwnsMe); - } else if (position == 14) { - this.nextBowl = new SmallBowl(8, 1, this, this.playerThatOwnsMe); - } - //TODO what happens if less than 0? + else + this.nextBowl = null; } - - }
\ No newline at end of file diff --git a/domain/src/test/java/mancala/domain/BowlTest.java b/domain/src/test/java/mancala/domain/BowlTest.java index 5863784..72ba444 100644 --- a/domain/src/test/java/mancala/domain/BowlTest.java +++ b/domain/src/test/java/mancala/domain/BowlTest.java @@ -167,6 +167,15 @@ class BowlTest { assertFalse(firstSmallBowlPlayer.getPlayerThatOwnsMe().hasTheTurn()); } + @Test + public void in_own_empty_small_bowl_and_opposite_has_rocks_When_player_plays_this_bowl_Then_opposite_and_rock_are_added_to_kalaha() { + firstSmallBowlPlayer.getNextSmallBowlRepeat(5).play(); + SmallBowl firstSmallBowlOpponent = firstSmallBowlPlayer.getNextSmallBowlRepeat(6); + firstSmallBowlOpponent.getNextSmallBowlRepeat(5).play(); + firstSmallBowlPlayer.play(); + assertEquals(7, firstSmallBowlPlayer.getKalaha().getMyRocks()); + } + } |
