Trouble writing blackjack and solitaire program in java

610 views Asked by At

I have been working on this project for a few days and I can't figure out why it's not working. Hopefully a fresh set of eyes will help. When I run this, several things don't happen.

The only thing that really works is the part that manages the bet from the user. Some problems include:

  • It does not bring a card value (supposed to come from a class that randomly generates a number from 1-13).

  • Does not wait for the user to enter 0 to draw more cards or not

  • When it says what the third card is it says Card@42a57993
  • Gives no total card value.
  • There's also an error that happens when I try to fix a problem. In the addition for cardTotal, the error says "the operator + is undefined for the argument types."

It's possible that It is missing or has extra brackets but I can't find where they should/shouldn't be. Here is my code:

import java.util.Scanner; 

public class Game {
   private double pot;
   private double bet; 
   private int answer; 
   private int cardTotal; 

   Card card1 = new Card();
   Card card2 = new Card();
   Card card3 = new Card(); 

   // constructor
   public Game(){
      pot = 100;
      bet = 0; 
   }

   Scanner keyboard = new Scanner(System.in);

   // GetBetFrom user, prompts user to enter a valid bet, reenter until a valid amount
   public double getBetFromUser(){
      System.out.println("Your current pot is: "+pot);

      System.out.println("Enter your bet amount");
      bet = keyboard.nextDouble();  

      //Rules for the bet
      while(bet>pot || bet<0){
         if(pot==0){
            break; 
         }
         if(bet>pot){
            System.out.println("Sorry, you don't have enough money for that, try again");
            bet = keyboard.nextDouble(); 
         }
         if(bet<0){
            System.out.println("That is not a valid bet, please try again");
            bet = keyboard.nextDouble(); 
         }
     }
     return bet;
  }

  // Game mechanics 
  public void gamePlay(){
     System.out.println("Your first card is: "+card1);
     System.out.println("Your second card is: "+card2);
     cardTotal= card1 + card2; 
     System.out.println("Your total is: "+cardTotal);
     System.out.println("Enter 0 to draw more, any other number to quit: ");

     if(answer == 0); {
        System.out.println("Your next card is: "+card3);
        System.out.println("Your total is: "); 
     }
 }

 public void results(){
    if(cardTotal > 21){
       System.out.println("You lose your bet");
       pot = pot-bet; 
    }
    if(cardTotal==21){
       System.out.println("You win three times your bet");
       pot = pot-bet+(bet*3);
    }
    if(cardTotal==20){
       System.out.println("You win double your bet amount");
       pot = pot-bet+(bet*2); 
    }
    if(cardTotal<=19){
       System.out.println("You win your bet amount");
    }
 }
}

This is the code for my Card class

import java.util.Random; 

public class Card {
private int cardValue; 
Random random = new Random();

public Card(){
    cardValue = 1;
}

public int getCard(){
    cardValue = (cardValue + random.nextInt(12));
    if(cardValue>10)
        return 10;
    else
        return cardValue;   
}

public int display(){
    switch (cardValue){
    case 1: System.out.println("Ace");
    break;
    case 2: System.out.println("Two");
    break; 
    case 3: System.out.println("Three"); 
    break; 
    case 4: System.out.println("Four");
    break;
    case 5: System.out.println("Five");
    break;
    case 6: System.out.println("Six");
    break;
    case 7: System.out.println("Seven");
    break; 
    case 8: System.out.println("Eight");
    break;
    case 9: System.out.println("Nine");
    break; 
    case 10: System.out.println("Ten");
    break; 
    case 11: System.out.println("Jack");
    break;
    case 12: System.out.println("Queen");
    break;
    case 13: System.out.println("King");
    break; 

    }   
    return cardValue; 
}       

}

2

There are 2 answers

2
Sam I am says Reinstate Monica On

I'm guessing that your error is happening here:

cardTotal= card1 + card2; 

card1 and card2 are Card types. the compiler doesn't know how to add them.

You should get your card total like this:

cardTotal = card1.cardValue+ card2.cardValue;
1
SkyMaster On

This may help you. I suggest you read about how the objects and their methods are referenced. Validation of the data entered by keyboard missing, but that's your job:

import java.util.Scanner;
public class Game {
   private double pot;
   private double bet; 
   private int answer; 
   private int cardTotal;

   Card card1 = new Card();
   Card card2 = new Card();
   Card card3 = new Card(); 

   // constructor
   public Game(){
      pot = 100;
      bet = 0; 
   }

   Scanner keyboard = new Scanner(System.in);

   // GetBetFrom user, prompts user to enter a valid bet, reenter until a valid amount
   public double getBetFromUser(){
      System.out.println("Your current pot is: "+pot);

      System.out.print("Enter your bet amount: ");
      bet = keyboard.nextDouble();  

      //Rules for the bet
      while(bet>pot || bet<0){
         if(pot==0){
            break; 
         }
         if(bet>pot){
            System.out.println("Sorry, you don't have enough money for that, try again");
            bet = keyboard.nextDouble(); 
         }
         if(bet<0){
            System.out.println("That is not a valid bet, please try again");
            bet = keyboard.nextDouble(); 
         }
     }
     return bet;
  }

  // Game mechanics 
  public void gamePlay(){
     int first= card1.getCard();
     int second = card2.getCard();
     int third = card3.getCard();
     System.out.println("Your first card is: "+first);
     System.out.println("Your second card is: "+second);
     cardTotal= first + second;
     System.out.println("Your total is: "+cardTotal);
     System.out.print("Enter 0 to draw more, any other number to quit: ");
     answer = keyboard.nextInt(); 

     if(answer == 0) {
        System.out.println("Your next card is: "+third);
        cardTotal=cardTotal + third;
     }
     System.out.println("Your total is: "+cardTotal); 
 }

 public void results(){
    if(cardTotal > 21){
       System.out.println("You lose your bet");
       pot = pot-bet; 
    }
    if(cardTotal==21){
       System.out.println("You win three times your bet");
       pot = pot-bet+(bet*3);
    }
    if(cardTotal==20){
       System.out.println("You win double your bet amount");
       pot = pot-bet+(bet*2); 
    }
    if(cardTotal<=19){
       System.out.println("You win your bet amount");
    }
 }

 public static void main(String[] args) {
      Game game = new Game();
      game.getBetFromUser();
      game.gamePlay();
      game.results();
 }     

}