commit 19d99e992247664af2529a93921e37671723d93a
parent 7e3c4610bd8811b6e4233c5407795d6cb072851a
Author: Michael Neuper <michael@michaelneuper.com>
Date: Sat, 29 Apr 2023 22:26:50 +0200
Replace nested if-statement with guard clause
Diffstat:
1 file changed, 72 insertions(+), 72 deletions(-)
diff --git a/src/controller/Controller.java b/src/controller/Controller.java
@@ -106,7 +106,7 @@ public class Controller {
* to the player.</p>
*/
public void startRound() {
-
+
hasPlayed = false;
// if we run out of cards
@@ -135,90 +135,90 @@ public class Controller {
* @return a message indicating the result of the game
*/
public String checkWhoWins() {
- if (!hasPlayed) {
- // check wether player has enough money
- if (bet > PLAYER.getBalance()) {
- JOptionPane.showMessageDialog(null, "You don't have enough money");
- return null;
- }
+ if (hasPlayed) {
+ JOptionPane.showMessageDialog(null, "Press deal before you can hit or stand again");
+ return "Press Deal";
+ }
- // check if dealer has blackjack
- if (DEALER.hasBlackjack()) {
- DEALER.printHand(); // show full hand
- hasPlayed = true;
-
- if (PLAYER.hasBlackjack()) {
- STATS.incrementPushes();
- return "You both have 21 - Push";
- } else {
- balance -= bet;
- PLAYER.setBalance(balance);
- STATS.incrementLosses();
- DEALER.printHand();
- return "Dealer has BlackJack - You lose";
- }
- }
+ // check whether player has enough money
+ if (bet > PLAYER.getBalance()) {
+ JOptionPane.showMessageDialog(null, "You don't have enough money");
+ return null;
+ }
- // check if player has blackjack
- if (PLAYER.hasBlackjack()) {
- balance += bet * 1.5;
- PLAYER.setBalance(balance);
- STATS.incrementWins();
- hasPlayed = true;
- return "You have BlackJack - You win";
- }
+ // check if dealer has blackjack
+ if (DEALER.hasBlackjack()) {
+ DEALER.printHand(); // show full hand
+ hasPlayed = true;
- // check whether player busted
- if (PLAYER.getHand().calculateValue() > 21) {
+ if (PLAYER.hasBlackjack()) {
+ STATS.incrementPushes();
+ return "You both have 21 - Push";
+ } else {
balance -= bet;
PLAYER.setBalance(balance);
STATS.incrementLosses();
- hasPlayed = true;
- return "Busted";
+ DEALER.printHand();
+ return "Dealer has BlackJack - You lose";
}
+ }
- // dealer's turn
- DEALER.printHand();
- while (DEALER.getHand().calculateValue() < 17) {
- DEALER.hit(DECK, DISCARDED);
- }
+ // check if player has blackjack
+ if (PLAYER.hasBlackjack()) {
+ balance += bet * 1.5;
+ PLAYER.setBalance(balance);
+ STATS.incrementWins();
+ hasPlayed = true;
+ return "You have BlackJack - You win";
+ }
- // check who wins
- if (DEALER.getHand().calculateValue() > 21) {
- balance += bet;
- PLAYER.setBalance(balance);
- STATS.incrementWins();
- hasPlayed = true;
- return "Dealer busts - You win";
- }
+ // check whether player busted
+ if (PLAYER.getHand().calculateValue() > 21) {
+ balance -= bet;
+ PLAYER.setBalance(balance);
+ STATS.incrementLosses();
+ hasPlayed = true;
+ return "Busted";
+ }
- if (DEALER.getHand().calculateValue() > PLAYER.getHand().calculateValue()
- && DEALER.getHand().calculateValue() <= 21) {
- balance -= bet;
- PLAYER.setBalance(balance);
- STATS.incrementLosses();
- hasPlayed = true;
- return "You lose";
- }
+ // dealer's turn
+ DEALER.printHand();
+ while (DEALER.getHand().calculateValue() < 17) {
+ DEALER.hit(DECK, DISCARDED);
+ }
- if (PLAYER.getHand().calculateValue() > DEALER.getHand().calculateValue()) {
- balance += bet;
- PLAYER.setBalance(balance);
- STATS.incrementWins();
- hasPlayed = true;
- return "You win";
- }
+ // check who wins
+ if (DEALER.getHand().calculateValue() > 21) {
+ balance += bet;
+ PLAYER.setBalance(balance);
+ STATS.incrementWins();
+ hasPlayed = true;
+ return "Dealer busts - You win";
+ }
- if (PLAYER.getHand().calculateValue() == DEALER.getHand().calculateValue()) {
- STATS.incrementPushes();
- hasPlayed = true;
- return "Push";
- }
- } else {
- JOptionPane.showMessageDialog(null, "Press deal before you can hit or stand again");
- return "You already played";
+ if (DEALER.getHand().calculateValue() > PLAYER.getHand().calculateValue()
+ && DEALER.getHand().calculateValue() <= 21) {
+ balance -= bet;
+ PLAYER.setBalance(balance);
+ STATS.incrementLosses();
+ hasPlayed = true;
+ return "You lose";
+ }
+
+ if (PLAYER.getHand().calculateValue() > DEALER.getHand().calculateValue()) {
+ balance += bet;
+ PLAYER.setBalance(balance);
+ STATS.incrementWins();
+ hasPlayed = true;
+ return "You win";
+ }
+
+ if (PLAYER.getHand().calculateValue() == DEALER.getHand().calculateValue()) {
+ STATS.incrementPushes();
+ hasPlayed = true;
+ return "Push";
}
-
+
return null;
}