summaryrefslogtreecommitdiffstats
path: root/src/jrummikub
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub')
-rw-r--r--src/jrummikub/control/AIUtil.java38
-rw-r--r--src/jrummikub/control/RoundControl.java48
-rw-r--r--src/jrummikub/model/Hand.java23
-rw-r--r--src/jrummikub/model/IHand.java12
-rw-r--r--src/jrummikub/model/Player.java8
-rw-r--r--src/jrummikub/model/RoundState.java14
6 files changed, 71 insertions, 72 deletions
diff --git a/src/jrummikub/control/AIUtil.java b/src/jrummikub/control/AIUtil.java
index bf6a078..1501483 100644
--- a/src/jrummikub/control/AIUtil.java
+++ b/src/jrummikub/control/AIUtil.java
@@ -7,18 +7,33 @@ import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;
+import jrummikub.model.GameSettings;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet;
import jrummikub.util.Pair;
/**
- * A collection of several AI utility methods
+ * A collection of several AI utility methods with given game settings
*
*/
public class AIUtil {
- private AIUtil() {
+ GameSettings settings;
+ List<StoneColor> stoneColors;
+
+ /**
+ * The utility class's methods calculate their results based on the game
+ * settings
+ *
+ * @param settings
+ * the underlying game settings
+ */
+ public AIUtil(GameSettings settings) {
+ this.settings = settings;
+
+ stoneColors = new ArrayList<StoneColor>(Arrays.asList(StoneColor.values()));
+ stoneColors.retainAll(settings.getStoneColors());
}
private static class SearchState {
@@ -47,7 +62,7 @@ public class AIUtil {
* @return the sets that have the desired point total or the highest found
*/
@SuppressWarnings("unchecked")
- public static Pair<List<StoneSet>, Integer> findSetsWithTotalPoints(
+ public Pair<List<StoneSet>, Integer> findSetsWithTotalPoints(
int pointsMissing,
TreeMap<Pair<Integer, StoneColor>, Integer> stoneCounts, int jokerCount) {
@@ -63,14 +78,13 @@ public class AIUtil {
bestResult = new Pair<List<StoneSet>, Integer>(
Collections.<StoneSet> emptyList(), 0);
- for (int value = 13; value >= 1; value--) {
- for (StoneColor color : StoneColor.values()) {
+ for (int value = settings.getHighestValue(); value >= 1; value--) {
+ for (StoneColor color : stoneColors) {
Pair<Integer, StoneColor> stone = new Pair<Integer, StoneColor>(value,
color);
if (stoneCounts.containsKey(stone)) {
decrementStoneCount(stoneCounts, stone);
-
result = findRunsWithTotalPoints(new SearchState(pointsMissing
- value, stoneCounts, jokerCount), stone, 1);
if (result.getSecond() >= pointsMissing)
@@ -109,7 +123,7 @@ public class AIUtil {
return bestResult;
}
- private static Pair<List<StoneSet>, Integer> findGroupsWithTotalPoints(
+ private Pair<List<StoneSet>, Integer> findGroupsWithTotalPoints(
SearchState searchState, int value, List<StoneColor> chosenColors,
StoneColor testedColor) {
@@ -175,7 +189,7 @@ public class AIUtil {
return bestResult;
}
- private static Pair<List<StoneSet>, Integer> findRunsWithTotalPoints(
+ private Pair<List<StoneSet>, Integer> findRunsWithTotalPoints(
SearchState searchState, Pair<Integer, StoneColor> testedStone,
int runLength) {
@@ -262,12 +276,12 @@ public class AIUtil {
}
}
- static StoneColor getNextColor(StoneColor color) {
- int index = Arrays.binarySearch(StoneColor.values(), color) + 1;
- if (index >= StoneColor.values().length) {
+ StoneColor getNextColor(StoneColor color) {
+ int index = stoneColors.indexOf(color) + 1;
+ if (index >= stoneColors.size()) {
return null;
}
- return StoneColor.values()[index];
+ return stoneColors.get(index);
}
private final static Comparator<Pair<Integer, StoneColor>> comparator = new Comparator<Pair<Integer, StoneColor>>() {
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java
index 124b196..6a1befa 100644
--- a/src/jrummikub/control/RoundControl.java
+++ b/src/jrummikub/control/RoundControl.java
@@ -43,9 +43,9 @@ public class RoundControl {
* Create a new RoundControl using the given gameState and view
*
* @param roundState
- * initial round state
+ * initial round state
* @param view
- * view used for user interaction
+ * view used for user interaction
*/
public RoundControl(IRoundState roundState, IView view) {
this.roundState = roundState;
@@ -89,10 +89,10 @@ public class RoundControl {
}
view.getTablePanel().setStoneSets(clonedTable);
- view.setCurrentPlayerName(roundState.getActivePlayer()
- .getPlayerSettings().getName());
- view.setCurrentPlayerColor(roundState.getActivePlayer()
- .getPlayerSettings().getColor());
+ view.setCurrentPlayerName(roundState.getActivePlayer().getPlayerSettings()
+ .getName());
+ view.setCurrentPlayerColor(roundState.getActivePlayer().getPlayerSettings()
+ .getColor());
view.setHasLaidOut(roundState.getActivePlayer().getLaidOut());
if (!isHuman)
@@ -106,15 +106,14 @@ public class RoundControl {
.getTurnControlType() == HUMAN;
boolean inspectOnly = roundState.getTurnNumber() < 1;
boolean mayRedeal = inspectOnly
- && roundState.getActivePlayer().getHand()
- .getIdenticalStoneCount() >= 3;
+ && roundState.getActivePlayer().getHand().getIdenticalStoneCount() >= 3;
if (isHuman) {
view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal);
}
turnControl = TurnControlFactory.getFactory(
- roundState.getActivePlayer().getPlayerSettings()
- .getTurnControlType()).create();
+ roundState.getActivePlayer().getPlayerSettings().getTurnControlType())
+ .create();
turnControl.setup(roundState.getActivePlayer(), clonedTable, view,
inspectOnly, mayRedeal);
turnControl.getEndOfTurnEvent().add(new IListener() {
@@ -136,10 +135,8 @@ public class RoundControl {
void deal() {
for (int i = 0; i < roundState.getPlayerCount(); i++) {
IHand hand = roundState.getNthNextPlayer(i).getHand();
- for (int j = 0; j < roundState.getGameSettings()
- .getNumberOfStonesDealt(); j++) {
- hand.drop(roundState.getGameHeap().drawStone(), new Position(0,
- 0));
+ for (int j = 0; j < roundState.getGameSettings().getNumberOfStonesDealt(); j++) {
+ hand.drop(roundState.getGameHeap().drawStone(), new Position(0, 0));
}
}
}
@@ -150,13 +147,11 @@ public class RoundControl {
int totalValue = 0;
for (StoneSet set : newSets) {
- totalValue += set.classify(roundState.getGameSettings())
- .getSecond();
+ totalValue += set.classify(roundState.getGameSettings()).getSecond();
}
return totalValue == 0
- || totalValue >= roundState.getGameSettings()
- .getInitialMeldThreshold();
+ || totalValue >= roundState.getGameSettings().getInitialMeldThreshold();
}
private void endOfTurn() {
@@ -195,8 +190,7 @@ public class RoundControl {
}
if (!roundState.getActivePlayer().getLaidOut()) {
// Player touched forbidden stones
- if (!tableSetDifference(clonedTable, roundState.getTable())
- .isEmpty()) {
+ if (!tableSetDifference(clonedTable, roundState.getTable()).isEmpty()) {
rejectMove();
return;
}
@@ -205,8 +199,7 @@ public class RoundControl {
return;
}
}
- Set<Stone> tableDiff = tableDifference(roundState.getTable(),
- clonedTable);
+ Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
roundState.setTable(clonedTable);
@@ -222,8 +215,7 @@ public class RoundControl {
}
private void rejectMove() {
- Set<Stone> tableDiff = tableDifference(roundState.getTable(),
- clonedTable);
+ Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
// deal penalty, reset
roundState.getGameHeap().putBack(tableDiff);
dealPenalty(tableDiff.size());
@@ -310,13 +302,13 @@ public class RoundControl {
int stonePoints = 0;
if (!player.getLaidOut()) {
- stonePoints = playerHand.isInitialMeldPossible() ? 200 : 100;
+ stonePoints = playerHand.isInitialMeldPossible(roundState
+ .getGameSettings()) ? 200 : 100;
} else {
- stonePoints = playerHand.getStonePoints();
+ stonePoints = playerHand.getStonePoints(roundState.getGameSettings());
}
- bestScore = updateBestScore(bestScore, -stonePoints,
- playerHand.getSize());
+ bestScore = updateBestScore(bestScore, -stonePoints, playerHand.getSize());
points.add(-stonePoints);
pointSum += stonePoints;
diff --git a/src/jrummikub/model/Hand.java b/src/jrummikub/model/Hand.java
index 344855d..b6d9eb4 100644
--- a/src/jrummikub/model/Hand.java
+++ b/src/jrummikub/model/Hand.java
@@ -18,18 +18,6 @@ public class Hand extends StoneTray<Stone> implements IHand {
*/
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;
@@ -74,7 +62,8 @@ public class Hand extends StoneTray<Stone> implements IHand {
}
}
- public int getStonePoints() {
+ @Override
+ public int getStonePoints(GameSettings settings) {
int points = 0;
for (Pair<Stone, Position> entry : this) {
@@ -89,7 +78,9 @@ public class Hand extends StoneTray<Stone> implements IHand {
}
@Override
- public boolean isInitialMeldPossible() {
+ public boolean isInitialMeldPossible(GameSettings settings) {
+ AIUtil aiUtil = new AIUtil(settings);
+
List<Stone> stones = new ArrayList<Stone>();
for (Iterator<Pair<Stone, Position>> iter = this.iterator(); iter.hasNext();) {
@@ -99,11 +90,9 @@ public class Hand extends StoneTray<Stone> implements IHand {
Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> stoneCounts = AIUtil
.countStones(stones);
- Pair<List<StoneSet>, Integer> result = AIUtil.findSetsWithTotalPoints(
+ Pair<List<StoneSet>, Integer> result = aiUtil.findSetsWithTotalPoints(
settings.getInitialMeldThreshold(), stoneCounts.getFirst(),
stoneCounts.getSecond());
-
- System.out.println(result);
return (result.getSecond() >= settings.getInitialMeldThreshold());
}
diff --git a/src/jrummikub/model/IHand.java b/src/jrummikub/model/IHand.java
index 5ac7537..c6755c7 100644
--- a/src/jrummikub/model/IHand.java
+++ b/src/jrummikub/model/IHand.java
@@ -16,7 +16,7 @@ public interface IHand extends IStoneTray<Stone> {
* Gets the amount of free space in a hand row
*
* @param row
- * the row number
+ * the row number
* @return the number of stones that can fit into the row
*/
int getFreeRowSpace(int row);
@@ -24,17 +24,23 @@ public interface IHand extends IStoneTray<Stone> {
/**
* Get the accumulated number of points of stones in the hand
*
+ * @param settings
+ * the game settings
+ *
* @return points
*/
- int getStonePoints();
+ int getStonePoints(GameSettings settings);
/**
* Tests whether it is possible to lay down an initial meld using the stones
* on the hand
*
+ * @param settings
+ * the game settings
+ *
* @return true if an initial meld is possible
*/
- public boolean isInitialMeldPossible();
+ public boolean isInitialMeldPossible(GameSettings settings);
/**
* Counts the pairs of identical stones
diff --git a/src/jrummikub/model/Player.java b/src/jrummikub/model/Player.java
index 1d54c4a..38d2587 100644
--- a/src/jrummikub/model/Player.java
+++ b/src/jrummikub/model/Player.java
@@ -10,14 +10,12 @@ public class Player implements IPlayer {
* Create a new player with a given name and color
*
* @param settings
- * the player settings
- * @param gameSettings
- * the game settings
+ * the player settings
*/
- public Player(PlayerSettings settings, GameSettings gameSettings) {
+ public Player(PlayerSettings settings) {
this.settings = settings;
- hand = new Hand(gameSettings);
+ hand = new Hand();
laidOut = false;
}
diff --git a/src/jrummikub/model/RoundState.java b/src/jrummikub/model/RoundState.java
index 01d67d3..fd7dc15 100644
--- a/src/jrummikub/model/RoundState.java
+++ b/src/jrummikub/model/RoundState.java
@@ -12,13 +12,13 @@ public class RoundState implements IRoundState {
private int activePlayer;
private StoneHeap gameHeap;
private IPlayer lastPlayer;
- private int turnNumber;
+ private int turnNumber;
/**
* Create a new RoundState with an empty table
*
* @param gameSettings
- * the game settings
+ * the game settings
*/
public RoundState(GameSettings gameSettings) {
this.gameSettings = gameSettings;
@@ -27,11 +27,11 @@ public class RoundState implements IRoundState {
players = new ArrayList<Player>();
for (PlayerSettings playerSettings : gameSettings.getPlayerList()) {
- players.add(new Player(playerSettings, gameSettings));
+ players.add(new Player(playerSettings));
}
- turnNumber = 1-gameSettings.getPlayerList().size();
-
+ turnNumber = 1 - gameSettings.getPlayerList().size();
+
activePlayer = 0;
gameHeap = new StoneHeap(gameSettings);
}
@@ -73,7 +73,7 @@ public class RoundState implements IRoundState {
}
return players.get(j);
}
-
+
@Override
public IPlayer getNthPlayer(int i) {
int j = i % players.size();
@@ -107,7 +107,7 @@ public class RoundState implements IRoundState {
public IPlayer getLastPlayer() {
return lastPlayer;
}
-
+
@Override
public int getTurnNumber() {
return turnNumber;