summaryrefslogtreecommitdiff
path: root/domain/src/main/java
diff options
context:
space:
mode:
authorMike Vink <mike1994vink@gmail.com>2021-06-21 18:13:31 +0200
committerMike Vink <mike1994vink@gmail.com>2021-06-21 18:13:31 +0200
commit653764b4fcd3fd951cb1ca7b7993a1736fd9dda6 (patch)
tree9c09d18a4c91a4641c88285aa730f19451ea5ce7 /domain/src/main/java
parentd70d47add575d03d43fbf1ce81b32734e556c4b5 (diff)
merge mainline domain with mvcFeature domain
Diffstat (limited to 'domain/src/main/java')
-rw-r--r--domain/src/main/java/mancala/domain/Bowl.java61
-rw-r--r--domain/src/main/java/mancala/domain/Kalaha.java51
-rw-r--r--domain/src/main/java/mancala/domain/Player.java52
-rw-r--r--domain/src/main/java/mancala/domain/SmallBowl.java114
4 files changed, 278 insertions, 0 deletions
diff --git a/domain/src/main/java/mancala/domain/Bowl.java b/domain/src/main/java/mancala/domain/Bowl.java
new file mode 100644
index 0000000..ade728d
--- /dev/null
+++ b/domain/src/main/java/mancala/domain/Bowl.java
@@ -0,0 +1,61 @@
+package mancala.domain;
+
+abstract class Bowl {
+ protected int myRocks;
+ protected Player myOwner;
+ protected Bowl nextBowl;
+
+ public int getMyRocks() {
+ return myRocks;
+ }
+
+ public Bowl getNextBowl() {
+ return nextBowl;
+ }
+
+ public Player getMyOwner() {
+ return myOwner;
+ }
+
+ abstract void distribute(int remainingRocks);
+
+ abstract SmallBowl getOpposite(int i);
+
+ abstract SmallBowl getNextSmallBowlTimes(int i);
+
+ abstract Kalaha getKalaha();
+
+ abstract SmallBowl getSmallBowl();
+
+ // abstract SmallBowl getNextSmallBowl();
+
+ void endTheGame() {
+ getNextBowl().endTheGame(this, 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();
+
+ if (this.equals(startOfLoop)) {
+
+ int playerKalaha = getKalaha().getMyRocks();
+
+ if (scorePlayer == playerKalaha) {
+
+ if (scorePlayer == scoreOpponent) getMyOwner().gotADraw();
+ else if (scorePlayer > scoreOpponent) getMyOwner().isTheWinner();
+ else getMyOwner().getOpponent().isTheWinner();
+
+ }
+
+
+ } else getNextBowl().endTheGame(startOfLoop, scorePlayer, scoreOpponent);
+ }
+
+}
diff --git a/domain/src/main/java/mancala/domain/Kalaha.java b/domain/src/main/java/mancala/domain/Kalaha.java
new file mode 100644
index 0000000..9e70e6c
--- /dev/null
+++ b/domain/src/main/java/mancala/domain/Kalaha.java
@@ -0,0 +1,51 @@
+package mancala.domain;
+
+class Kalaha extends Bowl {
+ Kalaha(int boardSize, int bowlsToAdd, Bowl startBowl, Player playerOwningThisSide) {
+ bowlsToAdd = bowlsToAdd - 1;
+
+ this.myRocks = 0;
+ this.myOwner = playerOwningThisSide;
+
+ if (bowlsToAdd == 0) this.nextBowl = startBowl;
+
+ else this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, startBowl, playerOwningThisSide.getOpponent());
+ }
+
+ Kalaha getKalaha() {
+ return this;
+ }
+
+ SmallBowl getSmallBowl() {
+ return getNextBowl().getSmallBowl();
+ }
+
+ SmallBowl getOpposite(int countTillThis) {
+ return getNextBowl().getNextSmallBowlTimes(countTillThis - 1);
+ }
+
+ SmallBowl getNextSmallBowlTimes(int i) {
+ return getNextBowl().getNextSmallBowlTimes(i);
+ }
+
+ void distribute(int remainingRocks) {
+ myRocks++;
+ // Skip?
+ if (getMyOwner().hasTheTurn() == false) {
+ myRocks--;
+ getNextBowl().distribute(remainingRocks);
+ } else if (remainingRocks == 1) {
+ endTheGame();
+ } else getNextBowl().distribute(--remainingRocks);
+ }
+
+ @Override
+ boolean isEmpty() {
+ return true;
+ }
+
+
+ void claimStolenBooty(int booty) {
+ myRocks = myRocks + booty;
+ }
+}
diff --git a/domain/src/main/java/mancala/domain/Player.java b/domain/src/main/java/mancala/domain/Player.java
new file mode 100644
index 0000000..1eef716
--- /dev/null
+++ b/domain/src/main/java/mancala/domain/Player.java
@@ -0,0 +1,52 @@
+package mancala.domain;
+
+public class Player {
+ private boolean hasTheTurn;
+ private boolean isTheWinner;
+ final private Player opponent;
+
+ public Player() {
+ this.hasTheTurn = true;
+
+ this.opponent = new Player(this);
+ }
+
+ private Player(Player opponent) {
+ this.hasTheTurn = false;
+
+ this.opponent = opponent;
+ }
+
+ public Player getOpponent() {
+ return opponent;
+ }
+
+ public boolean hasTheTurn() {
+ return hasTheTurn;
+ }
+
+ public void switchTurn() {
+ if (this.hasTheTurn == true) {
+ this.hasTheTurn = false;
+ this.opponent.hasTheTurn = true;
+ } else {
+ this.hasTheTurn = true;
+ this.opponent.hasTheTurn = false;
+ }
+ }
+
+ public boolean won() {
+ return this.isTheWinner;
+ }
+
+ void isTheWinner() {
+ this.isTheWinner = true;
+ this.opponent.isTheWinner = false;
+ }
+
+ void gotADraw() {
+ this.isTheWinner = true;
+ this.opponent.isTheWinner = true;
+ }
+
+}
diff --git a/domain/src/main/java/mancala/domain/SmallBowl.java b/domain/src/main/java/mancala/domain/SmallBowl.java
new file mode 100644
index 0000000..5c97d72
--- /dev/null
+++ b/domain/src/main/java/mancala/domain/SmallBowl.java
@@ -0,0 +1,114 @@
+package mancala.domain;
+
+public class SmallBowl extends Bowl {
+
+ public SmallBowl() {
+ this.myRocks = 4;
+ this.myOwner = new Player();
+
+ int boardSize = 14;
+ int bowlsToAdd = boardSize - 1;
+
+ this.nextBowl = new SmallBowl(boardSize, bowlsToAdd, 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) {
+ bowlsToAdd = bowlsToAdd - 1;
+ this.myOwner = playerOwningThisSide;
+ this.myRocks = 4;
+
+ if (bowlsToAdd == 0) nextBowl = startBowl;
+
+ else if (bowlsToAdd == (boardSize / 2) + 1) nextBowl = new Kalaha(boardSize, bowlsToAdd, startBowl, playerOwningThisSide);
+
+ else if (bowlsToAdd == 1) nextBowl = new Kalaha(boardSize, bowlsToAdd, startBowl, playerOwningThisSide);
+
+ else nextBowl = new SmallBowl(boardSize, bowlsToAdd, startBowl, playerOwningThisSide);
+ }
+
+ public SmallBowl getNextSmallBowlTimes(int remainingTimes) {
+ if (remainingTimes == 0)
+ return this;
+ else {
+ return getNextBowl().getNextSmallBowlTimes(--remainingTimes);
+ }
+ }
+
+ public void play() {
+ if (myOwner.hasTheTurn() == false) return;
+ if (isEmpty()) return;
+
+ int passThese = myRocks;
+ myRocks = 0;
+ getNextBowl().distribute(passThese);
+ }
+
+ @Override
+ boolean isEmpty() {
+ return this.myRocks == 0;
+ }
+
+ void distribute(int remainingRocks) {
+ this.myRocks++;
+ // last?
+ if (remainingRocks == 1)
+ lastSmallBowl();
+ else {
+ getNextBowl().distribute(--remainingRocks);
+ }
+ }
+
+ private void lastSmallBowl() {
+ // Did play end in smallbowl of my player? steal, otherwise do nothing
+ if (getMyOwner().hasTheTurn()) stealTheBooty(false);
+
+ getMyOwner().switchTurn();
+
+ endTheGame();
+ }
+
+ SmallBowl getSmallBowl() {
+ return this;
+ }
+
+ Kalaha getKalaha() {
+ return getNextBowl().getKalaha();
+ }
+
+ private void stealTheBooty(boolean victim) {
+ if (victim){
+ getOpposite().getKalaha().claimStolenBooty(myRocks);
+ myRocks = 0;
+
+ } else if (getMyRocks() == 1 &&
+ getOpposite().getMyRocks() != 0) {
+
+ getKalaha().claimStolenBooty(myRocks);
+ myRocks = 0;
+ getOpposite().stealTheBooty(true);
+ }
+ }
+
+ SmallBowl getOpposite() {
+ return getOpposite(0);
+ }
+
+ SmallBowl getOpposite(int count) {
+ count = count + 1;
+ return getNextBowl().getOpposite(count);
+ }
+}