summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--domain/src/main/java/mancala/domain/Bowl.java2
-rw-r--r--domain/src/main/java/mancala/domain/Kalaha.java39
-rw-r--r--domain/src/main/java/mancala/domain/SmallBowl.java83
3 files changed, 66 insertions, 58 deletions
diff --git a/domain/src/main/java/mancala/domain/Bowl.java b/domain/src/main/java/mancala/domain/Bowl.java
index 8c83396..b96cbfa 100644
--- a/domain/src/main/java/mancala/domain/Bowl.java
+++ b/domain/src/main/java/mancala/domain/Bowl.java
@@ -8,8 +8,6 @@ interface Bowl {
Player getPlayerThatOwnsMe();
- Bowl takeOneAndContinue(int remainingRocks);
-
static int calculateBoardPosition(int startPosition, int addedBowlsCount) {
if ((startPosition + addedBowlsCount) > 14) {
return ((addedBowlsCount + startPosition) - 14);
diff --git a/domain/src/main/java/mancala/domain/Kalaha.java b/domain/src/main/java/mancala/domain/Kalaha.java
index c61dc34..f22bf62 100644
--- a/domain/src/main/java/mancala/domain/Kalaha.java
+++ b/domain/src/main/java/mancala/domain/Kalaha.java
@@ -1,8 +1,6 @@
package mancala.domain;
-import java.beans.Expression;
-
-public class Kalaha implements Bowl {
+class Kalaha implements Bowl {
private int myRocks;
private final Player playerThatOwnsMe;
private final Bowl nextBowl;
@@ -26,12 +24,6 @@ public class Kalaha implements Bowl {
else {
this.nextBowl = new SmallBowl(startPosition, ++addedBowlsCount, startBowl, playerOwningThisSide.getOpponent());
}
-
-
- }
-
- public void acceptBooty(int booty) {
- this.myRocks = this.myRocks + booty;
}
@Override
@@ -49,12 +41,31 @@ public class Kalaha implements Bowl {
return this.playerThatOwnsMe;
}
- @Override
- public Bowl takeOneAndContinue(int remainingRocks) {
+ private Bowl takeOneAndContinue(int remainingRocks) {
this.myRocks++;
if (remainingRocks == 1)
- return SmallBowl.Recursive.distributeAntiClockWise(--remainingRocks, this);
- else
- return SmallBowl.Recursive.distributeAntiClockWise(--remainingRocks, this.getNextBowl());
+ return new RecursiveFlipFlop().distributeAntiClockWise(--remainingRocks, this);
+ else {
+ SmallBowl smallBowl = (SmallBowl) this.getNextBowl();
+ return smallBowl.new RecursiveFlipFlop().distributeAntiClockWise(--remainingRocks, smallBowl);
+ }
+ }
+
+ class RecursiveFlipFlop {
+ Bowl distributeAntiClockWise(int remainingRocks, Kalaha currentBowl) {
+ if (remainingRocks == 0)
+ return currentBowl;
+ else if (!(Kalaha.this.getPlayerThatOwnsMe().hasTheTurn())) {
+ SmallBowl smallBowl = (SmallBowl) currentBowl.getNextBowl();
+ return smallBowl.new RecursiveFlipFlop().distributeAntiClockWise(remainingRocks, smallBowl);
+ } else {
+ return takeOneAndContinue(remainingRocks);
+ }
+
+ }
+ }
+
+ void acceptBooty(int booty) {
+ this.myRocks = this.myRocks + booty;
}
}
diff --git a/domain/src/main/java/mancala/domain/SmallBowl.java b/domain/src/main/java/mancala/domain/SmallBowl.java
index 1e703d8..01fb966 100644
--- a/domain/src/main/java/mancala/domain/SmallBowl.java
+++ b/domain/src/main/java/mancala/domain/SmallBowl.java
@@ -33,7 +33,7 @@ public class SmallBowl implements Bowl {
public void play() {
if ((!this.playerThatOwnsMe.hasTheTurn()) || (this.myRocks == 0));
else {
- Bowl playEndedInThisBowl = Recursive.distributeAntiClockWise(this.myRocks, this.getNextBowl());
+ Bowl playEndedInThisBowl = new RecursiveFlipFlop().distributeAntiClockWise(this.myRocks, this.getNextBowl());
this.myRocks = 0;
if (!(playEndedInThisBowl.getClass() == Kalaha.class&&playEndedInThisBowl.getPlayerThatOwnsMe().equals(this.getPlayerThatOwnsMe()))) {
@@ -52,34 +52,33 @@ public class SmallBowl implements Bowl {
opposite.myRocks = 0;
this.getKalaha().acceptBooty(booty);
}
-
}
- }
- }
- SmallBowl getNextSmallBowlRepeat(int i) {
- Recursive recursive = new Recursive();
- return recursive.getNextSmallBowl(i, this);
+ // TODO everyting empty?
+
+ // TODO player rocks greater?
+ }
}
public SmallBowl getOpposite() {
Bowl kalaha = this.getNextBowl();
- Recursive recursive = new Recursive();
+ RecursiveFlipFlop recursiveFlipFlop = new RecursiveFlipFlop();
int i = 0;
while (kalaha.getClass() != Kalaha.class) {
i++;
kalaha = kalaha.getNextBowl();
}
- return (SmallBowl) recursive.getNextSmallBowl(i-1, (SmallBowl) kalaha.getNextBowl());
+ return (SmallBowl) recursiveFlipFlop.getNextSmallBowl(i-1, (SmallBowl) kalaha.getNextBowl());
}
- Kalaha getKalaha() {
+ public Kalaha getKalaha() {
Bowl kalaha = this.getNextBowl();
while (kalaha.getClass() != Kalaha.class)
kalaha = kalaha.getNextBowl();
return (Kalaha) kalaha;
}
+ @Override
public int getMyRocks() {
return this.myRocks;
}
@@ -89,48 +88,24 @@ public class SmallBowl implements Bowl {
return nextBowl;
}
+ public SmallBowl getNextSmallBowlRepeat(int i) {
+ RecursiveFlipFlop recursiveFlipFlop = new RecursiveFlipFlop();
+ return recursiveFlipFlop.getNextSmallBowl(i, this);
+ }
+
@Override
public Player getPlayerThatOwnsMe() {
return playerThatOwnsMe;
}
- @Override
- public Bowl takeOneAndContinue(int remainingRocks) {
+ private Bowl takeOneAndContinue(int remainingRocks) {
this.myRocks++;
if (remainingRocks == 1)
- return Recursive.distributeAntiClockWise(--remainingRocks, this);
+ return new RecursiveFlipFlop().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 {
- SmallBowl tmpSmallBowl = (SmallBowl) currentBowl;
- return tmpSmallBowl.takeOneAndContinue(remainingRocks);
- }
- }
-
- SmallBowl getNextSmallBowl(int remainingNexts, Bowl currentBowl) {
- if (remainingNexts > 0)
- return getNextSmallBowl(--remainingNexts, currentBowl.getNextBowl());
- else if (currentBowl.getClass() == Kalaha.class)
- return getNextSmallBowl(remainingNexts, currentBowl.getNextBowl());
- else
- return (SmallBowl) currentBowl;
-
- }
+ return new RecursiveFlipFlop().distributeAntiClockWise(--remainingRocks, this.getNextBowl());
}
-
// Recurses through board positions until connected again to startBowl
SmallBowl(int startPosition, int addedBowlsCount, Bowl startBowl, Player playerOwningThisSide) {
this.myRocks = 4;
@@ -150,4 +125,28 @@ public class SmallBowl implements Bowl {
else
this.nextBowl = null;
}
+
+ class RecursiveFlipFlop {
+ Bowl distributeAntiClockWise(int remainingRocks, Bowl currentBowl) {
+ if (remainingRocks == 0)
+ return currentBowl;
+ else if (currentBowl.getClass() == Kalaha.class) {
+ Kalaha tmpKalaha = (Kalaha) currentBowl;
+ return tmpKalaha.new RecursiveFlipFlop().distributeAntiClockWise(remainingRocks, tmpKalaha);
+ } else {
+ SmallBowl tmpSmallBowl = (SmallBowl) currentBowl;
+ return tmpSmallBowl.takeOneAndContinue(remainingRocks);
+ }
+ }
+
+ SmallBowl getNextSmallBowl(int remainingNexts, Bowl currentBowl) {
+ if (remainingNexts > 0)
+ return getNextSmallBowl(--remainingNexts, currentBowl.getNextBowl());
+ else if (currentBowl.getClass() == Kalaha.class)
+ return getNextSmallBowl(remainingNexts, currentBowl.getNextBowl());
+ else
+ return (SmallBowl) currentBowl;
+
+ }
+ }
} \ No newline at end of file