summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/jrummikub/model/GameState.java89
-rw-r--r--test/jrummikub/model/GameStateTest.java47
2 files changed, 135 insertions, 1 deletions
diff --git a/src/jrummikub/model/GameState.java b/src/jrummikub/model/GameState.java
index 4bb55bc..35de4f1 100644
--- a/src/jrummikub/model/GameState.java
+++ b/src/jrummikub/model/GameState.java
@@ -1,13 +1,19 @@
package jrummikub.model;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
/**
* Class that stores information for a game of multiple rounds
*/
public class GameState {
private int firstRoundFirstPlayer;
+ private List<Score> scores = new ArrayList<Score>();
/**
* Gets the number of the first player of the first round
+ *
* @return the number of the first player of the first round
*/
public int getFirstRoundFirstPlayer() {
@@ -16,9 +22,90 @@ public class GameState {
/**
* Sets the number of the first player of the first round
- * @param firstRoundFirstPlayer the number of the first player of the first round
+ *
+ * @param firstRoundFirstPlayer
+ * the number of the first player of the first round
*/
public void setFirstRoundFirstPlayer(int firstRoundFirstPlayer) {
this.firstRoundFirstPlayer = firstRoundFirstPlayer;
}
+
+ /**
+ * Returns the list of players' scores in the rounds played before
+ *
+ * @return the list of scores
+ */
+ public List<Score> getScores() {
+ return scores;
+ }
+
+ private Boolean[] getWinners(Integer[] points, int[] wins) {
+ int playerCount = scores.get(0).getPoints().size();
+ int maxWins = 0, maxPoints = 0;
+ Boolean[] winners = new Boolean[playerCount];
+
+ for (int i = 0; i < playerCount; ++i) {
+ winners[i] = false;
+ }
+
+ for (int i = 0; i < playerCount; ++i) {
+ if (wins[i] > maxWins) {
+ maxWins = wins[i];
+ maxPoints = points[i];
+
+ for (int j = 0; j < i; ++j) {
+ winners[j] = false;
+ }
+
+ winners[i] = true;
+ } else if (wins[i] == maxWins) {
+ if (points[i] > maxPoints) {
+ maxWins = wins[i];
+ maxPoints = points[i];
+
+ for (int j = 0; j < i; ++j) {
+ winners[j] = false;
+ }
+
+ winners[i] = true;
+ } else if (points[i] == maxPoints) {
+ winners[i] = true;
+ }
+ }
+ }
+
+ return winners;
+ }
+
+ /**
+ * Calculated the accumulated score over the played rounds
+ *
+ * @return the accumulated score
+ */
+ public Score getAccumulatedScore() {
+ if (scores.isEmpty()) {
+ return null;
+ }
+
+ int playerCount = scores.get(0).getPoints().size();
+ Integer[] points = new Integer[playerCount];
+ int[] wins = new int[playerCount];
+
+ for (int i = 0; i < playerCount; ++i) {
+ points[i] = 0;
+ }
+
+ for (Score roundScore : scores) {
+ for (int i = 0; i < playerCount; ++i) {
+ points[i] += roundScore.getPoints().get(i);
+ if (roundScore.getWinners().get(i)) {
+ wins[i]++;
+ }
+ }
+ }
+
+ Boolean[] winners = getWinners(points, wins);
+
+ return new Score(Arrays.asList(winners), Arrays.asList(points));
+ }
}
diff --git a/test/jrummikub/model/GameStateTest.java b/test/jrummikub/model/GameStateTest.java
new file mode 100644
index 0000000..8f311a6
--- /dev/null
+++ b/test/jrummikub/model/GameStateTest.java
@@ -0,0 +1,47 @@
+package jrummikub.model;
+
+import static org.junit.Assert.*;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+
+public class GameStateTest {
+ GameState gameState = new GameState();
+
+ @Test
+ public void testAccumulatedScore() {
+ Score score1 = new Score(Arrays.asList(true, false, false), Arrays.asList(
+ 30, -20, -10));
+ Score score2 = new Score(Arrays.asList(true, true, false), Arrays.asList(
+ -10, -10, -50));
+ Score score3 = new Score(Arrays.asList(false, false, true), Arrays.asList(
+ -30, -20, 50));
+ Score score4 = new Score(Arrays.asList(false, true, false), Arrays.asList(
+ -20, -10, -30));
+
+ gameState.getScores().addAll(Arrays.asList(score1, score2, score3, score4));
+
+ Score accum = gameState.getAccumulatedScore();
+ assertEquals(Arrays.asList(true, false, false), accum.getWinners());
+ assertEquals(Arrays.asList(-30, -60, -40), accum.getPoints());
+ }
+
+ @Test
+ public void testAccumulatedScoreDraw() {
+ Score score1 = new Score(Arrays.asList(true, false, false), Arrays.asList(
+ 10, -10, -5));
+ Score score2 = new Score(Arrays.asList(true, true, false), Arrays.asList(
+ -10, -10, -50));
+ Score score3 = new Score(Arrays.asList(false, false, true), Arrays.asList(
+ -20, -10, 30));
+ Score score4 = new Score(Arrays.asList(false, true, false), Arrays.asList(
+ -20, -10, -30));
+
+ gameState.getScores().addAll(Arrays.asList(score1, score2, score3, score4));
+
+ Score accum = gameState.getAccumulatedScore();
+ assertEquals(Arrays.asList(true, true, false), accum.getWinners());
+ assertEquals(Arrays.asList(-40, -40, -55), accum.getPoints());
+ }
+}