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

import static jrummikub.model.StoneTray.Direction.LEFT;
import static jrummikub.model.StoneTray.Direction.RIGHT;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

import jrummikub.ai.TurnLogic;
import jrummikub.util.Pair;

/** Class managing a {@link Player}'s {@link Stone}s */
public class Hand extends StoneTray<Stone> implements IHand {
	private static final long serialVersionUID = 192210056255744909L;

	/**
	 * 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) {
		double x = pos.getX();
		double 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);
			}
		}
	}

	@Override
	public int getStonePoints(GameSettings settings) {
		int points = 0;

		for (Pair<Stone, Position> entry : this) {
			if (entry.getFirst().isJoker()) {
				points += settings.getJokerPoints();
			} else {
				points += entry.getFirst().getValue();
			}
		}

		return points;
	}

	@Override
	public boolean isInitialMeldPossible(GameSettings settings) {
		List<Stone> handStones = new ArrayList<Stone>();

		for (Pair<Stone, Position> entry : this) {
			handStones.add(entry.getFirst());
		}

		TurnLogic turnLogic = new TurnLogic(settings,
				Collections.<Stone> emptyList(), handStones);
		turnLogic.needIntialMeldThreshold();
		return turnLogic.solve();
	}

	/**
	 * Increments the count of a stone in the list of all stones
	 * 
	 * @param stones
	 *            all stones and their respective numbers
	 * @param stone
	 *            the start stone
	 */
	private static void incrementStoneCount(
			TreeMap<Pair<Integer, StoneColor>, Integer> stones,
			Pair<Integer, StoneColor> stone) {
		if (stones.containsKey(stone)) {
			stones.put(stone, stones.get(stone) + 1);
		} else {
			stones.put(stone, 1);
		}
	}

	/**
	 * The measure to compare the stones by
	 */
	private final static Comparator<Pair<Integer, StoneColor>> comparator = new Comparator<Pair<Integer, StoneColor>>() {
		@Override
		public int compare(Pair<Integer, StoneColor> o1,
				Pair<Integer, StoneColor> o2) {
			int firstComparison = o1.getFirst().compareTo(o2.getFirst());
			if (firstComparison != 0) {
				return -firstComparison;
			} else {
				return o1.getSecond().compareTo(o2.getSecond());
			}
		}
	};

	/**
	 * Counts the numbers of stones
	 * 
	 * @param stones
	 *            the stones to count
	 * @return the numbers for all stones
	 */
	private static Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> countStones(
			List<Stone> stones) {
		int jokerCount = 0;
		TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts = new TreeMap<Pair<Integer, StoneColor>, Integer>(
				comparator);

		for (Stone stone : stones) {
			if (stone.isJoker()) {
				jokerCount++;
			} else {
				Pair<Integer, StoneColor> key = new Pair<Integer, StoneColor>(
						stone.getValue(), stone.getColor());

				incrementStoneCount(stoneCounts, key);
			}
		}
		return new Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer>(
				stoneCounts, jokerCount);
	}

	@Override
	public int getIdenticalStoneCount() {
		List<Stone> stones = new ArrayList<Stone>();

		for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter
				.hasNext();) {
			stones.add(iter.next().getFirst());
		}

		Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> stoneCounts = countStones(stones);

		int pairCount = 0;

		for (int count : stoneCounts.getFirst().values()) {
			pairCount += count / 2;
		}

		return pairCount;
	}
}