From c004a07a4267f3320d23f3d1d2cf483cd56f40b7 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 31 May 2011 01:50:56 +0200 Subject: Make highest value and number of stones dealt settable git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@337 72836036-5685-4462-b002-a69064685172 --- mock/jrummikub/view/MockSettingsPanel.java | 35 +++++++++++++++- src/jrummikub/control/SettingsControl.java | 29 +++++++++++++- src/jrummikub/model/GameSettings.java | 39 +++++++++--------- src/jrummikub/model/StoneHeap.java | 2 +- src/jrummikub/model/StoneSet.java | 12 +++--- src/jrummikub/view/ISettingsPanel.java | 24 +++++++---- src/jrummikub/view/impl/SettingsPanel.java | 38 ++++++++++++++++-- test/jrummikub/control/RoundControlTest.java | 2 +- test/jrummikub/control/SettingsControlTest.java | 53 +++++++++++++++++++++---- test/jrummikub/model/StoneHeapTest.java | 4 +- test/jrummikub/model/StoneSetTest.java | 4 +- 11 files changed, 190 insertions(+), 52 deletions(-) diff --git a/mock/jrummikub/view/MockSettingsPanel.java b/mock/jrummikub/view/MockSettingsPanel.java index f296286..5f69680 100644 --- a/mock/jrummikub/view/MockSettingsPanel.java +++ b/mock/jrummikub/view/MockSettingsPanel.java @@ -1,10 +1,13 @@ package jrummikub.view; import java.awt.Color; +import java.util.HashSet; +import java.util.Set; import jrummikub.control.turn.TurnControlFactory; import jrummikub.control.turn.TurnControlFactory.Type; import jrummikub.model.GameSettings; +import jrummikub.model.StoneColor; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.util.IEvent2; @@ -42,12 +45,24 @@ public class MockSettingsPanel implements ISettingsPanel { public MockEvent1 changeJokerNumberEvent = new MockEvent1(); /** */ public MockEvent1 changeStoneSetNumberEvent = new MockEvent1(); + /** */ + public MockEvent1 changeNumberOfStonesDealtEvent = new MockEvent1(); + /** */ + public MockEvent1 changeHighestValueEvent = new MockEvent1(); + /** */ + public MockEvent1> changeStoneColorsEvent = new MockEvent1>(); /** */ public int initialMeldThreshold; /** */ public int jokerNumber; - + /** */ public int stoneSetNumber; + /** */ + public int numberOfStonesDealt; + /** */ + public int highestValue; + /** */ + public Set stoneColors; @Override public IEvent getAddPlayerEvent() { @@ -74,6 +89,21 @@ public class MockSettingsPanel implements ISettingsPanel { return changeInitialMeldThresholdEvent; } + @Override + public IEvent1 getChangeNumberOfStonesDealtEvent() { + return changeNumberOfStonesDealtEvent; + } + + @Override + public IEvent1 getChangeHighestValueEvent() { + return changeHighestValueEvent; + } + + @Override + public IEvent1> getChangeStoneColorsEvent() { + return changeStoneColorsEvent; + } + @Override public IEvent getStartGameEvent() { return startGameEvent; @@ -108,6 +138,9 @@ public class MockSettingsPanel implements ISettingsPanel { initialMeldThreshold = gameSettings.getInitialMeldThreshold(); jokerNumber = gameSettings.getJokerNumber(); stoneSetNumber = gameSettings.getStoneSetNumber(); + numberOfStonesDealt = gameSettings.getNumberOfStonesDealt(); + highestValue = gameSettings.getHighestValue(); + stoneColors = new HashSet(gameSettings.getStoneColors()); } @Override diff --git a/src/jrummikub/control/SettingsControl.java b/src/jrummikub/control/SettingsControl.java index f12f1ad..fd01613 100644 --- a/src/jrummikub/control/SettingsControl.java +++ b/src/jrummikub/control/SettingsControl.java @@ -2,12 +2,15 @@ package jrummikub.control; import java.awt.Color; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import jrummikub.control.turn.TurnControlFactory; import jrummikub.control.turn.TurnControlFactory.Type; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; +import jrummikub.model.StoneColor; import jrummikub.util.Connection; import jrummikub.util.Event1; import jrummikub.util.IEvent1; @@ -83,6 +86,30 @@ public class SettingsControl { update(); } })); + connections.add(view.getSettingsPanel().getChangeNumberOfStonesDealtEvent() + .add(new IListener1() { + @Override + public void handle(Integer value) { + settings.setNumberOfStonesDealt(value); + update(); + } + })); + connections.add(view.getSettingsPanel().getChangeHighestValueEvent() + .add(new IListener1() { + @Override + public void handle(Integer value) { + settings.setHighestValue(value); + update(); + } + })); + connections.add(view.getSettingsPanel().getChangeStoneColorsEvent() + .add(new IListener1>() { + @Override + public void handle(Set value) { + settings.setStoneColors(new HashSet(value)); + update(); + } + })); addPlayerSettingsListeners(); @@ -242,7 +269,7 @@ public class SettingsControl { int totalStonesDealt = settings.getNumberOfStonesDealt() * settings.getPlayerList().size(); - int totalStones = settings.getHighestCard() + int totalStones = settings.getHighestValue() * settings.getStoneSetNumber() * settings.getStoneColors().size() + settings.getJokerNumber(); diff --git a/src/jrummikub/model/GameSettings.java b/src/jrummikub/model/GameSettings.java index d571a93..f3cf69a 100644 --- a/src/jrummikub/model/GameSettings.java +++ b/src/jrummikub/model/GameSettings.java @@ -1,11 +1,12 @@ package jrummikub.model; +import static jrummikub.model.StoneColor.*; + import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; -import static jrummikub.model.StoneColor.*; /** * The overall game settings @@ -16,7 +17,7 @@ public class GameSettings { private int initialMeldThreshold; private int jokerPoints; private int jokerNumber; - private int highestCard; + private int highestValue; private int stoneSetNumber; private int numberOfStonesDealt; private boolean noLimits; @@ -29,12 +30,12 @@ public class GameSettings { initialMeldThreshold = 30; jokerPoints = 50; jokerNumber = 2; - highestCard = 13; + highestValue = 13; stoneSetNumber = 2; numberOfStonesDealt = 14; noLimits = false; - stoneColors = new HashSet(Arrays.asList(BLACK, BLUE, - ORANGE, RED)); + stoneColors = new HashSet(Arrays.asList(BLACK, BLUE, ORANGE, + RED)); } /** @@ -50,7 +51,7 @@ public class GameSettings { * Sets the initial meld threshold * * @param value - * the value to set + * the value to set */ public void setInitialMeldThreshold(int value) { initialMeldThreshold = value; @@ -69,7 +70,7 @@ public class GameSettings { * Sets the points counted for a joker * * @param value - * the value to set + * the value to set */ public void setJokerPoints(int value) { jokerPoints = value; @@ -103,22 +104,22 @@ public class GameSettings { } /** - * Return the highest card value in use + * Return the highest stone value in use * - * @return highest card value + * @return highest stone value */ - public int getHighestCard() { - return highestCard; + public int getHighestValue() { + return highestValue; } /** - * Set the highest card value in use + * Set the highest stone value in use * - * @param highestCard - * highest card value + * @param highestValue + * highest stone value */ - public void setHighestCard(int highestCard) { - this.highestCard = highestCard; + public void setHighestValue(int highestValue) { + this.highestValue = highestValue; } /** @@ -134,7 +135,7 @@ public class GameSettings { * Set the number of sets of stones in use * * @param stoneSets - * sets of stones in use + * sets of stones in use */ public void setStoneSetNumber(int stoneSets) { this.stoneSetNumber = stoneSets; @@ -153,7 +154,7 @@ public class GameSettings { * Set whether "No-Limits" rules are used * * @param noLimits - * use no limit rules + * use no limit rules */ public void setNoLimits(boolean noLimits) { this.noLimits = noLimits; @@ -172,7 +173,7 @@ public class GameSettings { * Set stone colors used * * @param stoneColors - * used stone colors + * used stone colors */ public void setStoneColors(Set stoneColors) { this.stoneColors = stoneColors; diff --git a/src/jrummikub/model/StoneHeap.java b/src/jrummikub/model/StoneHeap.java index 27c8d01..b3c5fbe 100644 --- a/src/jrummikub/model/StoneHeap.java +++ b/src/jrummikub/model/StoneHeap.java @@ -23,7 +23,7 @@ public class StoneHeap { * */ public StoneHeap(GameSettings gameSettings) { heap = new ArrayList(); - for (int i = 1; i <= gameSettings.getHighestCard(); i++) { + for (int i = 1; i <= gameSettings.getHighestValue(); i++) { for (int j = 0; j < gameSettings.getStoneSetNumber(); j++) { for (StoneColor c : gameSettings.getStoneColors()) { heap.add(new Stone(i, c)); diff --git a/src/jrummikub/model/StoneSet.java b/src/jrummikub/model/StoneSet.java index efe7a2e..3324565 100644 --- a/src/jrummikub/model/StoneSet.java +++ b/src/jrummikub/model/StoneSet.java @@ -75,19 +75,19 @@ public class StoneSet implements Iterable, Sizeable { } if (nonJoker == -1) { - if (stones.size() > settings.getHighestCard()) { + if (stones.size() > settings.getHighestValue()) { return new Pair(INVALID, 0); } else if (stones.size() > settings.getStoneColors().size()) { return new Pair( RUN, - (settings.getHighestCard() * (settings.getHighestCard() + 1)) + (settings.getHighestValue() * (settings.getHighestValue() + 1)) / 2 - - (stones.size() - settings.getHighestCard()) - * (stones.size() - settings.getHighestCard() - 1) + - (stones.size() - settings.getHighestValue()) + * (stones.size() - settings.getHighestValue() - 1) / 2); } else { return new Pair(GROUP, stones.size() - * settings.getHighestCard()); + * settings.getHighestValue()); } } @@ -115,7 +115,7 @@ public class StoneSet implements Iterable, Sizeable { int startValue = stones.get(referencePosition).getValue() - referencePosition; int endValue = startValue + stones.size() - 1; - if (startValue < 1 || endValue > settings.getHighestCard()) { + if (startValue < 1 || endValue > settings.getHighestValue()) { return 0; } for (int i = 0; i < stones.size(); i++) { diff --git a/src/jrummikub/view/ISettingsPanel.java b/src/jrummikub/view/ISettingsPanel.java index 5c83f47..1d030fe 100644 --- a/src/jrummikub/view/ISettingsPanel.java +++ b/src/jrummikub/view/ISettingsPanel.java @@ -1,9 +1,11 @@ package jrummikub.view; import java.awt.Color; +import java.util.Set; import jrummikub.control.turn.TurnControlFactory; import jrummikub.model.GameSettings; +import jrummikub.model.StoneColor; import jrummikub.util.IEvent; import jrummikub.util.IEvent1; import jrummikub.util.IEvent2; @@ -34,8 +36,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 */ @@ -80,9 +82,15 @@ public interface ISettingsPanel { * @return the event */ public IEvent1 getChangeInitialMeldThresholdEvent(); - + public IEvent1 getChangeStoneSetNumberEvent(); + public IEvent1 getChangeNumberOfStonesDealtEvent(); + + public IEvent1 getChangeHighestValueEvent(); + + public IEvent1> getChangeStoneColorsEvent(); + /** * the start game event is emitted when the user wants to start the game * @@ -94,7 +102,7 @@ public interface ISettingsPanel { * Sets an error to display * * @param error - * the kind of error + * the kind of error */ public void setError(SettingsError error); @@ -102,7 +110,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); @@ -110,7 +118,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); @@ -118,7 +126,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); @@ -127,7 +135,7 @@ public interface ISettingsPanel { * Sets the game settings to display * * @param gameSettings - * the settings + * the settings */ public void setGameSettings(GameSettings gameSettings); diff --git a/src/jrummikub/view/impl/SettingsPanel.java b/src/jrummikub/view/impl/SettingsPanel.java index 5eb82fb..95089a3 100644 --- a/src/jrummikub/view/impl/SettingsPanel.java +++ b/src/jrummikub/view/impl/SettingsPanel.java @@ -17,6 +17,7 @@ import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Set; import javax.swing.Box; import javax.swing.BoxLayout; @@ -44,6 +45,7 @@ import jrummikub.control.turn.TurnControlFactory; import jrummikub.control.turn.TurnControlFactory.Type; import jrummikub.model.GameSettings; import jrummikub.model.PlayerSettings; +import jrummikub.model.StoneColor; import jrummikub.util.Event; import jrummikub.util.Event1; import jrummikub.util.Event2; @@ -66,6 +68,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private JSpinner initialMeldThresholdSpinner; private JSpinner stoneSetNumberSpinner; + private JSpinner highestValueSpinner; + private JSpinner numberOfStonesDealtSpinner; private JSpinner jokerNumberSpinner; private Event startGameEvent = new Event(); @@ -77,6 +81,9 @@ class SettingsPanel extends JPanel implements ISettingsPanel { private Event1 changeInitialMeldThresholdEvent = new Event1(); private Event1 changeJokerNumberEvent = new Event1(); private Event1 changeStoneSetNumberEvent = new Event1(); + private Event1 changeNumberOfStonesDealtEvent = new Event1(); + private Event1 changeHighestValueEvent = new Event1(); + private Event1> changeStoneColorsEvent = new Event1>(); @Override public IEvent getStartGameEvent() { @@ -113,6 +120,21 @@ class SettingsPanel extends JPanel implements ISettingsPanel { return changeStoneSetNumberEvent; } + @Override + public IEvent1 getChangeNumberOfStonesDealtEvent() { + return changeNumberOfStonesDealtEvent; + } + + @Override + public IEvent1 getChangeHighestValueEvent() { + return changeHighestValueEvent; + } + + @Override + public IEvent1> getChangeStoneColorsEvent() { + return changeStoneColorsEvent; + } + @Override public void setError(SettingsError error) { switch (error) { @@ -178,6 +200,8 @@ class SettingsPanel extends JPanel implements ISettingsPanel { initialMeldThresholdSpinner.setValue(gameSettings .getInitialMeldThreshold()); stoneSetNumberSpinner.setValue(gameSettings.getStoneSetNumber()); + highestValueSpinner.setValue(gameSettings.getHighestValue()); + numberOfStonesDealtSpinner.setValue(gameSettings.getNumberOfStonesDealt()); jokerNumberSpinner.setValue(gameSettings.getJokerNumber()); playerSettingsViewport.revalidate(); @@ -251,13 +275,21 @@ class SettingsPanel extends JPanel implements ISettingsPanel { stoneSetNumberSpinner = makeOptionSpinner(1, 1, 999, 1, changeStoneSetNumberEvent); - makeOptionLabel(2, "Jokeranzahl:"); - jokerNumberSpinner = makeOptionSpinner(2, 1, 999, 1, + makeOptionLabel(2, "H\u00f6chster Steinwert:"); + highestValueSpinner = makeOptionSpinner(2, 3, 99, 1, + changeHighestValueEvent); + + makeOptionLabel(3, "Anzahl Startsteine:"); + numberOfStonesDealtSpinner = makeOptionSpinner(3, 1, 999, 1, + changeNumberOfStonesDealtEvent); + + makeOptionLabel(4, "Jokeranzahl:"); + jokerNumberSpinner = makeOptionSpinner(4, 1, 999, 1, changeJokerNumberEvent); GridBagConstraints c = new GridBagConstraints(); c.gridx = 1; - c.gridy = 3; + c.gridy = 5; c.fill = GridBagConstraints.BOTH; c.weightx = 1; c.weighty = 1; diff --git a/test/jrummikub/control/RoundControlTest.java b/test/jrummikub/control/RoundControlTest.java index a71e231..1e960ff 100644 --- a/test/jrummikub/control/RoundControlTest.java +++ b/test/jrummikub/control/RoundControlTest.java @@ -80,7 +80,7 @@ public class RoundControlTest { private void checkCorrectlyDealt() { GameSettings settings = testRoundState.getGameSettings(); - int totalStones = settings.getHighestCard() + int totalStones = settings.getHighestValue() * settings.getStoneSetNumber() * settings.getStoneColors().size() + settings.getJokerNumber(); assertEquals( diff --git a/test/jrummikub/control/SettingsControlTest.java b/test/jrummikub/control/SettingsControlTest.java index 969b554..534904d 100644 --- a/test/jrummikub/control/SettingsControlTest.java +++ b/test/jrummikub/control/SettingsControlTest.java @@ -1,11 +1,15 @@ package jrummikub.control; +import static jrummikub.model.StoneColor.*; import static org.junit.Assert.*; import java.awt.Color; +import java.util.Arrays; +import java.util.HashSet; import jrummikub.control.turn.TurnControlFactory; import jrummikub.model.GameSettings; +import jrummikub.model.StoneColor; import jrummikub.util.IListener1; import jrummikub.view.ISettingsPanel; import jrummikub.view.MockView; @@ -37,8 +41,7 @@ public class SettingsControlTest { /** */ @Test public void initialStateTest() { - assertSame(ISettingsPanel.SettingsError.NO_ERROR, - view.settingsPanel.error); + assertSame(ISettingsPanel.SettingsError.NO_ERROR, view.settingsPanel.error); view.settingsPanel.startGameEvent.emit(); assertNotNull(gameSettings); assertEquals(2, gameSettings.getPlayerList().size()); @@ -159,8 +162,7 @@ public class SettingsControlTest { view.settingsPanel.changePlayerNameEvent.emit(1, name2); assertTrue(view.settingsPanel.startButtonEnabled); - assertSame(ISettingsPanel.SettingsError.NO_ERROR, - view.settingsPanel.error); + assertSame(ISettingsPanel.SettingsError.NO_ERROR, view.settingsPanel.error); view.settingsPanel.startGameEvent.emit(); assertNotNull(gameSettings); @@ -213,6 +215,41 @@ public class SettingsControlTest { assertEquals(3, gameSettings.getStoneSetNumber()); } + /** */ + @Test + public void changeNumberOfStonesDealtTest() { + assertEquals(initialSettings.getNumberOfStonesDealt(), + view.settingsPanel.numberOfStonesDealt); + + view.settingsPanel.changeNumberOfStonesDealtEvent.emit(15); + view.settingsPanel.startGameEvent.emit(); + assertEquals(15, gameSettings.getNumberOfStonesDealt()); + } + + /** */ + @Test + public void changeHighestValueTest() { + assertEquals(initialSettings.getHighestValue(), + view.settingsPanel.highestValue); + + view.settingsPanel.changeHighestValueEvent.emit(10); + view.settingsPanel.startGameEvent.emit(); + assertEquals(10, gameSettings.getHighestValue()); + } + + /** */ + @Test + public void changeStoneColorsTest() { + assertEquals(initialSettings.getStoneColors(), + view.settingsPanel.stoneColors); + + view.settingsPanel.changeStoneColorsEvent.emit(new HashSet(Arrays.asList( + BLUE, RED, BLACK))); + view.settingsPanel.startGameEvent.emit(); + assertEquals(new HashSet(Arrays.asList(BLUE, RED, BLACK)), + gameSettings.getStoneColors()); + } + /** */ @Test public void typeChangeTest() { @@ -225,10 +262,10 @@ public class SettingsControlTest { view.settingsPanel.startGameEvent.emit(); assertNotNull(gameSettings); assertEquals(2, gameSettings.getPlayerList().size()); - assertSame(TurnControlFactory.Type.COMPUTER, gameSettings - .getPlayerList().get(0).getTurnControlType()); - assertSame(TurnControlFactory.Type.HUMAN, gameSettings.getPlayerList() - .get(1).getTurnControlType()); + assertSame(TurnControlFactory.Type.COMPUTER, gameSettings.getPlayerList() + .get(0).getTurnControlType()); + assertSame(TurnControlFactory.Type.HUMAN, + gameSettings.getPlayerList().get(1).getTurnControlType()); } } diff --git a/test/jrummikub/model/StoneHeapTest.java b/test/jrummikub/model/StoneHeapTest.java index f0b88d3..48ca488 100644 --- a/test/jrummikub/model/StoneHeapTest.java +++ b/test/jrummikub/model/StoneHeapTest.java @@ -54,7 +54,7 @@ public class StoneHeapTest { } private int calculateTotalNumberOfStones(GameSettings testSettings) { - int totalStones = testSettings.getHighestCard() + int totalStones = testSettings.getHighestValue() * testSettings.getStoneSetNumber() * testSettings.getStoneColors().size() + testSettings.getJokerNumber(); @@ -81,7 +81,7 @@ public class StoneHeapTest { */ @Theory public void fullColor(Pair data) { - int stonesOfAColor = data.getFirst().getHighestCard() + int stonesOfAColor = data.getFirst().getHighestValue() * data.getFirst().getStoneSetNumber(); Map counters = new HashMap(); for (StoneColor c : data.getFirst().getStoneColors()) { diff --git a/test/jrummikub/model/StoneSetTest.java b/test/jrummikub/model/StoneSetTest.java index a5c44f0..b9fd961 100644 --- a/test/jrummikub/model/StoneSetTest.java +++ b/test/jrummikub/model/StoneSetTest.java @@ -31,8 +31,8 @@ public class StoneSetTest { moreColorSettings.setStoneColors(EnumSet.allOf(StoneColor.class)); lessColorSettings.setStoneColors(new HashSet(Arrays.asList( StoneColor.BLUE, StoneColor.RED, StoneColor.BLACK))); - higherValueSettings.setHighestCard(17); - lowerValueSettings.setHighestCard(10); + higherValueSettings.setHighestValue(17); + lowerValueSettings.setHighestValue(10); } private void assertSet(StoneSet.Type expectedType, Integer expectedValue, -- cgit v1.2.3