summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control
diff options
context:
space:
mode:
Diffstat (limited to 'src/jrummikub/control')
-rw-r--r--src/jrummikub/control/GameControl.java59
-rw-r--r--src/jrummikub/control/RoundControl.java40
-rw-r--r--src/jrummikub/control/TurnControl.java6
3 files changed, 89 insertions, 16 deletions
diff --git a/src/jrummikub/control/GameControl.java b/src/jrummikub/control/GameControl.java
new file mode 100644
index 0000000..ee87bd5
--- /dev/null
+++ b/src/jrummikub/control/GameControl.java
@@ -0,0 +1,59 @@
+package jrummikub.control;
+
+import jrummikub.model.GameState;
+import jrummikub.util.IListener;
+import jrummikub.view.IView;
+
+public class GameControl {
+ final static int HAND_HEIGHT = 2;
+ final static int HAND_WIDTH = 14;
+ private IView view;
+ private RoundControl roundControl;
+
+ public GameControl(IView view) {
+ this.view = view;
+ view.getPlayerPanel().getHandPanel().setHandHeight(HAND_HEIGHT);
+ view.getPlayerPanel().getHandPanel().setHandWidth(HAND_WIDTH);
+
+ view.getNewGameEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ startRound();
+ }
+ });
+ view.getQuitEvent().add(new IListener() {
+ @Override
+ public void handle() {
+ quitProgram();
+ }
+ });
+ }
+
+ public void startGame() {
+ startRound();
+ }
+
+ private void startRound() {
+ if (roundControl != null) {
+ return;
+ }
+
+ GameState gameState = new GameState();
+
+ roundControl = new RoundControl(gameState, view);
+ roundControl.getEndRoundEvent().add(new IListener() {
+
+ @Override
+ public void handle() {
+ roundControl = null;
+ }
+ });
+
+ roundControl.startRound();
+ }
+
+ private void quitProgram() {
+ System.exit(0);
+ }
+
+}
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java
index f536acc..63accaa 100644
--- a/src/jrummikub/control/RoundControl.java
+++ b/src/jrummikub/control/RoundControl.java
@@ -1,6 +1,8 @@
package jrummikub.control;
+import java.util.ArrayList;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import jrummikub.model.IGameState;
@@ -9,35 +11,39 @@ import jrummikub.model.ITable;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
+import jrummikub.util.Connection;
+import jrummikub.util.Event;
+import jrummikub.util.IEvent;
import jrummikub.util.IListener;
import jrummikub.util.Pair;
import jrummikub.view.IView;
public class RoundControl {
- // TODO move to game control once existent
- final static int HAND_HEIGHT = 2;
- final static int HAND_WIDTH = 14;
private IGameState gameState;
private IView view;
private ITable clonedTable;
+ private Event endRoundEvent = new Event();
+ private List<Connection> connections = new ArrayList<Connection>();
public RoundControl(IGameState gameState, IView view) {
this.gameState = gameState;
this.view = view;
- view.getPlayerPanel().getHandPanel().setHandHeight(HAND_HEIGHT);
- view.getPlayerPanel().getHandPanel().setHandWidth(HAND_WIDTH);
+ }
+
+ public IEvent getEndRoundEvent() {
+ return endRoundEvent;
}
public void startRound() {
deal();
- view.getStartTurnEvent().add(new IListener() {
+ connections.add(view.getStartTurnEvent().add(new IListener() {
@Override
public void handle() {
startTurn();
}
- });
+ }));
prepareTurn();
}
@@ -58,13 +64,13 @@ public class RoundControl {
private void startTurn() {
TurnControl turnControl = new TurnControl(gameState.getActivePlayer()
.getHand(), clonedTable, view);
- turnControl.getEndOfTurnEvent().add(new IListener() {
+ connections.add(turnControl.getEndOfTurnEvent().add(new IListener() {
@Override
public void handle() {
endOfTurn();
}
- });
+ }));
turnControl.startTurn();
}
@@ -73,14 +79,17 @@ public class RoundControl {
for (int i = 0; i < gameState.getPlayerCount(); i++) {
IHand hand = gameState.getNthNextPlayer(i).getHand();
for (int j = 0; j < 7; j++) {
- hand.drop(gameState.getGameHeap().drawStone(), new Position(j, 0));
- hand.drop(gameState.getGameHeap().drawStone(), new Position(j, 1));
+ hand.drop(gameState.getGameHeap().drawStone(), new Position(j,
+ 0));
+ hand.drop(gameState.getGameHeap().drawStone(), new Position(j,
+ 1));
}
}
}
private void endOfTurn() {
- Set<Stone> tableDiff = tableDifference(gameState.getTable(), clonedTable);
+ Set<Stone> tableDiff = tableDifference(gameState.getTable(),
+ clonedTable);
if (!tableDiff.isEmpty()) { // Player has made a move
if (clonedTable.isValid()) {
@@ -128,7 +137,8 @@ public class RoundControl {
.getActivePlayer()
.getHand()
.drop(gameState.getGameHeap().drawStone(),
- new Position(HAND_WIDTH - 1, HAND_HEIGHT - 1));
+ new Position(GameControl.HAND_WIDTH - 1,
+ GameControl.HAND_HEIGHT - 1));
}
private void dealPenalty(int count) {
@@ -137,6 +147,10 @@ public class RoundControl {
}
private void win() {
+ for (Connection c : connections) {
+ c.remove();
+ }
+ endRoundEvent.emit();
view.enableWinPanel(true);
}
}
diff --git a/src/jrummikub/control/TurnControl.java b/src/jrummikub/control/TurnControl.java
index 632c3ee..32e6379 100644
--- a/src/jrummikub/control/TurnControl.java
+++ b/src/jrummikub/control/TurnControl.java
@@ -30,7 +30,7 @@ public class TurnControl {
private List<Stone> selectedStones = new ArrayList<Stone>();
private Event endOfTurnEvent = new Event();
- List<Connection> connections = new ArrayList<Connection>();
+ private List<Connection> connections = new ArrayList<Connection>();
public TurnControl(IHand hand, ITable table, IView view) {
this.hand = hand;
@@ -203,11 +203,11 @@ public class TurnControl {
for (Stone stone : stones) {
hand.drop(stone, new Position(x, y));
x++;
- if (x >= RoundControl.HAND_WIDTH) {
+ if (x >= GameControl.HAND_WIDTH) {
x = 0;
y++;
- if (y >= RoundControl.HAND_HEIGHT) {
+ if (y >= GameControl.HAND_HEIGHT) {
throw new ArrayIndexOutOfBoundsException(); // TODO We can't
// handle
// this yet...