mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-26 04:41:52 -08:00
Update Player object and initialize Players
This commit is contained in:
@@ -32,7 +32,9 @@ public class Blackjack {
|
||||
deck.shuffle();
|
||||
|
||||
List<Player> 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);
|
||||
}
|
||||
|
||||
@@ -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<Card> 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<Card> hand) {
|
||||
this.hand = hand;
|
||||
public void resetHand() {
|
||||
this.hand = new LinkedList<>();
|
||||
}
|
||||
|
||||
public LinkedList<Card> getHand() {
|
||||
|
||||
Reference in New Issue
Block a user