From af3661fea084df6e089c2597dc4b311c77f39e4f Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 7 Jun 2011 00:23:00 +0200 Subject: Always operate on cloned hands in the turn controls git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@380 72836036-5685-4462-b002-a69064685172 --- .../control/turn/AbstractTurnControl.java | 13 +--- src/jrummikub/control/turn/BaseAIControl.java | 16 ++--- src/jrummikub/control/turn/HumanTurnControl.java | 56 +++++++-------- src/jrummikub/control/turn/ITurnControl.java | 81 +++++++++++++++++++--- 4 files changed, 110 insertions(+), 56 deletions(-) (limited to 'src/jrummikub/control/turn') diff --git a/src/jrummikub/control/turn/AbstractTurnControl.java b/src/jrummikub/control/turn/AbstractTurnControl.java index 404862a..554db61 100644 --- a/src/jrummikub/control/turn/AbstractTurnControl.java +++ b/src/jrummikub/control/turn/AbstractTurnControl.java @@ -1,8 +1,6 @@ package jrummikub.control.turn; import jrummikub.model.GameSettings; -import jrummikub.model.IPlayer; -import jrummikub.model.ITable; import jrummikub.util.Event; import jrummikub.util.IEvent; import jrummikub.view.IView; @@ -14,11 +12,9 @@ public abstract class AbstractTurnControl implements ITurnControl { protected Event endOfTurnEvent = new Event(); protected Event redealEvent = new Event(); + protected TurnInfo turnInfo; protected GameSettings settings; - protected IPlayer player; - protected ITable table; protected IView view; - protected TurnMode turnMode; @Override public IEvent getEndOfTurnEvent() { @@ -31,13 +27,10 @@ public abstract class AbstractTurnControl implements ITurnControl { } @Override - public void setup(GameSettings settings, IPlayer player, ITable table, - IView view, TurnMode turnMode) { + public void setup(TurnInfo info, GameSettings settings, IView view) { + turnInfo = info; this.settings = settings; - this.player = player; - this.table = table; this.view = view; - this.turnMode = turnMode; } } \ No newline at end of file diff --git a/src/jrummikub/control/turn/BaseAIControl.java b/src/jrummikub/control/turn/BaseAIControl.java index d06e263..dabdc81 100644 --- a/src/jrummikub/control/turn/BaseAIControl.java +++ b/src/jrummikub/control/turn/BaseAIControl.java @@ -63,7 +63,7 @@ public class BaseAIControl extends AbstractTurnControl { } private void compute() { - switch (turnMode) { + switch (turnInfo.getTurnMode()) { case MAY_REDEAL: emitRedeal(); break; @@ -77,14 +77,14 @@ public class BaseAIControl extends AbstractTurnControl { } private Stone findMatchingStone(Stone target) { - for (Pair entry : player.getHand()) { + for (Pair entry : turnInfo.getHand()) { Stone stone = entry.getFirst(); if (stone.getValue() == target.getValue() && stone.getColor() == target.getColor()) { return stone; } } - for (Pair entry : player.getHand()) { + for (Pair entry : turnInfo.getHand()) { Stone stone = entry.getFirst(); if (stone.isJoker()) { return stone; @@ -95,14 +95,14 @@ public class BaseAIControl extends AbstractTurnControl { private Stone pickUpMatchingStone(Stone target) { Stone match = findMatchingStone(target); - player.getHand().pickUp(match); + turnInfo.getHand().pickUp(match); return match; } private void turn() { List stones = new ArrayList(); - for (Pair entry : player.getHand()) { + for (Pair entry : turnInfo.getHand()) { stones.add(entry.getFirst()); } @@ -115,7 +115,7 @@ public class BaseAIControl extends AbstractTurnControl { Math.max(30, settings.getInitialMeldThreshold() * 2), counts.getFirst(), counts.getSecond()); - if (!player.getLaidOut() + if (!turnInfo.getLaidOut() && result.getSecond() < settings.getInitialMeldThreshold()) { emitEndOfTurn(); return; @@ -126,7 +126,7 @@ public class BaseAIControl extends AbstractTurnControl { for (Stone stone : set) { handStones.add(pickUpMatchingStone(stone)); } - table.drop(new StoneSet(handStones), new Position( + turnInfo.getTable().drop(new StoneSet(handStones), new Position( (float) Math.random() * 30 - 15, (float) Math.random() * 6 - 3)); } @@ -148,7 +148,7 @@ public class BaseAIControl extends AbstractTurnControl { private void emitEndOfTurn() { long timeElapsed = System.currentTimeMillis() - startTime; long timeNeeded = Math.min((long) (1000 + Math.random() - * player.getHand().getSize() * 100), 50000); + * turnInfo.getHand().getSize() * 100), 50000); long waitTime = timeNeeded - timeElapsed; if (waitTime > 0) { diff --git a/src/jrummikub/control/turn/HumanTurnControl.java b/src/jrummikub/control/turn/HumanTurnControl.java index f13b3a2..3bedc1a 100644 --- a/src/jrummikub/control/turn/HumanTurnControl.java +++ b/src/jrummikub/control/turn/HumanTurnControl.java @@ -73,11 +73,11 @@ public class HumanTurnControl extends AbstractTurnControl { addHandPanelHandlers(); addStoneCollectionHandlers(); - if (turnMode == TurnMode.NORMAL_TURN) { + if (turnInfo.getTurnMode() == TurnMode.NORMAL_TURN) { addTablePanelHandlers(); } - view.getHandPanel().setStones(player.getHand().clone()); + view.getHandPanel().setStones(turnInfo.getHand().clone()); view.getHandPanel().resetCurrentRow(); view.setBottomPanel(BottomPanelType.HUMAN_HAND_PANEL); @@ -227,7 +227,7 @@ public class HumanTurnControl extends AbstractTurnControl { private void handClick(Position pos) { List handStones = new ArrayList(); for (Stone s : selectedStones) { - if (player.getHand().pickUp(s)) { + if (turnInfo.getHand().pickUp(s)) { handStones.add(s); } } @@ -236,28 +236,28 @@ public class HumanTurnControl extends AbstractTurnControl { for (Stone s : handStones) { double x = Math.max(0, Math.min(13, pos.getX() - handStones.size() / 2.0f + i)); - player.getHand().drop(s, + turnInfo.getHand().drop(s, new Position(x, (float) Math.floor(pos.getY()))); selectedStones.remove(s); i++; } view.setSelectedStones(selectedStones); - view.getHandPanel().setStones(player.getHand()); + view.getHandPanel().setStones(turnInfo.getHand()); } private void sortStones(Comparator comparator) { List stones = new ArrayList(); - for (Pair entry : player.getHand()) { + for (Pair entry : turnInfo.getHand()) { stones.add(entry.getFirst()); } for (Stone stone : stones) { - player.getHand().pickUp(stone); + turnInfo.getHand().pickUp(stone); } Collections.sort(stones, comparator); int x = 0, y = 0; for (Stone stone : stones) { - player.getHand().drop(stone, new Position(x, y)); + turnInfo.getHand().drop(stone, new Position(x, y)); x++; if (x >= Hand.WIDTH) { x = 0; @@ -265,7 +265,7 @@ public class HumanTurnControl extends AbstractTurnControl { } } - view.getHandPanel().setStones(player.getHand()); + view.getHandPanel().setStones(turnInfo.getHand()); } private void sortByRuns() { @@ -306,8 +306,8 @@ public class HumanTurnControl extends AbstractTurnControl { private void pickUpSelectedStones() { for (Stone stone : selectedStones) { - player.getHand().pickUp(stone); - table.pickUpStone(stone); + turnInfo.getHand().pickUp(stone); + turnInfo.getTable().pickUpStone(stone); } } @@ -317,12 +317,12 @@ public class HumanTurnControl extends AbstractTurnControl { } pickUpSelectedStones(); - table.drop(new StoneSet(selectedStones), new Position(position.getX() + turnInfo.getTable().drop(new StoneSet(selectedStones), new Position(position.getX() - selectedStones.size() * 0.5f, position.getY() - 0.5f)); selectedStones.clear(); - view.getTablePanel().setStoneSets(table); - view.getHandPanel().setStones(player.getHand()); + view.getTablePanel().setStoneSets(turnInfo.getTable()); + view.getHandPanel().setStones(turnInfo.getHand()); view.setSelectedStones(selectedStones); } @@ -330,7 +330,7 @@ public class HumanTurnControl extends AbstractTurnControl { if (!collect) { selectedStones.clear(); } - StoneSet selectedSet = table.findStoneSet(stone); + StoneSet selectedSet = turnInfo.getTable().findStoneSet(stone); for (Stone setStone : selectedSet) { selectedStones.remove(setStone); selectedStones.add(setStone); @@ -344,8 +344,8 @@ public class HumanTurnControl extends AbstractTurnControl { return; } Stone lastStone = selectedStones.get(selectedStones.size() - 1); - StoneSet lastSet = table.findStoneSet(lastStone); - StoneSet selectedSet = table.findStoneSet(stone); + StoneSet lastSet = turnInfo.getTable().findStoneSet(lastStone); + StoneSet selectedSet = turnInfo.getTable().findStoneSet(stone); if (lastSet != selectedSet) { stoneClick(stone, true); return; @@ -376,13 +376,13 @@ public class HumanTurnControl extends AbstractTurnControl { return; } Stone lastStone = selectedStones.get(selectedStones.size() - 1); - StoneSet lastSet = table.findStoneSet(lastStone); + StoneSet lastSet = turnInfo.getTable().findStoneSet(lastStone); if (lastSet != null) { stoneClick(stone, true); return; } List> handPairs = new ArrayList>(); - for (Pair entry : player.getHand()) { + for (Pair entry : turnInfo.getHand()) { handPairs.add(entry); } @@ -410,7 +410,7 @@ public class HumanTurnControl extends AbstractTurnControl { private void connectorClick(StoneSet set, boolean right) { List stones = new LinkedList(); - Position pos = table.getPosition(set); + Position pos = turnInfo.getTable().getPosition(set); for (Stone stone : set) { stones.add(stone); } @@ -421,24 +421,24 @@ public class HumanTurnControl extends AbstractTurnControl { pickUpSelectedStones(); StoneSet newSet = null; for (Stone stone : stones) { - newSet = table.findStoneSet(stone); + newSet = turnInfo.getTable().findStoneSet(stone); if (newSet != null) { break; } } if (newSet != null) { - Position newPos = table.getPosition(newSet); - table.pickUp(newSet); + Position newPos = turnInfo.getTable().getPosition(newSet); + turnInfo.getTable().pickUp(newSet); if (right) { StoneSet joinedSet = newSet.join(new StoneSet(selectedStones)); - table.drop(joinedSet, newPos); + turnInfo.getTable().drop(joinedSet, newPos); } else { StoneSet joinedSet = new StoneSet(selectedStones).join(newSet); - table.drop(joinedSet, new Position(newPos.getX() + turnInfo.getTable().drop(joinedSet, new Position(newPos.getX() - selectedStones.size(), newPos.getY())); } } else { - table.drop( + turnInfo.getTable().drop( new StoneSet(selectedStones), new Position(pos.getX() + (set.getSize() - selectedStones.size()) * 0.5f, @@ -447,8 +447,8 @@ public class HumanTurnControl extends AbstractTurnControl { selectedStones.clear(); - view.getTablePanel().setStoneSets(table); - view.getHandPanel().setStones(player.getHand()); + view.getTablePanel().setStoneSets(turnInfo.getTable()); + view.getHandPanel().setStones(turnInfo.getHand()); view.setSelectedStones(selectedStones); } diff --git a/src/jrummikub/control/turn/ITurnControl.java b/src/jrummikub/control/turn/ITurnControl.java index 2dfe2ab..b8a39ce 100644 --- a/src/jrummikub/control/turn/ITurnControl.java +++ b/src/jrummikub/control/turn/ITurnControl.java @@ -1,7 +1,7 @@ package jrummikub.control.turn; import jrummikub.model.GameSettings; -import jrummikub.model.IPlayer; +import jrummikub.model.IHand; import jrummikub.model.ITable; import jrummikub.util.IEvent; import jrummikub.view.IView; @@ -14,19 +14,15 @@ public interface ITurnControl { /** * Start the turn * + * @param info + * the current turn state + * * @param settings * the game settings - * @param player - * the active player - * @param table - * current table * @param view * view for user interaction. - * @param turnMode - * whether it is turn zero and if one may redeal */ - public void setup(GameSettings settings, IPlayer player, ITable table, - IView view, TurnMode turnMode); + public void setup(TurnInfo info, GameSettings settings, IView view); /** * Get the event that is emitted when the turn is over @@ -45,6 +41,71 @@ public interface ITurnControl { /** * Start the turn */ - public abstract void startTurn(); + public void startTurn(); + + /** + * The TurnInfo class encapsulates all information concerning the current turn + */ + public class TurnInfo { + private ITable table; + private IHand hand; + private boolean hasLaidOut; + private TurnMode turnMode; + + /** + * Creates a new TurnInfo instance + * + * @param table + * the current table + * @param hand + * the current player's hand + * @param hasLaidOut + * has the player laid out yet? + * @param turnMode + * the turn mode + */ + public TurnInfo(ITable table, IHand hand, boolean hasLaidOut, + TurnMode turnMode) { + this.table = table; + this.hand = hand; + this.hasLaidOut = hasLaidOut; + this.turnMode = turnMode; + } + + /** + * Gets the current table + * + * @return the table + */ + public ITable getTable() { + return table; + } + + /** + * Gets the current player's hand + * + * @return the hand + */ + public IHand getHand() { + return hand; + } + + /** + * Returns if the current player has laid out yet + * + * @return if the player has laid out + */ + public boolean getLaidOut() { + return hasLaidOut; + } + /** + * Gets the current turn's mode + * + * @return the turn mode + */ + public TurnMode getTurnMode() { + return turnMode; + } + } } \ No newline at end of file -- cgit v1.2.3