librejack

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

commit 28edd3998aa3a9f7c959edff2ee81fb876ab4414
parent 74f7e3023ebdadc3efe0be2abeef831e1877d368
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date:   Sat,  8 Oct 2022 13:13:13 +0200

add bet system

Diffstat:
Msrc/controller/Controller.java | 22+++++++++++++++++++---
Msrc/model/Player.java | 33++++++++++++++++++++++++++++++++-
2 files changed, 51 insertions(+), 4 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(); @@ -44,7 +49,7 @@ public class Controller { // 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 +59,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 +90,7 @@ public class Controller { System.out.println("Dealer has BlackJack. You lose."); dealer.printHand(); + balance -= bet; ++losses; startRound(); } @@ -88,6 +99,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 +109,7 @@ public class Controller { // check whether they busted if(player.getHand().calculateValue() > 21) { System.out.println("Busted"); + balance -= bet; ++losses; startRound(); } @@ -110,16 +123,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 +144,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 */