Use double to store bets

The original basic allowed fractional bets.
This commit is contained in:
Dave Burke
2022-02-11 13:02:22 -06:00
parent df86d49bb7
commit db1e32a314
3 changed files with 36 additions and 11 deletions

View File

@@ -47,15 +47,15 @@ public class Game {
} }
while(true) { while(true) {
int[] bets = new int[nPlayers]; // empty array initialized with all '0' valuses. double[] bets = new double[nPlayers]; // empty array initialized with all '0' valuses.
while(!betsAreValid(bets)){ while(!betsAreValid(bets)){
userIo.println("BETS:"); userIo.println("BETS:");
for(int i = 0; i < nPlayers; i++) { for(int i = 0; i < nPlayers; i++) {
// Note that the bet for player "1" is at index "0" in the bets // Note that the bet for player "1" is at index "0" in the bets
// array and take care to avoid off-by-one errors. // array and take care to avoid off-by-one errors.
// TODO the BASIC version allows fractional bets (e.g. '1.5') of arbitrary precision // TODO the BASIC version allows fractional bets (e.g. '1.5') of arbitrary precision
// We can use a float if we want--the original basic has floating point errors. Or we could use BigDecimal if we really want to fix that. // We can use a double if we want--the original basic has doubleing point errors. Or we could use BigDecimal if we really want to fix that.
bets[i] = userIo.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 bets[i] = userIo.promptDouble("#" + (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]); players.get(i).setCurrentBet(bets[i]);
} }
} }
@@ -244,14 +244,14 @@ public class Game {
} }
/** /**
* Validates that all bets are between 1 and 500 (inclusive). * Validates that all bets are between 0 (exclusive) and 500 (inclusive). Fractional bets are valid.
* *
* @param bets The array of bets for each player. * @param bets The array of bets for each player.
* @return true if all bets are valid, false otherwise. * @return true if all bets are valid, false otherwise.
*/ */
public boolean betsAreValid(int[] bets) { public boolean betsAreValid(double[] bets) {
return Arrays.stream(bets) return Arrays.stream(bets)
.allMatch(bet -> bet >= 1 && bet <= 500); .allMatch(bet -> bet > 0 && bet <= 500);
} }
} }

View File

@@ -3,8 +3,8 @@ import java.util.LinkedList;
public class Player { public class Player {
private int playerNumber; // 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 double currentBet;
private int total; private double total;
private LinkedList<Card> hand; private LinkedList<Card> hand;
// TODO we'll need to decide how to deal with a split hand or doubled down bet. // TODO we'll need to decide how to deal with a split hand or doubled down bet.
@@ -26,11 +26,11 @@ public class Player {
return this.playerNumber; return this.playerNumber;
} }
public void setCurrentBet(int currentBet) { public void setCurrentBet(double currentBet) {
this.currentBet = currentBet; this.currentBet = currentBet;
} }
public int getCurrentBet() { public double getCurrentBet() {
return this.currentBet; return this.currentBet;
} }
@@ -52,7 +52,7 @@ public class Player {
* Returns the total of all bets won. * Returns the total of all bets won.
* @return Total value * @return Total value
*/ */
public int getTotal() { public double getTotal() {
return this.total; return this.total;
} }

View File

@@ -132,4 +132,29 @@ public class UserIo {
} }
} }
} }
/**
* Prompts the user for a double. As in Vintage Basic, "the optional
* prompt string is followed by a question mark and a space." and if the
* input is non-numeric, "an error will be generated and the user will be
* re-prompted.""
*
* @param prompt The prompt to display to the user.
* @return the number given by the user.
*/
public double promptDouble(String prompt) {
print(prompt + "? ");
while(true) {
String input = readLine();
try {
return Double.parseDouble(input);
} catch(NumberFormatException e) {
// Input was not numeric.
println("!NUMBER EXPECTED - RETRY INPUT LINE");
print("? ");
continue;
}
}
}
} }