From 2c4d2ba28baa858e9fb3eed52a9b76334330bcdf Mon Sep 17 00:00:00 2001 From: Ida Massow Date: Sat, 11 Jun 2011 04:09:29 +0200 Subject: No Limits implementiert und getestet, geht git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@413 72836036-5685-4462-b002-a69064685172 --- src/jrummikub/control/SettingsControl.java | 39 +++++--- src/jrummikub/model/StoneSet.java | 44 +++++---- src/jrummikub/view/ISettingsPanel.java | 22 +++-- src/jrummikub/view/impl/SettingsPanel.java | 149 ++++++++++++++++++----------- 4 files changed, 161 insertions(+), 93 deletions(-) diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java index 6f4e149..4351c59 100644 --- a/src/jrummikub/control/SettingsControl.java +++ b/src/jrummikub/control/SettingsControl.java @@ -34,9 +34,9 @@ public class SettingsControl { * Create a new settings control * * @param view - * the view to use + * the view to use * @param settings - * initial game settings + * initial game settings */ public SettingsControl(IView view, GameSettings settings) { this.view = view; @@ -44,8 +44,8 @@ public class SettingsControl { } /** - * the start game event is emitted when the user wants to start a game and the - * settings made are valid + * the start game event is emitted when the user wants to start a game and + * the settings made are valid * * @return the event */ @@ -78,7 +78,8 @@ public class SettingsControl { private void addOptionListeners1() { connections.add(view.getSettingsPanel() - .getChangeInitialMeldThresholdEvent().add(new IListener1() { + .getChangeInitialMeldThresholdEvent() + .add(new IListener1() { @Override public void handle(Integer value) { settings.setInitialMeldThreshold(value); @@ -103,7 +104,8 @@ public class SettingsControl { update(); } })); - connections.add(view.getSettingsPanel().getChangeNumberOfStonesDealtEvent() + connections.add(view.getSettingsPanel() + .getChangeNumberOfStonesDealtEvent() .add(new IListener1() { @Override public void handle(Integer value) { @@ -138,6 +140,15 @@ public class SettingsControl { update(); } })); + + connections.add(view.getSettingsPanel().getChangeNoLimitsEvent() + .add(new IListener1() { + @Override + public void handle(Boolean value) { + settings.setNoLimits(value); + update(); + } + })); } private void addPlayerSettingsListeners() { @@ -210,7 +221,8 @@ public class SettingsControl { break; } - settings.getPlayerList().add(new PlayerSettings("Spieler " + num, color)); + settings.getPlayerList().add( + new PlayerSettings("Spieler " + num, color)); update(); } @@ -251,8 +263,9 @@ public class SettingsControl { protected void update() { view.getSettingsPanel().enableRemovePlayerButtons( settings.getPlayerList().size() > 2); - view.getSettingsPanel().enableAddPlayerButton( - settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); + view.getSettingsPanel() + .enableAddPlayerButton( + settings.getPlayerList().size() < ISettingsPanel.PLAYER_COLORS.length); checkSettings(); @@ -273,8 +286,9 @@ public class SettingsControl { for (int j = i + 1; j < settings.getPlayerList().size(); ++j) { if (settings.getPlayerList().get(j).getName().equals(name)) { - view.getSettingsPanel().setError( - ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR); + view.getSettingsPanel() + .setError( + ISettingsPanel.SettingsError.DUPLICATE_PLAYER_NAME_ERROR); view.getSettingsPanel().enableStartGameButton(false); return false; } @@ -283,7 +297,8 @@ public class SettingsControl { int totalStonesDealt = settings.getNumberOfStonesDealt() * settings.getPlayerList().size(); - int totalStones = settings.getHighestValue() * settings.getStoneSetNumber() + int totalStones = settings.getHighestValue() + * settings.getStoneSetNumber() * settings.getStoneColors().size() + settings.getJokerNumber(); if (totalStones <= totalStonesDealt) { diff --git a/src/jrummikub/model/StoneSet.java b/src/jrummikub/model/StoneSet.java index edd6f3f..c1d7a5d 100644 --- a/src/jrummikub/model/StoneSet.java +++ b/src/jrummikub/model/StoneSet.java @@ -85,17 +85,20 @@ public class StoneSet implements Iterable, Sizeable, Serializable { if (nonJoker == -1) { if (stones.size() > settings.getHighestValue() - && stones.size() > settings.getStoneColors().size()) { + && stones.size() > settings.getStoneColors().size() + && !settings.isNoLimits()) { return new Pair(INVALID, 0); } else if (stones.size() > settings.getStoneColors().size()) { - return new Pair( - RUN, - (settings.getHighestValue() * (settings - .getHighestValue() + 1)) - / 2 - - (stones.size() - settings.getHighestValue()) - * (stones.size() - settings.getHighestValue() - 1) - / 2); + int value = 0; + int stoneValue = settings.getHighestValue(); + for (int i = 0; i < stones.size(); i++) { + value += stoneValue; + stoneValue--; + if (stoneValue == 0) { + stoneValue = settings.getHighestValue(); + } + } + return new Pair(RUN, value); } else { return new Pair(GROUP, stones.size() * settings.getHighestValue()); @@ -126,24 +129,33 @@ public class StoneSet implements Iterable, Sizeable, Serializable { int startValue = stones.get(referencePosition).getValue() - referencePosition; int endValue = startValue + stones.size() - 1; - if (startValue < 1 || endValue > settings.getHighestValue()) { - return 0; + if (!settings.isNoLimits()) { + if (startValue < 1 || endValue > settings.getHighestValue()) { + return 0; + } } + int value = 0; for (int i = 0; i < stones.size(); i++) { + int expectedValue = i + startValue; + while (expectedValue < 1) { + expectedValue += settings.getHighestValue(); + } + while (expectedValue > settings.getHighestValue()) { + expectedValue -= settings.getHighestValue(); + } + value += expectedValue; + if (stones.get(i).isJoker()) { continue; } if (stones.get(i).getColor() != runColor) { return 0; } - if (stones.get(i).getValue() != i + startValue) { + + if (stones.get(i).getValue() != expectedValue) { return 0; } } - int value = 0; - for (int i = 0; i < stones.size(); i++) { - value += startValue + i; - } return value; } diff --git a/src/jrummikub/view/ISettingsPanel.java b/src/jrummikub/view/ISettingsPanel.java index 09b0394..bc2593d 100644 --- a/src/jrummikub/view/ISettingsPanel.java +++ b/src/jrummikub/view/ISettingsPanel.java @@ -6,6 +6,7 @@ import java.util.Set; import jrummikub.control.turn.TurnControlFactory; import jrummikub.model.GameSettings; import jrummikub.model.StoneColor; +import jrummikub.util.Event1; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.util.IEvent2; @@ -36,8 +37,8 @@ public interface ISettingsPanel { }; /** - * The add player event is emitted when the user wants to add a player to the - * player list + * The add player event is emitted when the user wants to add a player to + * the player list * * @return the event */ @@ -84,8 +85,8 @@ public interface ISettingsPanel { public IEvent1 getChangeInitialMeldThresholdEvent(); /** - * The change StoneSet number event is emitted when the user wants to use more - * or less than 2 StoneSets per color + * The change StoneSet number event is emitted when the user wants to use + * more or less than 2 StoneSets per color * * @return number of SoneSets */ @@ -126,7 +127,7 @@ public interface ISettingsPanel { * Sets an error to display * * @param error - * the kind of error + * the kind of error */ public void setError(SettingsError error); @@ -134,7 +135,7 @@ public interface ISettingsPanel { * Enables or disables the start game button * * @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); @@ -142,7 +143,7 @@ public interface ISettingsPanel { * Enables or disables the add player button * * @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); @@ -150,7 +151,7 @@ public interface ISettingsPanel { * Enables or disables the remove player buttons * * @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); @@ -159,7 +160,7 @@ public interface ISettingsPanel { * Sets the game settings to display * * @param gameSettings - * the settings + * the settings */ public void setGameSettings(GameSettings gameSettings); @@ -172,6 +173,8 @@ public interface ISettingsPanel { public IEvent1 getChangeTimeEvent(); + public IEvent1 getChangeNoLimitsEvent(); + /** * Specifies the different kinds of settings errors that can be displayed */ @@ -193,4 +196,5 @@ public interface ISettingsPanel { /** Only computer players added */ COMPUTER_PLAYERS_ONLY_WARNING } + } \ No newline at end of file diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java index d7d9069..9536838 100644 --- a/src/jrummikub/view/impl/SettingsPanel.java +++ b/src/jrummikub/view/impl/SettingsPanel.java @@ -26,6 +26,7 @@ import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.Icon; import javax.swing.JButton; +import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; @@ -67,6 +68,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private JButton addPlayerButton; private JLabel errorMessageLabel; private JButton startButton; + private JCheckBox noLimitsBox; private boolean removeButtonsEnabled = true; private List playerSettingsPanels = new ArrayList(); @@ -96,6 +98,7 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private Event1 changeHighestValueEvent = new Event1(); private Event1> changeStoneColorsEvent = new Event1>(); private Event1 changeTimeEvent = new Event1(); + private Event1 changeNoLimitsEvent = new Event1(); @Override public IEvent getStartGameEvent() { @@ -161,10 +164,15 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } @Override - public Event1 getChangeTimeEvent() { + public IEvent1 getChangeTimeEvent() { return changeTimeEvent; } + @Override + public IEvent1 getChangeNoLimitsEvent() { + return changeNoLimitsEvent; + } + @Override public IEvent1> getChangeStoneColorsEvent() { return changeStoneColorsEvent; @@ -173,37 +181,37 @@ class SettingsPanel extends JPanel implements ISettingsPanel { @Override public void setError(SettingsError error) { switch (error) { - case NO_ERROR: - errorMessageLabel.setText(" "); - break; - case DUPLICATE_PLAYER_NAME_ERROR: - errorMessageLabel - .setText("Jeder Spielername darf nur einmal verwendet werden."); - errorMessageLabel.setForeground(Color.RED); - break; - case NO_PLAYER_NAME_ERROR: - errorMessageLabel.setText("Jeder Spieler muss einen Namen haben."); - errorMessageLabel.setForeground(Color.RED); - break; - case NOT_ENOUGH_STONES_ERROR: - errorMessageLabel - .setText("Es gibt nicht genug Steine f\u00fcr die gew\u00e4hlte Spieleranzahl."); - errorMessageLabel.setForeground(Color.RED); - break; - case NOT_ENOUGH_COLORS_ERROR: - errorMessageLabel - .setText("Es m\u00fcssen mindestens drei Farben ausgew\u00e4hlt werden."); - errorMessageLabel.setForeground(Color.RED); - break; - case COMPUTER_PLAYERS_ONLY_WARNING: - errorMessageLabel.setText("Es gibt keinen menschlichen Spieler."); - errorMessageLabel.setForeground(Color.ORANGE.darker()); - break; - case TOO_HIGH_THRESHOLD_WARNING: - errorMessageLabel - .setText("Die gew\u00e4hlte Schranke ist m\u00f6glicherweise zu hoch"); - errorMessageLabel.setForeground(Color.ORANGE.darker()); - break; + case NO_ERROR: + errorMessageLabel.setText(" "); + break; + case DUPLICATE_PLAYER_NAME_ERROR: + errorMessageLabel + .setText("Jeder Spielername darf nur einmal verwendet werden."); + errorMessageLabel.setForeground(Color.RED); + break; + case NO_PLAYER_NAME_ERROR: + errorMessageLabel.setText("Jeder Spieler muss einen Namen haben."); + errorMessageLabel.setForeground(Color.RED); + break; + case NOT_ENOUGH_STONES_ERROR: + errorMessageLabel + .setText("Es gibt nicht genug Steine f\u00fcr die gew\u00e4hlte Spieleranzahl."); + errorMessageLabel.setForeground(Color.RED); + break; + case NOT_ENOUGH_COLORS_ERROR: + errorMessageLabel + .setText("Es m\u00fcssen mindestens drei Farben ausgew\u00e4hlt werden."); + errorMessageLabel.setForeground(Color.RED); + break; + case COMPUTER_PLAYERS_ONLY_WARNING: + errorMessageLabel.setText("Es gibt keinen menschlichen Spieler."); + errorMessageLabel.setForeground(Color.ORANGE.darker()); + break; + case TOO_HIGH_THRESHOLD_WARNING: + errorMessageLabel + .setText("Die gew\u00e4hlte Schranke ist m\u00f6glicherweise zu hoch"); + errorMessageLabel.setForeground(Color.ORANGE.darker()); + break; } } @@ -228,7 +236,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { @Override public void setGameSettings(GameSettings gameSettings) { - while (playerSettingsPanels.size() > gameSettings.getPlayerList().size()) { + while (playerSettingsPanels.size() > gameSettings.getPlayerList() + .size()) { removePlayerSettingsPanel(); } @@ -236,13 +245,15 @@ class SettingsPanel extends JPanel implements ISettingsPanel { updatePlayerSettingsPanel(i, gameSettings.getPlayerList().get(i)); } - initialMeldThresholdSpinner - .setValue(gameSettings.getInitialMeldThreshold()); + initialMeldThresholdSpinner.setValue(gameSettings + .getInitialMeldThreshold()); stoneSetNumberSpinner.setValue(gameSettings.getStoneSetNumber()); highestValueSpinner.setValue(gameSettings.getHighestValue()); - numberOfStonesDealtSpinner.setValue(gameSettings.getNumberOfStonesDealt()); + numberOfStonesDealtSpinner.setValue(gameSettings + .getNumberOfStonesDealt()); jokerNumberSpinner.setValue(gameSettings.getJokerNumber()); timeSpinner.setValue(gameSettings.getTime()); + noLimitsBox.setSelected(gameSettings.isNoLimits()); for (StoneColor color : StoneColor.values()) { colorButtons.get(color).getModel() @@ -268,8 +279,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } private void removePlayerSettingsPanel() { - PlayerSettingsPanel p = playerSettingsPanels.remove(playerSettingsPanels - .size() - 1); + PlayerSettingsPanel p = playerSettingsPanels + .remove(playerSettingsPanels.size() - 1); playerSettingsViewport.remove(p); } @@ -291,7 +302,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { playerSettingsViewport.add(addPlayerPanel); addPlayerButton = new JButton("+"); - addPlayerButton.setFont(addPlayerButton.getFont().deriveFont(Font.BOLD)); + addPlayerButton + .setFont(addPlayerButton.getFont().deriveFont(Font.BOLD)); addPlayerButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -328,17 +340,21 @@ class SettingsPanel extends JPanel implements ISettingsPanel { changeNumberOfStonesDealtEvent); makeOptionLabel(4, "Jokeranzahl:"); - jokerNumberSpinner = makeOptionSpinner(4, 1, 999, 1, changeJokerNumberEvent); + jokerNumberSpinner = makeOptionSpinner(4, 1, 999, 1, + changeJokerNumberEvent); makeOptionLabel(5, "Zeit für Spielzug:"); timeSpinner = makeOptionSpinner(5, 1, 999, 1, changeTimeEvent); - makeOptionLabel(6, "Steinfarben:"); - createColorSelectionPanel(6); + makeOptionLabel(6, "No Limits:"); + noLimitsBox = makeOptionCheckbox(6, changeNoLimitsEvent); + + makeOptionLabel(7, "Steinfarben:"); + createColorSelectionPanel(7); GridBagConstraints c = new GridBagConstraints(); c.gridx = 1; - c.gridy = 7; + c.gridy = 8; c.fill = GridBagConstraints.BOTH; c.weightx = 1; c.weighty = 1; @@ -388,6 +404,24 @@ class SettingsPanel extends JPanel implements ISettingsPanel { changeStoneColorsEvent.emit(colors); } + private JCheckBox makeOptionCheckbox(int row, + final Event1 targetEvent) { + GridBagConstraints c = new GridBagConstraints(); + c.anchor = GridBagConstraints.WEST; + c.gridx = 1; + c.gridy = row; + c.insets = new Insets(2, 2, 2, 2); + final JCheckBox box = new JCheckBox(); + box.addChangeListener(new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + targetEvent.emit(box.isSelected()); + } + }); + optionsPanel.add(box, c); + return box; + } + private JSpinner makeOptionSpinner(int row, int min, int max, int step, final Event1 targetEvent) { GridBagConstraints c = new GridBagConstraints(); @@ -397,8 +431,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { c.insets = new Insets(2, 2, 2, 2); final JSpinner spinner = new JSpinner(); spinner.setModel(new SpinnerNumberModel(min, min, max, step)); - spinner - .setPreferredSize(new Dimension(60, spinner.getMinimumSize().height)); + spinner.setPreferredSize(new Dimension(60, + spinner.getMinimumSize().height)); spinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { @@ -458,8 +492,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { c.gridwidth = GridBagConstraints.REMAINDER; addButton("Netzwerkspiel...", networkGameEvent, c); - setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder( - 10, 10, 10, 10))); + setBorder(new CompoundBorder(new LineBorder(Color.BLACK), + new EmptyBorder(10, 10, 10, 10))); } void resetTabbedPane() { @@ -496,8 +530,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { if (index != playerType.getSelectedIndex()) { playerType.setSelectedIndex(index); } - colorButton - .setIcon(ImageUtil.createColorIcon(settings.getColor(), 16, 2)); + colorButton.setIcon(ImageUtil.createColorIcon(settings.getColor(), + 16, 2)); } public void enableRemoveButton(boolean enable) { @@ -545,7 +579,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { c.gridy = 0; add(colorButton, c); - nameField.getDocument().addDocumentListener(new NameChangeListener()); + nameField.getDocument().addDocumentListener( + new NameChangeListener()); c2.gridy = 0; c2.weightx = 1; c2.fill = GridBagConstraints.HORIZONTAL; @@ -578,8 +613,9 @@ class SettingsPanel extends JPanel implements ISettingsPanel { public Dimension getPreferredSize() { Dimension preferredSize = super.getPreferredSize(); for (Component component : this.getComponents()) { - preferredSize - .setSize(preferredSize.width, Math.max(preferredSize.height, + preferredSize.setSize( + preferredSize.width, + Math.max(preferredSize.height, component.getPreferredSize().height)); } return preferredSize; @@ -589,8 +625,9 @@ class SettingsPanel extends JPanel implements ISettingsPanel { public Dimension getMaximumSize() { Dimension preferredSize = super.getPreferredSize(); for (Component component : this.getComponents()) { - preferredSize - .setSize(Integer.MAX_VALUE, Math.max(preferredSize.height, + preferredSize.setSize( + Integer.MAX_VALUE, + Math.max(preferredSize.height, component.getPreferredSize().height)); } return preferredSize; @@ -605,8 +642,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { } private void changeType() { - changePlayerTypeEvent.emit(playerNumber, - TurnControlFactory.Type.values()[playerType.getSelectedIndex()]); + changePlayerTypeEvent.emit(playerNumber, TurnControlFactory.Type + .values()[playerType.getSelectedIndex()]); } private void remove() { -- cgit v1.2.3