commit 7781f2e8f8343e84265494ed7a16a61490b55994
parent 8a8dbd27348d223e4396ad6a17c4e9b3cfbddf56
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date: Sat, 8 Oct 2022 13:21:13 +0200
Merge remote-tracking branch 'origin/michaelneuper/issue3'
merge issue3 into main
Diffstat:
2 files changed, 53 insertions(+), 5 deletions(-)
diff --git a/src/controller/Controller.java b/src/controller/Controller.java
@@ -1,5 +1,7 @@
package controller;
+import java.util.Scanner;
+
import model.Dealer;
import model.Deck;
import model.Player;
@@ -15,7 +17,9 @@ public class Controller {
private Dealer dealer;
private Player player;
+ private double bet, balance;
private int wins, losses, pushes;
+
/**
* Controller Constructor:
@@ -26,6 +30,7 @@ public class Controller {
wins = 0;
losses = 0;
pushes = 0;
+ balance = 1000.0;
deck = new Deck(); // creates new populated deck
deck.populate();
@@ -38,13 +43,14 @@ public class Controller {
}
/**
- * Handles logic for each round
+ * Handles logic for each roun
+ *
*/
public void startRound() {
// if we run out of cards
if(wins > 0 || losses > 0 || pushes > 0) {
- System.out.println("\nStarting next round...\nWins: " + wins + "\nLosses: " + losses + "\nPushes: " + pushes);
+ System.out.println("\nStarting next round...\nWins: " + wins + "\nLosses: " + losses + "\nPushes: " + pushes + "\nBalance: " + balance);
dealer.getHand().discardHandToDeck(discarded);
player.getHand().discardHandToDeck(discarded);
@@ -54,6 +60,11 @@ public class Controller {
}
}
+ // ask the player to bet
+ System.out.println("Enter a bet amount");
+ Scanner inBet = new Scanner(System.in);
+ bet = Double.parseDouble(inBet.next());
+
// give the dealer 2 cards
dealer.getHand().takeCardFromDeck(deck);
dealer.getHand().takeCardFromDeck(deck);
@@ -80,6 +91,7 @@ public class Controller {
System.out.println("Dealer has BlackJack. You lose.");
dealer.printHand();
+ balance -= bet;
++losses;
startRound();
}
@@ -88,6 +100,7 @@ public class Controller {
// check if player has blackjack
if(player.hasBlackjack()) {
System.out.println("You have BlackJack. You win.");
+ balance += bet * 1.5;
++wins;
startRound();
}
@@ -97,6 +110,7 @@ public class Controller {
// check whether they busted
if(player.getHand().calculateValue() > 21) {
System.out.println("Busted");
+ balance -= bet;
++losses;
startRound();
}
@@ -110,16 +124,19 @@ public class Controller {
// check who wins
if(dealer.getHand().calculateValue() > 21) {
System.out.println("Dealer busts");
+ balance += bet;
++wins;
}
if(dealer.getHand().calculateValue() > player.getHand().calculateValue()) {
System.out.println("You lose");
+ balance -= bet;
++losses;
}
if(player.getHand().calculateValue() > dealer.getHand().calculateValue()) {
System.out.println("You win");
+ balance += bet;
++wins;
}
@@ -128,9 +145,9 @@ public class Controller {
++pushes;
}
- // start a new round
+ // clear bet and start a new round
+ inBet.close();
startRound();
-
}
}
diff --git a/src/model/Player.java b/src/model/Player.java
@@ -1,5 +1,10 @@
package model;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
import java.util.Scanner;
/**
@@ -8,7 +13,8 @@ import java.util.Scanner;
* @author Michael
*/
public class Player extends Person {
-
+
+ double balance; // TODO: make public
Scanner input = new Scanner(System.in);
/**
@@ -18,8 +24,33 @@ public class Player extends Person {
super.setName("Player");
}
+ public double getBalance(String inFile) {
+ try {
+ Scanner scFile = new Scanner(new File(inFile));
+ this.balance = scFile.nextDouble();
+ scFile.close();
+ } catch (FileNotFoundException e) {
+ System.out.println("Error:\n" + e);
+ }
+
+ return this.balance;
+ }
+
+ public void setBalance(String inFile, double balance) {
+ try {
+ PrintWriter outFile = new PrintWriter(new FileWriter(inFile), false);
+ outFile.print(balance);
+ outFile.close();
+ } catch (IOException e) {
+ System.out.println("Error:\n" + e);
+ }
+
+ this.balance = balance; // TODO: is neccesary?
+ }
+
/**
* Allows the player to decide to hit or stand
+ *
* @param deck deck to draw from
* @param discard deck to discard to
*/