2011-04-30 13:44:17 +02:00
|
|
|
package jrummikub.model;
|
|
|
|
|
2011-05-24 21:57:18 +02:00
|
|
|
import static jrummikub.model.StoneTray.Direction.LEFT;
|
|
|
|
import static jrummikub.model.StoneTray.Direction.RIGHT;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.TreeMap;
|
2011-05-09 23:29:01 +02:00
|
|
|
|
2011-05-24 21:57:18 +02:00
|
|
|
import jrummikub.util.Pair;
|
2011-05-09 23:29:01 +02:00
|
|
|
|
2011-04-30 14:47:42 +02:00
|
|
|
/** Class managing a {@link Player}'s {@link Stone}s */
|
2011-05-04 16:23:10 +02:00
|
|
|
public class Hand extends StoneTray<Stone> implements IHand {
|
2011-05-10 05:53:30 +02:00
|
|
|
/**
|
|
|
|
* The width of the hand
|
|
|
|
*/
|
|
|
|
public final static int WIDTH = 14;
|
2011-05-24 21:57:18 +02:00
|
|
|
|
2011-05-24 01:51:56 +02:00
|
|
|
private GameSettings settings;
|
2011-05-24 21:57:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new empty hand with given game settings
|
|
|
|
*
|
|
|
|
* @param settings
|
|
|
|
* the game settings
|
|
|
|
*/
|
2011-05-24 01:51:56 +02:00
|
|
|
public Hand(GameSettings settings) {
|
|
|
|
this.settings = settings;
|
|
|
|
}
|
2011-04-30 13:44:17 +02:00
|
|
|
|
2011-05-16 22:01:02 +02:00
|
|
|
@Override
|
|
|
|
public int getFreeRowSpace(int row) {
|
2011-05-16 19:07:47 +02:00
|
|
|
int count = 0;
|
|
|
|
for (Pair<Stone, Position> entry : this) {
|
|
|
|
if (entry.getSecond().getY() == row) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2011-05-16 22:01:02 +02:00
|
|
|
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;
|
2011-05-16 19:07:47 +02:00
|
|
|
}
|
2011-05-16 22:01:02 +02:00
|
|
|
|
2011-05-09 21:56:45 +02:00
|
|
|
@Override
|
2011-05-24 21:57:18 +02:00
|
|
|
protected Pair<Position, Direction> fixInvalidDrop(Stone stone,
|
|
|
|
Position pos, Direction dir) {
|
2011-05-09 23:29:01 +02:00
|
|
|
float x = pos.getX();
|
|
|
|
float y = pos.getY();
|
|
|
|
|
2011-05-10 05:53:30 +02:00
|
|
|
if (x >= 0 && x <= WIDTH - 1) {
|
2011-05-09 23:29:01 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (x < 0) {
|
2011-05-16 19:07:47 +02:00
|
|
|
return new Pair<Position, Direction>(new Position(0, y), RIGHT);
|
2011-05-09 23:29:01 +02:00
|
|
|
} else {
|
2011-05-16 22:01:02 +02:00
|
|
|
if (getFreeRowSpace((int) y) == 0) {
|
2011-05-24 21:57:18 +02:00
|
|
|
return new Pair<Position, Direction>(new Position(0, y + 1),
|
|
|
|
RIGHT);
|
2011-05-09 23:29:01 +02:00
|
|
|
} else {
|
2011-05-24 21:57:18 +02:00
|
|
|
return new Pair<Position, Direction>(
|
|
|
|
new Position(WIDTH - 1, y), LEFT);
|
2011-05-09 23:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
2011-05-09 21:56:45 +02:00
|
|
|
}
|
2011-05-24 01:51:56 +02:00
|
|
|
|
|
|
|
public int getStonePoints() {
|
|
|
|
int points = 0;
|
2011-05-24 21:57:18 +02:00
|
|
|
|
2011-05-24 01:51:56 +02:00
|
|
|
for (Pair<Stone, Position> entry : this) {
|
|
|
|
if (entry.getFirst().isJoker()) {
|
|
|
|
points += settings.getJokerPoints();
|
|
|
|
} else {
|
|
|
|
points += entry.getFirst().getValue();
|
|
|
|
}
|
|
|
|
}
|
2011-05-24 21:57:18 +02:00
|
|
|
|
2011-05-24 01:51:56 +02:00
|
|
|
return points;
|
|
|
|
}
|
2011-05-24 01:51:58 +02:00
|
|
|
|
2011-05-24 21:57:18 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-05-24 01:51:58 +02:00
|
|
|
@Override
|
|
|
|
public boolean isInitialMeldPossible() {
|
2011-05-24 21:57:18 +02:00
|
|
|
int jokerCount = 0;
|
|
|
|
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts = new TreeMap<Pair<Integer, StoneColor>, Integer>(
|
|
|
|
comparator);
|
|
|
|
|
|
|
|
for (Pair<Stone, Position> entry : this) {
|
|
|
|
if (entry.getFirst().isJoker()) {
|
|
|
|
jokerCount++;
|
|
|
|
} else {
|
|
|
|
Pair<Integer, StoneColor> key = new Pair<Integer, StoneColor>(
|
|
|
|
entry.getFirst().getValue(), entry.getFirst()
|
|
|
|
.getColor());
|
|
|
|
|
|
|
|
incrementStoneCount(stoneCounts, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return findSetsWithTotalPoints(settings.getInitialMeldThreshold(),
|
|
|
|
stoneCounts, jokerCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
private 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void decrementStoneCount(
|
|
|
|
TreeMap<Pair<Integer, StoneColor>, Integer> stones,
|
|
|
|
Pair<Integer, StoneColor> stone) {
|
|
|
|
int count = stones.get(stone);
|
|
|
|
count--;
|
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
stones.remove(stone);
|
|
|
|
} else {
|
|
|
|
stones.put(stone, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private boolean findSetsWithTotalPoints(int pointsMissing,
|
|
|
|
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts,
|
|
|
|
int jokerCount) {
|
|
|
|
|
|
|
|
if (pointsMissing <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
stoneCounts = (TreeMap<Pair<Integer, StoneColor>, Integer>) stoneCounts
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
for (int value = 13; value >= 1; value--) {
|
|
|
|
for (StoneColor color : StoneColor.values()) {
|
|
|
|
Pair<Integer, StoneColor> stone = new Pair<Integer, StoneColor>(
|
|
|
|
value, color);
|
|
|
|
|
|
|
|
if (stoneCounts.containsKey(stone)) {
|
|
|
|
decrementStoneCount(stoneCounts, stone);
|
|
|
|
|
|
|
|
if (findRunsWithTotalPoints(pointsMissing - value,
|
|
|
|
stoneCounts, jokerCount, stone, 1))
|
|
|
|
return true;
|
|
|
|
if (findGroupsWithTotalPoints(pointsMissing - value,
|
|
|
|
stoneCounts, jokerCount, stone, 1))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jokerCount > 0) {
|
|
|
|
if (findRunsWithTotalPoints(pointsMissing - value,
|
|
|
|
stoneCounts, jokerCount - 1, stone, 1))
|
|
|
|
return true;
|
|
|
|
if (findGroupsWithTotalPoints(pointsMissing - value,
|
|
|
|
stoneCounts, jokerCount - 1, stone, 1))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private StoneColor getNextColor(StoneColor color) {
|
|
|
|
int index = Arrays.binarySearch(StoneColor.values(), color) + 1;
|
|
|
|
if (index >= StoneColor.values().length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return StoneColor.values()[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean findGroupsWithTotalPoints(int pointsMissing,
|
|
|
|
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts,
|
|
|
|
int jokerCount, Pair<Integer, StoneColor> stone, int groupSize) {
|
|
|
|
|
|
|
|
StoneColor nextColor = getNextColor(stone.getSecond());
|
|
|
|
Pair<Integer, StoneColor> nextStone = new Pair<Integer, StoneColor>(
|
|
|
|
stone.getFirst(), nextColor);
|
|
|
|
|
|
|
|
if (nextColor != null) {
|
|
|
|
if (stoneCounts.containsKey(nextStone)) {
|
|
|
|
decrementStoneCount(stoneCounts, nextStone);
|
|
|
|
if (findGroupsWithTotalPoints(pointsMissing - stone.getFirst(),
|
|
|
|
stoneCounts, jokerCount, nextStone, groupSize + 1))
|
|
|
|
return true;
|
|
|
|
incrementStoneCount(stoneCounts, nextStone);
|
|
|
|
}
|
|
|
|
if (jokerCount > 0) {
|
|
|
|
if (findGroupsWithTotalPoints(pointsMissing - stone.getFirst(),
|
|
|
|
stoneCounts, jokerCount - 1, nextStone, groupSize + 1))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (findGroupsWithTotalPoints(pointsMissing, stoneCounts,
|
|
|
|
jokerCount, nextStone, groupSize))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupSize >= 3) {
|
|
|
|
if (findSetsWithTotalPoints(pointsMissing, stoneCounts, jokerCount))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean findRunsWithTotalPoints(int pointsMissing,
|
|
|
|
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts,
|
|
|
|
int jokerCount, Pair<Integer, StoneColor> stone, int runLength) {
|
|
|
|
|
|
|
|
Pair<Integer, StoneColor> nextStone = null;
|
|
|
|
if (stone.getFirst() > 1) {
|
|
|
|
int nextValue = stone.getFirst() - 1;
|
|
|
|
nextStone = new Pair<Integer, StoneColor>(nextValue,
|
|
|
|
stone.getSecond());
|
|
|
|
|
|
|
|
if (stoneCounts.containsKey(nextStone)) {
|
|
|
|
decrementStoneCount(stoneCounts, nextStone);
|
|
|
|
if (findRunsWithTotalPoints(pointsMissing - nextValue,
|
|
|
|
stoneCounts, jokerCount, nextStone, runLength + 1))
|
|
|
|
return true;
|
|
|
|
incrementStoneCount(stoneCounts, nextStone);
|
|
|
|
|
|
|
|
}
|
|
|
|
if (jokerCount > 0) {
|
|
|
|
if (findRunsWithTotalPoints(pointsMissing - nextValue,
|
|
|
|
stoneCounts, jokerCount - 1, nextStone, runLength + 1))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runLength >= 3) {
|
|
|
|
if (findSetsWithTotalPoints(pointsMissing, stoneCounts, jokerCount))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-05-24 01:51:58 +02:00
|
|
|
}
|
2011-05-25 17:27:18 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getIdenticalStoneCount() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return 0;
|
|
|
|
}
|
2011-04-30 13:44:17 +02:00
|
|
|
}
|