commit c62d326d517548bb070816026d67790d9dcdd6a4
parent 335a7ef3a73f63ae8fa49fcb05e4d62d8843936e
Author: Michael Neuper <michaelneuper@icloud.com>
Date: Thu, 27 Apr 2023 22:16:58 +0200
Merge pull request #18 from michaelneuper/persistent-balance
Persistent balance
Diffstat:
2 files changed, 68 insertions(+), 57 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() {
diff --git a/src/model/Player.java b/src/model/Player.java
@@ -8,44 +8,52 @@ import java.io.PrintWriter;
import java.util.Scanner;
/**
- * Handles player specific operations, such as deciding to hit or stand.
- *
- * @author Michael
+ * Represents a player in a Blackjack game
*/
public class Player extends Person {
-
- private double balance; // TODO: make public
- Scanner input = new Scanner(System.in);
+
+ private double balance; // Player's balance
+ private final String BALANCE_FILE = "db/balance.txt"; // File to store the player's balance
+ private Scanner input = new Scanner(System.in);
/**
- * Creates a new player
+ * Creates a new player with a default name of "Player"
*/
public Player() {
super.setName("Player");
}
-
- public double getBalance(String inFile) {
+
+ /**
+ * Gets the player's current balance from a file
+ *
+ * @return balance the player's current balance
+ */
+ public double getBalance() {
try {
- Scanner scFile = new Scanner(new File(inFile));
+ Scanner scFile = new Scanner(new File(BALANCE_FILE));
this.balance = scFile.nextDouble();
scFile.close();
} catch (FileNotFoundException e) {
- System.out.println("Error:\n" + e);
+ System.out.println("Error: " + BALANCE_FILE + " not found.");
}
return this.balance;
}
- public void setBalance(String inFile, double balance) {
+ /**
+ * Sets the player's balance and stores it in a file
+ *
+ * @param balance the new balance to set
+ */
+ public void setBalance(double balance) {
try {
- PrintWriter outFile = new PrintWriter(new FileWriter(inFile), false);
+ PrintWriter outFile = new PrintWriter(new FileWriter(BALANCE_FILE), false);
outFile.print(balance);
outFile.close();
} catch (IOException e) {
- System.out.println("Error:\n" + e);
+ System.out.println("Error writing to " + BALANCE_FILE);
}
-// this.balance = balance; // TODO: is neccesary?
+ this.balance = balance;
}
-
}