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/main/java | |
| parent | 38d59e2876b9f4d7c589d58295ef8acdf336a45b (diff) | |
| parent | 102b25f18d9b269c58d15677f10cd71c15003c4b (diff) | |
Merge branch 'mvcFeature' into mainline
Diffstat (limited to 'domain/src/main/java')
7 files changed, 334 insertions, 56 deletions
diff --git a/domain/src/main/java/mancala/domain/Bowl.java b/domain/src/main/java/mancala/domain/Bowl.java index ade728d..07859f7 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 getMyRocks() { - return myRocks; + public int getMyStones() { + return myStones; } public Bowl getNextBowl() { @@ -25,37 +25,43 @@ abstract class Bowl { abstract Kalaha getKalaha(); - abstract SmallBowl getSmallBowl(); + abstract SmallBowl getNextSmallBowl(); + abstract SmallBowl goToFirstBowlOfPlayerWithTurn(); + + abstract boolean isEmpty(); // abstract SmallBowl getNextSmallBowl(); void endTheGame() { - getNextBowl().endTheGame(this, 0, 0); + goToFirstBowlOfPlayerWithTurn().getNextBowl().endTheGame(goToFirstBowlOfPlayerWithTurn(), 0, 0); } - abstract boolean isEmpty(); - - private void endTheGame(Bowl startOfLoop, int scorePlayer, int scoreOpponent) { - if (isEmpty() == false && myOwner.hasTheTurn()) return; - - if (getMyOwner().equals(startOfLoop.getMyOwner())) { - scorePlayer = scorePlayer + getMyRocks(); - } else scoreOpponent = scoreOpponent + getMyRocks(); + protected void endTheGame(Bowl startOfLoop, int scorePlayer, int scoreOpponent) { + if (isEmpty() == false && getMyOwner().equals(startOfLoop.getMyOwner())) return; if (this.equals(startOfLoop)) { - int playerKalaha = getKalaha().getMyRocks(); + int playerKalaha = getKalaha().getMyStones(); if (scorePlayer == playerKalaha) { if (scorePlayer == scoreOpponent) getMyOwner().gotADraw(); else if (scorePlayer > scoreOpponent) getMyOwner().isTheWinner(); else getMyOwner().getOpponent().isTheWinner(); - } - } else getNextBowl().endTheGame(startOfLoop, scorePlayer, scoreOpponent); + } else { + if (getMyOwner().equals(startOfLoop.getMyOwner())) { + scorePlayer = scorePlayer + getMyStones(); + } else scoreOpponent = scoreOpponent + getMyStones(); + + getNextBowl().endTheGame(startOfLoop, scorePlayer, scoreOpponent); + } } + + protected abstract String makeString(String playerBowls, String opponentBowls, String kalahas); + + protected abstract int[] toStateArray(int[] stateArray, int index); } diff --git a/domain/src/main/java/mancala/domain/DomainSmallBowlException.java b/domain/src/main/java/mancala/domain/DomainSmallBowlException.java new file mode 100644 index 0000000..56e5de1 --- /dev/null +++ b/domain/src/main/java/mancala/domain/DomainSmallBowlException.java @@ -0,0 +1,7 @@ +package mancala.domain; + +public class DomainSmallBowlException extends Exception { + public DomainSmallBowlException(String message) { + super(message); + } +} diff --git a/domain/src/main/java/mancala/domain/Kalaha.java b/domain/src/main/java/mancala/domain/Kalaha.java index 9e70e6c..411a86d 100644 --- a/domain/src/main/java/mancala/domain/Kalaha.java +++ b/domain/src/main/java/mancala/domain/Kalaha.java @@ -1,23 +1,34 @@ 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().getNextSmallBowl(); + } else { + return getNextSmallBowl(); + } } SmallBowl getOpposite(int countTillThis) { @@ -29,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(); @@ -44,8 +55,34 @@ class Kalaha extends Bowl { return true; } + @Override + protected String makeString(String playerBowls, String opponentBowls, String kalahas) { + if (getMyOwner().equals(SmallBowl.referencePoint.getMyOwner().getOpponent())) { + return " " + opponentBowls + "\n" + + getMyStones() + "\t\t\t\t " + kalahas + "\n" + + " " + playerBowls; + } + else { + return getNextBowl().makeString( + playerBowls, + opponentBowls, + kalahas + getMyStones()); + } + } void claimStolenBooty(int booty) { - myRocks = myRocks + booty; + myStones = myStones + booty; } + + @Override + protected int[] toStateArray(int[] stateArray, int index) { + stateArray[index] = getMyStones(); + if (index == stateArray.length - 2) { + stateArray[stateArray.length - 1] = (getMyOwner().hasTheTurn() ? Mancala.PLAYER_TWO : Mancala.PLAYER_ONE); + return stateArray; + } else { + return getNextBowl().toStateArray(stateArray, ++index); + } + } + } diff --git a/domain/src/main/java/mancala/domain/Mancala.java b/domain/src/main/java/mancala/domain/Mancala.java new file mode 100644 index 0000000..d80e9e5 --- /dev/null +++ b/domain/src/main/java/mancala/domain/Mancala.java @@ -0,0 +1,55 @@ +package mancala.domain; + +public interface Mancala { + public static final int NO_PLAYERS = 0; + public static final int PLAYER_ONE = 1; + public static final int PLAYER_TWO = 2; + public static final int BOTH_PLAYERS = 3; + + /** + * 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. + */ + boolean isPlayersTurn(int player); + + /** + * 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). + */ + int[] playPit(int index) throws MancalaException; + + /** + * 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. + */ + int getStonesForPit(int index); + + /** + * Method for retrieving whether the game has ended or not. + * + * @return True is the game has ended otherwise False. + */ + boolean isEndOfGame(); + + /** + * Method for retrieving the player that has won the game. + * + * @return Integer value representing which player(s) (if any) won the game. + */ + int getWinner(); + +} diff --git a/domain/src/main/java/mancala/domain/MancalaException.java b/domain/src/main/java/mancala/domain/MancalaException.java new file mode 100644 index 0000000..783b229 --- /dev/null +++ b/domain/src/main/java/mancala/domain/MancalaException.java @@ -0,0 +1,7 @@ +package mancala.domain; + +public class MancalaException extends Exception { + public MancalaException(String message) { + super(message); + } +}
\ No newline at end of file diff --git a/domain/src/main/java/mancala/domain/MancalaImpl.java b/domain/src/main/java/mancala/domain/MancalaImpl.java new file mode 100644 index 0000000..576eeb4 --- /dev/null +++ b/domain/src/main/java/mancala/domain/MancalaImpl.java @@ -0,0 +1,117 @@ +package mancala.domain; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class MancalaImpl implements Mancala { + + public static final HashSet<Integer> PLAYER_ONE_PITS = new HashSet<>( + IntStream.rangeClosed(0, 5).boxed().collect(Collectors.toList()) + ); + public static final HashSet<Integer> PLAYER_TWO_PITS = new HashSet<>( + IntStream.rangeClosed(7, 12).boxed().collect(Collectors.toList()) + ); + public static final int PLAYER_ONE_KALAHA = 6; + public static final int PLAYER_TWO_KALAHA = 13; + + private SmallBowl domainReference; + + public MancalaImpl() { + try { + domainReference = new SmallBowl(); + } catch (DomainSmallBowlException e) { + e.printStackTrace(); + } + domainPlayer = domainReference.getMyOwner(); + domainOpponent = domainPlayer.getOpponent(); + } + + public MancalaImpl(int[] stonesArray) { + try { + domainReference = new SmallBowl( + Arrays.stream(stonesArray) + .boxed().collect(Collectors.toList()) + ); + } catch (DomainSmallBowlException e) { + e.printStackTrace(); + } + domainPlayer = domainReference.getMyOwner(); + domainOpponent = domainPlayer.getOpponent(); + } + + @Override + public boolean isPlayersTurn(int player) { + switch (player) { + case Mancala.PLAYER_ONE: + return domainReference.getMyOwner().hasTheTurn(); + case Mancala.PLAYER_TWO: + return domainReference.getMyOwner().getOpponent().hasTheTurn(); + default: + return false; + } + } + + @Override + public int[] playPit(int index) throws MancalaException { + if (isPlayersTurn(Mancala.PLAYER_ONE) && MancalaImpl.PLAYER_TWO_PITS.contains(index)) { + throw new MancalaException("Player one cannot play player two's pits."); + } + if (isPlayersTurn(Mancala.PLAYER_TWO) && MancalaImpl.PLAYER_ONE_PITS.contains(index)) { + throw new MancalaException("Player two cannot play player one's pits."); + } + if (index == MancalaImpl.PLAYER_ONE_KALAHA || index == MancalaImpl.PLAYER_TWO_KALAHA) { + throw new MancalaException("A kalaha can never be played!"); + } + if (getStonesForPit(index) == 0) { + throw new MancalaException("The pit was empty when played!"); + } + + if (isPlayersTurn(Mancala.PLAYER_ONE)) { + domainReference.getNextSmallBowlTimes(index).play(); + } else { + int skipKalahaIndex = index - 1; + domainReference.getNextSmallBowlTimes(skipKalahaIndex).play(); + } + + return domainReference.toStateArray(new int[15], 0); + } + + @Override + public int getStonesForPit(int index) { + if (MancalaImpl.PLAYER_ONE_PITS.contains(index)) { + return domainReference.getNextSmallBowlTimes(index).getMyStones(); + } + else if (MancalaImpl.PLAYER_TWO_PITS.contains(index)) { + return domainReference.getNextSmallBowlTimes(--index).getMyStones(); + } + else if (index == MancalaImpl.PLAYER_ONE_KALAHA) { + return domainReference.getKalaha().getMyStones(); + } + else if (index == MancalaImpl.PLAYER_TWO_KALAHA) { + return domainReference.getKalaha().getNextBowl().getKalaha().getMyStones(); + } + else + return -1; + } + + private final mancala.domain.Player domainPlayer; + private final mancala.domain.Player domainOpponent; + + @Override + public boolean isEndOfGame() { + return domainPlayer.won() || domainOpponent.won(); + } + + @Override + public int getWinner() { + if (!isEndOfGame()) return Mancala.NO_PLAYERS; + + if (domainPlayer.won() && domainOpponent.won()) return Mancala.BOTH_PLAYERS; + else if (domainPlayer.won()) return Mancala.PLAYER_ONE; + else if (domainOpponent.won()) return Mancala.PLAYER_TWO; + else return Mancala.NO_PLAYERS; + } + +} diff --git a/domain/src/main/java/mancala/domain/SmallBowl.java b/domain/src/main/java/mancala/domain/SmallBowl.java index 5c97d72..b62ef43 100644 --- a/domain/src/main/java/mancala/domain/SmallBowl.java +++ b/domain/src/main/java/mancala/domain/SmallBowl.java @@ -1,45 +1,53 @@ package mancala.domain; -public class SmallBowl extends Bowl { +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; - public SmallBowl() { - this.myRocks = 4; - this.myOwner = new Player(); +public class SmallBowl extends Bowl { - int boardSize = 14; - int bowlsToAdd = boardSize - 1; + public static SmallBowl referencePoint; - this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, this, this.getMyOwner()); + public SmallBowl() throws DomainSmallBowlException { + this(Arrays.stream(new int[] {4,4,4,4,4,4,0,4,4,4,4,4,4,0}).boxed().collect(Collectors.toList())); + referencePoint = this; } - 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(); + public SmallBowl(List<Integer> stonesList) throws DomainSmallBowlException { + if (stonesList.size() % 2 != 0) { + throw new DomainSmallBowlException("Stones List should contain even number of elements."); + } + if (stonesList.size() < 4) { + throw new DomainSmallBowlException("Stones list should have length greater than or equal to 4."); } - this.myRocks = 4; this.myOwner = new Player(); + + int boardSize = stonesList.size(); int bowlsToAdd = boardSize - 1; - this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, this, this.getMyOwner()); + + this.myStones = stonesList.remove(0); + + if (boardSize == 4) this.nextBowl = new Kalaha(boardSize, bowlsToAdd, stonesList, this, this.getMyOwner()); + else this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, stonesList, this, this.getMyOwner()); + + referencePoint = this; } - 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 +60,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(); @@ -76,29 +84,42 @@ public class SmallBowl extends Bowl { // Did play end in smallbowl of my player? steal, otherwise do nothing if (getMyOwner().hasTheTurn()) stealTheBooty(false); + endTheGame(); + getMyOwner().switchTurn(); endTheGame(); } - SmallBowl getSmallBowl() { + SmallBowl getNextSmallBowl() { return this; } + @Override + SmallBowl goToFirstBowlOfPlayerWithTurn() { + if (getMyOwner().hasTheTurn()) { + return getKalaha().getNextBowl().getKalaha().getNextSmallBowl(); + } else { + return getKalaha().getNextSmallBowl(); + } + } + Kalaha getKalaha() { return getNextBowl().getKalaha(); } private void stealTheBooty(boolean victim) { if (victim){ - getOpposite().getKalaha().claimStolenBooty(myRocks); - myRocks = 0; + System.out.println("help! i'm being robbed, my precious " + getMyStones() + " stones are gone :(."); + getOpposite().getKalaha().claimStolenBooty(myStones); + myStones = 0; - } else if (getMyRocks() == 1 && - getOpposite().getMyRocks() != 0) { + } else if (getMyStones() == 1 && + getOpposite().getMyStones() != 0) { + System.out.println("stealing"); - getKalaha().claimStolenBooty(myRocks); - myRocks = 0; + getKalaha().claimStolenBooty(myStones); + myStones = 0; getOpposite().stealTheBooty(true); } } @@ -111,4 +132,32 @@ public class SmallBowl extends Bowl { count = count + 1; return getNextBowl().getOpposite(count); } + + public String stateString() { + return SmallBowl.referencePoint.makeString("", "", ""); + } + + protected String makeString(String playerBowls, String opponentBowls, String kalahas) { + if (!this.getMyOwner().equals(SmallBowl.referencePoint.getMyOwner())) { + return getNextBowl().makeString( + playerBowls, + (getMyStones() + ", ") + opponentBowls, + kalahas); + } else { + return getNextBowl().makeString( + playerBowls + (playerBowls.equals("") ? getMyStones() : ", " + getMyStones()), + opponentBowls, + kalahas); + } + } + + @Override + protected int[] toStateArray(int[] stateArray, int index) { + stateArray[index] = getMyStones(); + if (index == stateArray.length - 2) { + return stateArray; + } else { + return getNextBowl().toStateArray(stateArray, ++index); + } + } } |
