From fec9f2512b73bd1497870afd3eda6f62205e1664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sas=CC=8Ca=20Kocic=CC=81?= Date: Thu, 18 Jun 2020 20:11:13 +0200 Subject: [PATCH] No winner implemented --- src/CardGame.java | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/CardGame.java b/src/CardGame.java index b5d8943..51f1aec 100644 --- a/src/CardGame.java +++ b/src/CardGame.java @@ -2,7 +2,7 @@ import java.util.ArrayList; public class CardGame { public Player[] players; - Pile pot; + Pile pot = new Pile(); Card[] draws; /** @@ -18,7 +18,6 @@ public class CardGame { for (int i = 0; i < numberOfPlayers; i++) { players[i] = new Player(String.valueOf(i), deck.getCardsForPlayer(cardsPerPlayer)); } - pot = new Pile(); } public Pile createDeck(int numberOfCards) { @@ -54,13 +53,24 @@ public class CardGame { if (max.number < drawn[i].number) { maxIndex = i; max = drawn[maxIndex]; - } else if (max.number == drawn[i].number) { - // what if there is no winner? } } - System.out.printf("Player %d wins this round\n\n", maxIndex); - players[maxIndex].discardPile.addAll(pot); - pot.clear(); + if (uniqueMaximum(drawn, max, maxIndex)) { + System.out.printf("Player %d wins this round\n\n", maxIndex); + players[maxIndex].discardPile.addAll(pot); + pot.clear(); + } else { + System.out.printf("No winner in this round\n\n"); + } + } + + private boolean uniqueMaximum(Card[] draws, Card max, int maxIndex) { + for (int i = 0; i < draws.length; i++) { + if (maxIndex != i && max.number == draws[i].number) { + return false; + } + } + return true; } private boolean noWinner() {