1
1
package core ;
2
2
3
3
import java .util .ArrayList ;
4
+ import java .util .HashMap ;
4
5
import java .util .Iterator ;
6
+ import java .util .Map ;
5
7
6
8
import core .Globals .PlayerType ;
7
9
import javafx .collections .FXCollections ;
@@ -16,6 +18,7 @@ public class GameModel {
16
18
17
19
private int numPlayers ;
18
20
private ArrayList <Pair <String , String >> playerData ;
21
+ private Map <Player , Integer > playerScores ;
19
22
20
23
private GameMemento memento ;
21
24
@@ -234,6 +237,10 @@ public void finishHumanMove() {
234
237
this .updateWorkspaceList ();
235
238
this .currentPlayer .updateHandList ();
236
239
this .currentPlayer .notifyObservers ();
240
+
241
+ if (this .currentPlayer .getHandSize () == 0 ) {
242
+ this .winner = this .currentPlayer ;
243
+ }
237
244
}
238
245
239
246
public void playAI () {
@@ -261,6 +268,7 @@ public void playAI() {
261
268
public void draw () {
262
269
if (this .stock .getSize () == 0 ) {
263
270
System .out .println ("[GAME] Stock is empty!" );
271
+ this .gameOver ();
264
272
} else {
265
273
Tile tile = this .stock .draw ();
266
274
System .out .println ("[GAME] " + this .currentPlayer .getName () + " drew " + tile .toString ());
@@ -296,8 +304,44 @@ public String nextPlayer(boolean drew) {
296
304
return this .currentPlayer .getPlayerType ().toString ();
297
305
}
298
306
307
+ public boolean gameOver () {
308
+ if (this .stock .getSize () == 0 && this .winner == null ) {
309
+ this .determineWinner ();
310
+ }
311
+
312
+ if (this .winner != null ) {
313
+ this .determinePlayerScores ();
314
+ System .out .println ("[GAME] " + this .winner .getName () + " won!" );
315
+ System .out .println ("[GAME] Scores:" );
316
+ for (Player player : this .players ) {
317
+ System .out .println ("\t " + player .getName () + ": " + this .playerScores .get (player ));
318
+ }
319
+ return true ;
320
+ }
321
+
322
+ return false ;
323
+ }
324
+
299
325
public void determineWinner () {
300
-
326
+ int lowestHandCount = this .players .get (0 ).getHandSize ();
327
+ winner = this .players .get (0 );
328
+ for (Player player : this .players .subList (1 , this .players .size ())) {
329
+ if (player .getHandSize () < lowestHandCount ) {
330
+ lowestHandCount = player .getHandSize ();
331
+ winner = player ;
332
+ }
333
+ }
334
+ }
335
+
336
+ private void determinePlayerScores () {
337
+ this .playerScores = new HashMap <Player , Integer >();
338
+ this .playerScores .put (this .winner , 0 );
339
+ for (Player player : this .players ) {
340
+ if (player != this .winner ) {
341
+ this .playerScores .put (player , player .getScore () * -1 );
342
+ this .playerScores .put (this .winner , this .playerScores .get (this .winner ) + player .getScore ());
343
+ }
344
+ }
301
345
}
302
346
303
347
public ArrayList <Meld > getWorkspace () {
0 commit comments