Implement pause function

git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@390 72836036-5685-4462-b002-a69064685172
This commit is contained in:
Matthias Schiffer 2011-06-08 21:58:16 +02:00
parent 2e376414b9
commit 45d5b3ae10
40 changed files with 642 additions and 425 deletions

View file

@ -17,6 +17,8 @@ public class MockPlayerPanel implements IPlayerPanel {
/** */ /** */
public MockEvent sortByRunsEvent = new MockEvent(); public MockEvent sortByRunsEvent = new MockEvent();
/** */ /** */
public MockEvent pauseEvent = new MockEvent();
/** */
public TurnMode turnMode; public TurnMode turnMode;
@Override @Override
@ -49,5 +51,4 @@ public class MockPlayerPanel implements IPlayerPanel {
public void setEndTurnMode(TurnMode turnMode) { public void setEndTurnMode(TurnMode turnMode) {
this.turnMode = turnMode; this.turnMode = turnMode;
} }
} }

View file

@ -49,6 +49,10 @@ public class MockView implements IView {
public MockEvent1<File> loadEvent = new MockEvent1<File>(); public MockEvent1<File> loadEvent = new MockEvent1<File>();
/** */ /** */
public MockEvent1<File> saveEvent = new MockEvent1<File>(); public MockEvent1<File> saveEvent = new MockEvent1<File>();
/** */
public MockEvent pauseEvent = new MockEvent();
/** */
public MockEvent endPauseEvent = new MockEvent();
@Override @Override
public MockTablePanel getTablePanel() { public MockTablePanel getTablePanel() {
@ -157,4 +161,20 @@ public class MockView implements IView {
@Override @Override
public void clearView() { public void clearView() {
} }
@Override
public void enablePauseMode(boolean enable) {
// TODO Auto-generated method stub
}
@Override
public IEvent getPauseEvent() {
return pauseEvent;
}
@Override
public IEvent getEndPauseEvent() {
return endPauseEvent;
}
} }

View file

@ -22,7 +22,7 @@ public class ApplicationControl {
* Creates a new application control * Creates a new application control
* *
* @param view * @param view
* the view to use * the view to use
*/ */
public ApplicationControl(IView view) { public ApplicationControl(IView view) {
this.view = view; this.view = view;
@ -51,14 +51,13 @@ public class ApplicationControl {
new IListener3<GameSettings, GameState, IRoundState>() { new IListener3<GameSettings, GameState, IRoundState>() {
@Override @Override
public void handle(GameSettings settings, public void handle(GameSettings settings, GameState gameState,
GameState gameState, IRoundState roundState) { IRoundState roundState) {
settingsControl.abort(); settingsControl.abort();
if (gameControl != null){ if (gameControl != null) {
gameControl.abortGame(); gameControl.abortGame();
} }
gameControl = new GameControl(settings, gameControl = new GameControl(settings, saveControl, view);
saveControl, view);
addGameControlListeners(gameControl); addGameControlListeners(gameControl);
gameControl.continueGame(gameState, roundState); gameControl.continueGame(gameState, roundState);
@ -70,8 +69,7 @@ public class ApplicationControl {
public void handle(GameSettings settings) { public void handle(GameSettings settings) {
saveControl.setGameSettings(settings); saveControl.setGameSettings(settings);
gameControl = new GameControl(settings, gameControl = new GameControl(settings, saveControl, view);
saveControl, view);
addGameControlListeners(gameControl); addGameControlListeners(gameControl);
gameControl.startGame(); gameControl.startGame();

View file

@ -33,11 +33,11 @@ public class GameControl {
* Constructor * Constructor
* *
* @param gameSettings * @param gameSettings
* the game settings * the game settings
* @param saveControl * @param saveControl
* the save control * the save control
* @param view * @param view
* the view * the view
*/ */
public GameControl(GameSettings gameSettings, SaveControl saveControl, public GameControl(GameSettings gameSettings, SaveControl saveControl,
IView view) { IView view) {
@ -174,8 +174,7 @@ public class GameControl {
view.getScorePanel().setPlayers(gameSettings.getPlayerList()); view.getScorePanel().setPlayers(gameSettings.getPlayerList());
view.getScorePanel().setScores(gameState.getScores()); view.getScorePanel().setScores(gameState.getScores());
view.getScorePanel().setAccumulatedScore( view.getScorePanel().setAccumulatedScore(gameState.getAccumulatedScore());
gameState.getAccumulatedScore());
view.getScorePanel().update(); view.getScorePanel().update();
view.showScorePanel(true); view.showScorePanel(true);
} }

View file

@ -47,9 +47,9 @@ public class RoundControl {
* Create a new RoundControl using the given gameState and view * Create a new RoundControl using the given gameState and view
* *
* @param roundState * @param roundState
* initial round state * initial round state
* @param view * @param view
* view used for user interaction * view used for user interaction
*/ */
public RoundControl(IRoundState roundState, IView view) { public RoundControl(IRoundState roundState, IView view) {
this.roundState = roundState; this.roundState = roundState;
@ -102,12 +102,11 @@ public class RoundControl {
: BottomPanelType.COMPUTER_HAND_PANEL); : BottomPanelType.COMPUTER_HAND_PANEL);
view.getTablePanel().setStoneSets(clonedTable.clone()); view.getTablePanel().setStoneSets(clonedTable.clone());
view.setCurrentPlayerName(roundState.getActivePlayer() view.setCurrentPlayerName(roundState.getActivePlayer().getPlayerSettings()
.getPlayerSettings().getName()); .getName());
view.setCurrentPlayerColor(roundState.getActivePlayer() view.setCurrentPlayerColor(roundState.getActivePlayer().getPlayerSettings()
.getPlayerSettings().getColor()); .getColor());
view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer() view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer().getLaidOut());
.getLaidOut());
if (!isHuman) if (!isHuman)
startTurn(); startTurn();
@ -132,11 +131,11 @@ public class RoundControl {
view.getPlayerPanel().setEndTurnMode(turnMode); view.getPlayerPanel().setEndTurnMode(turnMode);
} }
turnControl = TurnControlFactory.getFactory( turnControl = TurnControlFactory.getFactory(
roundState.getActivePlayer().getPlayerSettings() roundState.getActivePlayer().getPlayerSettings().getTurnControlType())
.getTurnControlType()).create(); .create();
turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand, turnControl.setup(new ITurnControl.TurnInfo(clonedTable, clonedHand,
roundState.getActivePlayer().getLaidOut(), turnMode), roundState.getActivePlayer().getLaidOut(), turnMode), roundState
roundState.getGameSettings(), view); .getGameSettings(), view);
turnControl.getEndOfTurnEvent().add(new IListener() { turnControl.getEndOfTurnEvent().add(new IListener() {
@Override @Override
public void handle() { public void handle() {
@ -156,10 +155,8 @@ public class RoundControl {
void deal() { void deal() {
for (int i = 0; i < roundState.getPlayerCount(); i++) { for (int i = 0; i < roundState.getPlayerCount(); i++) {
IHand hand = roundState.getNthNextPlayer(i).getHand(); IHand hand = roundState.getNthNextPlayer(i).getHand();
for (int j = 0; j < roundState.getGameSettings() for (int j = 0; j < roundState.getGameSettings().getNumberOfStonesDealt(); j++) {
.getNumberOfStonesDealt(); j++) { hand.drop(roundState.getGameHeap().drawStone(), new Position(0, 0));
hand.drop(roundState.getGameHeap().drawStone(), new Position(0,
0));
} }
} }
} }
@ -170,13 +167,11 @@ public class RoundControl {
int totalValue = 0; int totalValue = 0;
for (StoneSet set : newSets) { for (StoneSet set : newSets) {
totalValue += set.classify(roundState.getGameSettings()) totalValue += set.classify(roundState.getGameSettings()).getSecond();
.getSecond();
} }
return totalValue == 0 return totalValue == 0
|| totalValue >= roundState.getGameSettings() || totalValue >= roundState.getGameSettings().getInitialMeldThreshold();
.getInitialMeldThreshold();
} }
private void endOfTurn() { private void endOfTurn() {
@ -216,8 +211,7 @@ public class RoundControl {
} }
if (!roundState.getActivePlayer().getLaidOut()) { if (!roundState.getActivePlayer().getLaidOut()) {
// Player touched forbidden stones // Player touched forbidden stones
if (!tableSetDifference(clonedTable, roundState.getTable()) if (!tableSetDifference(clonedTable, roundState.getTable()).isEmpty()) {
.isEmpty()) {
rejectMove(); rejectMove();
return; return;
} }
@ -226,8 +220,7 @@ public class RoundControl {
return; return;
} }
} }
Set<Stone> tableDiff = tableDifference(roundState.getTable(), Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
clonedTable);
roundState.setTable(clonedTable); roundState.setTable(clonedTable);
@ -243,8 +236,7 @@ public class RoundControl {
} }
private void rejectMove() { private void rejectMove() {
Set<Stone> tableDiff = tableDifference(roundState.getTable(), Set<Stone> tableDiff = tableDifference(roundState.getTable(), clonedTable);
clonedTable);
// deal penalty, reset // deal penalty, reset
roundState.getGameHeap().putBack(tableDiff); roundState.getGameHeap().putBack(tableDiff);
dealPenalty(tableDiff.size()); dealPenalty(tableDiff.size());
@ -338,12 +330,10 @@ public class RoundControl {
stonePoints = playerHand.isInitialMeldPossible(roundState stonePoints = playerHand.isInitialMeldPossible(roundState
.getGameSettings()) ? 200 : 100; .getGameSettings()) ? 200 : 100;
} else { } else {
stonePoints = playerHand.getStonePoints(roundState stonePoints = playerHand.getStonePoints(roundState.getGameSettings());
.getGameSettings());
} }
bestScore = updateBestScore(bestScore, -stonePoints, bestScore = updateBestScore(bestScore, -stonePoints, playerHand.getSize());
playerHand.getSize());
points.add(-stonePoints); points.add(-stonePoints);
pointSum += stonePoints; pointSum += stonePoints;

View file

@ -27,7 +27,7 @@ public class SaveControl {
* Creates a new SaveControl * Creates a new SaveControl
* *
* @param view * @param view
* the view to use * the view to use
*/ */
public SaveControl(IView view) { public SaveControl(IView view) {
view.getSaveEvent().add(new IListener1<File>() { view.getSaveEvent().add(new IListener1<File>() {
@ -53,7 +53,7 @@ public class SaveControl {
* Sets the current game settings * Sets the current game settings
* *
* @param gameSettings * @param gameSettings
* the game settings * the game settings
*/ */
public void setGameSettings(GameSettings gameSettings) { public void setGameSettings(GameSettings gameSettings) {
this.gameSettings = gameSettings; this.gameSettings = gameSettings;
@ -63,7 +63,7 @@ public class SaveControl {
* Sets the current game state * Sets the current game state
* *
* @param gameState * @param gameState
* the game state * the game state
*/ */
public void setGameState(GameState gameState) { public void setGameState(GameState gameState) {
this.gameState = gameState; this.gameState = gameState;
@ -73,7 +73,7 @@ public class SaveControl {
* Sets the current round state * Sets the current round state
* *
* @param roundState * @param roundState
* the round state * the round state
*/ */
public void setRoundState(IRoundState roundState) { public void setRoundState(IRoundState roundState) {
this.roundState = roundState; this.roundState = roundState;
@ -110,8 +110,8 @@ public class SaveControl {
return; return;
} }
try { try {
ObjectOutputStream stream = new ObjectOutputStream( ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(
new FileOutputStream(file)); file));
stream.writeObject(gameSettings); stream.writeObject(gameSettings);
stream.writeObject(gameState); stream.writeObject(gameState);

View file

@ -22,7 +22,7 @@ public class TurnTimer implements ActionListener, ITurnTimer {
* Create a new timer using a given view to display the current time left * Create a new timer using a given view to display the current time left
* *
* @param view * @param view
* view to display * view to display
*/ */
public TurnTimer(IView view) { public TurnTimer(IView view) {
this.view = view; this.view = view;

View file

@ -16,7 +16,6 @@ import jrummikub.view.IView;
* Abstract base class for TurnControls * Abstract base class for TurnControls
*/ */
public abstract class AbstractTurnControl implements ITurnControl { public abstract class AbstractTurnControl implements ITurnControl {
protected Event endOfTurnEvent = new Event(); protected Event endOfTurnEvent = new Event();
protected Event redealEvent = new Event(); protected Event redealEvent = new Event();
protected TurnInfo turnInfo; protected TurnInfo turnInfo;
@ -24,8 +23,7 @@ public abstract class AbstractTurnControl implements ITurnControl {
protected IView view; protected IView view;
protected ITurnTimer timer; protected ITurnTimer timer;
protected List<Connection> connections = new ArrayList<Connection>(); protected List<Connection> connections = new ArrayList<Connection>();
@Override @Override
public IEvent getEndOfTurnEvent() { public IEvent getEndOfTurnEvent() {
return endOfTurnEvent; return endOfTurnEvent;
@ -35,7 +33,17 @@ public abstract class AbstractTurnControl implements ITurnControl {
public IEvent getRedealEvent() { public IEvent getRedealEvent() {
return redealEvent; return redealEvent;
} }
private void pauseTurn() {
timer.stopTimer();
view.enablePauseMode(true);
}
private void resumeTurn() {
timer.startTimer();
view.enablePauseMode(false);
}
protected abstract void timeOut(); protected abstract void timeOut();
@Override @Override
@ -52,15 +60,27 @@ public abstract class AbstractTurnControl implements ITurnControl {
timeOut(); timeOut();
} }
})); }));
connections.add(view.getPauseEvent().add(new IListener() {
@Override
public void handle() {
pauseTurn();
}
}));
connections.add(view.getEndPauseEvent().add(new IListener() {
@Override
public void handle() {
resumeTurn();
}
}));
} }
protected void cleanUp() { protected void cleanUp() {
timer.stopTimer(); timer.stopTimer();
for (Connection c : connections) { for (Connection c : connections) {
c.remove(); c.remove();
} }
} }
public void abortTurn() { public void abortTurn() {
cleanUp(); cleanUp();
} }

View file

@ -36,7 +36,7 @@ public class BaseAIControl extends AbstractTurnControl {
timer.startTimer(); timer.startTimer();
computeThread.start(); computeThread.start();
} }
protected void timeOut() { protected void timeOut() {
cleanUp(); cleanUp();
endOfTurnEvent.emit(); endOfTurnEvent.emit();
@ -49,15 +49,15 @@ public class BaseAIControl extends AbstractTurnControl {
private void compute() { private void compute() {
switch (turnInfo.getTurnMode()) { switch (turnInfo.getTurnMode()) {
case MAY_REDEAL: case MAY_REDEAL:
emitRedeal(); emitRedeal();
break; break;
case INSPECT_ONLY: case INSPECT_ONLY:
emitEndOfTurn(); emitEndOfTurn();
break; break;
case NORMAL_TURN: case NORMAL_TURN:
turn(); turn();
break; break;
} }
} }
@ -111,8 +111,10 @@ public class BaseAIControl extends AbstractTurnControl {
for (Stone stone : set) { for (Stone stone : set) {
handStones.add(pickUpMatchingStone(stone)); handStones.add(pickUpMatchingStone(stone));
} }
turnInfo.getTable().drop(new StoneSet(handStones), new Position( turnInfo.getTable().drop(
(float) Math.random() * 30 - 15, (float) Math.random() * 6 - 3)); new StoneSet(handStones),
new Position((float) Math.random() * 30 - 15,
(float) Math.random() * 6 - 3));
} }
emitEndOfTurn(); emitEndOfTurn();

View file

@ -48,7 +48,7 @@ public class HumanTurnControl extends AbstractTurnControl {
HumanTurnControl(ITurnTimer testTimer) { HumanTurnControl(ITurnTimer testTimer) {
this.timer = testTimer; this.timer = testTimer;
} }
protected void timeOut() { protected void timeOut() {
endOfTurn(false); endOfTurn(false);
} }
@ -97,13 +97,12 @@ public class HumanTurnControl extends AbstractTurnControl {
} }
})); }));
connections.add(view.getPlayerPanel().getRedealEvent() connections.add(view.getPlayerPanel().getRedealEvent().add(new IListener() {
.add(new IListener() { @Override
@Override public void handle() {
public void handle() { endOfTurn(true);
endOfTurn(true); }
} }));
}));
} }
private void addHandPanelHandlers() { private void addHandPanelHandlers() {
@ -304,8 +303,10 @@ public class HumanTurnControl extends AbstractTurnControl {
} }
pickUpSelectedStones(); pickUpSelectedStones();
turnInfo.getTable().drop(new StoneSet(selectedStones), new Position(position.getX() turnInfo.getTable().drop(
- selectedStones.size() * 0.5f, position.getY() - 0.5f)); new StoneSet(selectedStones),
new Position(position.getX() - selectedStones.size() * 0.5f, position
.getY() - 0.5f));
selectedStones.clear(); selectedStones.clear();
view.getTablePanel().setStoneSets(turnInfo.getTable()); view.getTablePanel().setStoneSets(turnInfo.getTable());
@ -421,15 +422,14 @@ public class HumanTurnControl extends AbstractTurnControl {
turnInfo.getTable().drop(joinedSet, newPos); turnInfo.getTable().drop(joinedSet, newPos);
} else { } else {
StoneSet joinedSet = new StoneSet(selectedStones).join(newSet); StoneSet joinedSet = new StoneSet(selectedStones).join(newSet);
turnInfo.getTable().drop(joinedSet, new Position(newPos.getX() turnInfo.getTable().drop(joinedSet,
- selectedStones.size(), newPos.getY())); new Position(newPos.getX() - selectedStones.size(), newPos.getY()));
} }
} else { } else {
turnInfo.getTable().drop( turnInfo.getTable().drop(
new StoneSet(selectedStones), new StoneSet(selectedStones),
new Position(pos.getX() new Position(pos.getX() + (set.getSize() - selectedStones.size())
+ (set.getSize() - selectedStones.size()) * 0.5f, * 0.5f, pos.getY()));
pos.getY()));
} }
selectedStones.clear(); selectedStones.clear();
@ -521,8 +521,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

@ -43,6 +43,11 @@ public interface ITurnControl {
*/ */
public void startTurn(); public void startTurn();
/**
* Abort the turn
*/
public void abortTurn();
/** /**
* The TurnInfo class encapsulates all information concerning the current turn * The TurnInfo class encapsulates all information concerning the current turn
*/ */
@ -108,9 +113,4 @@ public interface ITurnControl {
return turnMode; return turnMode;
} }
} }
/**
* Abort the turn
*/
public void abortTurn();
} }

View file

@ -32,10 +32,10 @@ public abstract class TurnControlFactory {
*/ */
static public TurnControlFactory getFactory(Type type) { static public TurnControlFactory getFactory(Type type) {
switch (type) { switch (type) {
case HUMAN: case HUMAN:
return HumanTurnControl.getFactory(); return HumanTurnControl.getFactory();
case COMPUTER: case COMPUTER:
return BaseAIControl.getFactory(); return BaseAIControl.getFactory();
} }
return null; return null;
} }

View file

@ -17,7 +17,8 @@ public interface IPlayer extends Serializable {
/** /**
* Set the current hand of the player * Set the current hand of the player
* *
* @param hand the new hand * @param hand
* the new hand
*/ */
public void setHand(IHand hand); public void setHand(IHand hand);
@ -39,7 +40,7 @@ public interface IPlayer extends Serializable {
* Set if the player laid out * Set if the player laid out
* *
* @param laidOut * @param laidOut
* the player laid out * the player laid out
* *
*/ */
void setLaidOut(boolean laidOut); void setLaidOut(boolean laidOut);

View file

@ -8,7 +8,7 @@ import jrummikub.util.Pair;
* Interface for the {@link StoneTray} model * Interface for the {@link StoneTray} model
* *
* @param <E> * @param <E>
* Objects held by the IStoneTray * Objects held by the IStoneTray
*/ */
public interface IStoneTray<E extends Sizeable> extends public interface IStoneTray<E extends Sizeable> extends
Iterable<Pair<E, Position>>, Cloneable, Serializable { Iterable<Pair<E, Position>>, Cloneable, Serializable {
@ -17,9 +17,9 @@ public interface IStoneTray<E extends Sizeable> extends
* Adds object to the tray * Adds object to the tray
* *
* @param object * @param object
* object to add to Hand * object to add to Hand
* @param position * @param position
* {@link Position} to put the object * {@link Position} to put the object
*/ */
public void drop(E object, Position position); public void drop(E object, Position position);
@ -27,7 +27,7 @@ public interface IStoneTray<E extends Sizeable> extends
* Returns the position of an object that is already on the tray * Returns the position of an object that is already on the tray
* *
* @param object * @param object
* object whose position is requested * object whose position is requested
* @return position of the object or null when the object is not on the tray * @return position of the object or null when the object is not on the tray
*/ */
public Position getPosition(E object); public Position getPosition(E object);
@ -36,7 +36,7 @@ public interface IStoneTray<E extends Sizeable> extends
* Tries to pick up (remove) a given object * Tries to pick up (remove) a given object
* *
* @param object * @param object
* object to pick up * object to pick up
* @return true when the object was successfully removed * @return true when the object was successfully removed
*/ */
public boolean pickUp(E object); public boolean pickUp(E object);

View file

@ -14,7 +14,7 @@ import java.util.Random;
public class StoneHeap implements Serializable { public class StoneHeap implements Serializable {
private static final long serialVersionUID = -5247740086907775125L; private static final long serialVersionUID = -5247740086907775125L;
ArrayList<Stone> heap; ArrayList<Stone> heap;
private Random generator = new Random(); private Random generator = new Random();

View file

@ -14,7 +14,7 @@ import static jrummikub.model.StoneSet.Type.*;
/** Class managing {@link Stone}s joined together to form sets */ /** Class managing {@link Stone}s joined together to form sets */
public class StoneSet implements Iterable<Stone>, Sizeable, Serializable { public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
private static final long serialVersionUID = -3852631195648599398L; private static final long serialVersionUID = -3852631195648599398L;
static final float VERTICAL_BORDER = 0.5f; static final float VERTICAL_BORDER = 0.5f;
static final float HORIZONTAL_BORDER = 0.125f; static final float HORIZONTAL_BORDER = 0.125f;
private ArrayList<Stone> stones; private ArrayList<Stone> stones;
@ -23,7 +23,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
* Create a new single stone stone set * Create a new single stone stone set
* *
* @param stone * @param stone
* single stone of the set * single stone of the set
*/ */
public StoneSet(Stone stone) { public StoneSet(Stone stone) {
stones = new ArrayList<Stone>(Collections.singletonList(stone)); stones = new ArrayList<Stone>(Collections.singletonList(stone));
@ -33,7 +33,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
* Create a stone set from a list of stones * Create a stone set from a list of stones
* *
* @param stones * @param stones
* list of stones to build a set of * list of stones to build a set of
*/ */
public StoneSet(List<Stone> stones) { public StoneSet(List<Stone> stones) {
this.stones = new ArrayList<Stone>(stones); this.stones = new ArrayList<Stone>(stones);
@ -53,7 +53,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
* Test for rule conflict within the StoneSet * Test for rule conflict within the StoneSet
* *
* @param settings * @param settings
* GameSettings * GameSettings
* *
* @return true when the set is valid according to the rules * @return true when the set is valid according to the rules
*/ */
@ -62,11 +62,11 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
} }
/** /**
* Test for rule conflict within the StoneSet and determine whether the set * Test for rule conflict within the StoneSet and determine whether the set is
* is a group or a run * a group or a run
* *
* @param settings * @param settings
* GameSettings * GameSettings
* *
* @return GROUP or RUN for valid sets, INVALID otherwise * @return GROUP or RUN for valid sets, INVALID otherwise
*/ */
@ -87,14 +87,10 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
if (stones.size() > settings.getHighestValue()) { if (stones.size() > settings.getHighestValue()) {
return new Pair<Type, Integer>(INVALID, 0); return new Pair<Type, Integer>(INVALID, 0);
} else if (stones.size() > settings.getStoneColors().size()) { } else if (stones.size() > settings.getStoneColors().size()) {
return new Pair<Type, Integer>( return new Pair<Type, Integer>(RUN,
RUN, (settings.getHighestValue() * (settings.getHighestValue() + 1)) / 2
(settings.getHighestValue() * (settings
.getHighestValue() + 1))
/ 2
- (stones.size() - settings.getHighestValue()) - (stones.size() - settings.getHighestValue())
* (stones.size() - settings.getHighestValue() - 1) * (stones.size() - settings.getHighestValue() - 1) / 2);
/ 2);
} else { } else {
return new Pair<Type, Integer>(GROUP, stones.size() return new Pair<Type, Integer>(GROUP, stones.size()
* settings.getHighestValue()); * settings.getHighestValue());
@ -117,7 +113,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
* Test for rule conflict within the StoneSet, assuming we have a run * Test for rule conflict within the StoneSet, assuming we have a run
* *
* @param referencePosition * @param referencePosition
* position of stone used as reference (any non-joker stone) * position of stone used as reference (any non-joker stone)
* @param settings * @param settings
*/ */
private int isValidRun(int referencePosition, GameSettings settings) { private int isValidRun(int referencePosition, GameSettings settings) {
@ -177,7 +173,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
* Stone Sets * Stone Sets
* *
* @param position * @param position
* Splitting {@link Position} * Splitting {@link Position}
* @return A pair of StoneSets, one for each split part * @return A pair of StoneSets, one for each split part
*/ */
public Pair<StoneSet, StoneSet> splitAt(int position) { public Pair<StoneSet, StoneSet> splitAt(int position) {
@ -187,8 +183,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
return new Pair<StoneSet, StoneSet>(this, null); return new Pair<StoneSet, StoneSet>(this, null);
} }
StoneSet firstSet = new StoneSet(stones.subList(0, position)); StoneSet firstSet = new StoneSet(stones.subList(0, position));
StoneSet secondSet = new StoneSet(stones.subList(position, StoneSet secondSet = new StoneSet(stones.subList(position, stones.size()));
stones.size()));
return new Pair<StoneSet, StoneSet>(firstSet, secondSet); return new Pair<StoneSet, StoneSet>(firstSet, secondSet);
} }
@ -196,7 +191,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
* Joins StoneSet to another StoneSet and returns the resulting new StoneSet * Joins StoneSet to another StoneSet and returns the resulting new StoneSet
* *
* @param other * @param other
* StoneSet to be joined to active StoneSet * StoneSet to be joined to active StoneSet
* @return the combined StoneSet * @return the combined StoneSet
*/ */
public StoneSet join(StoneSet other) { public StoneSet join(StoneSet other) {
@ -219,7 +214,7 @@ public class StoneSet implements Iterable<Stone>, Sizeable, Serializable {
* Returns the i-th stone of the set (starting with 0) * Returns the i-th stone of the set (starting with 0)
* *
* @param i * @param i
* number of the stone to return * number of the stone to return
* @return the i-th stone * @return the i-th stone
*/ */
public Stone get(int i) { public Stone get(int i) {

View file

@ -11,11 +11,11 @@ import jrummikub.util.Pair;
* or {@link StoneSet}s. * or {@link StoneSet}s.
* *
* @param <E> * @param <E>
* Type of positioned objects (must implement Sizeable) * Type of positioned objects (must implement Sizeable)
*/ */
public class StoneTray<E extends Sizeable> implements IStoneTray<E> { public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
private static final long serialVersionUID = -6329309928640027222L; private static final long serialVersionUID = -6329309928640027222L;
protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>(); protected HashMap<E, Pair<E, Position>> objects = new HashMap<E, Pair<E, Position>>();
/** Possible move directions in case of overlapping Stones/Sets */ /** Possible move directions in case of overlapping Stones/Sets */
@ -52,8 +52,7 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
if (currentObject == object) if (currentObject == object)
continue; continue;
Position currentPosition = getPosition(currentObject); Position currentPosition = getPosition(currentObject);
if (!objectsOverlap(object, position, currentObject, if (!objectsOverlap(object, position, currentObject, currentPosition)) {
currentPosition)) {
continue; continue;
} }
// Object would be placed inside the current object // Object would be placed inside the current object
@ -63,22 +62,23 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
Position newPosition = null; Position newPosition = null;
// Move object to avoid overlap // Move object to avoid overlap
switch (newDirection) { switch (newDirection) {
case TOP: case TOP:
newPosition = new Position(currentPosition.getX(), newPosition = new Position(currentPosition.getX(), position.getY()
position.getY() - currentObject.getHeight()); - currentObject.getHeight());
break; break;
case BOTTOM: case BOTTOM:
newPosition = new Position(currentPosition.getX(), newPosition = new Position(currentPosition.getX(), position.getY()
position.getY() + object.getHeight()); + object.getHeight());
break; break;
case LEFT: case LEFT:
newPosition = new Position(position.getX() newPosition = new Position(
- currentObject.getWidth(), currentPosition.getY()); position.getX() - currentObject.getWidth(),
break; currentPosition.getY());
case RIGHT: break;
newPosition = new Position(position.getX() + object.getWidth(), case RIGHT:
currentPosition.getY()); newPosition = new Position(position.getX() + object.getWidth(),
break; currentPosition.getY());
break;
} }
objects.remove(currentObject); objects.remove(currentObject);
@ -87,14 +87,14 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
} }
/** /**
* Checks whether the object may be placed on the given position, computes * Checks whether the object may be placed on the given position, computes new
* new position if not * position if not
* *
* @param object * @param object
* to be dropped * to be dropped
* @param dir * @param dir
* @param pos * @param pos
* the object is dropped at * the object is dropped at
* @return null if the drop is valid, new position otherwise * @return null if the drop is valid, new position otherwise
*/ */
protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos, protected Pair<Position, Direction> fixInvalidDrop(E object, Position pos,
@ -122,15 +122,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) { if (lessOrEqual(position1.getX() + object1.getWidth(), position2.getX())) {
return false; return false;
} }
if (lessOrEqual(position1.getY() + object1.getHeight(), if (lessOrEqual(position1.getY() + object1.getHeight(), position2.getY())) {
position2.getY())) {
return false; return false;
} }
if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) { if (lessOrEqual(position2.getX() + object2.getWidth(), position1.getX())) {
return false; return false;
} }
if (lessOrEqual(position2.getY() + object2.getHeight(), if (lessOrEqual(position2.getY() + object2.getHeight(), position1.getY())) {
position1.getY())) {
return false; return false;
} }
return true; return true;
@ -171,15 +169,13 @@ public class StoneTray<E extends Sizeable> implements IStoneTray<E> {
double blockingRight = blocking.getSecond().getX() double blockingRight = blocking.getSecond().getX()
+ blocking.getFirst().getWidth(); + blocking.getFirst().getWidth();
double overlapRight = Math.min(objectRight, blockingRight); double overlapRight = Math.min(objectRight, blockingRight);
double overlapLeft = Math.max(position.getX(), blocking.getSecond() double overlapLeft = Math.max(position.getX(), blocking.getSecond().getX());
.getX());
double overlapX = overlapRight - overlapLeft; double overlapX = overlapRight - overlapLeft;
double objectBottom = position.getY() + object.getHeight(); double objectBottom = position.getY() + object.getHeight();
double blockingBottom = blocking.getSecond().getY() double blockingBottom = blocking.getSecond().getY()
+ blocking.getFirst().getHeight(); + blocking.getFirst().getHeight();
double overlapBottom = Math.min(objectBottom, blockingBottom); double overlapBottom = Math.min(objectBottom, blockingBottom);
double overlapTop = Math.max(position.getY(), blocking.getSecond() double overlapTop = Math.max(position.getY(), blocking.getSecond().getY());
.getY());
double overlapY = overlapBottom - overlapTop; double overlapY = overlapBottom - overlapTop;
// vertical or horizontal Shift // vertical or horizontal Shift
// TODO magic factor // TODO magic factor

View file

@ -6,9 +6,9 @@ import java.util.HashSet;
* Simple single parameter event generator * Simple single parameter event generator
* *
* @param <T1> * @param <T1>
* type of the first event parameter * type of the first event parameter
* @param <T2> * @param <T2>
* type of the second event parameter * type of the second event parameter
*/ */
public class Event3<T1, T2, T3> implements IEvent3<T1, T2, T3> { public class Event3<T1, T2, T3> implements IEvent3<T1, T2, T3> {
private HashSet<IListener3<T1, T2, T3>> listeners = new HashSet<IListener3<T1, T2, T3>>(); private HashSet<IListener3<T1, T2, T3>> listeners = new HashSet<IListener3<T1, T2, T3>>();
@ -34,9 +34,9 @@ public class Event3<T1, T2, T3> implements IEvent3<T1, T2, T3> {
* Generate a single event * Generate a single event
* *
* @param value1 * @param value1
* the first event parameter * the first event parameter
* @param value2 * @param value2
* the second event parameter * the second event parameter
*/ */
public void emit(T1 value1, T2 value2, T3 value3) { public void emit(T1 value1, T2 value2, T3 value3) {
for (IListener3<T1, T2, T3> listener : listeners) { for (IListener3<T1, T2, T3> listener : listeners) {

View file

@ -6,7 +6,7 @@ public interface IEvent {
* Start to publish all events to a given listener * Start to publish all events to a given listener
* *
* @param listener * @param listener
* target listener * target listener
* @return a connection to remove the listener * @return a connection to remove the listener
*/ */
public Connection add(IListener listener); public Connection add(IListener listener);
@ -15,7 +15,7 @@ public interface IEvent {
* Stop publishing events to a given listener * Stop publishing events to a given listener
* *
* @param listener * @param listener
* target listener * target listener
*/ */
public void remove(IListener listener); public void remove(IListener listener);
} }

View file

@ -4,14 +4,14 @@ package jrummikub.util;
* Interface for classes that can generate events having a single parameter * Interface for classes that can generate events having a single parameter
* *
* @param <T> * @param <T>
* type of the event parameter * type of the event parameter
*/ */
public interface IEvent1<T> { public interface IEvent1<T> {
/** /**
* Start to publish all events to a given listener * Start to publish all events to a given listener
* *
* @param listener * @param listener
* target listener * target listener
* @return a connection to remove the listener * @return a connection to remove the listener
*/ */
public Connection add(IListener1<T> listener); public Connection add(IListener1<T> listener);
@ -20,7 +20,7 @@ public interface IEvent1<T> {
* Stop publishing events to a given listener * Stop publishing events to a given listener
* *
* @param listener * @param listener
* target listener * target listener
*/ */
public void remove(IListener1<T> listener); public void remove(IListener1<T> listener);
} }

View file

@ -4,16 +4,16 @@ package jrummikub.util;
* Interface for classes that can generate events having a two parameters * Interface for classes that can generate events having a two parameters
* *
* @param <T1> * @param <T1>
* type of the first event parameter * type of the first event parameter
* @param <T2> * @param <T2>
* type of the second event parameter * type of the second event parameter
*/ */
public interface IEvent3<T1, T2, T3> { public interface IEvent3<T1, T2, T3> {
/** /**
* Start to publish all events to a given listener * Start to publish all events to a given listener
* *
* @param listener * @param listener
* target listener * target listener
* @return a connection to remove the listener * @return a connection to remove the listener
*/ */
public Connection add(IListener3<T1, T2, T3> listener); public Connection add(IListener3<T1, T2, T3> listener);
@ -22,7 +22,7 @@ public interface IEvent3<T1, T2, T3> {
* Stop publishing events to a given listener * Stop publishing events to a given listener
* *
* @param listener * @param listener
* target listener * target listener
*/ */
public void remove(IListener3<T1, T2, T3> listener); public void remove(IListener3<T1, T2, T3> listener);
} }

View file

@ -4,6 +4,6 @@ package jrummikub.util;
* Interface for classes that can receive parameterless events * Interface for classes that can receive parameterless events
*/ */
public interface IListener { public interface IListener {
/** This method is called whenever a class we're listening to emits an event */ /** This method is called whenever a class we're listening to emits an event */
public void handle(); public void handle();
} }

View file

@ -5,14 +5,14 @@ package jrummikub.util;
* parameter * parameter
* *
* @param <T> * @param <T>
* type of the event parameter * type of the event parameter
*/ */
public interface IListener1<T> { public interface IListener1<T> {
/** /**
* This method is called whenever a class we're listening to emits an event * This method is called whenever a class we're listening to emits an event
* *
* @param value * @param value
* the event parameter * the event parameter
*/ */
public void handle(T value); public void handle(T value);
} }

View file

@ -5,18 +5,18 @@ package jrummikub.util;
* parameters * parameters
* *
* @param <T1> * @param <T1>
* type of the first event parameter * type of the first event parameter
* @param <T2> * @param <T2>
* type of the first event parameter * type of the first event parameter
*/ */
public interface IListener2<T1, T2> { public interface IListener2<T1, T2> {
/** /**
* This method is called whenever a class we're listening to emits an event * This method is called whenever a class we're listening to emits an event
* *
* @param value1 * @param value1
* the first event parameter * the first event parameter
* @param value2 * @param value2
* the second event parameter * the second event parameter
*/ */
public void handle(T1 value1, T2 value2); public void handle(T1 value1, T2 value2);
} }

View file

@ -5,18 +5,18 @@ package jrummikub.util;
* parameters * parameters
* *
* @param <T1> * @param <T1>
* type of the first event parameter * type of the first event parameter
* @param <T2> * @param <T2>
* type of the first event parameter * type of the first event parameter
*/ */
public interface IListener3<T1, T2, T3> { public interface IListener3<T1, T2, T3> {
/** /**
* This method is called whenever a class we're listening to emits an event * This method is called whenever a class we're listening to emits an event
* *
* @param value1 * @param value1
* the first event parameter * the first event parameter
* @param value2 * @param value2
* the second event parameter * the second event parameter
*/ */
public void handle(T1 value1, T2 value2, T3 value3); public void handle(T1 value1, T2 value2, T3 value3);
} }

View file

@ -7,12 +7,12 @@ import jrummikub.util.IEvent1;
* An interface for view elements that can emit click events * An interface for view elements that can emit click events
*/ */
public interface IClickable { public interface IClickable {
/** /**
* the click event is emitted when the player clicks on the table/hand/etc. * the click event is emitted when the player clicks on the table/hand/etc.
* *
* @return the event; the first parameter is the position of the click in grid * @return the event; the first parameter is the position of the click in grid
* coordinates, the second is true when the player wants to add stones * coordinates, the second is true when the player wants to add stones
* to his selection instead of replacing them * to his selection instead of replacing them
*/ */
public IEvent1<Position> getClickEvent(); public IEvent1<Position> getClickEvent();
} }

View file

@ -12,7 +12,7 @@ public interface IHandPanel extends IStonePanel, IClickable {
* Set the player's stones to display on the board * Set the player's stones to display on the board
* *
* @param stones * @param stones
* the stones * the stones
*/ */
public void setStones(Iterable<Pair<Stone, Position>> stones); public void setStones(Iterable<Pair<Stone, Position>> stones);

View file

@ -11,7 +11,7 @@ public interface IScorePanel {
* Sets the scores of the played rounds * Sets the scores of the played rounds
* *
* @param scores * @param scores
* the round scores * the round scores
*/ */
public void setScores(Iterable<Score> scores); public void setScores(Iterable<Score> scores);
@ -19,7 +19,7 @@ public interface IScorePanel {
* Sets the accumulated scores to display * Sets the accumulated scores to display
* *
* @param accumulatedScore * @param accumulatedScore
* the accumulated score * the accumulated score
*/ */
public void setAccumulatedScore(Score accumulatedScore); public void setAccumulatedScore(Score accumulatedScore);
@ -27,7 +27,7 @@ public interface IScorePanel {
* Sets the player list to display * Sets the player list to display
* *
* @param players * @param players
* the player list * the player list
*/ */
void setPlayers(Iterable<PlayerSettings> players); void setPlayers(Iterable<PlayerSettings> players);

View file

@ -36,8 +36,8 @@ public interface ISettingsPanel {
}; };
/** /**
* The add player event is emitted when the user wants to add a player to * The add player event is emitted when the user wants to add a player to the
* the player list * player list
* *
* @return the event * @return the event
*/ */
@ -84,8 +84,8 @@ public interface ISettingsPanel {
public IEvent1<Integer> getChangeInitialMeldThresholdEvent(); public IEvent1<Integer> getChangeInitialMeldThresholdEvent();
/** /**
* The change StoneSet number event is emitted when the user wants to use * The change StoneSet number event is emitted when the user wants to use more
* more or less than 2 StoneSets per color * or less than 2 StoneSets per color
* *
* @return number of SoneSets * @return number of SoneSets
*/ */
@ -126,7 +126,7 @@ public interface ISettingsPanel {
* Sets an error to display * Sets an error to display
* *
* @param error * @param error
* the kind of error * the kind of error
*/ */
public void setError(SettingsError error); public void setError(SettingsError error);
@ -134,7 +134,7 @@ public interface ISettingsPanel {
* Enables or disables the start game button * Enables or disables the start game button
* *
* @param enable * @param enable
* specifies if the button is to be enabled or disabled * specifies if the button is to be enabled or disabled
*/ */
public void enableStartGameButton(boolean enable); public void enableStartGameButton(boolean enable);
@ -142,7 +142,7 @@ public interface ISettingsPanel {
* Enables or disables the add player button * Enables or disables the add player button
* *
* @param enable * @param enable
* specifies if the button is to be enabled or disabled * specifies if the button is to be enabled or disabled
*/ */
public void enableAddPlayerButton(boolean enable); public void enableAddPlayerButton(boolean enable);
@ -150,7 +150,7 @@ public interface ISettingsPanel {
* Enables or disables the remove player buttons * Enables or disables the remove player buttons
* *
* @param enable * @param enable
* specifies if the buttons are to be enabled or disabled * specifies if the buttons are to be enabled or disabled
*/ */
public void enableRemovePlayerButtons(boolean enable); public void enableRemovePlayerButtons(boolean enable);
@ -159,7 +159,7 @@ public interface ISettingsPanel {
* Sets the game settings to display * Sets the game settings to display
* *
* @param gameSettings * @param gameSettings
* the settings * the settings
*/ */
public void setGameSettings(GameSettings gameSettings); public void setGameSettings(GameSettings gameSettings);

View file

@ -49,7 +49,7 @@ public interface IView {
* Sets the current player's name * Sets the current player's name
* *
* @param playerName * @param playerName
* the player name * the player name
*/ */
public void setCurrentPlayerName(String playerName); public void setCurrentPlayerName(String playerName);
@ -57,7 +57,7 @@ public interface IView {
* Sets the stones that are to be painted selected * Sets the stones that are to be painted selected
* *
* @param stones * @param stones
* the stones to be painted selected * the stones to be painted selected
*/ */
public void setSelectedStones(Collection<Stone> stones); public void setSelectedStones(Collection<Stone> stones);
@ -86,7 +86,7 @@ public interface IView {
* Shows or hides the game settings panel * Shows or hides the game settings panel
* *
* @param show * @param show
* specifies if the panel shall be shown or hidden * specifies if the panel shall be shown or hidden
*/ */
public void showSettingsPanel(boolean show); public void showSettingsPanel(boolean show);
@ -94,7 +94,7 @@ public interface IView {
* Shows or hides the score panel * Shows or hides the score panel
* *
* @param show * @param show
* specifies if the panel shall be shown or hidden * specifies if the panel shall be shown or hidden
*/ */
public void showScorePanel(boolean show); public void showScorePanel(boolean show);
@ -103,16 +103,16 @@ public interface IView {
* along with the name * along with the name
* *
* @param color * @param color
* the current player's color * the current player's color
*/ */
public void setCurrentPlayerColor(Color color); public void setCurrentPlayerColor(Color color);
/** /**
* Is used for the PlayerPanel to display if a player has laid out along * Is used for the PlayerPanel to display if a player has laid out along with
* with the name * the name
* *
* @param hasLaidOut * @param hasLaidOut
* specifies if the current player has laid out or not * specifies if the current player has laid out or not
*/ */
public void setCurrentPlayerHasLaidOut(boolean hasLaidOut); public void setCurrentPlayerHasLaidOut(boolean hasLaidOut);
@ -127,29 +127,13 @@ public interface IView {
* Sets the bottom panels type * Sets the bottom panels type
* *
* @param type * @param type
* the type of the bottom panel * the type of the bottom panel
*/ */
public void setBottomPanel(BottomPanelType type); public void setBottomPanel(BottomPanelType type);
/** /**
* Different types of bottom panels * The menu new game event is emitted when the user selects the new game menu
*/ * entry
public enum BottomPanelType {
/** */
START_GAME_PANEL,
/** */
START_TURN_PANEL,
/** */
HUMAN_HAND_PANEL,
/** */
COMPUTER_HAND_PANEL,
/** */
WIN_PANEL
}
/**
* The menu new game event is emitted when the user selects the new game
* menu entry
* *
* @return the event * @return the event
*/ */
@ -178,5 +162,27 @@ public interface IView {
*/ */
public IEvent1<File> getSaveEvent(); public IEvent1<File> getSaveEvent();
public IEvent getPauseEvent();
public IEvent getEndPauseEvent();
public void clearView(); public void clearView();
void enablePauseMode(boolean enable);
/**
* Different types of bottom panels
*/
public enum BottomPanelType {
/** */
START_GAME_PANEL,
/** */
START_TURN_PANEL,
/** */
HUMAN_HAND_PANEL,
/** */
COMPUTER_HAND_PANEL,
/** */
WIN_PANEL
}
} }

View file

@ -37,8 +37,8 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
HandPanel.class.getResource("/jrummikub/resource/dark_wood.png")); HandPanel.class.getResource("/jrummikub/resource/dark_wood.png"));
BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(), BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
BufferedImage.TYPE_INT_RGB); BufferedImage.TYPE_INT_RGB);
DARK_BACKGROUND = new BufferedImage(darkImage.getIconWidth(), darkImage.getIconHeight(), DARK_BACKGROUND = new BufferedImage(darkImage.getIconWidth(),
BufferedImage.TYPE_INT_RGB); darkImage.getIconHeight(), BufferedImage.TYPE_INT_RGB);
image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0); image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
darkImage.paintIcon(null, DARK_BACKGROUND.createGraphics(), 0, 0); darkImage.paintIcon(null, DARK_BACKGROUND.createGraphics(), 0, 0);
@ -54,6 +54,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
private boolean repaintAll = true; private boolean repaintAll = true;
private Collection<Stone> selectedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
/** /**
* Creates a new Board instance * Creates a new Board instance
*/ */
@ -92,9 +93,9 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
int size = height / HEIGHT; int size = height / HEIGHT;
BufferedImage background = isEnabled() ? scaledBackground
BufferedImage background = isEnabled() ? scaledBackground : scaledDarkBackground ; : scaledDarkBackground;
if (repaintAll) { if (repaintAll) {
if (background.getHeight() != size) { if (background.getHeight() != size) {
if (!isEnabled()) { if (!isEnabled()) {
@ -104,7 +105,7 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
} }
background = isEnabled() ? scaledBackground : scaledDarkBackground; background = isEnabled() ? scaledBackground : scaledDarkBackground;
} }
for (int i = 0; i < HEIGHT; ++i) { for (int i = 0; i < HEIGHT; ++i) {
for (int xpos = -size * i / 3; xpos < width; xpos += size) { for (int xpos = -size * i / 3; xpos < width; xpos += size) {
g.drawImage(background, xpos, size * i, null); g.drawImage(background, xpos, size * i, null);

View file

@ -19,7 +19,21 @@ class ImageUtil {
g.fillRect(0, 0, size, size); g.fillRect(0, 0, size, size);
g.setColor(c); g.setColor(c);
g.fillRect(border, border, size - 2*border, size - 2*border); g.fillRect(border, border, size - 2 * border, size - 2 * border);
return new ImageIcon(image);
}
static ImageIcon createPauseIcon(int size) {
BufferedImage image = new BufferedImage(size, size,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.BLACK);
int barWidth = (int) (size * 0.425f);
g.fillRect(0, 0, barWidth, size);
g.fillRect(size - barWidth, 0, barWidth, size);
return new ImageIcon(image); return new ImageIcon(image);
} }

View file

@ -0,0 +1,95 @@
package jrummikub.view.impl;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import jrummikub.util.Event;
import jrummikub.util.IEvent;
/**
* A panel that is displayed before a player's turn
*/
@SuppressWarnings("serial")
class PausePanel extends JPanel {
private final static int PANEL_INSET = 15;
private final static int PANEL_SEPARATOR = 10;
private final static float PANEL_FIRST_LINE_HEIGHT = 0.375f;
private final static int PANEL_MAX_WIDTH = 180;
private final static float MAX_BUTTON_FONT_SIZE = 12;
private JLabel pauseLabel;
private JButton endPauseButton;
private Event endPauseEvent = new Event();
/**
* Creates a new StartTurnPanel
*/
PausePanel() {
setLayout(null);
setBorder(new EmptyBorder(PANEL_INSET, PANEL_INSET, PANEL_INSET,
PANEL_INSET));
pauseLabel = new JLabel();
pauseLabel.setHorizontalAlignment(JLabel.CENTER);
pauseLabel.setHorizontalTextPosition(JLabel.CENTER);
pauseLabel.setVerticalAlignment(JLabel.CENTER);
pauseLabel.setVerticalTextPosition(JLabel.CENTER);
add(pauseLabel);
endPauseButton = new JButton("Spiel fortsetzen");
endPauseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
endPauseEvent.emit();
}
});
add(endPauseButton);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
rescale();
}
});
}
void setCurrentPlayerName(String playerName) {
pauseLabel.setText("Der Zug von " + playerName + " ist pausiert.");
}
IEvent getEndPauseEvent() {
return endPauseEvent;
}
private void rescale() {
Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.bottom;
if (width > PANEL_MAX_WIDTH) {
x += (width - PANEL_MAX_WIDTH) / 4;
width = width / 2 + PANEL_MAX_WIDTH / 2;
}
int firstLineHeight = (int) ((height - PANEL_SEPARATOR) * PANEL_FIRST_LINE_HEIGHT);
int buttonWidth = width;
int buttonHeight = height - PANEL_SEPARATOR - firstLineHeight;
float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5;
if (fontSize > MAX_BUTTON_FONT_SIZE)
fontSize = MAX_BUTTON_FONT_SIZE;
pauseLabel.setBounds(x, y, width, firstLineHeight);
endPauseButton.setBounds(x, y + firstLineHeight + PANEL_SEPARATOR,
buttonWidth, buttonHeight);
endPauseButton.setFont(endPauseButton.getFont().deriveFont(fontSize));
}
}

View file

@ -53,11 +53,13 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
private JButton endTurnButton; private JButton endTurnButton;
private JButton keepStonesButton; private JButton keepStonesButton;
private JButton redealButton; private JButton redealButton;
private JButton pauseButton;
private Event sortByGroupsEvent = new Event(); private Event sortByGroupsEvent = new Event();
private Event sortByRunsEvent = new Event(); private Event sortByRunsEvent = new Event();
private Event endTurnEvent = new Event(); private Event endTurnEvent = new Event();
private Event redealEvent = new Event(); private Event redealEvent = new Event();
private Event pauseEvent = new Event();
HandPanel getHandPanel() { HandPanel getHandPanel() {
return hand; return hand;
@ -117,6 +119,10 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
return redealEvent; return redealEvent;
} }
IEvent getPauseEvent() {
return pauseEvent;
}
private void createLeftPanel() { private void createLeftPanel() {
leftPanel = new JPanel(); leftPanel = new JPanel();
leftPanel.setLayout(null); leftPanel.setLayout(null);
@ -206,6 +212,13 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
redealEvent.emit(); redealEvent.emit();
} }
}); });
pauseButton = createButton(rightPanel, null, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pauseEvent.emit();
}
});
} }
private JButton createButton(JPanel panel, String caption, private JButton createButton(JPanel panel, String caption,
@ -271,6 +284,67 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
hand.addComponentListener(rescaleListener); hand.addComponentListener(rescaleListener);
} }
@Override
public void setEndTurnMode(TurnMode turnMode) {
switch (turnMode) {
case MAY_REDEAL:
endTurnButton.setVisible(false);
keepStonesButton.setVisible(true);
redealButton.setVisible(true);
break;
case INSPECT_ONLY:
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;
}
}
void showButtons(boolean show) {
currentPlayerNameLabel.setVisible(show);
hasLaidOutLabel.setVisible(show);
sortByGroupsButton.setVisible(show);
sortByRunsButton.setVisible(show);
timeBar.setVisible(show);
pauseButton.setVisible(show);
if (!show) {
handRowDownButton.setForeground(Color.GRAY);
handRowDownButton.setEnabled(false);
handRowUpButton.setForeground(Color.GRAY);
handRowUpButton.setEnabled(false);
endTurnButton.setVisible(false);
redealButton.setVisible(false);
keepStonesButton.setVisible(false);
}
}
void enableButtons(boolean enable) {
sortByGroupsButton.setEnabled(enable);
sortByRunsButton.setEnabled(enable);
if (!enable) {
setEndTurnMode(TurnMode.NORMAL_TURN);
endTurnButton.setText("<html><center>Computer denkt nach");
hand.setStones(Collections.<Pair<Stone, Position>> emptyList());
handRowDownButton.setForeground(Color.GRAY);
handRowDownButton.setEnabled(false);
handRowUpButton.setForeground(Color.GRAY);
handRowUpButton.setEnabled(false);
}
endTurnButton.setEnabled(enable);
redealButton.setEnabled(enable);
keepStonesButton.setEnabled(enable);
hand.setEnabled(enable);
}
private class LeftPanelResizeListener extends ComponentAdapter { private class LeftPanelResizeListener extends ComponentAdapter {
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
@ -341,10 +415,16 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
fontSize * 1.5f)); fontSize * 1.5f));
handRowDownButton.setBounds(0, getHeight() / 2, handButtonWidth, handRowDownButton.setBounds(0, getHeight() / 2, handButtonWidth,
getHeight() / 2); getHeight() / 2);
handRowDownButton.setFont(handRowDownButton.getFont().deriveFont( handRowDownButton.setFont(handRowDownButton.getFont()
fontSize * 1.5f)); .deriveFont(fontSize));
timeBar.setBounds(x, y, width - firstLineHeight - SIDE_PANEL_SEPARATOR,
firstLineHeight);
pauseButton.setBounds(x + width - firstLineHeight, y, firstLineHeight,
firstLineHeight);
pauseButton.setIcon(ImageUtil
.createPauseIcon((int) (firstLineHeight * 0.5f)));
timeBar.setBounds(x, y, width, firstLineHeight);
endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR, endTurnButton.setBounds(x, y + firstLineHeight + SIDE_PANEL_SEPARATOR,
buttonWidth, buttonHeight); buttonWidth, buttonHeight);
endTurnButton.setFont(endTurnButton.getFont().deriveFont(fontSize)); endTurnButton.setFont(endTurnButton.getFont().deriveFont(fontSize));
@ -360,64 +440,4 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
} }
} }
@Override
public void setEndTurnMode(TurnMode turnMode) {
switch (turnMode) {
case MAY_REDEAL:
endTurnButton.setVisible(false);
keepStonesButton.setVisible(true);
redealButton.setVisible(true);
break;
case INSPECT_ONLY:
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;
}
}
void showButtons(boolean show) {
currentPlayerNameLabel.setVisible(show);
hasLaidOutLabel.setVisible(show);
sortByGroupsButton.setVisible(show);
sortByRunsButton.setVisible(show);
timeBar.setVisible(show);
if (!show) {
handRowDownButton.setForeground(Color.GRAY);
handRowDownButton.setEnabled(false);
handRowUpButton.setForeground(Color.GRAY);
handRowUpButton.setEnabled(false);
endTurnButton.setVisible(false);
redealButton.setVisible(false);
keepStonesButton.setVisible(false);
}
}
void enableButtons(boolean enable) {
sortByGroupsButton.setEnabled(enable);
sortByRunsButton.setEnabled(enable);
if (!enable) {
setEndTurnMode(TurnMode.NORMAL_TURN);
endTurnButton.setText("<html><center>Computer denkt nach");
hand.setStones(Collections.<Pair<Stone, Position>> emptyList());
handRowDownButton.setForeground(Color.GRAY);
handRowDownButton.setEnabled(false);
handRowUpButton.setForeground(Color.GRAY);
handRowUpButton.setEnabled(false);
}
endTurnButton.setEnabled(enable);
redealButton.setEnabled(enable);
keepStonesButton.setEnabled(enable);
hand.setEnabled(enable);
}
} }

View file

@ -62,22 +62,10 @@ class StartTurnPanel extends JPanel {
}); });
} }
/**
* Sets the current player name
*
* @param playerName
* the player name
*/
void setCurrentPlayerName(String playerName) { void setCurrentPlayerName(String playerName) {
startTurnLabel.setText(playerName + " ist jetzt an der Reihe."); startTurnLabel.setText(playerName + " ist jetzt an der Reihe.");
} }
/**
* The start turn event is emitted when the current player has clicked the
* start turn button
*
* @return the event
*/
IEvent getStartTurnEvent() { IEvent getStartTurnEvent() {
return startTurnEvent; return startTurnEvent;
} }

View file

@ -37,6 +37,8 @@ class StoneCollectionPanel extends AbstractStonePanel implements
private Event1<Point> otherClickEvent = new Event1<Point>(); private Event1<Point> otherClickEvent = new Event1<Point>();
private boolean pauseMode = false;
/** /**
* Creates a new StoneCollection instance * Creates a new StoneCollection instance
*/ */
@ -121,6 +123,10 @@ class StoneCollectionPanel extends AbstractStonePanel implements
} }
} }
if (pauseMode) {
return;
}
int inset = (int) (getHeight() * INSET_RATIO); int inset = (int) (getHeight() * INSET_RATIO);
int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2 int width = getStonePainter().getStoneWidth() * selectedStones.size() + 2
* inset, height = getHeight(); * inset, height = getHeight();
@ -148,4 +154,9 @@ class StoneCollectionPanel extends AbstractStonePanel implements
} }
} }
} }
void enablePauseMode(boolean enable) {
pauseMode = enable;
repaint();
}
} }

View file

@ -60,8 +60,7 @@ class StonePainter {
int g = (int) (color.getGreen() * BRIGHTER_SCALE); int g = (int) (color.getGreen() * BRIGHTER_SCALE);
int b = (int) (color.getBlue() * BRIGHTER_SCALE); int b = (int) (color.getBlue() * BRIGHTER_SCALE);
return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
: b);
} }
private static Color hover(Color color) { private static Color hover(Color color) {
@ -69,28 +68,27 @@ class StonePainter {
int g = (int) (color.getGreen() * HOVER_RATIO + 255 * (1 - HOVER_RATIO)); int g = (int) (color.getGreen() * HOVER_RATIO + 255 * (1 - HOVER_RATIO));
int b = (int) (color.getBlue() * HOVER_RATIO + 255 * (1 - HOVER_RATIO)); int b = (int) (color.getBlue() * HOVER_RATIO + 255 * (1 - HOVER_RATIO));
return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 return new Color(r > 255 ? 255 : r, g > 255 ? 255 : g, b > 255 ? 255 : b);
: b);
} }
public static Color getColor(StoneColor color) { public static Color getColor(StoneColor color) {
switch (color) { switch (color) {
case BLACK: case BLACK:
return new Color(0.0f, 0.0f, 0.0f); return new Color(0.0f, 0.0f, 0.0f);
case BLUE: case BLUE:
return new Color(0.0f, 0.0f, 1.0f); return new Color(0.0f, 0.0f, 1.0f);
case ORANGE: case ORANGE:
return new Color(1.0f, 0.4f, 0.0f); return new Color(1.0f, 0.4f, 0.0f);
case RED: case RED:
return new Color(0.9f, 0.0f, 0.25f); return new Color(0.9f, 0.0f, 0.25f);
case AQUA: case AQUA:
return new Color(0.0f, 0.85f, 0.75f); return new Color(0.0f, 0.85f, 0.75f);
case GREEN: case GREEN:
return new Color(0.0f, 0.65f, 0.0f); return new Color(0.0f, 0.65f, 0.0f);
case VIOLET: case VIOLET:
return new Color(0.75f, 0.325f, 0.75f); return new Color(0.75f, 0.325f, 0.75f);
case GRAY: case GRAY:
return new Color(0.5f, 0.5f, 0.5f); return new Color(0.5f, 0.5f, 0.5f);
} }
return null; return null;
@ -100,7 +98,7 @@ class StonePainter {
* Sets the new grid scale * Sets the new grid scale
* *
* @param scale * @param scale
* the new scale * the new scale
*/ */
public void setScale(double scale) { public void setScale(double scale) {
this.scale = scale; this.scale = scale;
@ -114,9 +112,9 @@ class StonePainter {
/** /**
* @param x * @param x
* x position in screen coordinates * x position in screen coordinates
* @param y * @param y
* y position in screen coordinates * y position in screen coordinates
* @return position in grid coordinates * @return position in grid coordinates
*/ */
public Position calculatePosition(int x, int y) { public Position calculatePosition(int x, int y) {
@ -211,21 +209,19 @@ class StonePainter {
defaultStones.put(color, new HashMap<Integer, BufferedImage>()); defaultStones.put(color, new HashMap<Integer, BufferedImage>());
selectedStones.put(color, new HashMap<Integer, BufferedImage>()); selectedStones.put(color, new HashMap<Integer, BufferedImage>());
hoveredStones.put(color, new HashMap<Integer, BufferedImage>()); hoveredStones.put(color, new HashMap<Integer, BufferedImage>());
hoveredSelectedStones.put(color, hoveredSelectedStones.put(color, new HashMap<Integer, BufferedImage>());
new HashMap<Integer, BufferedImage>());
} }
} }
/** /**
* @param scale * @param scale
* the scaling factor for the grid coordinates * the scaling factor for the grid coordinates
*/ */
StonePainter(double scale) { StonePainter(double scale) {
setScale(scale); setScale(scale);
} }
private void paintStoneBackground(Graphics2D g, Rectangle r, private void paintStoneBackground(Graphics2D g, Rectangle r, Color background) {
Color background) {
// Paint background // Paint background
g.setColor(background); g.setColor(background);
g.fillRect(r.x, r.y, r.width, r.height); g.fillRect(r.x, r.y, r.width, r.height);
@ -326,9 +322,8 @@ class StonePainter {
pos + (fm.getAscent() - fm.getDescent()) / 2 + 1); pos + (fm.getAscent() - fm.getDescent()) / 2 + 1);
} }
g.setColor(color); g.setColor(color);
g.drawString(value, g.drawString(value, (int) (r.x + r.width / 2 - stringRect.getWidth() / 2),
(int) (r.x + r.width / 2 - stringRect.getWidth() / 2), pos pos + (fm.getAscent() - fm.getDescent()) / 2);
+ (fm.getAscent() - fm.getDescent()) / 2);
} }
private void paintCircle(Graphics2D g, Rectangle r, Color background) { private void paintCircle(Graphics2D g, Rectangle r, Color background) {
@ -337,42 +332,41 @@ class StonePainter {
// Paint circle // Paint circle
g.setColor(background.darker()); g.setColor(background.darker());
g.drawArc(r.x + r.width / 2 - size / 2, pos - size / 2, size, size, 50, g.drawArc(r.x + r.width / 2 - size / 2, pos - size / 2, size, size, 50, 170);
170);
g.setColor(brighter(background)); g.setColor(brighter(background));
g.drawArc((int) (r.x + r.width / 2 - size / 2), pos - size / 2, size, g.drawArc((int) (r.x + r.width / 2 - size / 2), pos - size / 2, size, size,
size, -130, 170); -130, 170);
} }
/** /**
* Paints a stone * Paints a stone
* *
* @param g * @param g
* the graphics context to paint the stone on * the graphics context to paint the stone on
* @param stone * @param stone
* the stone to paint * the stone to paint
* @param p * @param p
* the position of the stone * the position of the stone
* @param selected * @param selected
* if selected is true the stone will be painted darker * if selected is true the stone will be painted darker
* @param hovered * @param hovered
* if hovered is true the stone will be painted brighter * if hovered is true the stone will be painted brighter
*/ */
public void paintStone(Graphics2D g, Stone stone, Position p, public void paintStone(Graphics2D g, Stone stone, Position p,
boolean selected, boolean hovered) { boolean selected, boolean hovered) {
int width = getStoneWidth(); int width = getStoneWidth();
int height = getStoneHeight(); int height = getStoneHeight();
int x = (int) Math.round(p.getX() * width), y = (int) Math.round(p int x = (int) Math.round(p.getX() * width), y = (int) Math.round(p.getY()
.getY() * height); * height);
if (stone.isJoker()) { if (stone.isJoker()) {
g.drawImage(getStoneImage(stone.getColor(), 0, selected, hovered), g.drawImage(getStoneImage(stone.getColor(), 0, selected, hovered), x, y,
x, y, null); null);
} else { } else {
g.drawImage( g.drawImage(
getStoneImage(stone.getColor(), stone.getValue(), selected, getStoneImage(stone.getColor(), stone.getValue(), selected, hovered),
hovered), x, y, null); x, y, null);
} }
} }
} }

View file

@ -11,6 +11,7 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.Area; import java.awt.geom.Area;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -20,6 +21,7 @@ import javax.swing.SwingUtilities;
import jrummikub.model.Position; import jrummikub.model.Position;
import jrummikub.model.Stone; import jrummikub.model.Stone;
import jrummikub.model.StoneColor;
import jrummikub.model.StoneSet; import jrummikub.model.StoneSet;
import jrummikub.util.Event1; import jrummikub.util.Event1;
import jrummikub.util.IListener1; import jrummikub.util.IListener1;
@ -39,8 +41,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
private final static ImageIcon BRIGHT_BACKGROUND = new ImageIcon( private final static ImageIcon BRIGHT_BACKGROUND = new ImageIcon(
HandPanel.class.getResource("/jrummikub/resource/bright_felt.png")); HandPanel.class.getResource("/jrummikub/resource/bright_felt.png"));
private final static double MIN_VISIBLE_WIDTH = 15; private final static double MIN_VISIBLE_WIDTH = 10;
private final static double MIN_VISIBLE_HEIGHT = 7.5f; private final static double MIN_VISIBLE_HEIGHT = 5;
private final static double HORIZONTAL_MARGIN = 1; private final static double HORIZONTAL_MARGIN = 1;
private final static double VERTICAL_MARGIN = 0.7f; private final static double VERTICAL_MARGIN = 0.7f;
private final static double CONNECTOR_WIDTH = 0.25f; private final static double CONNECTOR_WIDTH = 0.25f;
@ -49,8 +51,9 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
private StoneCollectionPanel stoneCollection; private StoneCollectionPanel stoneCollection;
private Iterable<Pair<StoneSet, Position>> stoneSets = Collections private Iterable<Pair<StoneSet, Position>> stoneSets = Collections.emptySet();
.emptySet(); private List<Pair<StoneSet, Position>> pauseStoneSets;
private Collection<Stone> selectedStones = Collections.emptyList(); private Collection<Stone> selectedStones = Collections.emptyList();
private Event1<StoneSet> leftConnectorClickEvent = new Event1<StoneSet>(); private Event1<StoneSet> leftConnectorClickEvent = new Event1<StoneSet>();
@ -59,6 +62,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
private StoneSet leftHoveredConnector; private StoneSet leftHoveredConnector;
private StoneSet rightHoveredConnector; private StoneSet rightHoveredConnector;
private boolean pauseMode = false;
@Override @Override
public Event1<StoneSet> getLeftConnectorClickEvent() { public Event1<StoneSet> getLeftConnectorClickEvent() {
return leftConnectorClickEvent; return leftConnectorClickEvent;
@ -99,7 +104,7 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
* Sets the currently selected stones * Sets the currently selected stones
* *
* @param stones * @param stones
* the selected stones * the selected stones
*/ */
void setSelectedStones(Collection<Stone> stones) { void setSelectedStones(Collection<Stone> stones) {
selectedStones = stones; selectedStones = stones;
@ -107,12 +112,35 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
repaint(); repaint();
} }
void createPauseStoneSets() {
pauseStoneSets = new ArrayList<Pair<StoneSet, Position>>();
Stone stoneCoffee1 = new Stone(-'\u2615', StoneColor.BLACK);
Stone stoneP = new Stone(-'P', StoneColor.BLACK);
Stone stonea = new Stone(-'a', StoneColor.ORANGE);
Stone stoneu = new Stone(-'u', StoneColor.BLUE);
Stone stones = new Stone(-'s', StoneColor.RED);
Stone stonee = new Stone(-'e', StoneColor.BLACK);
Stone stoneCoffee2 = new Stone(-'\u2615', StoneColor.RED);
pauseStoneSets.add(new Pair<StoneSet, Position>(new StoneSet(stoneCoffee1),
new Position(-4, 0)));
pauseStoneSets.add(new Pair<StoneSet, Position>(new StoneSet(Arrays.asList(
stoneP, stonea, stoneu, stones, stonee)), new Position(-2.5, 0)));
pauseStoneSets.add(new Pair<StoneSet, Position>(new StoneSet(stoneCoffee2),
new Position(3, 0)));
}
/** /**
* Creates a new Table instance * Creates a new Table instance
*/ */
TablePanel() { TablePanel() {
setLayout(null); setLayout(null);
createPauseStoneSets();
stoneCollection = new StoneCollectionPanel(); stoneCollection = new StoneCollectionPanel();
stoneCollection.getOtherClickEvent().add(new IListener1<Point>() { stoneCollection.getOtherClickEvent().add(new IListener1<Point>() {
@ -138,7 +166,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
double minx = -MIN_VISIBLE_WIDTH / 2, maxx = MIN_VISIBLE_WIDTH / 2; double minx = -MIN_VISIBLE_WIDTH / 2, maxx = MIN_VISIBLE_WIDTH / 2;
double miny = -MIN_VISIBLE_HEIGHT / 2, maxy = MIN_VISIBLE_HEIGHT / 2; double miny = -MIN_VISIBLE_HEIGHT / 2, maxy = MIN_VISIBLE_HEIGHT / 2;
for (Pair<StoneSet, Position> entry : stoneSets) { for (Pair<StoneSet, Position> entry : (pauseMode ? pauseStoneSets
: stoneSets)) {
Position p = entry.getSecond(); Position p = entry.getSecond();
StoneSet stoneSet = entry.getFirst(); StoneSet stoneSet = entry.getFirst();
@ -156,19 +185,19 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
return new Rectangle2D.Double(minx - HORIZONTAL_MARGIN, miny return new Rectangle2D.Double(minx - HORIZONTAL_MARGIN, miny
- VERTICAL_MARGIN, maxx - minx + 2 * HORIZONTAL_MARGIN, maxy - VERTICAL_MARGIN, maxx - minx + 2 * HORIZONTAL_MARGIN, maxy - miny + 2
- miny + 2 * VERTICAL_MARGIN); * VERTICAL_MARGIN);
} }
private void rescale() { private void rescale() {
Insets insets = getInsets(); Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth() - insets.left int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.right, height = getHeight() - insets.top - insets.bottom;
- insets.bottom;
int collectionHeight = (int) (height * COLLECTION_RATIO); int collectionHeight = (int) (height * COLLECTION_RATIO);
stoneCollection.setBounds(x, y + height - collectionHeight stoneCollection
- COLLECTION_GAP, width, collectionHeight); .setBounds(x, y + height - collectionHeight - COLLECTION_GAP, width,
collectionHeight);
setScale(); setScale();
@ -205,8 +234,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
// right connector // right connector
rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, CONNECTOR_WIDTH,
CONNECTOR_WIDTH, 1); 1);
if (rect.contains(pos.getX(), pos.getY())) { if (rect.contains(pos.getX(), pos.getY())) {
rightConnectorClickEvent.emit(stoneSet); rightConnectorClickEvent.emit(stoneSet);
return true; return true;
@ -237,8 +266,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
// right connector // right connector
rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, rect = new Rectangle2D.Double(x + stoneSet.getSize(), y, CONNECTOR_WIDTH,
CONNECTOR_WIDTH, 1); 1);
if (rect.contains(pos.getX(), pos.getY())) { if (rect.contains(pos.getX(), pos.getY())) {
rightHoveredConnector = stoneSet; rightHoveredConnector = stoneSet;
break; break;
@ -260,10 +289,10 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
.getStoneHeight(); .getStoneHeight();
Rectangle2D extent = calculateTableExtent(); Rectangle2D extent = calculateTableExtent();
return new Pair<Integer, Integer>( return new Pair<Integer, Integer>((int) (width / 2 - extent.getCenterX()
(int) (width / 2 - extent.getCenterX() * stoneWidth), * stoneWidth),
(int) ((height * (1 - COLLECTION_RATIO)) / 2 - extent (int) ((height * (1 - COLLECTION_RATIO)) / 2 - extent.getCenterY()
.getCenterY() * stoneHeight)); * stoneHeight));
} }
private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos, private void paintStoneSet(Graphics2D g, StoneSet stoneSet, Position pos,
@ -278,10 +307,9 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
// Left connector // Left connector
leftConnectorArea.add(new Area(new Rectangle2D.Double(Math.round(x leftConnectorArea.add(new Area(new Rectangle2D.Double(Math.round(x * width)
* width) - (int) width * CONNECTOR_WIDTH + 1, Math.round(pos.getY() * height),
- (int) width * CONNECTOR_WIDTH + 1, Math.round(pos.getY() (int) (width * CONNECTOR_WIDTH), height)));
* height), (int) (width * CONNECTOR_WIDTH), height)));
for (Stone stone : stoneSet) { for (Stone stone : stoneSet) {
getStonePainter().paintStone(g, stone, new Position(x, pos.getY()), getStonePainter().paintStone(g, stone, new Position(x, pos.getY()),
@ -290,8 +318,8 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
} }
// Right connector // Right connector
rightConnectorArea.add(new Area(new Rectangle2D.Double(Math.round(x rightConnectorArea.add(new Area(new Rectangle2D.Double(Math
* width), Math.round(pos.getY() * height), .round(x * width), Math.round(pos.getY() * height),
(int) (width * CONNECTOR_WIDTH), height))); (int) (width * CONNECTOR_WIDTH), height)));
} }
@ -314,17 +342,21 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
Area connectorArea = new Area(); Area connectorArea = new Area();
Area hoveredConnectorArea = new Area(); Area hoveredConnectorArea = new Area();
for (Pair<StoneSet, Position> entry : stoneSets) { for (Pair<StoneSet, Position> entry : (pauseMode ? pauseStoneSets
paintStoneSet(g, entry.getFirst(), entry.getSecond(), : stoneSets)) {
connectorArea, hoveredConnectorArea); paintStoneSet(g, entry.getFirst(), entry.getSecond(), connectorArea,
hoveredConnectorArea);
}
if (pauseMode) {
return;
} }
g.setClip(connectorArea); g.setClip(connectorArea);
g.setTransform(oldTransform); g.setTransform(oldTransform);
for (int x = 0; x < getWidth(); x += DARK_BACKGROUND.getIconWidth()) { for (int x = 0; x < getWidth(); x += DARK_BACKGROUND.getIconWidth()) {
for (int y = 0; y < getHeight(); y += DARK_BACKGROUND for (int y = 0; y < getHeight(); y += DARK_BACKGROUND.getIconHeight()) {
.getIconHeight()) {
DARK_BACKGROUND.paintIcon(this, g, x, y); DARK_BACKGROUND.paintIcon(this, g, x, y);
} }
} }
@ -340,12 +372,18 @@ class TablePanel extends AbstractStonePanel implements ITablePanel {
g.setTransform(oldTransform); g.setTransform(oldTransform);
for (int x = 0; x < getWidth(); x += BRIGHT_BACKGROUND.getIconWidth()) { for (int x = 0; x < getWidth(); x += BRIGHT_BACKGROUND.getIconWidth()) {
for (int y = 0; y < getHeight(); y += BRIGHT_BACKGROUND for (int y = 0; y < getHeight(); y += BRIGHT_BACKGROUND.getIconHeight()) {
.getIconHeight()) {
BRIGHT_BACKGROUND.paintIcon(this, g, x, y); BRIGHT_BACKGROUND.paintIcon(this, g, x, y);
} }
} }
g.setClip(oldClip); g.setClip(oldClip);
} }
void enablePauseMode(boolean enable) {
stoneCollection.enablePauseMode(enable);
pauseMode = enable;
repaint();
}
} }

View file

@ -53,10 +53,13 @@ public class View extends JFrame implements IView {
private TablePanel table; private TablePanel table;
private PlayerPanel playerPanel; private PlayerPanel playerPanel;
private StartTurnPanel startTurnPanel; private StartTurnPanel startTurnPanel;
private PausePanel pausePanel;
private WinPanel winPanel; private WinPanel winPanel;
private SettingsPanel settingsPanel; private SettingsPanel settingsPanel;
private ScorePanel scorePanel; private ScorePanel scorePanel;
private BottomPanelType bottomPanelType;
private JFileChooser chooser; private JFileChooser chooser;
private Event menuNewGameEvent = new Event(); private Event menuNewGameEvent = new Event();
@ -114,12 +117,21 @@ public class View extends JFrame implements IView {
return saveEvent; return saveEvent;
} }
@Override
public IEvent getPauseEvent() {
return playerPanel.getPauseEvent();
}
@Override
public IEvent getEndPauseEvent() {
return pausePanel.getEndPauseEvent();
}
@Override @Override
public void clearView() { public void clearView() {
showScorePanel(false); showScorePanel(false);
showSettingsPanel(false); showSettingsPanel(false);
getHandPanel().setStones( getHandPanel().setStones(Collections.<Pair<Stone, Position>> emptyList());
Collections.<Pair<Stone, Position>> emptyList());
getTablePanel().setStoneSets( getTablePanel().setStoneSets(
Collections.<Pair<StoneSet, Position>> emptyList()); Collections.<Pair<StoneSet, Position>> emptyList());
setSelectedStones(Collections.<Stone> emptyList()); setSelectedStones(Collections.<Stone> emptyList());
@ -219,14 +231,18 @@ public class View extends JFrame implements IView {
mainLayer.add(table); mainLayer.add(table);
playerPanel = new PlayerPanel(); playerPanel = new PlayerPanel();
playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0,
0, Color.BLACK)); Color.BLACK));
mainLayer.add(playerPanel); mainLayer.add(playerPanel);
startTurnPanel = new StartTurnPanel(); startTurnPanel = new StartTurnPanel();
startTurnPanel.setVisible(false); startTurnPanel.setVisible(false);
mainLayer.add(startTurnPanel); mainLayer.add(startTurnPanel);
pausePanel = new PausePanel();
pausePanel.setVisible(false);
mainLayer.add(pausePanel);
winPanel = new WinPanel(); winPanel = new WinPanel();
winPanel.setVisible(false); winPanel.setVisible(false);
mainLayer.add(winPanel); mainLayer.add(winPanel);
@ -251,6 +267,14 @@ public class View extends JFrame implements IView {
setVisible(true); setVisible(true);
} }
@Override
public void enablePauseMode(boolean enable) {
table.enablePauseMode(enable);
doSetBottomPanel(enable ? null : bottomPanelType);
pausePanel.setVisible(enable);
}
private void rescale() { private void rescale() {
int width = getContentPane().getWidth(), height = getContentPane() int width = getContentPane().getWidth(), height = getContentPane()
.getHeight(); .getHeight();
@ -270,6 +294,7 @@ public class View extends JFrame implements IView {
table.validate(); table.validate();
playerPanel.setBounds(0, tableHeight, width, playerPanelHeight); playerPanel.setBounds(0, tableHeight, width, playerPanelHeight);
startTurnPanel.setBounds(0, tableHeight, width, playerPanelHeight); startTurnPanel.setBounds(0, tableHeight, width, playerPanelHeight);
pausePanel.setBounds(0, tableHeight, width, playerPanelHeight);
winPanel.setBounds(0, tableHeight, width, playerPanelHeight); winPanel.setBounds(0, tableHeight, width, playerPanelHeight);
settingsPanel.setBounds(width / 4, height / 4, width / 2, height / 2); settingsPanel.setBounds(width / 4, height / 4, width / 2, height / 2);
scorePanel.setBounds(width / 8, height / 4, width * 3 / 4, height / 2); scorePanel.setBounds(width / 8, height / 4, width * 3 / 4, height / 2);
@ -295,6 +320,7 @@ public class View extends JFrame implements IView {
public void setCurrentPlayerName(String playerName) { public void setCurrentPlayerName(String playerName) {
playerPanel.setCurrentPlayerName(playerName); playerPanel.setCurrentPlayerName(playerName);
startTurnPanel.setCurrentPlayerName(playerName); startTurnPanel.setCurrentPlayerName(playerName);
pausePanel.setCurrentPlayerName(playerName);
} }
@Override @Override
@ -329,24 +355,24 @@ public class View extends JFrame implements IView {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private List<Pair<Stone, Position>> createDecorationStones() { private List<Pair<Stone, Position>> createDecorationStones() {
Pair<Stone, Position> stoneJ = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneJ = new Pair<Stone, Position>(new Stone(-'J',
-'J', StoneColor.BLACK), new Position(2.5f, 0)); StoneColor.BLACK), new Position(2.5f, 0));
Pair<Stone, Position> stoneR = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneR = new Pair<Stone, Position>(new Stone(-'R',
-'R', StoneColor.ORANGE), new Position(3.5f, 0)); StoneColor.ORANGE), new Position(3.5f, 0));
Pair<Stone, Position> stoneu1 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneu1 = new Pair<Stone, Position>(new Stone(-'u',
-'u', StoneColor.BLUE), new Position(4.5f, 0)); StoneColor.BLUE), new Position(4.5f, 0));
Pair<Stone, Position> stonem1 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stonem1 = new Pair<Stone, Position>(new Stone(-'m',
-'m', StoneColor.RED), new Position(5.5f, 0)); StoneColor.RED), new Position(5.5f, 0));
Pair<Stone, Position> stonem2 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stonem2 = new Pair<Stone, Position>(new Stone(-'m',
-'m', StoneColor.GREEN), new Position(6.5f, 0)); StoneColor.GREEN), new Position(6.5f, 0));
Pair<Stone, Position> stonei = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stonei = new Pair<Stone, Position>(new Stone(-'i',
-'i', StoneColor.VIOLET), new Position(7.5f, 0)); StoneColor.VIOLET), new Position(7.5f, 0));
Pair<Stone, Position> stonek = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stonek = new Pair<Stone, Position>(new Stone(-'k',
-'k', StoneColor.AQUA), new Position(8.5f, 0)); StoneColor.AQUA), new Position(8.5f, 0));
Pair<Stone, Position> stoneu2 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneu2 = new Pair<Stone, Position>(new Stone(-'u',
-'u', StoneColor.GRAY), new Position(9.5f, 0)); StoneColor.GRAY), new Position(9.5f, 0));
Pair<Stone, Position> stoneb = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stoneb = new Pair<Stone, Position>(new Stone(-'b',
-'b', StoneColor.BLACK), new Position(10.5f, 0)); StoneColor.BLACK), new Position(10.5f, 0));
Pair<Stone, Position> stone1 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stone1 = new Pair<Stone, Position>(new Stone(
StoneColor.RED), new Position(2, 1)); StoneColor.RED), new Position(2, 1));
@ -361,21 +387,26 @@ public class View extends JFrame implements IView {
Pair<Stone, Position> stone6 = new Pair<Stone, Position>(new Stone( Pair<Stone, Position> stone6 = new Pair<Stone, Position>(new Stone(
StoneColor.BLACK), new Position(11, 1)); StoneColor.BLACK), new Position(11, 1));
return Arrays.asList(stoneJ, stoneR, stoneu1, stonem1, stonem2, stonei, return Arrays
stonek, stoneu2, stoneb, stone1, stone2, stone3, stone4, .asList(stoneJ, stoneR, stoneu1, stonem1, stonem2, stonei, stonek,
stone5, stone6); stoneu2, stoneb, stone1, stone2, stone3, stone4, stone5, stone6);
} }
@Override @Override
public void setBottomPanel(BottomPanelType type) { public void setBottomPanel(BottomPanelType type) {
bottomPanelType = type;
doSetBottomPanel(type);
}
private void doSetBottomPanel(BottomPanelType type) {
startTurnPanel.setVisible(type == BottomPanelType.START_TURN_PANEL); startTurnPanel.setVisible(type == BottomPanelType.START_TURN_PANEL);
winPanel.setVisible(type == BottomPanelType.WIN_PANEL); winPanel.setVisible(type == BottomPanelType.WIN_PANEL);
playerPanel.setVisible(type != BottomPanelType.START_TURN_PANEL playerPanel.setVisible(type != BottomPanelType.START_TURN_PANEL
&& type != BottomPanelType.WIN_PANEL); && type != BottomPanelType.WIN_PANEL && type != null);
if (type == BottomPanelType.START_GAME_PANEL) { if (type == BottomPanelType.START_GAME_PANEL) {
table.setStoneSets(Collections table.setStoneSets(Collections.<Pair<StoneSet, Position>> emptyList());
.<Pair<StoneSet, Position>> emptyList());
playerPanel.getHandPanel().setStones(createDecorationStones()); playerPanel.getHandPanel().setStones(createDecorationStones());
} }

View file

@ -101,8 +101,7 @@ class WinPanel extends JPanel {
private void rescale() { private void rescale() {
Insets insets = getInsets(); Insets insets = getInsets();
int x = insets.left, y = insets.top, width = getWidth() - insets.left int x = insets.left, y = insets.top, width = getWidth() - insets.left
- insets.right, height = getHeight() - insets.top - insets.right, height = getHeight() - insets.top - insets.bottom;
- insets.bottom;
if (width > PANEL_MAX_WIDTH) { if (width > PANEL_MAX_WIDTH) {
x += (width - PANEL_MAX_WIDTH) / 4; x += (width - PANEL_MAX_WIDTH) / 4;
@ -118,14 +117,13 @@ class WinPanel extends JPanel {
newRoundButton.setBounds(x, y, buttonWidth, buttonHeight); newRoundButton.setBounds(x, y, buttonWidth, buttonHeight);
newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize)); newRoundButton.setFont(newRoundButton.getFont().deriveFont(fontSize));
newGameButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, y, newGameButton.setBounds(x + buttonWidth + PANEL_SEPARATOR, y, buttonWidth,
buttonWidth, buttonHeight); buttonHeight);
newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize)); newGameButton.setFont(newGameButton.getFont().deriveFont(fontSize));
endProgramButton.setBounds(x + 2 * (buttonWidth + PANEL_SEPARATOR), y, endProgramButton.setBounds(x + 2 * (buttonWidth + PANEL_SEPARATOR), y,
buttonWidth, buttonHeight); buttonWidth, buttonHeight);
endProgramButton.setFont(endProgramButton.getFont() endProgramButton.setFont(endProgramButton.getFont().deriveFont(fontSize));
.deriveFont(fontSize));
} }
} }