summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model/GameState.java
blob: 7cd38ab8770e04e2a33015b998941c39717ccda3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package jrummikub.model;

import java.io.Serializable;
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 implements Serializable {
	private static final long serialVersionUID = -5787975403310108391L;

	private int firstRoundFirstPlayer;
	private ArrayList<Score> scores = new ArrayList<Score>();
	private PlayerSettings lastPlayerRedealed;

	/**
	 * 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() {
		return firstRoundFirstPlayer;
	}

	/**
	 * Sets 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;
	}

	/**
	 * Sets the player that has redealed last
	 * 
	 * @param lastPlayerRedealed
	 *          the player to set
	 */
	public void setLastPlayerRedealed(PlayerSettings lastPlayerRedealed) {
		this.lastPlayerRedealed = lastPlayerRedealed;
	}

	/**
	 * Returns the player that has redealed last
	 * 
	 * @return the player
	 */
	public PlayerSettings getLastPlayerRedealed() {
		return lastPlayerRedealed;
	}

	/**
	 * Returns the list of players' scores in the rounds played before
	 * 
	 * @return the list of scores
	 */
	public List<Score> getScores() {
		return scores;
	}

	/**
	 * Returns whether players have won
	 * 
	 * @param points
	 *          the player's points
	 * @param wins
	 *          the number of wins per player
	 * @return whether a player has won
	 */
	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));
	}
}