summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model/StoneTray.java
blob: 21412a949b3d1da470ab1d4d750b4a09341d13e6 (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
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
		Iterable<Pair<E, Position>> {
	protected HashMap<E, Position> objects = new HashMap<E, Position>();

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

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

	/**
	 * Removes object from tray and returns it
	 * 
	 * @param position
	 *            position of the object that will be removed
	 * @return the picked up stone
	 */
	public E pickUp(Position position) {
		for (Map.Entry<E, Position> i : objects.entrySet()) {
			Position currentPosition = i.getValue();
			E currentObject = i.getKey();
			// Tests if position is left of, above ... the current object
			if (position.getX() < currentPosition.getX()) {
				continue;
			}
			if (position.getY() < currentPosition.getY()) {
				continue;
			}
			if (position.getX() > currentPosition.getX()
					+ currentObject.getWidth()) {
				continue;
			}
			if (position.getY() > currentPosition.getY()
					+ currentObject.getHeight()) {
				continue;
			}
			// Position is inside the current object
			objects.remove(i.getKey());
			return currentObject;
		}
		return null;
	}

	/**
	 * Adds object to the tray
	 * 
	 * @param object
	 *            object to add to Hand
	 * @param position
	 *            {@link Position} to put the object
	 */
	public void drop(E object, Position position) {
		if (object != null) {
			drop(object, position, null);
		}
	}

	@SuppressWarnings("unchecked")
	private void drop(E object, Position position, Direction direction) {
		for (Map.Entry<E, Position> i : ((Map<E,Position>)objects.clone()).entrySet()) {
			Position currentPosition = i.getValue();
			E currentObject = i.getKey();
			if (!objectsOverlap(object, position, currentObject,
					currentPosition)) {
				continue;
			}
			// Object would be placed inside the current object
			if (direction == null) {
				direction = getMoveDirection(object, position, i);
			}
			Position newPosition = null;
			// Move object to avoid overlap
			switch (direction) {
			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;
			}
			
			objects.remove(i.getKey());
			drop(currentObject, newPosition, direction);
		}
		objects.put(object, position);
	}

	/** Tests whether two objects overlap **/
	private boolean objectsOverlap(E object1, Position position1, E object2,
			Position position2) {
		// Tests if position is left of, above ... the current object
		if (position1.getX() + object1.getWidth() <= position2.getX()) {
			return false;
		}
		if (position1.getY() + object1.getHeight() <= position2.getY()) {
			return false;
		}
		if (position1.getX() >= position2.getX() + object2.getWidth()) {
			return false;
		}
		if (position1.getY() >= position2.getY() + object2.getHeight()) {
			return false;
		}
		return true;
	}

	private Direction getMoveDirection(E object, Position position,
			Map.Entry<E, Position> blocking) {
		boolean isVertical = getMoveOrientationn(object, position, blocking);
		float objectMidpointX = position.getX() + object.getWidth() / 2;
		float objectMidpointY = position.getY() + object.getHeight() / 2;
		float blockingMidpointX = blocking.getValue().getX()
				+ blocking.getKey().getWidth() / 2;
		float blockingMidpointY = blocking.getValue().getY()
				+ blocking.getKey().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;
			}
		}
	}

	private boolean getMoveOrientationn(E object, Position position,
			Map.Entry<E, Position> blocking) {
		float objectRight = position.getX() + object.getWidth();
		float blockingRight = blocking.getValue().getX()
				+ blocking.getKey().getWidth();
		float overlapRight = Math.min(objectRight, blockingRight);
		float overlapLeft = Math.max(position.getX(), blocking.getValue()
				.getX());
		float overlapX = overlapRight - overlapLeft;
		float objectBottom = position.getY() + object.getHeight();
		float blockingBottom = blocking.getValue().getY()
				+ blocking.getKey().getHeight();
		float overlapBottom = Math.min(objectBottom, blockingBottom);
		float overlapTop = Math.max(position.getY(), blocking.getValue()
				.getY());
		float overlapY = overlapBottom - overlapTop;
		// vertical or horizontal Shift
		// TODO magic factor
		return overlapX > overlapY;
	}

	/**
	 * Returns the position of an object that is already on the tray
	 * 
	 * @param object
	 *            object whose position is requested
	 * @return position of the object or null when the object is not on the tray
	 */
	public Position getPosition(E object) {
		return objects.get(object);
	}

	@Override
	public Iterator<Pair<E, Position>> iterator() {
		final Iterator<Map.Entry<E, Position>> entryIterator = objects.entrySet().iterator();
		return new Iterator<Pair<E, Position>>() {
			Iterator<Map.Entry<E, Position>> iterator = entryIterator;
			@Override
			public boolean hasNext() {
				return iterator.hasNext();
			}

			@Override
			public Pair<E, Position> next() {
				Map.Entry<E, Position> entry = iterator.next();
				return new Pair<E, Position>(entry.getKey(), entry.getValue());
			}

			@Override
			public void remove() {
				iterator.remove();
			}
			
		};
	}

	public void pickUp(E object) {
		objects.remove(object);
	}
	
	
	@Override
	public StoneTray<E> clone() {
		StoneTray<E> copy = new StoneTray();
		copy.objects = (HashMap<E, Position>) objects.clone();
		return copy;
	}

}