aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam NAILI2018-01-14 00:13:01 +0100
committerAdam NAILI2018-01-14 00:13:01 +0100
commit88eb9363202ad415b2c49b6ecd57bb3d49900739 (patch)
tree8f5b4c9f1e6fee6ef2faa7f05daeae28a502cb86 /src
parentb25ae7d212fb5c802421a518866e43f8194b2868 (diff)
downloadwallj-88eb9363202ad415b2c49b6ecd57bb3d49900739.tar.gz
Moving Stage inside Game, and implementing methods related to this new management or not
Diffstat (limited to 'src')
-rw-r--r--src/main/java/fr/umlv/java/wallj/context/Game.java66
1 files changed, 42 insertions, 24 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/context/Game.java b/src/main/java/fr/umlv/java/wallj/context/Game.java
index 7ddac02..c7dd6f1 100644
--- a/src/main/java/fr/umlv/java/wallj/context/Game.java
+++ b/src/main/java/fr/umlv/java/wallj/context/Game.java
@@ -1,66 +1,84 @@
1package fr.umlv.java.wallj.context; 1package fr.umlv.java.wallj.context;
2 2
3import fr.umlv.java.wallj.board.Board; 3import fr.umlv.java.wallj.board.Board;
4import fr.umlv.java.wallj.controller.Controller;
4import fr.umlv.java.wallj.controller.GameController; 5import fr.umlv.java.wallj.controller.GameController;
6import fr.umlv.java.wallj.event.Event;
7import fr.umlv.java.wallj.model.Stage;
5 8
6import java.util.Collections; 9import java.util.*;
7import java.util.List;
8import java.util.NoSuchElementException;
9import java.util.Objects;
10 10
11/** 11/**
12 * A game. 12 * A game.
13 *
13 * @author Adam NAILI 14 * @author Adam NAILI
14 */ 15 */
15public final class Game { 16public final class Game {
16 //TODO 17 private Stage currentStage;
17 private final GameController gameController; 18 private final List<Controller> controllers;
18 private int indexBoard; 19 private int indexBoard;
19 private final List<Board> boards; 20 private final List<Board> boards;
20 21
21 /** 22 /**
22 * @param gameController the controller of the game 23 * @param boards the list of boards charged for the game
23 * @param boards the list of boards charged for the game
24 */ 24 */
25 public Game(GameController gameController, List<Board> boards) { 25 public Game(List<Board> boards) {
26 this.gameController = Objects.requireNonNull(gameController); 26 this.controllers = new LinkedList<>();
27 this.controllers.add(new GameController());
27 Objects.requireNonNull(boards); 28 Objects.requireNonNull(boards);
28 if (boards.isEmpty()) { 29 if (boards.isEmpty()) {
29 throw new IllegalArgumentException("The list of boards is empty, not able to create a correct game from this."); 30 throw new IllegalArgumentException("The list of boards is empty, not able to create a correct game from this.");
30 } 31 }
31 this.boards = Collections.unmodifiableList(boards); 32 this.boards = Collections.unmodifiableList(boards);
32 this.indexBoard = 0; 33 this.indexBoard = 0;
34 this.currentStage = new Stage(this.boards.get(0));
33 } 35 }
34 36
35 /** 37 /**
36 * @return the game controller 38 * @return a boolean on the condition of having a next Board in our game.
37 */ 39 */
38 public GameController getGameController() { 40 public boolean hasNextBoard() {
39 return gameController; 41 return indexBoard + 1 < boards.size();
40 } 42 }
41 43
42 /** 44 /**
43 * @return the current board of the game. 45 * @return the next board
44 */ 46 */
45 public Board getCurrentBoard() { 47 private Board nextBoard() {
48 if (!hasNextBoard()) {
49 throw new IllegalStateException("No more board for the game.");
50 }
51 indexBoard++;
46 return boards.get(indexBoard); 52 return boards.get(indexBoard);
47 } 53 }
48 54
49 /** 55 /**
50 * @return a boolean on the condition of having a next Board in our game. 56 * @return the current stage
51 */ 57 */
52 public boolean hasNextBoard() { 58 public Stage getCurrentStage() {
53 return indexBoard + 1 < boards.size(); 59 return currentStage;
60 }
61
62 public void nextStage() {
63 if (hasNextBoard()) {
64 currentStage = new Stage(nextBoard());
65 }
66 }
67
68 public void retryStage() {
69 currentStage = new Stage(currentStage.getCurrentBoard());
54 } 70 }
55 71
56 /** 72 /**
57 * @return the next board 73 * @param context the current context
74 * @return a list of new events
58 */ 75 */
59 public Board nextBoard() { 76 public List<Event> update(Context context) {
60 if (indexBoard >= boards.size()) { 77 LinkedList<Event> events = new LinkedList<>();
61 throw new NoSuchElementException("No more board for the game."); 78 for (Controller controller : controllers) {
79 events.addAll(controller.update(context));
62 } 80 }
63 indexBoard++; 81 events.addAll(currentStage.update(context));
64 return boards.get(indexBoard); 82 return events;
65 } 83 }
66} 84}