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

import jrummikub.util.Pair;

import static jrummikub.model.StoneTray.Direction.*;

/** Class managing a {@link Player}'s {@link Stone}s */
public class Hand extends StoneTray<Stone> implements IHand {
	/**
	 * The width of the hand
	 */
	public final static int WIDTH = 14;

	@Override
	public int getFreeRowSpace(int row) {
		int count = 0;
		for (Pair<Stone, Position> entry : this) {
			if (entry.getSecond().getY() == row) {
				count++;
			}
		}
		return WIDTH - count;
	}

	@Override
	public int getRowCount() {
		int rows = 0;

		for (Pair<Stone, Position> entry : this) {
			if (entry.getSecond().getY() > rows) {
				rows = (int) entry.getSecond().getY();
			}
		}

		return rows + 1;
	}

	@Override
	protected Pair<Position, Direction> fixInvalidDrop(Stone stone, Position pos,
			Direction dir) {
		float x = pos.getX();
		float y = pos.getY();

		if (x >= 0 && x <= WIDTH - 1) {
			return null;
		}
		if (x < 0) {
			return new Pair<Position, Direction>(new Position(0, y), RIGHT);
		} else {
			if (getFreeRowSpace((int) y) == 0) {
				return new Pair<Position, Direction>(new Position(0, y + 1), RIGHT);
			} else {
				return new Pair<Position, Direction>(new Position(WIDTH - 1, y), LEFT);
			}
		}
	}
}