I'm relatively new to Java and I'm writing a code that implements a simple "Hi-Lo" game using a standard deck of 52 playing cards. I could give you the entire description of the program's requirements, but I won't let you all suffer the same headache I do. :P Anyhow, here's my two chunks of code:

import java.util.Random;

public class PlayingCard
{
   private int face;
   private int suit;

   //-------------------------------------------------------------------
   //  Sets up the playing card by drawing it initially.
   //-------------------------------------------------------------------
   public PlayingCard ()
   {
      drawCard();
   }

   //-------------------------------------------------------------------
   //  Draws the playing card by randomly choosing face and suit values.
   //-------------------------------------------------------------------
   public void drawCard ()
   {
      face = (int) (Math.random() * 13) + 2;
      suit = (int) (Math.random() * 4);
   }

   //-------------------------------------------------------------------
   //  Returns true if the current playing card is exactly the same as
   //  the card passed in as a parameter.
   //-------------------------------------------------------------------
   public boolean isEquals (PlayingCard c)
   {
      return (face == c.face && suit == c.suit);
   }

   //-------------------------------------------------------------------
   //  Returns the face value of the current playing card.
   //-------------------------------------------------------------------
   public int getFace()
   {  
      return face;
   }

   //-------------------------------------------------------------------
   //  Returns the suit value of the current playing card.
   //-------------------------------------------------------------------
   public int getSuit()
   {  
      return suit;
   }

   //-------------------------------------------------------------------
   //  Returns the current playing card as a string.
   //-------------------------------------------------------------------
   public String toString()
   {
      String cardName = null;

      switch (face)
      {
         case 2:   cardName = "Two";
                   break;
         case 3:   cardName = "Three";
                   break;
         case 4:   cardName = "Four";
                   break;
         case 5:   cardName = "Five";
                   break;
         case 6:   cardName = "Six";
                   break;
         case 7:   cardName = "Seven";
                   break;
         case 8:   cardName = "Eight";
                   break;
         case 9:   cardName = "Nine";
                   break;
         case 10:  cardName = "Ten";
                   break;
         case 11:  cardName = "Jack";
                   break;
         case 12:  cardName = "Queen";
                   break;
         case 13:  cardName = "King";
                   break;
         case 14:  cardName = "Ace";
      }

      switch (suit)
      {
         case 0:   cardName += " of Clubs";
                   break;
         case 1:   cardName += " of Spades";
                   break;
         case 2:   cardName += " of Hearts";
                   break;
         case 3:   cardName += " of Diamonds";
                   break;
      }

      return cardName;
   }
}
import java.util.Scanner;
import java.util.Random;

public class HiLoGame
{
   public static void main (String[] args)
   {
        
      double bankroll, bet;
      int lostGames, tiedGames, totalGames, wonGames;

      Scanner scan = new Scanner (System.in);
      System.out.println("\n\nThis program implements a Hi-Lo game using");
      System.out.println("a standard deck of 52 playing cards. You are");
      System.out.println("required to enter your bankroll as well as the");
      System.out.println("amount of money you wish to bet.");
      System.out.print("\n\nHow much is in your bank? ");
      bankroll = scan.nextDouble();
      System.out.print("\n\nHow much would you like to bet? ");
      bet = scan.nextDouble();
                
      System.out.println("First card: " + getFace + " of " + getSuit);
      System.out.print("\n\nDo you think the second card will be higher or lower?");
      String choiceString = scan.nextLine();
                
      choiceString = choiceString.toLowerCase();

      PlayingCard = cardOne = new PlayingCard();
      PlayingCard = cardTwo = new PlayingCard();
                     
                          if (cardOne.getface() < cardTwo.getTwo.getface() && guess.equals("higher"))
                               do
                               {
                                    cardTwo.drawCard();
                               }
                               while (cardOne.isEquals(cardTwo));
                
                if (choiceString > 0)
                     if (choiceString == cardTwo)
                          {    
                          System.out.println("Good job! You're right!");
                               bankroll = bankroll + bet;
                                         wonGames++;
                               totalGames++;
                                         System.out.println("The amount in your bankroll is " + bankroll + ".");
                                         System.out.println("Would you like to play again?");
                          }
                               if (choiceString < cardTwo)
                               {
                                              System.out.println("Sorry, your guess was too low.");
                                              bankroll = bankroll - bet;
                                              lostGames++;
                                              totalGames++;
                                              System.out.println("The amount in your bankroll is " + bankroll + ".");
                                              System.out.println("Would you like to play again?");
                               }
                                                      else
                                                      {
                                                             System.out.println("Sorry, your guess was too high.");
                                                                  bankroll = bankroll - bet;
                                                                  lostGames++;
                                                                  totalGames++;
                                                                  System.out.println("The amount in your bankroll is " + bankroll + ".");
                                                                  System.out.println("Would you like to play again?");
                                                      }
                
      System.out.println("You played "+ totalGames +" games total.");
      System.out.println("You won " + wonGames + " times, lost " + lostGames + " times, and tied " + tiedGames + " times.");
      System.out.println("The amount left in your bankroll is " + bankroll + ".");
         
   }
}

I do realize there's a number of errors in this, but what I want to start with is how can I configure the HiLo.java so that it reads off of the PlayingCard.java?

Recommended Answers

All 5 Replies

When you create PlayingCard objects in HiLo then you are using your other code... is that what you mean? Like if in HiLo you say PlayingCard pc = new PlayingCard(); Then you can call all the PlayingCard methods from the pc object -- pc.drawCard(), pc.getSuit(), etc. are all valid calls that "read off of the PlayingCard class.

Ah! Thank you! I accidently placed an operator in front of the oneCard object, which gave me a list of errors when I attempted to compile it. Might I add on one last question?

System.out.print("\n\nDo you think the second card will be higher or lower?");
      String choiceString = scan.nextLine();
 
      choiceString = choiceString.toLowerCase();
 
      PlayingCard cardOne = new PlayingCard();
      PlayingCard cardTwo = new PlayingCard();
 
                          if (cardOne.getface() < cardTwo.getTwo.getface() && choiceString.equals("higher"))
                               do
                               {
                                    cardTwo.drawCard();
                               }
                               while (cardOne.isEquals(cardTwo));
 
                if (choiceString > 0)
                     if (choiceString == cardTwo)
                     {    
                          System.out.println("Good job! You're right!");
                               bankroll = bankroll + bet;
                               wonGames++;
                               totalGames++;
                     }

How can I have my code read the choices 'Higher' or 'Lower' from the input provided by the user? Just so it can count whether or not the user won or lost the bet.

I don't get what you are trying to do from lines 16-23... can you write in English the steps you want and what is tripping you up? comment your code with what you are trying to do at each step.

System.out.print("\n\nDo you think the second card will be higher or lower?");
      String choiceString = scan.nextLine(); // Asks input from user //
 
      choiceString = choiceString.toLowerCase();
 
      PlayingCard cardOne = new PlayingCard(); // Creates new object called cardOne //
      PlayingCard cardTwo = new PlayingCard(); // Creates new objects called cardTwo //

// The face value of first card is less than the face value of the second card and is equal to the input "higher" //

                          if (cardOne.getface() < cardTwo.getTwo.getface() && choiceString.equals("higher"))

                               do
                               {
                                    cardTwo.drawCard(); // Draws a random playing card //
                               }
                               while (cardOne.isEquals(cardTwo));
 
                if (choiceString > 0)
                     if (choiceString == cardTwo) // Input is equal to second card drawn //
                     {    

                          // If the if statement is true, the program will print the following statement //

                          System.out.println("Good job! You're right!");

                               bankroll = bankroll + bet; // Adds the money bet to the user's bankroll //
                               wonGames++;  // Adds 1 to integer wonGames //
                               totalGames++;  // Adds 1 to integer totalGames //
                     }

You may have to eye my first code for reference on what I'm doing. If that still confuses you, I can post the requirements of my program.

Your first problem is in line 11. What does "getTwo" mean to the computer? Even if you did have parenthesis after it like "getTwo()", the code would still be invalid because there is no method getTwo() in the PlayingCard class. The line should read: if (cardOne.getface() < cardTwo.getTwo.getface() && choiceString.equals("higher")) The next errors you come across (lines 19-20) seem to be based on a lack of identifying what "choiceString" is. You defined it literally as the string that holds either the word "higher" or "lower". Does it make sense to say if ("higher">0) or if ("lower">0) ? Not really. Therefore, saying if (choiceString > 0) does not really make sense either.

A similar error occurs on the next line -- if (choiceString == cardTwo) . Can you say if (someString == someCard) ? No, because a string can never be a card; it doesn't logically make sense to compare the two. choiceString holds some string, and cardTwo holds some card, so comparing the two variables does not make logical sense. Make sure you compare strings to strings, cards to cards, integers to integers, etc.

If I were you I would recode this section according to the following pseudo-code:

Loop
    Prompt user to guess a number
    X <-- guessed number
    if (X = correct number)
        Congratulate user
    else
        Inform user whether the number is higher or lower than the number he guessed.
End loop when guessed correct.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.