summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model/StoneSet.java
blob: 7755a3a131602b31f986de08cf6e3ba17368b849 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package jrummikub.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import jrummikub.util.Pair;
import static jrummikub.model.StoneSet.Type.*;

/** Class managing {@link Stone}s joined together to form sets */
public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
	private static final long serialVersionUID = -3852631195648599398L;

	static final float VERTICAL_BORDER = 0.5f;
	static final float HORIZONTAL_BORDER = 0.125f;
	private ArrayList<Stone> stones;

	/**
	 * Create a new single stone stone set
	 * 
	 * @param stone
	 *          single stone of the set
	 */
	public StoneSet(Stone stone) {
		stones = new ArrayList<Stone>(Collections.singletonList(stone));
	}

	/**
	 * Create a stone set from a list of stones
	 * 
	 * @param stones
	 *          list of stones to build a set of
	 */
	public StoneSet(List<Stone> stones) {
		this.stones = new ArrayList<Stone>(stones);
	}

	/** Validity type of the set */
	public enum Type {
		/** Set is a valid group */
		GROUP,
		/** Set is a valid run */
		RUN,
		/** Set is invalid */
		INVALID
	}

	/**
	 * Test for rule conflict within the StoneSet
	 * 
	 * @param settings
	 *          GameSettings
	 * 
	 * @return true when the set is valid according to the rules
	 */
	public boolean isValid(GameSettings settings) {
		return classify(settings).getFirst() != INVALID;
	}

	/**
	 * Test for rule conflict within the StoneSet and determine whether the set is
	 * a group or a run
	 * 
	 * @param settings
	 *          GameSettings
	 * 
	 * @return GROUP or RUN for valid sets, INVALID otherwise and the points
	 */

	public Pair<Type, Integer> classify(GameSettings settings) {
		if (stones.size() < 3) {
			return new Pair<Type, Integer>(INVALID, 0);
		}
		int nonJoker = -1;
		for (int i = 0; i < stones.size(); i++) {
			if (stones.get(i).isJoker()) {
				continue;
			}
			nonJoker = i;
		}

		if (nonJoker == -1) {
			return classifyJokersOnly(settings);
		}

		int runScore = isValidRun(nonJoker, settings);
		int groupScore = isValidGroup(stones.get(nonJoker).getValue(), settings);

		if (runScore > groupScore) {
			return new Pair<Type, Integer>(RUN, runScore);
		} else if (groupScore == 0) {
			return new Pair<Type, Integer>(INVALID, 0);
		} else {
			return new Pair<Type, Integer>(GROUP, groupScore);
		}
	}

	/**
	 * Test for rule conflict within a StoneSet with jokers only and determine
	 * whether the set is a group or a run
	 * 
	 * @param settings
	 *          the game settings
	 * @return GROUP or RUN for valid sets, INVALID otherwise and the points
	 */
	private Pair<Type, Integer> classifyJokersOnly(GameSettings settings) {
		if (stones.size() > settings.getHighestValue()
				&& stones.size() > settings.getStoneColors().size()
				&& !settings.isNoLimits()) {
			return new Pair<Type, Integer>(INVALID, 0);
		} else if (stones.size() > settings.getStoneColors().size()) {
			int value = 0;
			int stoneValue = settings.getHighestValue();
			for (int i = 0; i < stones.size(); i++) {
				value += stoneValue;
				stoneValue--;
				if (stoneValue == 0) {
					stoneValue = settings.getHighestValue();
				}
			}
			return new Pair<Type, Integer>(RUN, value);
		} else {
			return new Pair<Type, Integer>(GROUP, stones.size()
					* settings.getHighestValue());
		}
	}

	/**
	 * Test for rule conflict within the StoneSet, assuming we have a run
	 * 
	 * @param referencePosition
	 *          position of stone used as reference (any non-joker stone)
	 * @param settings
	 *          the game settings
	 * @return the set's points
	 */
	private int isValidRun(int referencePosition, GameSettings settings) {
		StoneColor runColor = stones.get(referencePosition).getColor();
		int startValue = stones.get(referencePosition).getValue()
				- referencePosition;
		int endValue = startValue + stones.size() - 1;
		if (!settings.isNoLimits()) {
			if (startValue < 1 || endValue > settings.getHighestValue()) {
				return 0;
			}
		}
		int value = 0;
		for (int i = 0; i < stones.size(); i++) {
			int expectedValue = i + startValue;
			while (expectedValue < 1) {
				expectedValue += settings.getHighestValue();
			}
			while (expectedValue > settings.getHighestValue()) {
				expectedValue -= settings.getHighestValue();
			}
			value += expectedValue;

			if (stones.get(i).isJoker()) {
				continue;
			}
			if (stones.get(i).getColor() != runColor) {
				return 0;
			}

			if (stones.get(i).getValue() != expectedValue) {
				return 0;
			}
		}
		return value;
	}

	/**
	 * Test for rule conflict within the StoneSet, assuming we have a group
	 * 
	 * @param value
	 *          the value of the stones (all have the same in a group)
	 * 
	 * @param settings
	 *          the game settings
	 * @return the set's points
	 */
	private int isValidGroup(int value, GameSettings settings) {
		if (stones.size() > settings.getStoneColors().size()) {
			return 0;
		}
		Set<StoneColor> seenColors = new HashSet<StoneColor>();
		for (Stone i : stones) {
			if (i.isJoker()) {
				continue;
			}
			if (i.getValue() != value) {
				return 0;
			}
			if (seenColors.contains(i.getColor())) {
				return 0;
			} else {
				seenColors.add(i.getColor());
			}
		}
		return value * stones.size();
	}

	/**
	 * Splits the StoneSet at a specified {@link Position} and returns two new
	 * Stone Sets
	 * 
	 * @param position
	 *          Splitting {@link Position}
	 * @return A pair of StoneSets, one for each split part
	 */
	public Pair<StoneSet, StoneSet> splitAt(int position) {
		if (position == 0) {
			return new Pair<StoneSet, StoneSet>(null, this);
		} else if (position == stones.size()) {
			return new Pair<StoneSet, StoneSet>(this, null);
		}
		StoneSet firstSet = new StoneSet(stones.subList(0, position));
		StoneSet secondSet = new StoneSet(stones.subList(position, stones.size()));
		return new Pair<StoneSet, StoneSet>(firstSet, secondSet);
	}

	/**
	 * Joins StoneSet to another StoneSet and returns the resulting new StoneSet
	 * 
	 * @param other
	 *          StoneSet to be joined to active StoneSet
	 * @return the combined StoneSet
	 */
	public StoneSet join(StoneSet other) {
		List<Stone> joinedList = new ArrayList<Stone>();
		joinedList.addAll(stones);
		joinedList.addAll(other.stones);
		return new StoneSet(joinedList);
	}

	/**
	 * Returns the number of stones in the set.
	 * 
	 * @return number of stones
	 */
	public int getSize() {
		return stones.size();
	}

	/**
	 * Returns the i-th stone of the set (starting with 0)
	 * 
	 * @param i
	 *          number of the stone to return
	 * @return the i-th stone
	 */
	public Stone get(int i) {
		return stones.get(i);
	}

	@Override
	public Iterator<Stone> iterator() {
		final Iterator<Stone> it = stones.iterator();

		return new Iterator<Stone>() {

			@Override
			public boolean hasNext() {
				return it.hasNext();
			}

			@Override
			public Stone next() {
				return it.next();
			}

			@Override
			public void remove() {
				// removing stones is impossible

				throw new UnsupportedOperationException();
			}
		};
	}

	@Override
	public float getWidth() {
		return stones.size() + 2 * VERTICAL_BORDER;
	}

	@Override
	public float getHeight() {
		return 1 + 2 * HORIZONTAL_BORDER;
	}

	@Override
	public String toString() {
		String ret = "StoneSet[";
		boolean first = true;

		for (Stone stone : stones) {
			if (!first)
				ret += ",";

			ret += stone.toString();
			first = false;
		}

		return ret + "]";
	}
}