librejack

Blackjack clone written in java
git clone https://github.com/michaelneuper/librejack.git
README | Files | Log | Refs | LICENSE

commit d617630d679c3c1f4bc6fe45b975e428e1044cda
parent 5fcb268793497766018ab9f561426b547101c1b1
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date:   Sat,  8 Oct 2022 15:10:14 +0200

Merge pull request #9 from michaelneuper/cleanup1

Cleanup1
Diffstat:
Msrc/controller/Controller.java | 53+++++++++++++++++++++++++++--------------------------
Msrc/model/Player.java | 2+-
2 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/src/controller/Controller.java b/src/controller/Controller.java @@ -49,7 +49,7 @@ public class Controller { public void startRound() { // if we run out of cards -// if(wins > 0 || losses > 0 || pushes > 0) { + if(wins > 0 || losses > 0 || pushes > 0) { // TODO: change code :) System.out.println("\nStarting next round...\nWins: " + wins + "\nLosses: " + losses + "\nPushes: " + pushes + "\nBalance: " + balance); dealer.getHand().discardHandToDeck(discarded); player.getHand().discardHandToDeck(discarded); @@ -58,27 +58,16 @@ public class Controller { if(deck.cardsLeft() < 4) { deck.reloadDeckFromDiscard(discarded); } -// } - - // ask the player to bet - // Scanner inBet = new Scanner(System.in); - // try { - // // System.out.println("Enter a bet amount: "); - // // bet = Double.parseDouble(inBet.next()); // TODO: add parseDouble - // // inBet.close(); - - // // in case someone bets an amount bigger than their balance - // if(bet > balance) { - // System.out.println("You don't have that much money, try again:"); - // Scanner newBet = new Scanner(System.in); - // bet = newBet.nextDouble(); - // newBet.close(); - // } - // } catch (NumberFormatException e) { - // System.out.println(e); - // } + } + + // ask the player what their bet is bet = Double.parseDouble(JOptionPane.showInputDialog("Enter a bet amount")); + if(bet > balance) { // in case the player tries to bet more than they have + System.out.println("You don't have that much money! Please try again"); + bet = Double.parseDouble(JOptionPane.showInputDialog("Enter a bet amount")); + } + // give the dealer 2 cards dealer.getHand().takeCardFromDeck(deck); dealer.getHand().takeCardFromDeck(deck); @@ -121,7 +110,15 @@ public class Controller { player.makeDecision(deck, discarded); - // check whether they busted + checkWhoWins(); + + // start a new round + startRound(); + } + + private void checkWhoWins() { + + // check whether player busted if(player.getHand().calculateValue() > 21) { System.out.println("Busted"); balance -= bet; @@ -139,8 +136,15 @@ public class Controller { if(dealer.getHand().calculateValue() > 21) { System.out.println("Dealer busts"); balance += bet; - ++wins; - } else if(dealer.getHand().calculateValue() > player.getHand().calculateValue()) { + } + + if(dealer.getHand().calculateValue() > player.getHand().calculateValue() && dealer.getHand().calculateValue() <= 21) { + System.out.println("You lose"); + balance -= bet; + ++losses; + } + + if(dealer.getHand().calculateValue() > player.getHand().calculateValue()) { System.out.println("You lose"); balance -= bet; ++losses; @@ -156,9 +160,6 @@ public class Controller { System.out.println("Push"); ++pushes; } - - // start a new round - startRound(); } } diff --git a/src/model/Player.java b/src/model/Player.java @@ -72,7 +72,7 @@ public class Player extends Person { } // if they decide to hit - if (decision == 1) { + if (decision == 1) { // TODO: add option to double this.hit(deck, discard); // hit the deck and discard