Metric fixes

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@358 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Bennet Gerlach 2011-05-31 20:42:46 +02:00
parent 6319ec8ab6
commit 640a1e0fb6
13 changed files with 153 additions and 99 deletions

View file

@ -1,5 +1,6 @@
package jrummikub.view; package jrummikub.view;
import jrummikub.control.turn.TurnMode;
import jrummikub.util.IEvent; import jrummikub.util.IEvent;
import jrummikub.util.MockEvent; import jrummikub.util.MockEvent;
@ -16,9 +17,7 @@ public class MockPlayerPanel implements IPlayerPanel {
/** */ /** */
public MockEvent sortByRunsEvent = new MockEvent(); public MockEvent sortByRunsEvent = new MockEvent();
/** */ /** */
public boolean inspectOnly; public TurnMode turnMode;
/** */
public boolean mayRedeal;
@Override @Override
public void setTimeLeft(int time) { public void setTimeLeft(int time) {
@ -46,9 +45,9 @@ public class MockPlayerPanel implements IPlayerPanel {
return redealEvent; return redealEvent;
} }
public void setEndTurnMode(boolean inspectOnly, boolean mayRedeal) { @Override
this.inspectOnly = inspectOnly; public void setEndTurnMode(TurnMode turnMode) {
this.mayRedeal = mayRedeal; this.turnMode = turnMode;
} }
} }

View file

@ -9,6 +9,7 @@ import java.util.Set;
import jrummikub.control.turn.ITurnControl; import jrummikub.control.turn.ITurnControl;
import jrummikub.control.turn.TurnControlFactory; import jrummikub.control.turn.TurnControlFactory;
import jrummikub.control.turn.TurnMode;
import jrummikub.model.Hand; import jrummikub.model.Hand;
import jrummikub.model.IHand; import jrummikub.model.IHand;
import jrummikub.model.IPlayer; import jrummikub.model.IPlayer;
@ -103,19 +104,24 @@ public class RoundControl {
return; return;
boolean isHuman = roundState.getActivePlayer().getPlayerSettings() boolean isHuman = roundState.getActivePlayer().getPlayerSettings()
.getTurnControlType() == HUMAN; .getTurnControlType() == HUMAN;
boolean inspectOnly = roundState.getTurnNumber() < 1;
boolean mayRedeal = inspectOnly TurnMode turnMode = TurnMode.NORMAL_TURN;
&& roundState.getActivePlayer().getHand().getIdenticalStoneCount() >= 3;
if (roundState.getTurnNumber() < 1) {
turnMode = TurnMode.INSPECT_ONLY;
if (roundState.getActivePlayer().getHand().getIdenticalStoneCount() >= 3) {
turnMode = TurnMode.MAY_REDEAL;
}
}
if (isHuman) { if (isHuman) {
view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal); view.getPlayerPanel().setEndTurnMode(turnMode);
} }
turnControl = TurnControlFactory.getFactory( turnControl = TurnControlFactory.getFactory(
roundState.getActivePlayer().getPlayerSettings().getTurnControlType()) roundState.getActivePlayer().getPlayerSettings().getTurnControlType())
.create(); .create();
turnControl turnControl.setup(roundState.getGameSettings(),
.setup(roundState.getGameSettings(), roundState.getActivePlayer(), roundState.getActivePlayer(), clonedTable, view, turnMode);
clonedTable, view, inspectOnly, mayRedeal);
turnControl.getEndOfTurnEvent().add(new IListener() { turnControl.getEndOfTurnEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {

View file

@ -20,8 +20,7 @@ public abstract class AbstractTurnControl implements ITurnControl {
protected IHand hand; protected IHand hand;
protected ITable table; protected ITable table;
protected IView view; protected IView view;
protected boolean inspectOnly; protected TurnMode turnMode;
protected boolean mayRedeal;
@Override @Override
public IEvent getEndOfTurnEvent() { public IEvent getEndOfTurnEvent() {
@ -35,14 +34,13 @@ public abstract class AbstractTurnControl implements ITurnControl {
@Override @Override
public void setup(GameSettings settings, IPlayer player, ITable table, public void setup(GameSettings settings, IPlayer player, ITable table,
IView view, boolean inspectOnly, boolean mayRedeal) { IView view, TurnMode turnMode) {
this.settings = settings; this.settings = settings;
this.player = player; this.player = player;
this.hand = player.getHand(); this.hand = player.getHand();
this.table = table; this.table = table;
this.view = view; this.view = view;
this.inspectOnly = inspectOnly; this.turnMode = turnMode;
this.mayRedeal = mayRedeal;
} }
} }

View file

@ -63,14 +63,16 @@ public class BaseAIControl extends AbstractTurnControl {
} }
private void compute() { private void compute() {
if (inspectOnly) { switch (turnMode) {
if (mayRedeal) { case MAY_REDEAL:
emitRedeal(); emitRedeal();
} else { break;
emitEndOfTurn(); case INSPECT_ONLY:
} emitEndOfTurn();
} else { break;
case NORMAL_TURN:
turn(); turn();
break;
} }
} }
@ -124,7 +126,8 @@ public class BaseAIControl extends AbstractTurnControl {
for (Stone stone : set) { for (Stone stone : set) {
handStones.add(pickUpMatchingStone(stone)); handStones.add(pickUpMatchingStone(stone));
} }
table.drop(new StoneSet(handStones), new Position((float)Math.random() * 30 - 15, (float)Math.random() * 6 - 3)); table.drop(new StoneSet(handStones), new Position(
(float) Math.random() * 30 - 15, (float) Math.random() * 6 - 3));
} }
emitEndOfTurn(); emitEndOfTurn();
@ -144,8 +147,8 @@ public class BaseAIControl extends AbstractTurnControl {
private void emitEndOfTurn() { private void emitEndOfTurn() {
long timeElapsed = System.currentTimeMillis() - startTime; long timeElapsed = System.currentTimeMillis() - startTime;
long timeNeeded = Math.min( long timeNeeded = Math.min((long) (1000 + Math.random() * hand.getSize()
(long) (1000 + Math.random() * hand.getSize() * 100), 50000); * 100), 50000);
long waitTime = timeNeeded - timeElapsed; long waitTime = timeNeeded - timeElapsed;
if (waitTime > 0) { if (waitTime > 0) {

View file

@ -60,32 +60,23 @@ public class HumanTurnControl extends AbstractTurnControl {
if (this.timer == null) { if (this.timer == null) {
this.timer = new TurnTimer(view); this.timer = new TurnTimer(view);
} }
IListener endOfTurnListener = new IListener() {
connections.add(timer.getTimeRunOutEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
endOfTurn(false); endOfTurn(false);
} }
}; }));
connections.add(timer.getTimeRunOutEvent().add(endOfTurnListener)); addButtonHandlers();
connections.add(view.getPlayerPanel().getEndTurnEvent()
.add(endOfTurnListener));
connections.add(view.getPlayerPanel().getRedealEvent()
.add(new IListener() {
@Override
public void handle() {
endOfTurn(true);
}
}));
addHandPanelHandlers(); addHandPanelHandlers();
addStoneCollectionHandlers(); addStoneCollectionHandlers();
if (!inspectOnly)
if (turnMode == TurnMode.NORMAL_TURN) {
addTablePanelHandlers(); addTablePanelHandlers();
}
addListeners();
view.getHandPanel().setStones(hand.clone()); view.getHandPanel().setStones(hand.clone());
view.getHandPanel().resetCurrentRow(); view.getHandPanel().resetCurrentRow();
view.setBottomPanel(BottomPanelType.HUMAN_HAND_PANEL); view.setBottomPanel(BottomPanelType.HUMAN_HAND_PANEL);
@ -93,7 +84,7 @@ public class HumanTurnControl extends AbstractTurnControl {
timer.startTimer(); timer.startTimer();
} }
private void addListeners() { private void addButtonHandlers() {
connections.add(view.getPlayerPanel().getSortByGroupsEvent() connections.add(view.getPlayerPanel().getSortByGroupsEvent()
.add(new IListener() { .add(new IListener() {
@Override @Override
@ -109,6 +100,22 @@ public class HumanTurnControl extends AbstractTurnControl {
sortByRuns(); sortByRuns();
} }
})); }));
connections.add(view.getPlayerPanel().getEndTurnEvent()
.add(new IListener() {
@Override
public void handle() {
endOfTurn(false);
}
}));
connections.add(view.getPlayerPanel().getRedealEvent().add(new IListener() {
@Override
public void handle() {
endOfTurn(true);
}
}));
} }
private void addHandPanelHandlers() { private void addHandPanelHandlers() {
@ -425,12 +432,15 @@ public class HumanTurnControl extends AbstractTurnControl {
table.drop(joinedSet, newPos); table.drop(joinedSet, newPos);
} else { } else {
StoneSet joinedSet = new StoneSet(selectedStones).join(newSet); StoneSet joinedSet = new StoneSet(selectedStones).join(newSet);
table.drop(joinedSet, new Position(newPos.getX() table.drop(joinedSet,
- selectedStones.size(), newPos.getY())); new Position(newPos.getX() - selectedStones.size(), newPos.getY()));
} }
} else { } else {
table.drop(new StoneSet(selectedStones), new Position(pos.getX() table.drop(
+ (set.size() - selectedStones.size()) * 0.5f, pos.getY())); new StoneSet(selectedStones),
new Position(
pos.getX() + (set.size() - selectedStones.size()) * 0.5f, pos
.getY()));
} }
selectedStones.clear(); selectedStones.clear();
@ -525,8 +535,7 @@ public class HumanTurnControl extends AbstractTurnControl {
static class HandStonePositionComparator implements static class HandStonePositionComparator implements
Comparator<Pair<Stone, Position>> { Comparator<Pair<Stone, Position>> {
@Override @Override
public int compare(Pair<Stone, Position> pair1, public int compare(Pair<Stone, Position> pair1, Pair<Stone, Position> pair2) {
Pair<Stone, Position> pair2) {
Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond(); Position pos1 = pair1.getSecond(), pos2 = pair2.getSecond();
if (pos1.getY() < pos2.getY()) { if (pos1.getY() < pos2.getY()) {
return -1; return -1;

View file

@ -11,26 +11,23 @@ import jrummikub.view.IView;
* Interface containing shared methods of human and computer turn control * Interface containing shared methods of human and computer turn control
* *
*/ */
// TODO zu viele parameter
public interface ITurnControl { public interface ITurnControl {
/** /**
* Start the turn * Start the turn
* *
* @param settings * @param settings
* the game settings * the game settings
* @param player * @param player
* the active player * the active player
* @param table * @param table
* current table * current table
* @param view * @param view
* view for user interaction. * view for user interaction.
* @param inspectOnly * @param turnMode
* the current turn doesn't allow any table manipulation * whether it is turn zero and if one may redeal
* @param mayRedeal
* true when the current player may decide to redeal
*/ */
public void setup(GameSettings settings, IPlayer player, ITable table, public void setup(GameSettings settings, IPlayer player, ITable table,
IView view, boolean inspectOnly, boolean mayRedeal); IView view, TurnMode turnMode);
/** /**
* Get the event that is emitted when the turn is over * Get the event that is emitted when the turn is over

View file

@ -16,14 +16,18 @@ public abstract class TurnControlFactory {
COMPUTER COMPUTER
}; };
/**
* Creates a new turn control instance
*
* @return the turn control
*/
public abstract ITurnControl create(); public abstract ITurnControl create();
/** /**
* returns the turn control factory for the specified type * returns the turn control factory for the specified type
* *
* @param type * @param type
* Human or Computer * Human or Computer
* @return TurnControlFactory for the player kind * @return TurnControlFactory for the player kind
*/ */
static public TurnControlFactory getFactory(Type type) { static public TurnControlFactory getFactory(Type type) {

View file

@ -0,0 +1,13 @@
package jrummikub.control.turn;
/**
* Different kinds of turns
*/
public enum TurnMode {
/** Turn zero with possibility to redeal */
MAY_REDEAL,
/** Turn zero without possibility to redeal */
INSPECT_ONLY,
/** A normal turn */
NORMAL_TURN
}

View file

@ -1,5 +1,6 @@
package jrummikub.view; package jrummikub.view;
import jrummikub.control.turn.TurnMode;
import jrummikub.util.IEvent; import jrummikub.util.IEvent;
/** /**
@ -10,7 +11,7 @@ public interface IPlayerPanel {
* Sets the time the player has left for his turn * Sets the time the player has left for his turn
* *
* @param time * @param time
* the time left * the time left
*/ */
public void setTimeLeft(int time); public void setTimeLeft(int time);
@ -23,8 +24,8 @@ public interface IPlayerPanel {
public IEvent getSortByGroupsEvent(); public IEvent getSortByGroupsEvent();
/** /**
* The sort by runs event is emitted when the player wants to sort his * The sort by runs event is emitted when the player wants to sort his stones
* stones by runs * by runs
* *
* @return the event * @return the event
*/ */
@ -47,11 +48,8 @@ public interface IPlayerPanel {
/** /**
* Sets the buttons available to end the turn * Sets the buttons available to end the turn
* *
* @param inspectOnly * @param turnMode
* true for each player's first turn * the {@link TurnMode}
* @param mayRedeal
* true if the player is allowed to trigger a redealing of all
* stones
*/ */
public abstract void setEndTurnMode(boolean inspectOnly, boolean mayRedeal); public abstract void setEndTurnMode(TurnMode turnMode);
} }

View file

@ -121,9 +121,27 @@ public interface IView {
*/ */
public IEvent getNewGameEvent(); public IEvent getNewGameEvent();
/**
* Sets the bottom panels type
*
* @param type
* the type of the bottom panel
*/
public void setBottomPanel(BottomPanelType type); public void setBottomPanel(BottomPanelType type);
/**
* Different types of bottom panels
*/
public enum BottomPanelType { public enum BottomPanelType {
START_GAME_PANEL, START_TURN_PANEL, HUMAN_HAND_PANEL, COMPUTER_HAND_PANEL, WIN_PANEL /** */
START_GAME_PANEL,
/** */
START_TURN_PANEL,
/** */
HUMAN_HAND_PANEL,
/** */
COMPUTER_HAND_PANEL,
/** */
WIN_PANEL
} }
} }

View file

@ -17,6 +17,7 @@ import javax.swing.JProgressBar;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import jrummikub.control.turn.TurnMode;
import jrummikub.model.Position; import jrummikub.model.Position;
import jrummikub.model.Stone; import jrummikub.model.Stone;
import jrummikub.util.Event; import jrummikub.util.Event;
@ -268,8 +269,6 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
addComponentListener(rescaleListener); addComponentListener(rescaleListener);
hand.addComponentListener(rescaleListener); hand.addComponentListener(rescaleListener);
setEndTurnMode(true, true);
} }
private class LeftPanelResizeListener extends ComponentAdapter { private class LeftPanelResizeListener extends ComponentAdapter {
@ -363,16 +362,27 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
} }
@Override @Override
public void setEndTurnMode(boolean inspectOnly, boolean mayRedeal) { public void setEndTurnMode(TurnMode turnMode) {
if (!inspectOnly) {
endTurnButton.setText("Zug beenden"); switch (turnMode) {
} else if (!mayRedeal) { case MAY_REDEAL:
endTurnButton.setVisible(false);
keepStonesButton.setVisible(true);
redealButton.setVisible(true);
break;
case INSPECT_ONLY:
endTurnButton.setText("N\u00e4chster Spieler"); endTurnButton.setText("N\u00e4chster Spieler");
endTurnButton.setVisible(true);
keepStonesButton.setVisible(false);
redealButton.setVisible(false);
break;
case NORMAL_TURN:
endTurnButton.setText("Zug beenden");
endTurnButton.setVisible(true);
keepStonesButton.setVisible(false);
redealButton.setVisible(false);
break;
} }
boolean smallButtons = mayRedeal && inspectOnly;
endTurnButton.setVisible(!smallButtons);
keepStonesButton.setVisible(smallButtons);
redealButton.setVisible(smallButtons);
} }
void showButtons(boolean show) { void showButtons(boolean show) {
@ -397,7 +407,7 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
sortByGroupsButton.setEnabled(enable); sortByGroupsButton.setEnabled(enable);
sortByRunsButton.setEnabled(enable); sortByRunsButton.setEnabled(enable);
if (!enable) { if (!enable) {
setEndTurnMode(false, false); setEndTurnMode(TurnMode.NORMAL_TURN);
endTurnButton.setText("<html><center>Computer denkt nach"); endTurnButton.setText("<html><center>Computer denkt nach");
hand.setStones(Collections.<Pair<Stone, Position>> emptyList()); hand.setStones(Collections.<Pair<Stone, Position>> emptyList());
handRowDownButton.setForeground(Color.GRAY); handRowDownButton.setForeground(Color.GRAY);

View file

@ -10,6 +10,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import jrummikub.control.turn.TurnMode;
import jrummikub.model.GameSettings; import jrummikub.model.GameSettings;
import jrummikub.model.Hand; import jrummikub.model.Hand;
import jrummikub.model.IHand; import jrummikub.model.IHand;
@ -934,13 +935,12 @@ public class RoundControlTest {
hand.drop(new Stone(1, BLUE), new Position(0, 0)); hand.drop(new Stone(1, BLUE), new Position(0, 0));
testRoundState.players.get(0).hand = hand; testRoundState.players.get(0).hand = hand;
view.startTurnEvent.emit(); view.startTurnEvent.emit();
assertTrue(view.playerPanel.inspectOnly); assertEquals(view.playerPanel.turnMode, TurnMode.INSPECT_ONLY);
assertFalse(view.playerPanel.mayRedeal);
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
view.playerPanel.endTurnEvent.emit(); view.playerPanel.endTurnEvent.emit();
view.startTurnEvent.emit(); view.startTurnEvent.emit();
} }
assertFalse(view.playerPanel.inspectOnly); assertEquals(view.playerPanel.turnMode, TurnMode.NORMAL_TURN);
} }
/** */ /** */
@ -953,12 +953,11 @@ public class RoundControlTest {
} }
testRoundState.players.get(0).hand = hand; testRoundState.players.get(0).hand = hand;
view.startTurnEvent.emit(); view.startTurnEvent.emit();
assertTrue(view.playerPanel.inspectOnly); assertEquals(view.playerPanel.turnMode, TurnMode.MAY_REDEAL);
assertTrue(view.playerPanel.mayRedeal);
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
view.playerPanel.endTurnEvent.emit(); view.playerPanel.endTurnEvent.emit();
view.startTurnEvent.emit(); view.startTurnEvent.emit();
} }
assertFalse(view.playerPanel.inspectOnly); assertEquals(view.playerPanel.turnMode, TurnMode.NORMAL_TURN);
} }
} }

View file

@ -111,7 +111,7 @@ public class TurnControlTest {
mockPlayer.hand = mockHand; mockPlayer.hand = mockHand;
testControl = new HumanTurnControl(mockTimer); testControl = new HumanTurnControl(mockTimer);
testControl.setup(new GameSettings(), mockPlayer, mockTable, mockView, testControl.setup(new GameSettings(), mockPlayer, mockTable, mockView,
false, false); TurnMode.NORMAL_TURN);
} }
/** */ /** */
@ -136,7 +136,7 @@ public class TurnControlTest {
testControl = new HumanTurnControl(mockTimer); testControl = new HumanTurnControl(mockTimer);
testControl.setup(new GameSettings(), mockPlayer, mockTable, mockView, testControl.setup(new GameSettings(), mockPlayer, mockTable, mockView,
false, false); TurnMode.NORMAL_TURN);
testControl.startTurn(); testControl.startTurn();
int i = 0; int i = 0;
@ -629,8 +629,8 @@ public class TurnControlTest {
public void testAddLeft() { public void testAddLeft() {
AccessibleTable table = new AccessibleTable(); AccessibleTable table = new AccessibleTable();
HumanTurnControl turnControl = new HumanTurnControl(mockTimer); HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
turnControl.setup(new GameSettings(), mockPlayer, table, mockView, false, turnControl.setup(new GameSettings(), mockPlayer, table, mockView,
false); TurnMode.NORMAL_TURN);
turnControl.startTurn(); turnControl.startTurn();
Stone blueOne = new Stone(1, BLUE); Stone blueOne = new Stone(1, BLUE);
Stone redOne = new Stone(1, RED); Stone redOne = new Stone(1, RED);
@ -747,8 +747,8 @@ public class TurnControlTest {
public void testAddRight() { public void testAddRight() {
AccessibleTable table = new AccessibleTable(); AccessibleTable table = new AccessibleTable();
HumanTurnControl turnControl = new HumanTurnControl(mockTimer); HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
turnControl.setup(new GameSettings(), mockPlayer, table, mockView, false, turnControl.setup(new GameSettings(), mockPlayer, table, mockView,
false); TurnMode.NORMAL_TURN);
turnControl.startTurn(); turnControl.startTurn();
Stone blueOne = new Stone(1, BLUE); Stone blueOne = new Stone(1, BLUE);
Stone redOne = new Stone(1, RED); Stone redOne = new Stone(1, RED);
@ -865,8 +865,8 @@ public class TurnControlTest {
public void testAddNewSet() { public void testAddNewSet() {
AccessibleTable table = new AccessibleTable(); AccessibleTable table = new AccessibleTable();
HumanTurnControl turnControl = new HumanTurnControl(mockTimer); HumanTurnControl turnControl = new HumanTurnControl(mockTimer);
turnControl.setup(new GameSettings(), mockPlayer, table, mockView, false, turnControl.setup(new GameSettings(), mockPlayer, table, mockView,
false); TurnMode.NORMAL_TURN);
turnControl.startTurn(); turnControl.startTurn();
Stone blueOne = new Stone(1, BLUE); Stone blueOne = new Stone(1, BLUE);
Stone redOne = new Stone(1, RED); Stone redOne = new Stone(1, RED);