From d6c4da62243310b85a82e73e55249de9af7fdb11 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 22 Jun 2011 03:08:37 +0200 Subject: Show in start turn panel if a player has redealed or drawn the last stone git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@570 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/GameControl.java | 8 +- src/jrummikub/control/RoundControl.java | 80 ++++++++++------- .../control/network/NetworkRoundControl.java | 65 ++++++++------ .../control/turn/AbstractTurnControl.java | 4 +- src/jrummikub/model/GameState.java | 26 +++++- src/jrummikub/model/IRoundState.java | 2 +- src/jrummikub/model/RoundState.java | 8 +- src/jrummikub/view/IView.java | 8 ++ src/jrummikub/view/impl/StartTurnPanel.java | 99 +++++++++++++++++----- src/jrummikub/view/impl/View.java | 14 ++- 10 files changed, 218 insertions(+), 96 deletions(-) (limited to 'src') diff --git a/src/jrummikub/control/GameControl.java b/src/jrummikub/control/GameControl.java index 1f659f8..48c1b85 100644 --- a/src/jrummikub/control/GameControl.java +++ b/src/jrummikub/control/GameControl.java @@ -8,6 +8,7 @@ import jrummikub.model.GameSettings; import jrummikub.model.GameState; import jrummikub.model.IPlayer; import jrummikub.model.IRoundState; +import jrummikub.model.PlayerSettings; import jrummikub.model.RoundState; import jrummikub.model.Score; import jrummikub.util.Connection; @@ -183,14 +184,15 @@ public class GameControl { roundControl.getEndOfRoundEvent().add(new IListener1() { @Override public void handle(Score roundScore) { + gameState.setLastPlayerRedealed(null); endOfRound(roundScore); } }); - roundControl.getRestartRoundEvent().add(new IListener() { - + roundControl.getRestartRoundEvent().add(new IListener1() { @Override - public void handle() { + public void handle(PlayerSettings player) { + gameState.setLastPlayerRedealed(player); restartRound(); } }); diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java index 4227741..2bcbc7e 100644 --- a/src/jrummikub/control/RoundControl.java +++ b/src/jrummikub/control/RoundControl.java @@ -8,7 +8,6 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import jrummikub.control.turn.AIControl; import jrummikub.control.turn.ITurnControl; import jrummikub.control.turn.TurnControlFactory; import jrummikub.control.turn.TurnMode; @@ -16,15 +15,14 @@ import jrummikub.model.IHand; import jrummikub.model.IPlayer; import jrummikub.model.IRoundState; import jrummikub.model.ITable; +import jrummikub.model.PlayerSettings; import jrummikub.model.PlayerSettings.Type; import jrummikub.model.Position; import jrummikub.model.Score; import jrummikub.model.Stone; import jrummikub.model.StoneSet; import jrummikub.util.Connection; -import jrummikub.util.Event; import jrummikub.util.Event1; -import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.util.IListener; import jrummikub.util.IListener2; @@ -113,7 +111,7 @@ public class RoundControl { private ITurnControl turnControl; protected IRoundState roundState; private IView view; - private Event restartRoundEvent = new Event(); + private Event1 restartRoundEvent = new Event1(); private Event1 roundStateUpdateEvent = new Event1(); private Event1 endOfRoundEvent = new Event1(); protected List connections = new ArrayList(); @@ -180,7 +178,6 @@ public class RoundControl { * Continue a saved round after loading */ public void continueRound() { - connections.add(view.getStartTurnEvent().add(new IListener() { @Override public void handle() { @@ -222,6 +219,35 @@ public class RoundControl { roundStateUpdateEvent.emit(state); } + protected BottomPanelType showStartTurnPanel() { + boolean isHuman = roundState.getActivePlayer().getPlayerSettings() + .getType() == HUMAN; + boolean oneHuman = roundState.getGameSettings().oneHuman(); + + if (!isHuman) { + return null; + } + + if (roundState.getLastPlayer() != null) { + view.setLastStonePlayerName(roundState.getLastPlayer() + .getPlayerSettings().getName()); + return BottomPanelType.START_LAST_TURN_PANEL; + } + + if (roundState.getTurnNumber() < 1 + && roundState.getGameState().getLastPlayerRedealed() != null) { + view.setRedealedPlayerName(roundState.getGameState() + .getLastPlayerRedealed().getName()); + return BottomPanelType.START_REDEAL_TURN_PANEL; + } + + if (oneHuman) { + return null; + } + + return BottomPanelType.START_TURN_PANEL; + } + /** * Prepare a player's turn by checking the player types and setting the * correct turn control @@ -229,12 +255,7 @@ public class RoundControl { protected void prepareTurn() { doPrepareTurn(); - boolean isHuman = roundState.getActivePlayer().getPlayerSettings() - .getType() == HUMAN; - boolean oneHuman = roundState.getGameSettings().oneHuman(); - boolean isAI = (turnControl instanceof AIControl); - - if (isAI || (isHuman && oneHuman)) { + if (showStartTurnPanel() == null) { startTurn(); } } @@ -245,12 +266,9 @@ public class RoundControl { protected void doPrepareTurn() { updateSidePanel(); - boolean isHuman = roundState.getActivePlayer().getPlayerSettings() - .getType() == HUMAN; - boolean oneHuman = roundState.getGameSettings().oneHuman(); - - if (isHuman && !oneHuman) { - view.setBottomPanel(BottomPanelType.START_TURN_PANEL); + BottomPanelType startTurnPanel = showStartTurnPanel(); + if (startTurnPanel != null) { + view.setBottomPanel(startTurnPanel); } view.getTablePanel().setStoneSets(roundState.getTable().clone()); @@ -259,19 +277,13 @@ public class RoundControl { view.setCurrentPlayerColor(roundState.getActivePlayer().getPlayerSettings() .getColor()); view.setCurrentPlayerHasLaidOut(roundState.getActivePlayer().getLaidOut()); - - turnControl = createTurnControl(roundState.getActivePlayer() - .getPlayerSettings().getType()); } /** * Start a players turn with the correct turn control */ protected void startTurn() { - if (turnControl == null) { - doPrepareTurn(); - } - + doPrepareTurn(); boolean isHuman = roundState.getActivePlayer().getPlayerSettings() .getType() == HUMAN; @@ -294,6 +306,7 @@ public class RoundControl { view.getPlayerPanel().setEndTurnMode(turnMode); } + turnControl = createTurnControl(); turnControl.setup( new ITurnControl.TurnInfo(roundState, turnMode, mayPause), roundState.getGameSettings(), view); @@ -330,7 +343,7 @@ public class RoundControl { view.getSidePanel().setPlayers(players); view.getSidePanel().setHeapCapacity( roundState.getGameSettings().getTotalStones()); - view.getSidePanel().setHeapSize(roundState.getGameHeap().getSize()); + view.getSidePanel().setHeapSize(roundState.getStoneHeap().getSize()); } /** @@ -349,8 +362,9 @@ public class RoundControl { * of the new turn control * @return the new turn control */ - protected ITurnControl createTurnControl(Type type) { - return TurnControlFactory.getFactory(type).create(); + protected ITurnControl createTurnControl() { + return TurnControlFactory.getFactory( + roundState.getActivePlayer().getPlayerSettings().getType()).create(); } /** @@ -361,11 +375,11 @@ public class RoundControl { for (int i = 0; i < roundState.getPlayerCount(); i++) { IHand hand = roundState.getNthNextPlayer(i).getHand(); for (int j = 0; j < roundState.getGameSettings().getNumberOfStonesDealt(); j++) { - hand.drop(roundState.getGameHeap().drawStone(), new Position(0, 0)); + hand.drop(roundState.getStoneHeap().drawStone(), new Position(0, 0)); } } - view.getSidePanel().setHeapSize(roundState.getGameHeap().getSize()); + view.getSidePanel().setHeapSize(roundState.getStoneHeap().getSize()); } /** @@ -429,7 +443,7 @@ public class RoundControl { } if (roundState.getLastPlayer() == null) { - if (roundState.getGameHeap().getSize() == 0) { + if (roundState.getStoneHeap().getSize() == 0) { roundState.setLastPlayer(roundState.getActivePlayer()); roundState.nextPlayer(); roundState.nextTurn(); @@ -547,7 +561,7 @@ public class RoundControl { * * @return the event */ - public IEvent getRestartRoundEvent() { + public IEvent1 getRestartRoundEvent() { return restartRoundEvent; } @@ -556,10 +570,12 @@ public class RoundControl { * to do so */ protected void redeal() { + view.setBottomPanel(BottomPanelType.NONHUMAN_HAND_PANEL); + turnControl = null; for (Connection c : new ArrayList(connections)) { c.remove(); } - restartRoundEvent.emit(); + restartRoundEvent.emit(roundState.getActivePlayer().getPlayerSettings()); } } diff --git a/src/jrummikub/control/network/NetworkRoundControl.java b/src/jrummikub/control/network/NetworkRoundControl.java index af7f675..05a3424 100644 --- a/src/jrummikub/control/network/NetworkRoundControl.java +++ b/src/jrummikub/control/network/NetworkRoundControl.java @@ -4,7 +4,6 @@ import jrummikub.control.RoundControl; import jrummikub.control.turn.ITurnControl; import jrummikub.model.IRoundState; import jrummikub.model.ITable; -import jrummikub.model.PlayerSettings.Type; import jrummikub.util.IListener; import jrummikub.util.IListener1; import jrummikub.view.IView; @@ -14,6 +13,7 @@ import jrummikub.view.IView; */ public class NetworkRoundControl extends RoundControl { private IConnectionControl connectionControl; + private boolean wasActive; private boolean currentlyActive; /** @@ -33,6 +33,7 @@ public class NetworkRoundControl extends RoundControl { super(roundState, view, false); this.connectionControl = connectionControl; + wasActive = startActive; currentlyActive = startActive; connections.add(connectionControl.getRoundStateUpdateEvent().add( @@ -40,22 +41,21 @@ public class NetworkRoundControl extends RoundControl { @Override public void handle(IRoundState state) { setRoundState(state); + prepareTurn(); } })); - connections.add(connectionControl.getTurnStartEvent().add( - new IListener() { - @Override - public void handle() { - startTurn(); - } - })); - connections.add(connectionControl.getNextPlayerEvent().add( - new IListener() { - @Override - public void handle() { - NetworkRoundControl.super.nextPlayer(); - } - })); + connections.add(connectionControl.getTurnStartEvent().add(new IListener() { + @Override + public void handle() { + NetworkRoundControl.super.startTurn(); + } + })); + connections.add(connectionControl.getNextPlayerEvent().add(new IListener() { + @Override + public void handle() { + NetworkRoundControl.super.nextPlayer(); + } + })); } @Override @@ -69,30 +69,39 @@ public class NetworkRoundControl extends RoundControl { } @Override - protected ITurnControl createTurnControl(Type type) { - switch (type) { - case HUMAN: - currentlyActive = true; - break; - case NETWORK: - currentlyActive = false; - break; - } - + protected ITurnControl createTurnControl() { if (!currentlyActive) { return new NetworkTurnControl(connectionControl); } - return super.createTurnControl(type); + return super.createTurnControl(); } @Override protected void prepareTurn() { - if (currentlyActive) { - connectionControl.startTurn(); + switch (roundState.getActivePlayer().getPlayerSettings().getType()) { + case HUMAN: + currentlyActive = true; + break; + case NETWORK: + currentlyActive = false; + break; } doPrepareTurn(); + + if (showStartTurnPanel() == null) { + if (currentlyActive) { + connectionControl.startTurn(); + } + } + } + + @Override + protected void startTurn() { + if (currentlyActive) { + connectionControl.startTurn(); + } } @Override diff --git a/src/jrummikub/control/turn/AbstractTurnControl.java b/src/jrummikub/control/turn/AbstractTurnControl.java index 9bdd6e2..19a2be4 100644 --- a/src/jrummikub/control/turn/AbstractTurnControl.java +++ b/src/jrummikub/control/turn/AbstractTurnControl.java @@ -226,7 +226,7 @@ public abstract class AbstractTurnControl implements ITurnControl { Set tableDiff = tableDifference(turnInfo.getOldTable(), turnInfo.getTable()); // deal penalty, reset - turnInfo.getRoundState().getGameHeap().putBack(tableDiff); + turnInfo.getRoundState().getStoneHeap().putBack(tableDiff); turnInfo.getRoundState().getActivePlayer().setLastTurnInvalid(true); dealPenalty(tableDiff.size()); } @@ -240,7 +240,7 @@ public abstract class AbstractTurnControl implements ITurnControl { rowCount++; } - hand.drop(turnInfo.getRoundState().getGameHeap().drawStone(), + hand.drop(turnInfo.getRoundState().getStoneHeap().drawStone(), new Position(Hand.WIDTH - 1, rowCount - 1)); } diff --git a/src/jrummikub/model/GameState.java b/src/jrummikub/model/GameState.java index ee5c2d3..7cd38ab 100644 --- a/src/jrummikub/model/GameState.java +++ b/src/jrummikub/model/GameState.java @@ -13,6 +13,7 @@ public class GameState implements Serializable { private int firstRoundFirstPlayer; private ArrayList scores = new ArrayList(); + private PlayerSettings lastPlayerRedealed; /** * Gets the number of the first player of the first round @@ -33,6 +34,25 @@ public class GameState implements Serializable { this.firstRoundFirstPlayer = firstRoundFirstPlayer; } + /** + * Sets the player that has redealed last + * + * @param lastPlayerRedealed + * the player to set + */ + public void setLastPlayerRedealed(PlayerSettings lastPlayerRedealed) { + this.lastPlayerRedealed = lastPlayerRedealed; + } + + /** + * Returns the player that has redealed last + * + * @return the player + */ + public PlayerSettings getLastPlayerRedealed() { + return lastPlayerRedealed; + } + /** * Returns the list of players' scores in the rounds played before * @@ -45,8 +65,10 @@ public class GameState implements Serializable { /** * Returns whether players have won * - * @param points the player's points - * @param wins the number of wins per player + * @param points + * the player's points + * @param wins + * the number of wins per player * @return whether a player has won */ private Boolean[] getWinners(Integer[] points, int[] wins) { diff --git a/src/jrummikub/model/IRoundState.java b/src/jrummikub/model/IRoundState.java index 6d43f02..3eedc4c 100644 --- a/src/jrummikub/model/IRoundState.java +++ b/src/jrummikub/model/IRoundState.java @@ -51,7 +51,7 @@ public interface IRoundState extends Serializable { * * @return heap of stones */ - public StoneHeap getGameHeap(); + public StoneHeap getStoneHeap(); /** * Returns the player that would be the active player after i turns diff --git a/src/jrummikub/model/RoundState.java b/src/jrummikub/model/RoundState.java index 04a977d..f7a75d5 100644 --- a/src/jrummikub/model/RoundState.java +++ b/src/jrummikub/model/RoundState.java @@ -12,7 +12,7 @@ public class RoundState implements IRoundState { private ITable table; private ArrayList players; private int activePlayer; - private StoneHeap gameHeap; + private StoneHeap stoneHeap; private IPlayer lastPlayer; private int turnNumber; @@ -38,7 +38,7 @@ public class RoundState implements IRoundState { turnNumber = 1 - gameSettings.getPlayerList().size(); activePlayer = 0; - gameHeap = new StoneHeap(gameSettings); + stoneHeap = new StoneHeap(gameSettings); } @Override @@ -94,8 +94,8 @@ public class RoundState implements IRoundState { } @Override - public StoneHeap getGameHeap() { - return gameHeap; + public StoneHeap getStoneHeap() { + return stoneHeap; } @Override diff --git a/src/jrummikub/view/IView.java b/src/jrummikub/view/IView.java index 91a2e23..c3670cb 100644 --- a/src/jrummikub/view/IView.java +++ b/src/jrummikub/view/IView.java @@ -61,6 +61,10 @@ public interface IView { */ public void setCurrentPlayerName(String playerName); + public void setRedealedPlayerName(String name); + + public void setLastStonePlayerName(String name); + /** * Sets the stones that are to be painted selected * @@ -368,6 +372,10 @@ public interface IView { /** */ START_TURN_PANEL, /** */ + START_REDEAL_TURN_PANEL, + /** */ + START_LAST_TURN_PANEL, + /** */ INVALID_TURN_PANEL, /** */ HUMAN_HAND_PANEL, diff --git a/src/jrummikub/view/impl/StartTurnPanel.java b/src/jrummikub/view/impl/StartTurnPanel.java index 4c895b5..d3f139d 100644 --- a/src/jrummikub/view/impl/StartTurnPanel.java +++ b/src/jrummikub/view/impl/StartTurnPanel.java @@ -23,17 +23,23 @@ import jrummikub.view.IView.BottomPanelType; class StartTurnPanel 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 float PANEL_FIRST_LINE_HEIGHT = 0.2f; private final static int PANEL_MAX_WIDTH = 180; private final static float MAX_BUTTON_FONT_SIZE = 12; private JLabel startTurnLabel; + private JLabel extraLabel; private JButton startTurnButton; private Event startTurnEvent = new Event(); private Event acknowledgeInvalidEvent = new Event(); private Event buttonEvent = startTurnEvent; + private BottomPanelType type; + private Color currentPlayerColor; + private String currentPlayerName; + private String redealedPlayerName; + private String lastStonePlayerName; /** * Creates a new StartTurnPanel @@ -49,6 +55,12 @@ class StartTurnPanel extends JPanel { startTurnLabel.putClientProperty("html.disable", Boolean.TRUE); add(startTurnLabel); + extraLabel = new JLabel(); + extraLabel.setHorizontalAlignment(JLabel.CENTER); + extraLabel.setVerticalAlignment(JLabel.CENTER); + extraLabel.putClientProperty("html.disable", Boolean.TRUE); + add(extraLabel); + startTurnButton = new JButton("Zug beginnen"); startTurnButton.addActionListener(new ActionListener() { @Override @@ -66,14 +78,6 @@ class StartTurnPanel extends JPanel { }); } - void setCurrentPlayerName(String playerName) { - startTurnLabel.setText(playerName + " ist jetzt an der Reihe."); - } - - void setCurrentPlayerColor(Color color) { - startTurnLabel.setIcon(ImageUtil.createColorIcon(color, 12, 1)); - } - void setInitialMeldError(int points) { startTurnLabel.setIcon(null); startTurnLabel.setText("Es wurden weniger als " + points @@ -105,34 +109,83 @@ class StartTurnPanel extends JPanel { int firstLineHeight = (int) ((height - PANEL_SEPARATOR) * PANEL_FIRST_LINE_HEIGHT); int buttonWidth = width; - int buttonHeight = height - PANEL_SEPARATOR - firstLineHeight; + int buttonHeight = height - 2 * PANEL_SEPARATOR - 2 * firstLineHeight; float fontSize = (float) Math.sqrt(buttonWidth * buttonHeight) / 5; if (fontSize > MAX_BUTTON_FONT_SIZE) fontSize = MAX_BUTTON_FONT_SIZE; - startTurnLabel.setBounds(x, y, width, firstLineHeight); - startTurnButton.setBounds(x, y + firstLineHeight + PANEL_SEPARATOR, + if (type == BottomPanelType.START_REDEAL_TURN_PANEL + || type == BottomPanelType.START_LAST_TURN_PANEL) { + startTurnLabel.setBounds(x, y, width, firstLineHeight); + extraLabel.setBounds(x, y + firstLineHeight + PANEL_SEPARATOR, width, + firstLineHeight); + extraLabel.setVisible(true); + } else { + startTurnLabel.setBounds(x, y, width, 2 * firstLineHeight + + PANEL_SEPARATOR); + extraLabel.setVisible(false); + } + startTurnButton.setBounds(x, y + 2 * firstLineHeight + 2 * PANEL_SEPARATOR, buttonWidth, buttonHeight); startTurnButton.setFont(startTurnButton.getFont().deriveFont(fontSize)); } - public void setType(BottomPanelType type) { + void setCurrentPlayerName(String playerName) { + currentPlayerName = playerName; + update(); + } + + void setCurrentPlayerColor(Color color) { + currentPlayerColor = color; + update(); + } + + void setRedealedPlayerName(String name) { + redealedPlayerName = name; + update(); + } + + void setLastStonePlayerName(String name) { + lastStonePlayerName = name; + update(); + } + + void setType(BottomPanelType type) { + this.type = type; + update(); + } + + private void update() { if (type == null) { return; } + startTurnLabel + .setIcon(ImageUtil.createColorIcon(currentPlayerColor, 12, 1)); + startTurnLabel.setText(currentPlayerName + " ist jetzt an der Reihe."); + extraLabel.setText(""); + startTurnButton.setText("Zug beginnen"); + buttonEvent = startTurnEvent; + switch (type) { - case START_TURN_PANEL: - startTurnButton.setText("Zug beginnen"); - buttonEvent = startTurnEvent; - break; - case INVALID_TURN_PANEL: - startTurnLabel.setIcon(null); - startTurnLabel.setText("Es liegen ung\u00FCltige Serien auf dem Tisch."); - startTurnButton.setText("N\u00E4chster Spieler"); - buttonEvent = acknowledgeInvalidEvent; - break; + case START_REDEAL_TURN_PANEL: + extraLabel.setText(redealedPlayerName + " hat neu geben lassen."); + break; + + case START_LAST_TURN_PANEL: + extraLabel.setText(lastStonePlayerName + + " hat den letzten Stein gezogen."); + break; + + case INVALID_TURN_PANEL: + startTurnLabel.setIcon(null); + startTurnLabel + .setText("Es liegen ung\u00FCltige Serien auf dem Tisch."); + startTurnButton.setText("N\u00E4chster Spieler"); + buttonEvent = acknowledgeInvalidEvent; + break; } + rescale(); } } diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index fe2c1eb..4940999 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -581,6 +581,16 @@ public class View extends JFrame implements IView { pausePanel.setCurrentPlayerColor(color); } + @Override + public void setRedealedPlayerName(String name) { + startTurnPanel.setRedealedPlayerName(name); + } + + @Override + public void setLastStonePlayerName(String name) { + startTurnPanel.setLastStonePlayerName(name); + } + @Override public void setCurrentPlayerHasLaidOut(boolean hasLaidOut) { playerPanel.setHasLaidOut(hasLaidOut); @@ -657,7 +667,9 @@ public class View extends JFrame implements IView { } private void doSetBottomPanel(BottomPanelType type) { - boolean showStartTurnPanel = (type == BottomPanelType.START_TURN_PANEL || type == BottomPanelType.INVALID_TURN_PANEL); + boolean showStartTurnPanel = (type == BottomPanelType.START_TURN_PANEL + || type == BottomPanelType.START_REDEAL_TURN_PANEL + || type == BottomPanelType.START_LAST_TURN_PANEL || type == BottomPanelType.INVALID_TURN_PANEL); startTurnPanel.setType(type); startTurnPanel.setVisible(showStartTurnPanel); -- cgit v1.2.3