commit 9c2c05f6cd50a94d941c1d7a1dc036a5883b6f0f
parent c46fc66258a975a38e4f7cb90f1c4f3fa09e242d
Author: Michael Neuper <michaelneuper@icloud.com>
Date: Thu, 27 Apr 2023 22:42:57 +0200
Merge branch 'main' into format
Diffstat:
2 files changed, 33 insertions(+), 17 deletions(-)
diff --git a/src/controller/Controller.java b/src/controller/Controller.java
@@ -7,6 +7,7 @@ import model.Player;
import model.Statistics;
/**
+
* Handles most of the game logic.
*
* @author Michael Neuper
@@ -25,8 +26,11 @@ public class Controller {
* 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
@@ -36,10 +40,6 @@ public class Controller {
// create new empty deck
discarded = new Deck();
-
- // create new dealer and player
- dealer = new Dealer();
- player = new Player();
}
public double getBet() {
@@ -98,6 +98,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";
@@ -107,6 +108,7 @@ public class Controller {
// check if player has blackjack
if (player.hasBlackjack()) {
balance += bet * 1.5;
+ player.setBalance(balance);
stats.incrementWins();
return "You have BlackJack - You win";
}
@@ -114,6 +116,7 @@ public class Controller {
// check whether player busted
if (player.getHand().calculateValue() > 21) {
balance -= bet;
+ player.setBalance(balance);
stats.incrementLosses();
return "Busted";
}
@@ -127,18 +130,21 @@ public class Controller {
// check who wins
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) {
balance -= bet;
+ player.setBalance(balance);
stats.incrementLosses();
return "You lose";
}
if (player.getHand().calculateValue() > dealer.getHand().calculateValue()) {
balance += bet;
+ player.setBalance(balance);
stats.incrementWins();
return "You win";
}
diff --git a/src/model/Player.java b/src/model/Player.java
@@ -8,45 +8,55 @@ import java.io.PrintWriter;
import java.util.Scanner;
/**
- * Handles player specific operations, such as deciding to hit or stand.
+ * Represents a player in a Blackjack game.
*
* @author Michael Neuper
* <a href="mailto:michael@michaelneuper.com">michael@michaelneuper.com</a>
*/
public class Player extends Person {
- private double balance;
- 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;
}
-
}