Base AI is now able to meld initially properly
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@346 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
58d58c0c07
commit
061e7ab305
5 changed files with 129 additions and 65 deletions
|
@ -1,5 +1,6 @@
|
|||
package jrummikub.control.turn;
|
||||
|
||||
import jrummikub.model.GameSettings;
|
||||
import jrummikub.model.IHand;
|
||||
import jrummikub.model.IPlayer;
|
||||
import jrummikub.model.ITable;
|
||||
|
@ -8,19 +9,19 @@ import jrummikub.util.IEvent;
|
|||
import jrummikub.view.IView;
|
||||
|
||||
/**
|
||||
* Abstract base class for TurnControls
|
||||
* Abstract base class for TurnControls
|
||||
*/
|
||||
public abstract class AbstractTurnControl implements ITurnControl {
|
||||
|
||||
protected Event endOfTurnEvent = new Event();
|
||||
protected Event redealEvent = new Event();
|
||||
protected GameSettings settings;
|
||||
protected IPlayer player;
|
||||
protected IHand hand;
|
||||
protected ITable table;
|
||||
protected IView view;
|
||||
protected boolean inspectOnly;
|
||||
protected boolean mayRedeal;
|
||||
|
||||
|
||||
@Override
|
||||
public IEvent getEndOfTurnEvent() {
|
||||
|
@ -31,10 +32,11 @@ public abstract class AbstractTurnControl implements ITurnControl {
|
|||
public Event getRedealEvent() {
|
||||
return redealEvent;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setup(IPlayer player, ITable table, IView view,
|
||||
boolean inspectOnly, boolean mayRedeal) {
|
||||
public void setup(GameSettings settings, IPlayer player, ITable table,
|
||||
IView view, boolean inspectOnly, boolean mayRedeal) {
|
||||
this.settings = settings;
|
||||
this.player = player;
|
||||
this.hand = player.getHand();
|
||||
this.table = table;
|
||||
|
|
|
@ -2,13 +2,20 @@ package jrummikub.control.turn;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import jrummikub.control.AIUtil;
|
||||
import jrummikub.control.ITurnTimer;
|
||||
import jrummikub.control.TurnTimer;
|
||||
import jrummikub.model.Position;
|
||||
import jrummikub.model.Stone;
|
||||
import jrummikub.model.StoneColor;
|
||||
import jrummikub.model.StoneSet;
|
||||
import jrummikub.util.Connection;
|
||||
import jrummikub.util.IListener;
|
||||
import jrummikub.util.Pair;
|
||||
|
||||
/**
|
||||
* Base class for AI players
|
||||
|
@ -59,15 +66,61 @@ public class BaseAIControl extends AbstractTurnControl {
|
|||
if (mayRedeal) {
|
||||
emitRedeal();
|
||||
} else {
|
||||
if (player.getLaidOut()) {
|
||||
layOut();
|
||||
} else {
|
||||
emitEndOfTurn();
|
||||
}
|
||||
turn();
|
||||
}
|
||||
}
|
||||
|
||||
private void layOut() {
|
||||
private Stone findMatchingStone(Stone target) {
|
||||
for(Pair<Stone, Position> entry : hand){
|
||||
Stone stone = entry.getFirst();
|
||||
if (stone.getValue() == target.getValue() && stone.getColor() == target.getColor()) {
|
||||
return stone;
|
||||
}
|
||||
}
|
||||
for(Pair<Stone, Position> entry : hand){
|
||||
Stone stone = entry.getFirst();
|
||||
if (stone.isJoker()) {
|
||||
return stone;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Stone pickUpMatchingStone(Stone target) {
|
||||
Stone match = findMatchingStone(target);
|
||||
hand.pickUp(match);
|
||||
return match;
|
||||
}
|
||||
|
||||
private void turn() {
|
||||
List<Stone> stones = new ArrayList<Stone>();
|
||||
|
||||
for (Pair<Stone, Position> entry : hand) {
|
||||
stones.add(entry.getFirst());
|
||||
}
|
||||
|
||||
Pair<TreeMap<Pair<Integer, StoneColor>, Integer>, Integer> counts = AIUtil
|
||||
.countStones(stones);
|
||||
|
||||
AIUtil aiUtil = new AIUtil(settings);
|
||||
|
||||
Pair<List<StoneSet>, Integer> result = aiUtil.findSetsWithTotalPoints(
|
||||
Integer.MAX_VALUE, counts.getFirst(), counts.getSecond());
|
||||
|
||||
if (!player.getLaidOut()
|
||||
&& result.getSecond() < settings.getInitialMeldThreshold()) {
|
||||
emitEndOfTurn();
|
||||
return;
|
||||
}
|
||||
|
||||
for (StoneSet set : result.getFirst()) {
|
||||
List<Stone> handStones = new ArrayList<Stone>();
|
||||
for (Stone stone : set) {
|
||||
handStones.add(pickUpMatchingStone(stone));
|
||||
}
|
||||
table.drop(new StoneSet(handStones), new Position(0, 0));
|
||||
}
|
||||
|
||||
emitEndOfTurn();
|
||||
}
|
||||
|
||||
|
@ -75,8 +128,10 @@ public class BaseAIControl extends AbstractTurnControl {
|
|||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cleanUp();
|
||||
redealEvent.emit();
|
||||
if (!stopRunning) {
|
||||
cleanUp();
|
||||
redealEvent.emit();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -98,8 +153,10 @@ public class BaseAIControl extends AbstractTurnControl {
|
|||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
cleanUp();
|
||||
endOfTurnEvent.emit();
|
||||
if (!stopRunning) {
|
||||
cleanUp();
|
||||
endOfTurnEvent.emit();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package jrummikub.control.turn;
|
||||
|
||||
import jrummikub.model.GameSettings;
|
||||
import jrummikub.model.IPlayer;
|
||||
import jrummikub.model.ITable;
|
||||
import jrummikub.util.Event;
|
||||
|
@ -10,19 +11,21 @@ public interface ITurnControl {
|
|||
/**
|
||||
* Start the turn
|
||||
*
|
||||
* @param hand
|
||||
* active player's hand
|
||||
* @param settings
|
||||
* the game settings
|
||||
* @param player
|
||||
* the active player
|
||||
* @param table
|
||||
* current table
|
||||
* current table
|
||||
* @param view
|
||||
* view for user interaction.
|
||||
* view for user interaction.
|
||||
* @param inspectOnly
|
||||
* the current turn doesn't allow any table manipulation
|
||||
* the current turn doesn't allow any table manipulation
|
||||
* @param mayRedeal
|
||||
* true when the current player may decide to redeal
|
||||
* true when the current player may decide to redeal
|
||||
*/
|
||||
public void setup(IPlayer player, ITable table, IView view,
|
||||
boolean inspectOnly, boolean mayRedeal);
|
||||
public void setup(GameSettings settings, IPlayer player, ITable table,
|
||||
IView view, boolean inspectOnly, boolean mayRedeal);
|
||||
|
||||
/**
|
||||
* Get the event that is emitted when the turn is over
|
||||
|
|
Reference in a new issue