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-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-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
|
|
|
|
* the game settings
|
2011-05-10 05:53:30 +02:00
|
|
|
* @param view
|
|
|
|
* 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-24 01:51:54 +02:00
|
|
|
|
|
|
|
gameState = new GameState();
|
|
|
|
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
|
|
|
}));
|
|
|
|
connections.add(view.getFinalScoreEvent().add(new IListener() {
|
2011-05-10 03:13:11 +02:00
|
|
|
@Override
|
|
|
|
public void handle() {
|
2011-05-24 23:15:21 +02:00
|
|
|
finalScore();
|
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-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-24 01:51:53 +02:00
|
|
|
IRoundState roundState = new RoundState(gameSettings);
|
2011-05-10 03:13:11 +02:00
|
|
|
|
2011-05-24 01:51:54 +02:00
|
|
|
// TODO: add number of already played rounds
|
|
|
|
roundState.setActivePlayerNumber(gameState.getFirstRoundFirstPlayer());
|
|
|
|
|
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-24 23:15:21 +02:00
|
|
|
endOfRound();
|
2011-05-10 03:13:11 +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-24 23:15:21 +02:00
|
|
|
|
2011-05-27 17:54:46 +02:00
|
|
|
private void restartRound() {
|
|
|
|
roundControl = null;
|
|
|
|
startRound();
|
|
|
|
}
|
|
|
|
|
2011-05-24 23:15:21 +02:00
|
|
|
private void endOfRound() {
|
|
|
|
roundControl = null;
|
|
|
|
view.enableWinPanel(true);
|
|
|
|
}
|
2011-05-10 03:13:11 +02:00
|
|
|
|
2011-05-24 23:15:21 +02:00
|
|
|
private void finalScore() {
|
2011-05-10 03:13:11 +02:00
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|