From 20c786923279c84ee71af3eeda169a4397bc2cff Mon Sep 17 00:00:00 2001 From: Mitch Peck Date: Tue, 1 Feb 2022 20:21:30 -0600 Subject: [PATCH] Update Player object and initialize Players --- 10_Blackjack/java/src/Blackjack.java | 14 +++++--- 10_Blackjack/java/src/Player.java | 52 +++++++++++++++++++++------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/10_Blackjack/java/src/Blackjack.java b/10_Blackjack/java/src/Blackjack.java index ff47705b..65573b0a 100644 --- a/10_Blackjack/java/src/Blackjack.java +++ b/10_Blackjack/java/src/Blackjack.java @@ -32,7 +32,9 @@ public class Blackjack { deck.shuffle(); List players = new ArrayList<>(); - // TODO instantiate Player instances and update below to set their current bets. Finish TODOs in Player.java first. + for(int i = 0; i < nPlayers; i++) { + players.add(new Player(i + 1)); + } while(true) { int[] bets = new int[nPlayers]; // empty array initialized with all '0' valuses. @@ -41,14 +43,14 @@ public class Blackjack { for(int i = 0; i < nPlayers; i++) { // Note that the bet for player "1" is at index "0" in the bets // array and take care to avoid off-by-one errors. - bets[i] = promptInt("#" + (i + 1)); + bets[i] = promptInt("#" + (i + 1)); //TODO: If there isn't a need for a separate Bets in the future, combine these two lines and convert to enhanced FOR loop + Players.get(i).setCurrentBet(bets[i]); } } - printInitialDeal(); - for(Player player : players){ - // TODO deal two cards to each player from the deck. + player.dealCard(deck.deal()); + player.dealCard(deck.deal()); //TODO: This could be in a separate loop to more acurrately follow how a game would be dealt, I couldn't figure out of the BASIC version did it } // Consider adding a Dealer class to track the dealer's hand and running total. @@ -58,6 +60,8 @@ public class Blackjack { // TODO handle 'insurance' if the dealer's card is an Ace. + printInitialDeal(); + for(Player player : players){ play(player, deck); } diff --git a/10_Blackjack/java/src/Player.java b/10_Blackjack/java/src/Player.java index 17fa2770..f41fec83 100644 --- a/10_Blackjack/java/src/Player.java +++ b/10_Blackjack/java/src/Player.java @@ -2,16 +2,30 @@ import java.util.LinkedList; public class Player { - // TODO add 'playerNumber' property. e.g. playerNumber = 1 means "this is Player 1" + private int playerNumber; // e.g. playerNumber = 1 means "this is Player 1" private int currentBet; private int total; private LinkedList hand; // TODO we'll need to decide how to deal with a split hand or doubled down bet. - public Player() { - // TODO initilize 'total' to zero and 'hand' to an empty List + /** + * Represents a player in the game with cards, bets, total and a playerNumber. + */ + public Player(int playerNumber) { + this.playerNumber = playerNumber; + currentBet = 0; + total = 0; + hand = new LinkedList<>(); } + public void setPlayerNumber(int playerNumber) { //TODO: Is this needed if set in constructor? + this.playerNumber = playerNumber; + } + + public int getPlayerNumber() { + return this.playerNumber; + } + public void setCurrentBet(int currentBet) { this.currentBet = currentBet; } @@ -20,22 +34,36 @@ public class Player { return this.currentBet; } - // TODO replace Player.setTotal with recordWin and recordLoss - // recordWin adds 'currentBet' to 'total' and then sets 'currentBet' to zero - // recordLoss subtracts 'currentBet' to 'total' and then sets 'currentBet' to zero - public void setTotal(int total) { - this.total = total; + /** + * RecordWin adds 'currentBet' to 'total' and then sets 'currentBet' to zero + */ + public void recordWin() { + this.total = this.total + this.currentBet; + this.currentBet = 0; } - + /** + * RecordLoss subtracts 'currentBet' to 'total' and then sets 'currentBet' to zero + */ + public void recordLoss() { + total = total - currentBet; + currentBet = 0; + } + /** + * Returns the total of all bets won. + * @return Total value + */ public int getTotal() { return this.total; } - // TODO replace Player.setHand with 'dealCard(Card card)' and resetHand() // dealCard adds the given card to the player's hand + public void dealCard(Card card) { + hand.add(card); + } + // resetHand resets 'hand' to an empty list - public void setHand(LinkedList hand) { - this.hand = hand; + public void resetHand() { + this.hand = new LinkedList<>(); } public LinkedList getHand() {