-2

Im trying to design a game that plays the Hi/Lo card game until the user gets 4 correct answers in a row, although theres a problem with the code.. I dont know how to make it so the card number that pops up once the user states "Higher", "Lower" or "Equal" is the number that they were comparing the last 'cardGenerated' number to.

Right now its comparing it to a number that the user doesnt see or is unknown to, so they dont know wether they were right or wrong. I know i can just add the 'nextCard' variable into the showOptionDialog output although i'd prefer to just have one number being output, so if the program prints:

"The Card pulled is the 9
 Is the next card Higher, Lower or Equal?"

the next number/card outputted is the number that the user was comparing the previous number(9) to.

Also,

I have set the constants, but I'm not sure how to make it so instead of printing 11, 12, 13, 1, it prints JACK, QUEEN, KING, ACE, and what not.

import java.util.Random;
import javax.swing.JOptionPane;

public class HiLo {

public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
public static final int ACE = 1;

    public static void main(String[] args) {

        int correctGuesses = 0;

        Random generator = new Random();
        int currentCard;
        int nextCard = generator.nextInt( KING+1 );

        while (correctGuesses < 4)
        {           
            currentCard = nextCard;
            nextCard = generator.nextInt( KING+1 );

            Object[] options = {"Higher",
                "Lower",
                "Equal"};
            int Input = JOptionPane.showOptionDialog(null, 
                "The Card pulled is the " + currentCard +
                " \nis the next card Higher, Lower or Equal?",
                "HiLo Card Game",  
            JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null, options, options[0]);

            if ( nextCard > currentCard && Input == JOptionPane.YES_OPTION )
            {
                correctGuesses++;
            }
            else if ( nextCard > currentCard && Input == JOptionPane.NO_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard > currentCard && Input == JOptionPane.CANCEL_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard < currentCard && Input == JOptionPane.YES_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard < currentCard && Input == JOptionPane.NO_OPTION )
            {
                correctGuesses++;
            }

            else if ( nextCard < currentCard && Input == JOptionPane.CANCEL_OPTION )
            {   
                correctGuesses = 0;
            }

            else if ( nextCard == currentCard && Input == JOptionPane.YES_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard == currentCard && Input == JOptionPane.NO_OPTION )
            {
                correctGuesses = 0;
            }

            else if ( nextCard == currentCard && Input == JOptionPane.CANCEL_OPTION )
            {
                correctGuesses++;
            }       
        }

        JOptionPane.showMessageDialog(null,  "Congratulations, You guessed correctly 4 times"
                 + "\nthe Last Card was the " + nextCard + " resart to play again" );
    }

}
roughosing
  • 71
  • 11
  • 1
    http://stackoverflow.com/help/how-to-ask – David Waters Nov 02 '14 at 22:30
  • There are easier ways to write `0` than `correctGuesses - correctGuesses` – Deltharis Nov 02 '14 at 22:37
  • I know, its just that was the first mathematical way i thought of, i know i could just make it to correctGuesses = 0; but I just havent changed it to that because its not one of my biggest problems at the moment – roughosing Nov 02 '14 at 22:39
  • The shorter, cleaner and better the code relevant to your problem the more probable it is for someone to answer your question. It didn't matter now, it might when you encounter harder problems – Deltharis Nov 02 '14 at 23:01
  • true, i changed it just there, cheers for the advice – roughosing Nov 02 '14 at 23:07

2 Answers2

-1

If you want variable to not be contained within single loop iteration (and your problem wants you to use nextCard value in two iterations) you do not declare it within the loop. You also do not need new generator or options object every iteration.

Random generator = new Random();
int currentCard;
int nextCard = generator.nextInt( KING+1 );
while (correctGuesses < 4)
{
    currentCard = nextCard;
    nextCard = generator.nextInt( KING+1 );
    ...
}

As for cards printing - you should probably create an enum for cards, one containing relevant information (value, suite) as well as overriden toString method that takes care of printing. Writing that should be simple enough.

Deltharis
  • 2,320
  • 1
  • 18
  • 29
  • Thanks man, was thinking it was something like that although i was trying to do that within the loop which was my problem :P – roughosing Nov 02 '14 at 22:37
  • @GregPenrose Never change the core of the question to something completely different, especially after getting answers! As it was, my correct answer that solved your main problem appears to be irrelevant. StackOverflow has a one-question one-problem policy. – Deltharis Nov 02 '14 at 23:29
  • oh sorry, so should i delete the question now or jsut revert the changes? – roughosing Nov 02 '14 at 23:32
  • Revert the changes, accept my answer if it's enough, or just go read a guide on how to ask questions that was suggested in the first comment and write another one targetted at the card printing problem – Deltharis Nov 02 '14 at 23:34
-1

Nested if are messy. You should simplify so that you don't re-evaluate the same thing over and over. For instance, your first three layers evaluate nextCard > cardGenerated. If you extract that to its own if, the code will be more readable. You can also replace the other part of the evaluation (Input == JOptionPane.XX_OPTION) with a switch():

        if(nextCard > cardGenerated)
        {
            switch(input)
            {
                case JOptionPane.YES_OPTION:
                    correctGuesses++;
                    break;
                case JOptionPane.NO_OPTION:
                case JOptionPane.CANCEL_OPTION:
                    correctGuesses = 0;
                    break;
                default:
                    System.out.println("Should never happen, but default case should always be included");
            }
        }
        else if(nextCard < cardGenerated)
        {
            switch(input)
            {
                case JOptionPane.NO_OPTION:
                    correctGuesses++;
                    break;
                case JOptionPane.YES_OPTION:
                case JOptionPane.CANCEL_OPTION:
                    correctGuesses = 0;
                    break;
                default:
                    System.out.println("Should never happen, but default case should always be included");
            }
        }
        else
        {
            switch(input)
            {
                case JOptionPane.CANCEL_OPTION:
                    correctGuesses++;
                    break;
                case JOptionPane.YES_OPTION:
                case JOptionPane.NO_OPTION:
                    correctGuesses = 0;
                    break;
                default:
                    System.out.println("Should never happen, but default case should always be included");
            }
        }
hfontanez
  • 5,774
  • 2
  • 25
  • 37