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;
|
|
|
|
|
2011-05-30 21:01:48 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2011-05-24 21:57:18 +02:00
|
|
|
import java.util.TreeMap;
|
2011-05-09 23:29:01 +02:00
|
|
|
|
2011-05-30 21:01:48 +02:00
|
|
|
import jrummikub.control.AIUtil;
|
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-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-30 21:01:48 +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-30 21:01:48 +02:00
|
|
|
return new Pair<Position, Direction>(new Position(0, y + 1), RIGHT);
|
2011-05-09 23:29:01 +02:00
|
|
|
} else {
|
2011-05-30 21:01:48 +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
|
|
|
|
2011-05-31 03:45:32 +02:00
|
|
|
@Override
|
|
|
|
public int getStonePoints(GameSettings settings) {
|
2011-05-24 01:51:56 +02:00
|
|
|
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
|
|
|
|
|
|
|
@Override
|
2011-05-31 03:45:32 +02:00
|
|
|
public boolean isInitialMeldPossible(GameSettings settings) {
|
|
|
|
AIUtil aiUtil = new AIUtil(settings);
|
|
|
|
|
2011-05-30 21:01:48 +02:00
|
|
|
List<Stone> stones = new ArrayList<Stone>();
|
2011-05-25 17:27:19 +02:00
|
|
|
|
2011-05-30 21:01:48 +02:00
|
|
|
for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter.hasNext();) {
|
|
|
|
stones.add(iter.next().getFirst());
|
2011-05-24 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 21:01:48 +02:00
|
|
|
Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> stoneCounts = AIUtil
|
|
|
|
.countStones(stones);
|
2011-05-24 21:57:18 +02:00
|
|
|
|
2011-05-31 03:45:32 +02:00
|
|
|
Pair<List<StoneSet>, Integer> result = aiUtil.findSetsWithTotalPoints(
|
2011-05-31 03:45:21 +02:00
|
|
|
settings.getInitialMeldThreshold(), stoneCounts.getFirst(),
|
|
|
|
stoneCounts.getSecond());
|
|
|
|
|
|
|
|
return (result.getSecond() >= settings.getInitialMeldThreshold());
|
2011-05-24 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 21:01:48 +02:00
|
|
|
@Override
|
|
|
|
public int getIdenticalStoneCount() {
|
|
|
|
List<Stone> stones = new ArrayList<Stone>();
|
2011-05-24 21:57:18 +02:00
|
|
|
|
2011-05-30 21:01:48 +02:00
|
|
|
for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter.hasNext();) {
|
|
|
|
stones.add(iter.next().getFirst());
|
2011-05-24 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2011-05-30 21:01:48 +02:00
|
|
|
Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> stoneCounts = AIUtil
|
|
|
|
.countStones(stones);
|
2011-05-25 17:27:18 +02:00
|
|
|
|
2011-05-25 17:27:19 +02:00
|
|
|
int pairCount = 0;
|
2011-05-30 21:01:48 +02:00
|
|
|
|
|
|
|
for (int count : stoneCounts.getFirst().values()) {
|
2011-05-25 17:27:19 +02:00
|
|
|
pairCount += count / 2;
|
|
|
|
}
|
2011-05-30 21:01:48 +02:00
|
|
|
|
2011-05-25 17:27:19 +02:00
|
|
|
return pairCount;
|
2011-05-25 17:27:18 +02:00
|
|
|
}
|
2011-04-30 13:44:17 +02:00
|
|
|
}
|