librejack

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

commit e8165afb4330be5a5610898dbcb9ce1c91ddbe68
parent 18e718ff392f7c4be2fa93178f45c6376af18ce7
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date:   Thu, 27 Apr 2023 22:16:24 +0200

Read and write balance to balance.txt

Diffstat:
Msrc/controller/Controller.java | 85+++++++++++++++++++++++++++++++++++++++++--------------------------------------
1 file changed, 44 insertions(+), 41 deletions(-)

diff --git a/src/controller/Controller.java b/src/controller/Controller.java @@ -8,7 +8,7 @@ import model.Statistics; /** * Handles most of the game logic - * + * * @author Michael */ public class Controller { @@ -19,28 +19,25 @@ public class Controller { private Player player; private double bet, balance; private Statistics stats = new Statistics(); - /** - * Controller Constructor: - * Initializes score to 0 + * Controller Constructor: Initializes score to 0 */ public Controller() { + // create new dealer and player + dealer = new Dealer(); + player = new Player(); - balance = 1000.0; + balance = player.getBalance(); bet = 100.0; // create new populated deck - deck = new Deck(); + deck = new Deck(); deck.populate(); deck.shuffle(); // create new empty deck - discarded = new Deck(); - - // create new dealer and player - dealer = new Dealer(); - player = new Player(); + discarded = new Deck(); } public double getBet() { @@ -48,7 +45,7 @@ public class Controller { } public void setBet(double bet) { - if(bet < balance) { + if (bet < balance) { this.bet = bet; } else { // in case they try to make a bet bigger than their balance JOptionPane.showMessageDialog(null, "You don't have that much money"); @@ -65,16 +62,16 @@ public class Controller { /** * Handles logic for each round - * + * */ public void startRound() { // if we run out of cards dealer.getHand().discardHandToDeck(discarded); player.getHand().discardHandToDeck(discarded); - + // make sure deck has at least 4 cards left - if(deck.cardsLeft() < 4) { + if (deck.cardsLeft() < 4) { deck.reloadDeckFromDiscard(discarded); } @@ -85,11 +82,11 @@ public class Controller { // give the player 2 cards player.getHand().takeCardFromDeck(deck); player.getHand().takeCardFromDeck(deck); - + } public String checkWhoWins() { - + // check if dealer has blackjack if (dealer.hasBlackjack()) { dealer.printHand(); // show full hand @@ -99,6 +96,7 @@ public class Controller { return "You both have 21 - Push"; } else { balance -= bet; + player.setBalance(balance); stats.incrementLosses(); dealer.printHand(); return "Dealer has BlackJack - You lose"; @@ -106,59 +104,64 @@ public class Controller { } // check if player has blackjack - if(player.hasBlackjack()) { + if (player.hasBlackjack()) { balance += bet * 1.5; + player.setBalance(balance); stats.incrementWins(); return "You have BlackJack - You win"; } // check whether player busted - if(player.getHand().calculateValue() > 21) { + if (player.getHand().calculateValue() > 21) { balance -= bet; + player.setBalance(balance); stats.incrementLosses(); return "Busted"; } - + // dealer's turn dealer.printHand(); - while(dealer.getHand().calculateValue() < 17) { + while (dealer.getHand().calculateValue() < 17) { dealer.hit(deck, discarded); } - + // check who wins - if(dealer.getHand().calculateValue() > 21) { + if (dealer.getHand().calculateValue() > 21) { balance += bet; + player.setBalance(balance); stats.incrementWins(); return "Dealer busts - You win"; } - - if(dealer.getHand().calculateValue() > player.getHand().calculateValue() && dealer.getHand().calculateValue() <= 21) { + + if (dealer.getHand().calculateValue() > player.getHand().calculateValue() && dealer.getHand().calculateValue() <= 21) { balance -= bet; + player.setBalance(balance); stats.incrementLosses(); return "You lose"; } - if(player.getHand().calculateValue() > dealer.getHand().calculateValue()) { + if (player.getHand().calculateValue() > dealer.getHand().calculateValue()) { balance += bet; + player.setBalance(balance); stats.incrementWins(); return "You win"; } - - if(player.getHand().calculateValue() == dealer.getHand().calculateValue()) { + + if (player.getHand().calculateValue() == dealer.getHand().calculateValue()) { stats.incrementPushes(); return "Push"; } - + return "Something went wrong"; } - + /** * Hits the player with a card */ public void hitPlayer() { player.hit(deck, discarded); } - + /** * * @return player hand @@ -166,41 +169,41 @@ public class Controller { public String displayPlayerHand() { return player.printHand(); } - + /** - * + * * @return the value of the player's hand */ public String displayPlayerHandValue() { return Integer.toString(player.getHand().calculateValue()); } - + /** - * + * * @return dealer hand */ public String displayDealerHand() { return dealer.printHand(); } - + /** - * + * * @return value of dealer's hand */ public String displayDealerHandValue() { return Integer.toString(dealer.getHand().calculateValue()); } - + /** - * + * * @return dealer's first card */ public String displayDealerFirstCard() { return dealer.printFirstHand(); } - + /** - * + * * @return value of the first card */ public String displayDealerFirstCardValue() {