14
14
import java .util .ArrayList ;
15
15
import java .util .List ;
16
16
import java .util .Map ;
17
+ import java .util .function .Function ;
17
18
18
19
/**
19
20
* This class represents the graphical representation
@@ -43,7 +44,10 @@ private PlayersUI() {
43
44
* @return a graphical node containing the occupants of the game with their names, their colours,
44
45
* their points and available occupants, with the current player highlighted
45
46
*/
46
- public static Node create (ObservableValue <GameState > gameStateO , TextMaker textMaker ) {
47
+ public static Node create (
48
+ ObservableValue <GameState > gameStateO ,
49
+ TextMaker textMaker
50
+ ) {
47
51
48
52
ObservableValue <Map <PlayerColor , Integer >> pointsO = gameStateO .map (gState -> gState .messageBoard ().points ());
49
53
@@ -52,45 +56,57 @@ public static Node create(ObservableValue<GameState> gameStateO, TextMaker textM
52
56
vBox .getStylesheets ().add ("players.css" );
53
57
vBox .setId ("players" );
54
58
55
- // for each player color, create a text flow
56
- PlayerColor .ALL .forEach (playerColor -> {
57
- String name = textMaker .playerName (playerColor );
58
- if (name == null ) return ;
59
+ ObservableValue <List <PlayerColor >> players = gameStateO .map (GameState ::players );
60
+
61
+ Function <List <PlayerColor >, Boolean > addPlayersNodes = (newPlayers ) -> {
62
+ newPlayers
63
+ .stream ()
64
+ .skip (vBox .getChildren ().size ())
65
+ .forEach (playerColor -> {
66
+ String name = textMaker .playerName (playerColor );
67
+
68
+ if (name == null ) return ;
69
+
70
+ TextFlow textFlow = new TextFlow ();
71
+ textFlow .getStyleClass ().add ("player" );
59
72
60
- TextFlow textFlow = new TextFlow ();
61
- textFlow .getStyleClass ().add ("player" );
73
+ // we update here the current player
74
+ ObservableValue <Boolean > isCurrentPlayer = gameStateO .map (gState -> gState .currentPlayer () == playerColor );
75
+ isCurrentPlayer .addListener ((_ , _ , newValue ) -> {
76
+ if (newValue ) textFlow .getStyleClass ().add ("current" );
77
+ else textFlow .getStyleClass ().remove ("current" );
78
+ });
79
+ // todo check null --> currentplayer should trigger listener
80
+ if (gameStateO .getValue ().currentPlayer () == playerColor )
81
+ textFlow .getStyleClass ().add ("current" );
62
82
63
- // we update here the current player
64
- ObservableValue <Boolean > isCurrentPlayer = gameStateO .map (gState -> gState .currentPlayer () == playerColor );
65
- isCurrentPlayer .addListener ((_ , _ , newValue ) -> {
66
- if (newValue ) textFlow .getStyleClass ().add ("current" );
67
- else textFlow .getStyleClass ().remove ("current" );
68
- });
69
- // todo check null --> currentplayer should trigger listener
70
- if (gameStateO .getValue ().currentPlayer () == playerColor ) textFlow .getStyleClass ().add ("current" );
83
+ Circle circle = new Circle (5 );
84
+ circle .setFill (ColorMap .fillColor (playerColor ));
71
85
72
- Circle circle = new Circle (5 );
73
- circle .setFill (ColorMap .fillColor (playerColor ));
86
+ ObservableValue <String > pointsTextO = pointsO .map (points -> STR ." \{name } : \{textMaker .points (points .getOrDefault (playerColor , 0 ))}\n " );
87
+ ObservableValue <Map <Occupant .Kind , Integer >> occupantsO = gameStateO
88
+ .map (gState -> Map .of (
89
+ Occupant .Kind .PAWN , gState .freeOccupantsCount (playerColor , Occupant .Kind .PAWN ),
90
+ Occupant .Kind .HUT , gState .freeOccupantsCount (playerColor , Occupant .Kind .HUT )
91
+ ));
74
92
75
- ObservableValue <String > pointsTextO = pointsO .map (points -> STR ." \{name } : \{textMaker .points (points .getOrDefault (playerColor , 0 ))}\n " );
76
- ObservableValue <Map <Occupant .Kind , Integer >> occupantsO = gameStateO
77
- .map (gState -> Map .of (
78
- Occupant .Kind .PAWN , gState .freeOccupantsCount (playerColor , Occupant .Kind .PAWN ),
79
- Occupant .Kind .HUT , gState .freeOccupantsCount (playerColor , Occupant .Kind .HUT )
80
- ));
93
+ Text pointsText = new Text ();
94
+ pointsText .textProperty ().bind (pointsTextO );
81
95
82
- Text pointsText = new Text ();
83
- pointsText .textProperty ().bind (pointsTextO );
96
+ textFlow .getChildren ().addAll (circle , pointsText );
84
97
85
- textFlow .getChildren ().addAll (circle , pointsText );
98
+ textFlow .getChildren ().addAll (getOccupants (playerColor , Occupant .Kind .HUT , occupantsO ));
99
+ textFlow .getChildren ().add (new Text (" " ));
100
+ textFlow .getChildren ().addAll (getOccupants (playerColor , Occupant .Kind .PAWN , occupantsO ));
86
101
87
- textFlow .getChildren ().addAll (getOccupants (playerColor , Occupant .Kind .HUT , occupantsO ));
88
- textFlow .getChildren ().add (new Text (" " ));
89
- textFlow .getChildren ().addAll (getOccupants (playerColor , Occupant .Kind .PAWN , occupantsO ));
102
+ vBox .getChildren ().add (textFlow );
90
103
91
- vBox .getChildren ().add (textFlow );
104
+ });
105
+ return true ;
106
+ };
92
107
93
- });
108
+ players .addListener ((_ , _ , newPlayers ) -> addPlayersNodes .apply (newPlayers ));
109
+ addPlayersNodes .apply (players .getValue ());
94
110
95
111
return vBox ;
96
112
}
0 commit comments