This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
JRummikub/src/jrummikub/model/Hand.java
Bennet Gerlach 278edc37a9 findSetsWithTotalPoints now finds sets (and total points)
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@341 72836036-5685-4462-b002-a69064685172
2011-05-31 03:45:21 +02:00

130 lines
2.9 KiB
Java

package jrummikub.model;
import static jrummikub.model.StoneTray.Direction.LEFT;
import static jrummikub.model.StoneTray.Direction.RIGHT;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import jrummikub.control.AIUtil;
import jrummikub.util.Pair;
/** 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;
private GameSettings settings;
/**
* Create a new empty hand with given game settings
*
* @param settings
* the game settings
*/
public Hand(GameSettings settings) {
this.settings = settings;
}
@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);
}
}
}
public int getStonePoints() {
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() {
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 = AIUtil
.countStones(stones);
Pair<List<StoneSet>, Integer> result = AIUtil.findSetsWithTotalPoints(
settings.getInitialMeldThreshold(), stoneCounts.getFirst(),
stoneCounts.getSecond());
System.out.println(result);
return (result.getSecond() >= settings.getInitialMeldThreshold());
}
@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 = AIUtil
.countStones(stones);
int pairCount = 0;
for (int count : stoneCounts.getFirst().values()) {
pairCount += count / 2;
}
return pairCount;
}
}