1
1
package ch .epfl .chacun ;
2
2
3
+ import ch .epfl .chacun .audio .SoundManager ;
4
+
3
5
import java .util .*;
4
6
import java .util .stream .Collectors ;
5
7
import java .util .stream .Stream ;
13
15
* @param board the board of the game, where the tiles can be placed
14
16
* @param nextAction the next action to be performed
15
17
* @param messageBoard the message board of the game, containing the messages generated during the game
18
+ * @param nextSound the sound to play when the game state is updated
16
19
* @author Valerio De Santis (373247)
17
20
* @author Simon Lefort (371918)
18
21
*/
@@ -22,7 +25,8 @@ public record GameState(
22
25
Tile tileToPlace ,
23
26
Board board ,
24
27
Action nextAction ,
25
- MessageBoard messageBoard
28
+ MessageBoard messageBoard ,
29
+ SoundManager .Sound nextSound
26
30
) {
27
31
28
32
/**
@@ -52,9 +56,11 @@ public enum Action {
52
56
* @param nextAction the next action to be performed, must not be null
53
57
* @param messageBoard the message board of the game,
54
58
* containing the messages generated during the game, must not be null
59
+ * @param nextSound the sound to play when the game state is updated, can be null
55
60
* @throws NullPointerException if any of the arguments is null, except tileToPlace
56
61
* @throws IllegalArgumentException if there are not enough players or if the tileToPlace is null and
57
- * the next action is to place a tile, or if the tileToPlace is not null and the next action is not to place a tile.
62
+ * the next action is to place a tile, or if the tileToPlace is not null and the
63
+ * next action is not to place a tile.
58
64
*/
59
65
public GameState {
60
66
@@ -65,11 +71,36 @@ public enum Action {
65
71
66
72
Objects .requireNonNull (players );
67
73
players = List .copyOf (players );
68
- Preconditions .checkArgument (players .size () >= MIN_PLAYER_COUNT );
69
74
70
75
Preconditions .checkArgument (tileToPlace == null ^ nextAction == Action .PLACE_TILE );
71
76
}
72
77
78
+ /**
79
+ * Constructs a new game state, with no tile to place, an empty board, and the start game action.
80
+ *
81
+ * @param players the ordered list of the game players, must contain at least 2 players and not be null
82
+ * @param tileDecks the tile decks of the game, containing the cards to place, must not be null
83
+ * @param tileToPlace the tile to place on the board, may be null if no tile is to be placed
84
+ * @param board the board of the game, where the tiles can be placed, must not be null
85
+ * @param nextAction the next action to be performed, must not be null
86
+ * @param messageBoard the message board of the game,
87
+ * containing the messages generated during the game, must not be null
88
+ * @throws NullPointerException if any of the arguments is null, except tileToPlace
89
+ * @throws IllegalArgumentException if there are not enough players or if the tileToPlace is null and
90
+ * the next action is to place a tile, or if the tileToPlace is not null and the
91
+ * next action is not to place a tile.
92
+ */
93
+ public GameState (
94
+ List <PlayerColor > players ,
95
+ TileDecks tileDecks ,
96
+ Tile tileToPlace ,
97
+ Board board ,
98
+ Action nextAction ,
99
+ MessageBoard messageBoard
100
+ ) {
101
+ this (players , tileDecks , tileToPlace , board , nextAction , messageBoard , SoundManager .Sound .SILENT );
102
+ }
103
+
73
104
/**
74
105
* Constructs the initial game state, with no tile to place, an empty board, and the start game action.
75
106
*
@@ -301,8 +332,10 @@ public GameState withPlacedTile(PlacedTile tile) {
301
332
}
302
333
}
303
334
304
- return new GameState (players , tileDecks , null , newBoard , Action .OCCUPY_TILE , newMessageBoard )
305
- .withTurnFinishedIfOccupationImpossible ();
335
+ return new GameState (
336
+ players , tileDecks , null , newBoard , Action .OCCUPY_TILE ,
337
+ newMessageBoard , SoundManager .Sound .PLACED_TILE
338
+ ).withTurnFinishedIfOccupationImpossible ();
306
339
}
307
340
308
341
/**
@@ -334,6 +367,8 @@ private GameState withTurnFinished() {
334
367
Preconditions .checkArgument (board .lastPlacedTile () != null );
335
368
336
369
MessageBoard newMessageBoard = messageBoard ;
370
+ Map <PlayerColor , Integer > currentPoints = messageBoard .points ();
371
+
337
372
// the tile has already been added to the board previously
338
373
Board newBoard = board ;
339
374
TileDecks newTileDecks = tileDecks ;
@@ -376,9 +411,13 @@ private GameState withTurnFinished() {
376
411
if (newTileDecks .deckSize (Tile .Kind .NORMAL ) > 0 ) {
377
412
List <PlayerColor > newPlayers = new LinkedList <>(players );
378
413
Collections .rotate (newPlayers , -1 );
414
+
415
+ SoundManager .Sound nextSound = newMessageBoard .points ().equals (currentPoints )
416
+ ? nextSound () : SoundManager .Sound .GAINED_POINTS ;
417
+
379
418
return new GameState (newPlayers , newTileDecks .withTopTileDrawn (Tile .Kind .NORMAL ),
380
419
newTileDecks .topTile (Tile .Kind .NORMAL ),
381
- newBoard , Action .PLACE_TILE , newMessageBoard
420
+ newBoard , Action .PLACE_TILE , newMessageBoard , nextSound
382
421
);
383
422
} else {
384
423
return new GameState (players , newTileDecks , null , newBoard , Action .END_GAME , newMessageBoard )
@@ -444,4 +483,34 @@ private GameState withFinalPointsCounted() {
444
483
return new GameState (players , tileDecks , null , newBoard , Action .END_GAME , newMessageBoard );
445
484
}
446
485
486
+ /**
487
+ * Returns a new game state with the given players list as new players
488
+ *
489
+ * @param players the new players list
490
+ * @return a new game state with the given players list as new players
491
+ */
492
+ public GameState withPlayers (List <PlayerColor > players ) {
493
+ return new GameState (players , tileDecks , tileToPlace , board , nextAction , messageBoard );
494
+ }
495
+
496
+ /**
497
+ * Returns a new game state with the given chat message added to the message board
498
+ *
499
+ * @param message the chat message to add
500
+ * @return a new game state with the given chat message added to the message board
501
+ */
502
+ public GameState withGameChatMessage (String message ) {
503
+ return new GameState (players , tileDecks , tileToPlace , board , nextAction , messageBoard .withGameChatMessage (message ));
504
+ }
505
+
506
+ /**
507
+ * Returns a new game state with the given chat message added to the message board
508
+ *
509
+ * @param textMaker the text maker to generate the message
510
+ * @return a new game state with the given chat message added to the message board
511
+ */
512
+ public GameState withTextMaker (TextMaker textMaker ) {
513
+ return new GameState (players , tileDecks , tileToPlace , board , nextAction , messageBoard .withTextMaker (textMaker ));
514
+ }
515
+
447
516
}
0 commit comments