Skip to content

Commit c959677

Browse files
committed
[code:GameController:GameModel] Implement winning and scoring
1 parent 109fcf4 commit c959677

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/main/java/core/GameController.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,16 @@ private void finishHumanMove() {
392392
this.timer.cancel();
393393
this.timerLabel.setVisible(false);
394394
}
395-
396395
this.model.finishHumanMove();
397396
this.nextPlayer(false);
398397
}
399398

400399
private void nextPlayer(boolean drew) {
400+
if (this.model.gameOver()) {
401+
this.gameOver();
402+
return;
403+
}
404+
401405
if (this.model.nextPlayer(drew).equals("StrategyHuman")) {
402406
this.nextAIMoveButton.setDisable(true);
403407
this.drawButton.setDisable(false);
@@ -413,6 +417,20 @@ private void nextPlayer(boolean drew) {
413417
this.currentPlayerLabel.setText(this.model.getCurrentPlayer().getName());
414418
}
415419

420+
private void gameOver() {
421+
this.tableListView.setDisable(true);
422+
this.p1HandListView.setDisable(true);
423+
this.p2HandListView.setDisable(true);
424+
this.p3HandListView.setDisable(true);
425+
this.p4HandListView.setDisable(true);
426+
this.drawButton.setDisable(true);
427+
this.finishButton.setDisable(true);
428+
this.nextAIMoveButton.setDisable(true);
429+
}
430+
431+
/*
432+
* Stuff to set and pass to the model.
433+
*/
416434
public void setNumPlayers(int numPlayers) {
417435
this.model.setNumPlayers(numPlayers);
418436
}

src/main/java/core/GameModel.java

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package core;
22

33
import java.util.ArrayList;
4+
import java.util.HashMap;
45
import java.util.Iterator;
6+
import java.util.Map;
57

68
import core.Globals.PlayerType;
79
import javafx.collections.FXCollections;
@@ -16,6 +18,7 @@ public class GameModel {
1618

1719
private int numPlayers;
1820
private ArrayList<Pair<String, String>> playerData;
21+
private Map<Player, Integer> playerScores;
1922

2023
private GameMemento memento;
2124

@@ -234,6 +237,10 @@ public void finishHumanMove() {
234237
this.updateWorkspaceList();
235238
this.currentPlayer.updateHandList();
236239
this.currentPlayer.notifyObservers();
240+
241+
if (this.currentPlayer.getHandSize() == 0) {
242+
this.winner = this.currentPlayer;
243+
}
237244
}
238245

239246
public void playAI() {
@@ -261,6 +268,7 @@ public void playAI() {
261268
public void draw() {
262269
if (this.stock.getSize() == 0) {
263270
System.out.println("[GAME] Stock is empty!");
271+
this.gameOver();
264272
} else {
265273
Tile tile = this.stock.draw();
266274
System.out.println("[GAME] " + this.currentPlayer.getName() + " drew " + tile.toString());
@@ -296,8 +304,44 @@ public String nextPlayer(boolean drew) {
296304
return this.currentPlayer.getPlayerType().toString();
297305
}
298306

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+
299325
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+
}
301345
}
302346

303347
public ArrayList<Meld> getWorkspace() {

0 commit comments

Comments
 (0)