I have been working on this program for about a week now and I think I have it down pack. The issue I am having when I ask the user at the beginning of the game (human vs computer) every time I run the program it asks me what my name is again. Here is what I have thus far:
import java.util.Scanner;
public class Assignment
{
Scanner usersName;
Boolean humanTurn = true;
Boolean computerTurn = true;
int dice;
int humanTurnPoints, computerTurnPoints;
int humanTotalPoints = 0;
int computerTotalPoints = 0;
private Scanner keyboard;
private Scanner key;
//System.out.print("Please enter your name: ");
//usersName = new Scanner(System.in);
//setStart(usersName.nextLine());
public void roll()
{
dice = (int)(Math.random()*6) + 1;
}
public int humanTurnScore()
{
{
humanTurnPoints = dice + humanTurnPoints;
System.out.println("You threw: " + dice);
System.out.println("You have scored: " + humanTurnPoints + " in your turn.");
} return humanTurnPoints;
}
public void humanTurnZero()
{
humanTurnPoints = 0;
}
public int computerTurnScore()
{
{
computerTurnPoints = dice + computerTurnPoints;
System.out.println("Computer has scored: " + computerTurnPoints + " in its turn.");
} return computerTurnPoints;
}
public void computerTurnZero()
{
computerTurnPoints = 0;
}
public Assignment()
{
humanGame();
if(!humanTurn)
{
computerTurn();
}
}
public int humanGame()
{
System.out.println("To start the game please press 'r'.");
key = new Scanner(System.in);
String start = key.nextLine();
if(!start.equalsIgnoreCase("R"))
{
System.out.println("Make sure you are pressing 'r'.");
humanGame();
}
if(start.equalsIgnoreCase("R"))
{
System.out.println("You pressed 'r'.");
System.out.println("Lets start.");
do{
roll();
if(dice == 1)
{
System.out.println("You got 1 and you lost your turn.");
System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
humanTurnZero();
computerTurn();
}
else if(dice != 1)
{
humanTotalPoints += dice;
if(humanTotalPoints >= 100)
{
System.out.println("You threw: " + dice);
System.out.println("Your GRAND TOTAL score is: " + humanTotalPoints);
System.out.println("Congratulations, you win!");
System.exit(0);
}
humanTurnScore();
System.out.println("Your GRAND TOTAL score is: " + humanTotalPoints);
System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
System.out.println("You can hold or roll again.");
System.out.println("To roll again press 'r' or 'h' to hold.");
keyboard = new Scanner(System.in);
String choice = keyboard.nextLine();
if(choice.equalsIgnoreCase("R"))
{
System.out.println("You pressed 'r'.");
System.out.println("Lets roll again.");
roll();
if(!choice.equalsIgnoreCase("R"))
{
System.out.println("You didn't press 'r'. To make sure the program is running correctly please press 'r' to roll or 'h' to hold.");
humanGame();
}
}
if(choice.equalsIgnoreCase("h"))
{
System.out.println("You pressed 'h' and loose your turn.");
System.out.println("Your Grand total is: " + humanTotalPoints);
humanTurnZero();
computerTurn();
}
}
}while(humanTurn);
}return dice;
}
public int computerTurn()
{
System.out.println("Now it's computer turn.");
do {
roll();
if(dice != 1)
{
computerTotalPoints += dice;
if(computerTotalPoints >=100)
{
System.out.println("Computer threw: " + dice);
System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
System.out.println("Game Over! the computer wins");
System.exit(0);
}
System.out.println("Computer threw: " + dice);
System.out.println("Computer's GRAND TOTAL score is: " + computerTotalPoints);
System.out.println("Your Grand total is: " + humanTotalPoints);
computerTurnScore();
roll();
}
if(dice == 1)
{
System.out.println("Computer thrown 1 therefore it's your turn now.");
computerTurnZero();
humanGame();
}
if(computerTurnPoints >= 20)
{
System.out.println("Computer scored already " + computerTurnPoints + " you'd better start to focus.");
System.out.println("Please play again");
humanGame();
}
}while (computerTurn);
return dice;
}
public static void main(String[] args)
{
new Assignment();
}
}
I commented out (up close to the top of the program) where I am asking the user at the beginning of the program what their name is. I actually need in all the System.out.println where it says 'You' I need it to say the usersName.
Would someone please help me with this program. I know someone is kind enough to help me out here.
Thank you in advance.
Of course it will ask you that every time!
You need to save this in a file, and then read from it!
//Write
PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
writer.println(usersName.nextLine());
writer.close();
//Read
BufferedReader br = new BufferedReader(new FileReader("the-file-name.txt"));
try {
while (line != null) {
usersName = line;
line = br.readLine();
}
} finally {
br.close();
}
Add your logic for determining if there is sth in that file yourself please.
Okay, I think you might be getting confused about the lifetimes of variables. In java, variables like String username; exist only in their own scope.
If you define a variable inside a method, it will be forgotten about when the program exists the method.
If you define it as a field inside a class (what you were doing), it will only exist as long as the instance of the class exists. As soon as the program forgets about the instance of the class, it also forgets about anything attached to that instance, including its variables.
Once the program shuts down, the computer naturally forgets about all objects that the program held, and so the username ceases to exist.
If you want the program to remember some value across shutdowns, you have to store that value in a file or a database or whatever. I recommend using a file, to make a database for this would be overkill squared. I will refer you to Roberto's answer from an hour ago for more info on how to achieve this exacly.
Related
I'm making a gambling game. The player enters a bet amount. After that a die is rolled three times, then based on the value of the die rolls, 1 of 4 things happen. Then you get to start again with the new Pot amount. I cant figure out how to start it again. I tried putting the method name i want to start at the end of the last method in the sequence and it kinda of works. It starts but when i put in a bet amount it doesnt do anything.
Here is my code
import java.util.Scanner;
public class Game {
private double Bet;
private double Pot = 50;
// private double TotalPot;
private int[] die = new int[3];
public Game() {
Pot = 50;
Bet = 0;
}
public void introText() {
System.out.println(
"Welcome to game Bet an amount.\nIf all of the three die together is greater than 12, \nyou get to keep your bet, if you roll doubles you win \ndouble your bet, if you roll triples you win triple \nyour bet. If your roll meets non of this criteria you lose your bet.A bet of 0 \nends the game.");
}
public void inputBet() {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your bet: ");
Bet = keyboard.nextDouble();
if (Bet > Pot) {
System.out.println(
"Error, Bet out of range. Please enter a bet amount that is lower or the same as the current Pot. ");
inputBet();
} else if (Bet == 0) {
System.out.println("Thank you for playing. You end the game with a pot of " + Pot);
System.exit(1);
} else if (Bet < 0) {
System.out.println("Error,");
inputBet();
}
}
public void removeBet() {
Pot = Pot - Bet;
}
public void rollDie() {
Die bob = new Die();
int total = 0;
for (int i = 0; i < 3; i++) {
die[i] = bob.rollDie();
System.out.println("Your die number is listed below");
bob.displayDie(die[i]);
total = total + die[i];
}
bob.displayDie(total);
}
public void displayingDie() {
System.out.println("Pot amount before dice rolls $" + Pot);
}
public void dieComparison1() {
if ((die[1] == die[2]) && (die[0] == die[2])) {
Pot = (Bet * 3) + Pot;
System.out.println(+Pot + "if theyre all equal");
} else if ((die[0] == die[1]) || (die[0] == die[2]) || (die[1] == die[2])) {
Pot = (Bet * 2) + Pot;
System.out.println(" Congradulations! You win double your bet.");
} else if (die[0] + die[1] + die[2] > 12) {
Pot = Pot + Bet;
System.out.println("You win. But only your bet amount back");
} else if (die[0] + die[1] + die[2] < 12) {
// Pot = Pot - Bet;
System.out.println("Sorry You lose you bet amount");
}
}
public void print() {
System.out.println("The current Pot amount is: " + Pot);
inputBet();
}
}
You have a conception problem. You should not control your execution flow nor do io actions from your Game class. This is mainly a problem of separation of concerns. Each method of this class should only do actions relative to gambling. For exemple inputBet must only change bet and do controls, in fact it must only be a setter and be renamed setBet and throws IllegalArgumentException for exemple.
You can then do something like this in your main :
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
Game game = new Game();
while(true){
Double bet = keyboard.nextDouble();
if(bet == 0){
//main loop break case
break;
}
try{
game.setBet(bet);
}catch(IllegalArguementException excpt){
system.out.println("error " + excpt.getMessage());
// skip next steps and do new loop iteration
continue;
}
// rest of execution flow
game.rollDice();
game.displayDice();
game.compareDice();
game.print();
}
System.exit(0);
}
and the setBet method could be :
public void setBet(Double bet){
// Range controls
if (bet > Pot || bet < 0) {
Throw new IllegalArgumentException("Bet out of range. Please enter a bet amount that is lower or the same as the current Pot. ");
}
this.bet = bet;
}
I am almost finished with my second Java assignment for class, but I am stuck on how to take the user's last choice and either play again or quit. Any ideas?
import java.util.Random;
import java.util.Scanner;
public class HighLow {
public static void main(String[]args) {
Random randomNumber = new Random();
int answer = randomNumber.nextInt(100) + 1;
int numberOfTries = 0;
Scanner userInput = new Scanner(System.in);
String userGuess = null;
boolean gameWin = false;
do {
while (!gameWin) {
System.out.println("Welcome to the guessing game, guess a number between 1 - 100 or type 'quit' to exit: ");
userGuess = userInput.nextLine();
numberOfTries++;
if (userGuess.equals("quit"))
break;
if (Integer.parseInt(userGuess) == answer) {
gameWin = true;
} else if (Integer.parseInt(userGuess) < answer) {
System.out.println("Your guess is too low");
} else if (Integer.parseInt(userGuess) > answer) {
System.out.println("Your guess is too high");
}
}
if (userGuess.equals("quit")) {
System.out.println("You choose to quit! Thanks for playing");
System.out.println("The number was " + answer);
System.exit(0);
}
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
}
while (userGuess.equals("yes"));
userInput.nextLine();
}
}
}
The problem is because you have userInput.nextLine() prompt for the user to play again or not outside of the game loop and it isn't assigned to any value. I am referring to this section...
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
}
while (userGuess.equals("yes"));
userInput.nextLine(); <---
}
To fix this problem you simply need to assign the final input call to userGuess and place it back into the game loop like so.
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
userGuess = userInput.nextLine();
}
while (userGuess.equals("yes"));
}
Doing this will assign the play again value you prompt the user for into the variable that your testing with the do-whileloop. In your original, you had the condition testing for a value that userGuess wasn't going to be storing and instead prompting the user for that option AFTER the loop would have been exited instead of inside where it needs to be.
To reset the values for the next game, you can simply move the variables' initialization into the game loop at the top like so...
import java.util.Random;
import java.util.Scanner;
public class HighLow {
public static void main(String[]args) {
Random randomNumber = new Random();
int numberOfTries, answer;
Scanner userInput = new Scanner(System.in);
String userGuess;
boolean gameWin;
do {
answer = randomNumber.nextInt(100) + 1;
gameWin = false;
userGuess = null;
numberOfTries = 0;
while (!gameWin) {
System.out.println("Welcome to the guessing game, guess a number between 1 - 100 or type 'quit' to exit: ");
userGuess = userInput.nextLine();
numberOfTries++;
if (userGuess.equals("quit"))
break;
if (Integer.parseInt(userGuess) == answer) {
gameWin = true;
} else if (Integer.parseInt(userGuess) < answer) {
System.out.println("Your guess is too low");
} else if (Integer.parseInt(userGuess) > answer) {
System.out.println("Your guess is too high");
}
}
if (userGuess.equals("quit")) {
System.out.println("You choose to quit! Thanks for playing");
System.out.println("The number was " + answer);
System.exit(0);
}
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
userGuess = userInput.nextLine();
}
while (userGuess.equals("yes"));
}
}
}
this way every time the game loops through, all the values will be reset at the beginning of the new game loop.
Also you should close the Random object and Scanner object at the end of the entire program using close() on each instance.
You need to initialize most of your variables and call userInput.nextLine(); inside your outer loop. Untested suggestion:
import java.util.Random;
import java.util.Scanner;
public class HighLow {
public static void main(String[]args) {
String userGuess = null;
do {
Random randomNumber = new Random();
int answer = randomNumber.nextInt(100) + 1;
int numberOfTries = 0;
Scanner userInput = new Scanner(System.in);
boolean gameWin = false;
while (!gameWin) {
System.out.println("Welcome to the guessing game, guess a number between 1 - 100 or type 'quit' to exit: ");
userGuess = userInput.nextLine();
numberOfTries++;
if (userGuess.equals("quit"))
break;
if (Integer.parseInt(userGuess) == answer) {
gameWin = true;
} else if (Integer.parseInt(userGuess) < answer) {
System.out.println("Your guess is too low");
} else if (Integer.parseInt(userGuess) > answer) {
System.out.println("Your guess is too high");
}
}
if (userGuess.equals("quit")) {
System.out.println("You choose to quit! Thanks for playing");
System.out.println("The number was " + answer);
System.exit(0);
}
System.out.println("The number was " + answer);
System.out.println("It took you " + numberOfTries + " tries");
System.out.println("You win! Play again(type: yes or no)");
}
userInput.nextLine();
while (userGuess.equals("yes"));
}
}
}
So i'm still a beginner but I managed to get this code but it didn't work like I wanted, my main problem is that every time I press 1 it resets the enemy instead of keeping the same one. I would really appreciate if someone could help me. So far I have only made writing 1 do something.
package Game;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random r = new Random();
System.out.println("Welcome to Dragon Heart");
System.out.println("1. Start");
System.out.println("2. Quit");
int input = 0, enemyhealth = 75, enemyattack = 15, playerhealth = 100, playerattack, random;
boolean enemydead = true, playerdead = false;
input = in.nextInt();
if (input == 1) {
System.out.println("Game started!");
while (0 != 1) {
if (enemydead = true) {
enemyhealth = r.nextInt(50) + 51;
enemyattack = r.nextInt(15) + 6;
System.out.println("An enemy appears, it has " + enemyhealth + " health points and " + enemyattack + " attack points");
} else {
System.out.println("The enemy now has " + enemyhealth + "health points");
}
System.out.println("1. Attack");
System.out.println("2. Defend");
System.out.println("3. Run away");
System.out.println("4. Do nothing");
input = in.nextInt();
if (input == 1) {
playerattack = r.nextInt(5) + 21;
random = r.nextInt(2) + 1;
enemyhealth = enemyhealth - playerattack;
if (random == 1) {
playerhealth = playerhealth - enemyattack;
}
if (enemyhealth <= 0) {
enemydead = true;
System.out.println("The enemy has been killed");
} else {
enemydead = false;
}
}
}
} else if (input == 2) {
System.out.println("Game quit.");
}
}
}
Your logic for defense away is dubious, but your problem is here:
if(enemydead = true)
You're reassigning enemydead to true every single time.
You really want to check if the enemy is dead, which is accomplished with this:
if(enemydead)
Further, you could clean up while (0 != 1) to be while(true) instead. However, you're going to need to include a break statement somewhere in that loop so that it's not an infinite loop like it is now.
Lastly, it's a good idea to have lower-case package names as opposed to upper-case package names.
So I'm working on a copy of a simple Dice game that was an example from the Maxwell Sanchez YouTube JAVA on Eclipse tutorials. What I started playing around with is simple ways to implement a text based menu of sorts.
What I'm trying to accomplish is a Y or N input method of either restarting the program, or killing it. I'm a total noob, coming here after a tiny bit of Arduino. I'm liking JAVA but there are many things I don't understand.
My problem right now is, everything appears to work so far, except that if you get to the end and type N to quit, It requires 2 inputs of N to actually execute the else if statement. Is that something that is a bug? Or am I just mis-programing what I'm trying to accomplish.
import java.util.*;
public class diceGame
{
static int money;
static Scanner in = new Scanner(System.in);
static Random random = new Random();
static String userName;
static String tryAgain;
public static void main(String[] args)
{
money = 1000;
System.out.println("Welcome to this simple dice game! " +
"Please enter your name.");
String userName = in.nextLine();
System.out.println("Hey " + userName + ".");
rollDice();
}
public static void rollDice()
{
System.out.println("You have " + money + " coins!");
System.out.println("Please select a number (1-6) to bet on!");
int betRoll = in.nextInt();
System.out.println("Please place your bet!");
int betMoney = in.nextInt();
while (betMoney > money)
{
System.out.println("You don't have enough coins... you only " +
"have " + money + "coins.");
System.out.println("Please place a realistic bet!");
betMoney = in.nextInt();
}
int dice;
dice = random.nextInt(6)+1;
if (betRoll == dice)
{
System.out.println("You Win!");
money+=betMoney*6;
System.out.println("You have " + money + " coins.");
}
else
{
System.out.println("Snap! You lost your coins!");
money-=betMoney;
System.out.println("You have " + money + " coins.");
}
if (money <= 0)
{
System.out.println("You've lost all yer coins!");
System.out.println("Play again?" + " Type y or n");
if (in.next().equalsIgnoreCase("y"))
{
System.out.println("Maybe you'll win this time!");
money = 1000;
rollDice();
}
else if (in.next().equalsIgnoreCase("n"))
{
System.out.println("Maybe next time...");
System.exit(0);
}
else
{
System.out.println("Invalid character");
}
}
else
{
rollDice();
}
}
}
Store the input in a variable, and compare it... or you'll have to input twice.
String choice = in.next();
if (choice.equalsIgnoreCase("y"))
{
System.out.println("Maybe you'll win this time!");
money = 1000;
rollDice();
}
else if (choice.equalsIgnoreCase("n")) // <-- not in.next()
Every time you call in.next() you read user input.
if (in.next().equalsIgnoreCase("y"))
else if (in.next().equalsIgnoreCase("n"))
In this code, you are calling in.next() twice, once for each condition, so it will read two inputs.
You need to separate the reading from the comparison.
String input = in.next();
if (input.equalsIgnoreCase("y"))
else if (input.equalsIgnoreCase("n"))
This question already has answers here:
How do I get the variable of one method to be a variable in another method for Java?
(4 answers)
Closed 8 years ago.
I'm trying to take the output of another method and use it in another method. I know there are other questions that are similar to mine but the solutions that were in those questions never solved my problem, though they did help a little. Here's where I'm stuck (problem is at rewardBet() method):
class Player {
private ArrayList<Card>hand;
private double cash, bet;
//double cash, bet;
public Player(double theCash)
{
cash = theCash;
hand = new ArrayList<Card>();
bet = 0;
}
public double wagerBet()
{
Scanner in = new Scanner(System.in);
System.out.print("Wager a bet: ");
double bet = in.nextDouble();
cash = cash - bet;
System.out.println("You wagered " + bet + ". " + "Now you have " + cash + " cash left.");
return bet;
}
public void rewardBet()
{
bet = wagerBet(); //this is supposed to be taking whatever the user wagered as a bet in the previous method and
cash = cash + (bet * 2); // apply it to this formula in order to mutate the total cash the player has
System.out.println("You now have " + cash + "cash.");
}
Any suggestions as to how to get this bet variable input to carry over?
EDIT, here's the main method like you guys requested:
class BlackJack {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Deck myDeck = new Deck();
myDeck.shuffle();
Player me = new Player(1000);
Player dealer = new Player(0);
Card c = myDeck.dealCard();
me.wagerBet();
System.out.println("Your first card is " + c);
me.hit(c);
c = myDeck.dealCard();
System.out.println("Your next card is " + c);
me.hit(c);
c = myDeck.dealCard();
System.out.println("Your total hand is currently " + me.totalHand() + ".");
System.out.println("Dealer showing " + c);
dealer.hit(c);
c = myDeck.dealCard();
String answer;
System.out.print("Hit or Stay?");
answer = in.nextLine();
while(answer.equals("Hit") || answer.equals("hit"))
{
System.out.println("Your next card is " + c);
me.hit(c);
c = myDeck.dealCard();
System.out.println("Your total hand is currently " + me.totalHand() + ".");
if(me.totalHand() == 21)
{
System.out.println("You win");
me.rewardBet();
System.exit(0);
}
else if(me.totalHand() < 21)
{
System.out.print("Hit or Stay?");
answer = in.nextLine();
}
else{
System.out.println("Player bust.");
System.exit(0);
}}
while(dealer.totalHand() < 17)
{
System.out.println("Dealer draws " + c);
dealer.hit(c);
c = myDeck.dealCard();
System.out.println("Dealer's total hand is currently " + dealer.totalHand() + ".");
if(dealer.totalHand() == 21)
{
System.out.println("Dealer wins.");
System.exit(0);
}
else if(dealer.totalHand() > 21)
{
System.out.println("Dealer bust. You win.");
me.rewardBet();
System.exit(0);
}
}
if(me.totalHand() > dealer.totalHand())
System.out.println("You win!");
me.rewardBet();
if(me.totalHand() < dealer.totalHand())
System.out.println("Loooooser");
if(me.totalHand() == dealer.totalHand())
System.out.println("Push. Nobody wins");
}
}
and to clarify my problem, the wagerBet() method asks for a double input from the user in the form of a bet. If the player wins his hand then the rewardBet() method will reward the player, giving him back the amount he bet plus the reward, hence 'bet * 2'. The problem is the rewardBet() method isn't recognizing the 'bet' input at all, I'm trying to figure out how to make it so. So for example I make a bet of 50, so now I have 950 dollars (1000 is default). I win the round so rewardBet() needs to give me 100 dollars. Right now it isn't giving me anything for winning.
Well, one problem is at the very last line of your main method:
if(me.totalHand() > dealer.totalHand())
System.out.println("You win!");
me.rewardBet();
You need to wrap this body in braces - the if statement if only functioning on the print statement. Although this doesn't seem as though it will fix the problem that you've described.
Perhaps you should consider doing a different design altogether, and avoid using so much duplicate code.
BlackJack:
public class BlackJack
{
private Deck deck;
private Player me;
private Player dealer;
public static void main(String[] args)
{
BlackJack game = new BlackJack();
game.run();
}
public BlackJack()
{
deck = new Deck();
deck.shuffle();
me = new Player("Joe", 1000.0);
dealer = new Player("Dealer", 0);
}
public void run()
{
double bet = requestBet(me);
// Deal your first two cards
dealNextCard(me, "Your first card is ");
dealNextCard(me, "Your second card is ");
me.printHandTotal();
// Deal dealer's first card
dealNextCard(dealer, "Dealer showing ");
while(requestHitOrStay())
{
dealNextCard(me, "Your next card is ");
me.printHandTotal();
if(me.totalHand() == 21)
{
System.out.println(me.getName() + " wins!");
rewardBet(me, bet);
System.exit(0);
}
else if(me.totalHand() > 21)
{
System.out.println(me.getName() + " bust!");
System.exit(0);
}
}
while(dealer.totalHand() < 17)
{
dealNextCard(dealer, "Dealer draws ");
dealer.printHandTotal();
if(dealer.totalHand() == 21)
{
System.out.println(dealer.getName() + " wins!");
System.exit(0);
}
else if(dealer.totalHand() > 21)
{
System.out.println(dealer.getName() + " bust. You win!");
rewardBet(me, bet);
System.exit(0);
}
}
if(me.totalHand() > dealer.totalHand())
{
System.out.println("You win!");
rewardBet(me, bet);
}
else if(me.totalHand() < dealer.totalHand())
{
System.out.println("Loooooser");
}
else
{
System.out.println("Push. Nobody wins");
}
}
public boolean requestHitOrStay()
{
System.out.print("Hit or Stay? ");
Scanner in = new Scanner(System.in);
return in.nextLine().toLowerCase().equals("hit");
}
public void dealNextCard(Player p, String prefix)
{
Card c = deck.dealCard();
System.out.println(prefix + c);
p.addCard(c);
}
public double requestBet(Player p)
{
Scanner in = new Scanner(System.in);
double bet = Integer.MAX_VALUE;
while(bet > p.getCash())
{
System.out.print("Wager a bet: ");
bet = in.nextDouble();
}
p.setCash(p.getCash() - bet);
System.out.println(p.getName() + " wagered " + bet + ". " + "Now they have " + p.getCash() + " cash left.");
return bet;
}
public void rewardBet(Player p, double bet)
{
p.setCash(p.getCash() + bet * 2);
System.out.println(p.getName() + " now has " + p.getCash() + " cash.");
}
}
Player:
public class Player
{
private ArrayList<Card> hand;
private double cash;
private String name;
public Player(String playerName, double startingCash)
{
hand = new ArrayList<Card>();
cash = startingCash;
name = playerName;
}
public void addCard(Card c)
{
hand.add(c);
}
public int totalHand()
{
int total = 0;
for(Card c : hand)
{
total += c.getValue();
}
return total;
}
public void printHandTotal()
{
System.out.println(name + "'s' total hand is currently " + totalHand() + ".");
}
public String getName()
{
return name;
}
public double getCash()
{
return cash;
}
public void setCash(double cash)
{
this.cash = cash;
}
}