9
9
import java .util .stream .IntStream ;
10
10
11
11
import core .Globals .Colour ;
12
+ import core .Globals .JokerRules ;
12
13
13
14
public class Meld {
14
15
public enum MeldType {
@@ -60,7 +61,7 @@ public Tile addTile(Tile tile) {
60
61
}
61
62
62
63
public Tile removeTile (int index ) {
63
- if (this .isLocked ) { return null ; }
64
+ if (! this . isInitialMeld && this .isLocked ) { return null ; }
64
65
if (index < 0 || index >= this .meld .size ()) {
65
66
return null ;
66
67
}
@@ -97,7 +98,7 @@ public Tile removeTileObject(Tile tile) {
97
98
}
98
99
99
100
public Meld splitMeld (int index ) {
100
- if (this .isLocked || index <= 0 || index >= this .meld .size ()) { return null ; }
101
+ if ((! this .isInitialMeld && this . isLocked ) || index <= 0 || index >= this .meld .size ()) { return null ; }
101
102
102
103
Meld newMeld = new Meld ();
103
104
ArrayList <Tile > secondHalf = new ArrayList <>();
@@ -133,7 +134,13 @@ public Tile addTile(ArrayList<Tile> tiles) {
133
134
if (tiles .size () > 1 && tiles .removeIf (t -> t .isJoker ())) { return null ; }
134
135
135
136
// Disallow two jokers in one meld
136
- if (this .containsJoker () && tiles .get (0 ).isJoker ()) { return null ; }
137
+ if (this .containsJoker () && tiles .get (0 ).isJoker ()) { return null ; }
138
+
139
+ // Disallow adding a tile to a full meld with a joker if Lenient Jokers are allowed
140
+ if (Globals .getJokerRules () == JokerRules .LENIENT ) {
141
+ if (this .meld .size () == 13 && this .meldType == MeldType .RUN ) { return null ; }
142
+ if (this .meld .size () == 4 && this .meldType == MeldType .SET ) { return null ; }
143
+ }
137
144
138
145
// Check if the tile being added is part of an initial meld (so the joker inside is not replaced by it right away)
139
146
boolean meldIsFromHand = this .meld .stream ().allMatch (t -> t .isOnTable () == false );
@@ -153,6 +160,10 @@ public Tile addTile(ArrayList<Tile> tiles) {
153
160
if (this .meld .size () == 4 && this .meldType == MeldType .SET ) { return null ; }
154
161
155
162
if (tiles .get (0 ).isJoker ()) {
163
+ if (Globals .getJokerRules () == JokerRules .NO_EXISTING_MELDS && this .isInitialMeld == false ) {
164
+ return null ;
165
+ }
166
+
156
167
tempMeld .addAll (this .meld );
157
168
Tile joker = this .determineJokerType (tiles .get (0 ), tempMeld );
158
169
tempMeld .add (joker );
@@ -177,25 +188,26 @@ public Tile addTile(ArrayList<Tile> tiles) {
177
188
178
189
// Adding tile to a meld with a joker and one or more tiles
179
190
if (this .meld .size () > 0 ) {
180
- // Check if the joker can be replaced as long as its not part of an initial move
181
- if (!this .isInitialMeld && joker .jokerEquals (tile )) {
182
- // If this tile is on the table but is being added to a meld that is locked
183
- if (tile .onTable && this .isLocked ) {
184
- // Add the joker back
185
- joker = this .determineJokerType (joker , tempMeld );
186
- this .meld .add (joker );
187
- this .buildMeld (this .meld , releasedJoker );
188
- return null ;
189
- }
190
- // Otherwise this meld is not locked or the tile being added is from the hand
191
- // In both cases, replace the joker
192
- releasedJoker = jokers .remove (0 );
193
- tempMeld .add (tile );
194
- tempMeld .addAll (this .meld );
195
- return this .buildMeld (tempMeld , releasedJoker );
191
+ if (Globals .getJokerRules () != JokerRules .LENIENT ) {
192
+ // Check if the joker can be replaced as long as its not part of an initial move
193
+ if (!this .isInitialMeld && joker .jokerEquals (tile )) {
194
+ // If this tile is on the table but is being added to a meld that is locked
195
+ if (tile .onTable && this .isLocked ) {
196
+ // Add the joker back
197
+ joker = this .determineJokerType (joker , tempMeld );
198
+ this .meld .add (joker );
199
+ this .buildMeld (this .meld , releasedJoker );
200
+ return null ;
201
+ }
202
+ // Otherwise this meld is not locked or the tile being added is from the hand
203
+ // In both cases, replace the joker
204
+ releasedJoker = jokers .remove (0 );
205
+ tempMeld .add (tile );
206
+ tempMeld .addAll (this .meld );
207
+ return this .buildMeld (tempMeld , releasedJoker );
208
+ }
196
209
}
197
210
198
-
199
211
// Otherwise the joker can't be replaced. Check if the tile can still be added to the meld
200
212
tempMeld .addAll (this .meld );
201
213
tempMeld .add (tile );
@@ -234,8 +246,10 @@ private Tile buildMeld(ArrayList<Tile> tempMeld, Tile releasedJoker) {
234
246
if (tempMeldType != MeldType .INVALID && tempMeld .size () < 3 || tempMeldType == MeldType .RUN || tempMeldType == MeldType .SET ) {
235
247
Collections .sort (tempMeld , Comparator .comparingInt (Tile ::getValue )); // Sort numerically
236
248
237
- // Lock the meld if a joker from the hand was added
238
- this .isLocked = this .determineLockedMeld (tempMeld );
249
+ if (Globals .getJokerRules () != JokerRules .LENIENT ) {
250
+ // Lock the meld if a joker from the hand was added
251
+ this .isLocked = this .determineLockedMeld (tempMeld );
252
+ }
239
253
240
254
this .meld = tempMeld ;
241
255
this .meldType = tempMeldType ;
@@ -410,28 +424,34 @@ private MeldType determineMeldType(ArrayList<Tile> tiles) {
410
424
411
425
// Locked meld = meld with a joker that has not been replaced yet
412
426
private boolean determineLockedMeld (ArrayList <Tile > meld ) {
413
- for (Tile tile : meld ) {
414
- if (tile .isJoker () && tile .isReplaced () == false ) {
415
- return true ;
416
- }
427
+ if (Globals .getJokerRules () != JokerRules .LENIENT ) {
428
+ for (Tile tile : meld ) {
429
+ if (tile .isJoker () && tile .isReplaced () == false ) {
430
+ return true ;
431
+ }
432
+ }
417
433
}
418
434
return false ;
419
435
}
420
436
421
437
public boolean isLocked () {
422
- // This meld is locked if it has a joker which has not been replaced by a tile from the hand
423
- return this .meld .stream ().anyMatch (t -> t .isJoker () && !t .isReplaced ());
438
+ if (Globals .getJokerRules () != JokerRules .LENIENT ) {
439
+ // This meld is locked if it has a joker which has not been replaced by a tile from the hand
440
+ return this .meld .stream ().anyMatch (t -> t .isJoker () && !t .isReplaced ());
441
+ }
442
+ return false ;
424
443
}
425
444
426
445
public void setIsLocked (boolean isLocked ) {
427
- // Lock this meld manually by looking for a joker and setting isReplaced
428
- for (Tile tile : this .meld ) {
429
- if (tile .isJoker ()) {
430
- tile .setIsReplaced (!isLocked );
431
- this .isLocked = isLocked ;
432
- }
446
+ if (Globals .getJokerRules () != JokerRules .LENIENT ) {
447
+ // Lock this meld manually by looking for a joker and setting isReplaced
448
+ for (Tile tile : this .meld ) {
449
+ if (tile .isJoker ()) {
450
+ tile .setIsReplaced (!isLocked );
451
+ this .isLocked = isLocked ;
452
+ }
453
+ }
433
454
}
434
-
435
455
}
436
456
437
457
public boolean isInitialMeld () {
0 commit comments