librejack

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

commit 3b72ec41a7a90a228f6e8d6d4c9e92d213eca6cf
parent e10f4e96ac7252002aa8396ad6ee9dfb5953f244
Author: Michael Neuper <73108749+michaelneuper@users.noreply.github.com>
Date:   Fri,  7 Oct 2022 21:04:52 +0200

update Controller.java

Diffstat:
Msrc/controller/Controller.java | 76+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 71 insertions(+), 5 deletions(-)

diff --git a/src/controller/Controller.java b/src/controller/Controller.java @@ -1,20 +1,87 @@ package controller; +import model.Dealer; +import model.Deck; +import model.Player; + /** * Handles most of the game logic - * @author michael + * + * @author Michael */ public class Controller { - + + private Deck deck, discarded; + + private Dealer dealer; + private Player player; private int wins, losses, pushes; /** - * Controller constructor + * Controller Constructor: * Initialises score to 0 */ public Controller() { + wins = 0; losses = 0; pushes = 0; + + deck = new Deck(); // creates new populated deck + deck.populate(); + deck.shuffle(); + + discarded = new Deck(); // creates new empty deck + + dealer = new Dealer(); + player = new Player(); + + startRound(); + } + + /** + * Handles logic for each round + */ + private void startRound() { + + // give the dealer 2 cards + dealer.getHand().takeCardFromDeck(deck); + dealer.getHand().takeCardFromDeck(deck); + + // give the player 2 cards + player.getHand().takeCardFromDeck(deck); + player.getHand().takeCardFromDeck(deck); + + // print their hands + dealer.printFirstHand(); + player.printHand(); + + // check if dealer has blackjack + if (dealer.hasBlackjack()) { + dealer.printHand(); // show full hand + + if (player.hasBlackjack()) { + + System.out.println("You both have 21 - Push"); + ++ pushes; + startRound(); + + } else { + + System.out.println("Dealer has BlackJack. You lose."); + dealer.printHand(); + ++ losses; + startRound(); + } + } + + // check if player has blackjack + if(player.hasBlackjack()) { + System.out.println("You have BlackJack. You win."); + ++ wins; + startRound(); + } + + player.makeDecision(); } -} -\ No newline at end of file +}