-1

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.

SJSharks
  • 39
  • 2
  • 9

1 Answers1

1

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;
    }
}
Geoffrey Tucker
  • 670
  • 1
  • 9
  • 18
  • Hi Geoffrey thanks for your reply. Yeah I'm worried that if worst comes to worst I'll have to completely re-do this betting system. I'm still trying to find a way to get my rewardBet() method to accept the input 'bet' from the wagerBet() method before it, if I can manage that the whole program will be done - which is why I'm so adamant to follow through with the code I have now, I feel so close to figuring this method out. – SJSharks Mar 17 '14 at 04:31
  • For that you would do something like this: `double bet = me.wagerBet();` and then later on when you decide to reward the winner, do `me.rewardBet(bet);`. And get rid of the bet field from the Player, since it will be kept track of in the main method. – Geoffrey Tucker Mar 17 '14 at 04:43
  • In other words, you need to change `public void rewardBet()` in the Player class to take a double: `public void rewardBet(double bet)` and use the bet parameter here instead of the member field from the Player. – Geoffrey Tucker Mar 17 '14 at 04:44
  • 1
    Wow. I just implemented your suggestion and as far as I can tell this is working exactly how I want it to. Thanks very much! If there aren't any loose ends I'm going to take your solution and see how I can learn from this for future decision making regarding programming. Cheers Geoffrey. – SJSharks Mar 17 '14 at 05:00