summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mock/jrummikub/model/MockRoundState.java20
-rw-r--r--src/jrummikub/control/GameControl.java3
-rw-r--r--src/jrummikub/control/RoundControl.java22
-rw-r--r--src/jrummikub/model/IRoundState.java21
-rw-r--r--src/jrummikub/model/RoundState.java19
-rw-r--r--src/jrummikub/model/StoneHeap.java4
-rw-r--r--test/jrummikub/control/RoundControlTest.java3
7 files changed, 82 insertions, 10 deletions
diff --git a/mock/jrummikub/model/MockRoundState.java b/mock/jrummikub/model/MockRoundState.java
index 5f478fb..32fce33 100644
--- a/mock/jrummikub/model/MockRoundState.java
+++ b/mock/jrummikub/model/MockRoundState.java
@@ -20,7 +20,9 @@ public class MockRoundState implements IRoundState {
public StoneHeap gameHeap;
/** */
public GameSettings gameSettings;
-
+ /** */
+ public IPlayer lastPlayer;
+
/** */
public MockRoundState() {
table = new MockTable();
@@ -63,7 +65,11 @@ public class MockRoundState implements IRoundState {
@Override
public IPlayer getNthNextPlayer(int i) {
- return players.get((activePlayer + i) % players.size());
+ int j = (activePlayer + i) % players.size();
+ if (j < 0) {
+ j += players.size();
+ }
+ return players.get(j);
}
@Override
@@ -75,4 +81,14 @@ public class MockRoundState implements IRoundState {
public GameSettings getGameSettings() {
return gameSettings;
}
+
+ @Override
+ public IPlayer getLastPlayer() {
+ return lastPlayer;
+ }
+
+ @Override
+ public void setLastPlayer(IPlayer lastPlayer) {
+ this.lastPlayer = lastPlayer;
+ }
}
diff --git a/src/jrummikub/control/GameControl.java b/src/jrummikub/control/GameControl.java
index b5d763a..1d93c63 100644
--- a/src/jrummikub/control/GameControl.java
+++ b/src/jrummikub/control/GameControl.java
@@ -1,6 +1,7 @@
package jrummikub.control;
import jrummikub.model.GameSettings;
+import jrummikub.model.IRoundState;
import jrummikub.model.RoundState;
import jrummikub.util.IListener;
import jrummikub.view.IView;
@@ -51,7 +52,7 @@ public class GameControl {
return;
}
- RoundState roundState = new RoundState(gameSettings);
+ IRoundState roundState = new RoundState(gameSettings);
roundControl = new RoundControl(roundState, view);
roundControl.getEndRoundEvent().add(new IListener() {
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java
index 072aa7f..2080cf4 100644
--- a/src/jrummikub/control/RoundControl.java
+++ b/src/jrummikub/control/RoundControl.java
@@ -28,6 +28,7 @@ public class RoundControl {
private ITable clonedTable;
private Event endRoundEvent = new Event();
private List<Connection> connections = new ArrayList<Connection>();
+ private boolean roundFinished;
/**
* Create a new RoundControl using the given gameState and view
@@ -123,8 +124,24 @@ public class RoundControl {
private void endOfTurn() {
checkTurn();
- roundState.nextPlayer();
- prepareTurn();
+
+ if (roundState.getLastPlayer() == null) {
+ if (roundState.getGameHeap().getSize() == 0) {
+ roundState.setLastPlayer(roundState.getNthNextPlayer(-1));
+ } else {
+ roundState.nextPlayer();
+ }
+ } else {
+ if (roundState.getActivePlayer() == roundState.getLastPlayer()) {
+ // TODO check who has won
+ win();
+ } else {
+ roundState.nextPlayer();
+ }
+ }
+ if (!roundFinished) {
+ prepareTurn();
+ }
}
private void checkTurn() {
@@ -224,5 +241,6 @@ public class RoundControl {
}
endRoundEvent.emit();
view.enableWinPanel(true);
+ roundFinished = true;
}
}
diff --git a/src/jrummikub/model/IRoundState.java b/src/jrummikub/model/IRoundState.java
index af7a478..dea225b 100644
--- a/src/jrummikub/model/IRoundState.java
+++ b/src/jrummikub/model/IRoundState.java
@@ -23,7 +23,7 @@ public interface IRoundState {
* Sets the current {@link Table}
*
* @param table
- * The new Table
+ * The new Table
*/
public void setTable(ITable table);
@@ -55,9 +55,26 @@ public interface IRoundState {
* Returns the player that would be the active player after i turns
*
* @param i
- * number of turns
+ * number of turns
* @return player active after i turns
*/
public IPlayer getNthNextPlayer(int i);
+ /**
+ * Sets the player that will make the last turn before the round ends when
+ * the heap is empty
+ *
+ * @param lastPlayer
+ * the last player
+ */
+ public abstract IPlayer getLastPlayer();
+
+ /**
+ * Gets the player that will make the last turn before the round ends when
+ * the heap is empty
+ *
+ * @return lastPlayer the last player
+ */
+ public abstract void setLastPlayer(IPlayer lastPlayer);
+
} \ No newline at end of file
diff --git a/src/jrummikub/model/RoundState.java b/src/jrummikub/model/RoundState.java
index 56f7904..0bda737 100644
--- a/src/jrummikub/model/RoundState.java
+++ b/src/jrummikub/model/RoundState.java
@@ -11,12 +11,13 @@ public class RoundState implements IRoundState {
private List<Player> players;
private int activePlayer;
private StoneHeap gameHeap;
+ private IPlayer lastPlayer;
/**
* Create a new RoundState with an empty table
*
* @param gameSettings
- * the game settings
+ * the game settings
*/
public RoundState(GameSettings gameSettings) {
this.gameSettings = gameSettings;
@@ -54,7 +55,11 @@ public class RoundState implements IRoundState {
@Override
public IPlayer getNthNextPlayer(int i) {
- return players.get((activePlayer + i) % players.size());
+ int j = (activePlayer + i) % players.size();
+ if (j < 0) {
+ j += players.size();
+ }
+ return players.get(j);
}
@Override
@@ -71,4 +76,14 @@ public class RoundState implements IRoundState {
public GameSettings getGameSettings() {
return gameSettings;
}
+
+ @Override
+ public void setLastPlayer(IPlayer lastPlayer) {
+ this.lastPlayer = lastPlayer;
+ }
+
+ @Override
+ public IPlayer getLastPlayer() {
+ return lastPlayer;
+ }
}
diff --git a/src/jrummikub/model/StoneHeap.java b/src/jrummikub/model/StoneHeap.java
index c013570..048f6e7 100644
--- a/src/jrummikub/model/StoneHeap.java
+++ b/src/jrummikub/model/StoneHeap.java
@@ -36,6 +36,9 @@ public class StoneHeap {
* @return the drawn stone
*/
public Stone drawStone() {
+ if (heap.isEmpty()) {
+ return null;
+ }
return heap.remove(generator.nextInt(heap.size()));
}
@@ -48,6 +51,7 @@ public class StoneHeap {
*/
public List<Stone> drawStones(int number) {
List<Stone> drawnStones = new ArrayList<Stone>();
+ number = Math.min(number, heap.size());
for (int i = 0; i < number; i++) {
drawnStones.add(drawStone());
}
diff --git a/test/jrummikub/control/RoundControlTest.java b/test/jrummikub/control/RoundControlTest.java
index 9e3948c..20d87f3 100644
--- a/test/jrummikub/control/RoundControlTest.java
+++ b/test/jrummikub/control/RoundControlTest.java
@@ -22,6 +22,7 @@ import jrummikub.model.GameSettings;
import jrummikub.model.Hand;
import jrummikub.model.IHand;
import jrummikub.model.IPlayer;
+import jrummikub.model.IRoundState;
import jrummikub.model.ITable;
import jrummikub.model.MockRoundState;
import jrummikub.model.MockTable;
@@ -47,7 +48,7 @@ public class RoundControlTest {
private MockTable testTable;
private GameSettings gameSettings;
- private RoundState roundState;
+ private IRoundState roundState;
private RoundControl roundControl;
/**