summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model/StoneTray.java
blob: 8681c75fa989a3978009b2df01bb34daaee6b2c2 (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
package jrummikub.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import jrummikub.util.Pair;

/**
 * A StoneTray is a collection of positioned objects (for example {@link Stone}s
 * or {@link StoneSet}s.
 * 
 * @param <E>
 *            Type of positioned objects (must implement Sizeable)
 */
public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
	private static final long serialVersionUID = -6329309928640027222L;

	protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();

	/** Possible move directions in case of overlapping Stones/Sets */

	protected static enum Direction {
		LEFT, RIGHT, TOP, BOTTOM;
	}

	@Override
	public void drop(E object, Position position) {
		if (object != null) {
			drop(object, position, null);
		}
	}

	/**
	 * Subroutine to "drop" to consider and determine the direction the objects
	 * dropped one collides with position-wise evade in
	 * 
	 * @param object
	 *            the object to add to Hand
	 * @param position
	 *            {@link Position} to put the object
	 * @param direction
	 *            the direction the other stones evade in
	 */
	private void drop(E object, Position position, Direction direction) {
		Pair<Position, Direction> update = fixInvalidDrop(object, position,
				direction);
		if (update != null) {
			position = update.getFirst();
			direction = update.getSecond();
		}

		dropUnchecked(object, position, direction);
	}

	/**
	 * Subroutine to "drop" to execute the actual drop
	 * 
	 * @param object
	 *            the object to add to Hand
	 * @param position
	 *            {@link Position} to put the object
	 * @param direction
	 *            the direction the other stones evade in
	 */
	@SuppressWarnings("unchecked")
	private void dropUnchecked(E object, Position position, Direction direction) {
		objects.put(object, new Pair<E, Position>(object, position));
		for (Pair<E, Position> i : ((Map<E, Pair<E, Position>>) objects.clone())
				.values()) {
			Direction newDirection = direction;
			E currentObject = i.getFirst();
			if (currentObject == object)
				continue;
			Position currentPosition = getPosition(currentObject);
			if (!objectsOverlap(object, position, currentObject,
					currentPosition)) {
				continue;
			}
			// Object would be placed inside the current object
			if (newDirection == null) {
				newDirection = getMoveDirection(object, position, i);
			}
			Position newPosition = null;
			// Move object to avoid overlap
			newPosition = getNewPosition(object, position, newDirection,
					currentObject, currentPosition);

			objects.remove(currentObject);
			drop(currentObject, newPosition, newDirection);
		}
	}

	private Position getNewPosition(E object, Position position,
			Direction newDirection, E currentObject, Position currentPosition) {
		Position newPosition = new Position(0, 0);
		switch (newDirection) {
		case TOP:
			newPosition = new Position(currentPosition.getX(), position.getY()
					- currentObject.getHeight());
			break;
		case BOTTOM:
			newPosition = new Position(currentPosition.getX(), position.getY()
					+ object.getHeight());
			break;
		case LEFT:
			newPosition = new Position(position.getX()
					- currentObject.getWidth(), currentPosition.getY());
			break;
		case RIGHT:
			newPosition = new Position(position.getX() + object.getWidth(),
					currentPosition.getY());
			break;
		}
		return newPosition;
	}

	/**
	 * Checks whether the object may be placed on the given position, computes
	 * new position if not
	 * 
	 * @param object
	 *            to be dropped
	 * @param dir
	 * @param pos
	 *            the object is dropped at
	 * @return null if the drop is valid, new position otherwise
	 */
	protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos,
			Direction dir) {
		return null;
	}

	/**
	 * Static method for determining a less or equal relation considering a
	 * small fuzziness
	 * 
	 * @param d
	 *            the value to be less or equal
	 * @param e
	 *            than the other one
	 * @return if d is less or equal e
	 */
	private static boolean lessOrEqual(double d, double e) {
		if (-0.000001 < e && e < 0.000001) {
			return (d < e + 0.000001);
		}

		double q = d / e;
		if (0.999999 < q && q < 1.000001) {
			return true;
		}

		return d < e;
	}

	/**
	 * Tests whether two objects overlap
	 * 
	 * @param object1
	 *            first object
	 * @param position1
	 *            first object's position
	 * @param object2
	 *            second object
	 * @param position2
	 *            second object's position
	 * 
	 * @return whether they overlap
	 **/
	private boolean objectsOverlap(E object1, Position position1, E object2,
			Position position2) {
		// Tests if position is left of, above ... the current object
		if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) {
			return false;
		}
		if (lessOrEqual(position1.getY() + object1.getHeight(),
				position2.getY())) {
			return false;
		}
		if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) {
			return false;
		}
		if (lessOrEqual(position2.getY() + object2.getHeight(),
				position1.getY())) {
			return false;
		}
		return true;
	}

	/**
	 * Returns the direction to move the object in
	 * 
	 * @param object
	 *            the object
	 * @param position
	 *            the object's position
	 * @param blocking
	 *            the object thats blocking
	 * @return the direction
	 */
	private Direction getMoveDirection(E object, Position position,
			Pair<E, Position> blocking) {
		boolean isVertical = getMoveOrientation(object, position, blocking);
		double objectMidpointX = position.getX() + object.getWidth() / 2;
		double objectMidpointY = position.getY() + object.getHeight() / 2;
		double blockingMidpointX = blocking.getSecond().getX()
				+ blocking.getFirst().getWidth() / 2;
		double blockingMidpointY = blocking.getSecond().getY()
				+ blocking.getFirst().getHeight() / 2;
		if (isVertical) {
			if (objectMidpointY < blockingMidpointY) {
				return Direction.BOTTOM;
			} else {
				return Direction.TOP;
			}
		} else {
			if (objectMidpointX < blockingMidpointX) {
				return Direction.RIGHT;
			} else {
				return Direction.LEFT;
			}
		}
	}

	/**
	 * Will the object be moved horizontally or vertically
	 * 
	 * @param object
	 *            the object
	 * @param position
	 *            the objects position
	 * @param blocking
	 *            the object thats blocking
	 * 
	 * @return boolean vertical movement
	 */
	private boolean getMoveOrientation(E object, Position position,
			Pair<E, Position> blocking) {
		double objectRight = position.getX() + object.getWidth();
		double blockingRight = blocking.getSecond().getX()
				+ blocking.getFirst().getWidth();
		double overlapRight = Math.min(objectRight, blockingRight);
		double overlapLeft = Math.max(position.getX(), blocking.getSecond()
				.getX());
		double overlapX = overlapRight - overlapLeft;
		double objectBottom = position.getY() + object.getHeight();
		double blockingBottom = blocking.getSecond().getY()
				+ blocking.getFirst().getHeight();
		double overlapBottom = Math.min(objectBottom, blockingBottom);
		double overlapTop = Math.max(position.getY(), blocking.getSecond()
				.getY());
		double overlapY = overlapBottom - overlapTop;
		return overlapX > overlapY;
	}

	@Override
	public Position getPosition(E object) {
		Pair<E, Position> entry = objects.get(object);
		if (entry == null) {
			return null;
		}
		return entry.getSecond();
	}

	@Override
	public boolean contains(E object) {
		return objects.get(object) != null;
	}

	@Override
	public Iterator<Pair<E, Position>> iterator() {
		return objects.values().iterator();
	}

	@Override
	public boolean pickUp(E object) {
		return null != objects.remove(object);
	}

	@SuppressWarnings("unchecked")
	@Override
	public IStoneTray<E> clone() {
		try {
			StoneTray<E> copy = (StoneTray<E>) super.clone();
			copy.objects = (HashMap<E, Pair<E, Position>>) objects.clone();

			return copy;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();

			return null;
		}
	}

	@Override
	public int getSize() {
		return objects.size();
	}

}