2011-05-10 03:13:11 +02:00
|
|
|
package jrummikub.control;
|
|
|
|
|
2011-05-24 23:15:21 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2011-05-24 01:51:49 +02:00
|
|
|
import jrummikub.model.GameSettings;
|
2011-05-24 01:51:54 +02:00
|
|
|
import jrummikub.model.GameState;
|
2011-05-24 01:51:53 +02:00
|
|
|
import jrummikub.model.IRoundState;
|
2011-05-18 15:25:26 +02:00
|
|
|
import jrummikub.model.RoundState;
|
2011-05-25 15:51:34 +02:00
|
|
|
import jrummikub.model.Score;
|
2011-05-24 23:15:21 +02:00
|
|
|
import jrummikub.util.Connection;
|
2011-05-29 20:19:17 +02:00
|
|
|
import jrummikub.util.Event;
|
|
|
|
import jrummikub.util.IEvent;
|
2011-05-10 03:13:11 +02:00
|
|
|
import jrummikub.util.IListener;
|
2011-05-25 15:51:34 +02:00
|
|
|
import jrummikub.util.IListener1;
|
2011-05-10 03:13:11 +02:00
|
|
|
import jrummikub.view.IView;
|
|
|
|
|
2011-05-10 04:05:52 +02:00
|
|
|
/**
|
|
|
|
* Controls a Game, at some point including all Rounds, starts new Rounds
|
|
|
|
*/
|
2011-05-10 03:13:11 +02:00
|
|
|
public class GameControl {
|
2011-05-24 01:51:49 +02:00
|
|
|
private GameSettings gameSettings;
|
2011-05-10 03:13:11 +02:00
|
|
|
private IView view;
|
2011-05-27 17:54:46 +02:00
|
|
|
RoundControl roundControl;
|
2011-05-24 01:51:54 +02:00
|
|
|
private GameState gameState;
|
2011-05-24 23:15:21 +02:00
|
|
|
private List<Connection> connections = new ArrayList<Connection>();
|
2011-05-29 20:19:17 +02:00
|
|
|
private Event endOfGameEvent = new Event();
|
2011-05-10 03:13:11 +02:00
|
|
|
|
2011-05-10 04:05:52 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2011-05-18 16:02:23 +02:00
|
|
|
* @param gameSettings
|
2011-05-29 20:19:17 +02:00
|
|
|
* the game settings
|
2011-05-10 05:53:30 +02:00
|
|
|
* @param view
|
2011-05-29 20:19:17 +02:00
|
|
|
* the view
|
2011-05-10 04:05:52 +02:00
|
|
|
*/
|
2011-05-24 01:51:49 +02:00
|
|
|
public GameControl(GameSettings gameSettings, IView view) {
|
2011-05-18 16:02:23 +02:00
|
|
|
this.gameSettings = gameSettings;
|
2011-05-10 03:13:11 +02:00
|
|
|
this.view = view;
|
2011-05-29 15:50:30 +02:00
|
|
|
|
2011-05-24 01:51:54 +02:00
|
|
|
gameState = new GameState();
|
2011-05-29 15:50:30 +02:00
|
|
|
gameState.setFirstRoundFirstPlayer((int) (Math.random() * gameSettings
|
|
|
|
.getPlayerList().size()));
|
2011-05-10 03:13:11 +02:00
|
|
|
|
2011-05-24 23:15:21 +02:00
|
|
|
connections.add(view.getNewRoundEvent().add(new IListener() {
|
2011-05-10 03:13:11 +02:00
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
startRound();
|
|
|
|
}
|
2011-05-24 23:15:21 +02:00
|
|
|
}));
|
2011-05-29 20:19:17 +02:00
|
|
|
|
|
|
|
connections.add(view.getNewGameEvent().add(new IListener() {
|
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
endGame();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
connections.add(view.getEndProgramEvent().add(new IListener() {
|
2011-05-10 03:13:11 +02:00
|
|
|
@Override
|
|
|
|
public void handle() {
|
2011-05-29 20:19:17 +02:00
|
|
|
endProgram();
|
2011-05-10 03:13:11 +02:00
|
|
|
}
|
2011-05-24 23:15:21 +02:00
|
|
|
}));
|
2011-05-10 03:13:11 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 20:36:26 +02:00
|
|
|
/**
|
|
|
|
* Is emitted when the user ends the game and start a new one
|
|
|
|
*
|
|
|
|
* @return the endOfGameEvent
|
|
|
|
*/
|
2011-05-29 20:19:17 +02:00
|
|
|
public IEvent getEndOfGameEvent() {
|
|
|
|
return endOfGameEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void endGame() {
|
|
|
|
for (Connection c : connections) {
|
|
|
|
c.remove();
|
|
|
|
}
|
|
|
|
endOfGameEvent.emit();
|
|
|
|
}
|
|
|
|
|
2011-05-10 04:05:52 +02:00
|
|
|
/**
|
|
|
|
* Game gets started by initializing the first Round
|
|
|
|
*/
|
2011-05-10 03:13:11 +02:00
|
|
|
public void startGame() {
|
|
|
|
startRound();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startRound() {
|
|
|
|
if (roundControl != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-29 15:50:30 +02:00
|
|
|
view.showScorePanel(false);
|
|
|
|
|
2011-05-24 01:51:53 +02:00
|
|
|
IRoundState roundState = new RoundState(gameSettings);
|
2011-05-10 03:13:11 +02:00
|
|
|
|
2011-05-29 15:50:30 +02:00
|
|
|
roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer()
|
|
|
|
+ gameState.getScores().size());
|
|
|
|
|
2011-05-18 17:01:11 +02:00
|
|
|
roundControl = new RoundControl(roundState, view);
|
2011-05-25 15:51:34 +02:00
|
|
|
roundControl.getEndOfRoundEvent().add(new IListener1<Score>() {
|
2011-05-10 03:13:11 +02:00
|
|
|
|
|
|
|
@Override
|
2011-05-25 15:51:34 +02:00
|
|
|
public void handle(Score roundScore) {
|
2011-05-29 15:50:30 +02:00
|
|
|
endOfRound(roundScore);
|
2011-05-10 03:13:11 +02:00
|
|
|
}
|
|
|
|
});
|
2011-05-29 15:50:30 +02:00
|
|
|
|
2011-05-27 17:54:46 +02:00
|
|
|
roundControl.getRestartRoundEvent().add(new IListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle() {
|
|
|
|
restartRound();
|
|
|
|
}
|
|
|
|
});
|
2011-05-10 03:13:11 +02:00
|
|
|
|
|
|
|
roundControl.startRound();
|
|
|
|
}
|
2011-05-29 15:50:30 +02:00
|
|
|
|
2011-05-27 17:54:46 +02:00
|
|
|
private void restartRound() {
|
|
|
|
roundControl = null;
|
|
|
|
startRound();
|
|
|
|
}
|
|
|
|
|
2011-05-29 15:50:30 +02:00
|
|
|
private void endOfRound(Score roundScore) {
|
|
|
|
gameState.getScores().add(roundScore);
|
|
|
|
|
2011-05-24 23:15:21 +02:00
|
|
|
roundControl = null;
|
|
|
|
view.enableWinPanel(true);
|
2011-05-29 15:50:30 +02:00
|
|
|
|
|
|
|
view.getScorePanel().setPlayers(gameSettings.getPlayerList());
|
|
|
|
view.getScorePanel().setScores(gameState.getScores());
|
2011-05-29 20:19:17 +02:00
|
|
|
view.getScorePanel().setAccumulatedScore(
|
|
|
|
gameState.getAccumulatedScore());
|
2011-05-29 15:50:30 +02:00
|
|
|
view.getScorePanel().update();
|
|
|
|
view.showScorePanel(true);
|
2011-05-24 23:15:21 +02:00
|
|
|
}
|
2011-05-10 03:13:11 +02:00
|
|
|
|
2011-05-29 20:19:17 +02:00
|
|
|
private void endProgram() {
|
2011-05-10 03:13:11 +02:00
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|