summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/model/Table.java
blob: 232986f38f4be1b68a80b9cc4970790cd129a360 (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
package jrummikub.model;

import jrummikub.util.Pair;

/** Class administering the {@link Stone}s on the game-Table */

public class Table extends StoneTray<StoneSet> implements ITable {

	/**
	 * Removes {@link Stone} from the Table
	 * 
	 * @param stone
	 *            stone to pick up
	 */
	@Override
	public void pickUpStone(Stone stone) {
		// Find the set of the stone
		StoneSet set = null;
		Position setPosition = null;
		int stonePosition = 0;
		setLoop: for (Pair<StoneSet, Position> i : this) {
			set = i.getFirst();
			setPosition = i.getSecond();
			stonePosition = 0;
			for (Stone j : set) {
				if (j == stone) {
					break setLoop;
				}
				stonePosition++;
			}
		}
		// Stone not found
		if (set == null) {
			return;
		}
		
		splitSet(set, setPosition, stonePosition);
	}

	private void splitSet(StoneSet set, Position setPosition, int stonePosition) {
		pickUp(set);
		
		Pair<StoneSet, StoneSet> firstSplit = set.splitAt(stonePosition);
		Pair<StoneSet, StoneSet> secondSplit = firstSplit.getSecond().splitAt(1);
		
		StoneSet leftSet = firstSplit.getFirst();
		StoneSet rightSet = secondSplit.getSecond();
		
		if (set.classify() == StoneSet.Type.RUN) {
			Position leftPosition, rightPosition;
			leftPosition = setPosition;
			rightPosition = new Position(setPosition.getX() + stonePosition, setPosition.getY());
			
			drop(leftSet, leftPosition);
			drop(rightSet, rightPosition);
		} else {
			Position newPosition = new Position(setPosition.getX() + 0.5f, setPosition.getY());
			
			if (leftSet == null) {
				drop(rightSet, newPosition);
			} else if (rightSet == null) {
				drop(leftSet, newPosition);
			} else {
				drop(leftSet.join(rightSet), newPosition);
			}
		}
	}

	/** Tests the Table for rule conflicts by checking all the {@link StoneSet} */
	@Override
	public boolean isValid() {
		for (Pair<StoneSet, Position> i : this) {
			if (!i.getFirst().isValid()) {
				return false;
			}
		}
		return true;
	}

}