Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Guessing Game

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Guessing Game

    Hello, I am trying to create a program that plays a game that has four different coloured blocks (red, green, blue, and yellow). The computer hides three of these coloured blocks from the user and then the user will try to guess the colours and order of the blocks. After display the colour of the three hidden blocks, the computer will display how many are correct and how many colours are in the right position. Based on the following information, the user can make another guess and so on until the user has determined the correct order and colour of the blocks. We have to use the following code - checkColoursCorrect(), checkPositionsCorrect(), randomWholeNumber() and newGame() which will write the code to generate three unique numbers from 1 to 4 for the block numbers. Here is what I have so far:

     
    package guesscolorblocks;
    import javax.swing.JOptionPane;
    import java.lang.String;
     
    public class GuessColorBlocks {
     
        private static int colours = 0, positions = 0; 
        /**
         *
         */
        public static void main(String[] args) {
            // Declare variables 
            int userSelection;
     
            //Set a button text in the array
            Object[] options = new String[] {"Yes", "No"};
     
            //Display the menu to allow the user to click an option
            int menu = JOptionPane.showOptionDialog(null, "Welcome to the Guess the Blocks Game!"
                    + "\nReady to start?:", "Red, Green, Blue, or Yellow", 
            JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
     
        if (menu == JOptionPane.YES_OPTION) {
                playGame();
            } else if (menu == JOptionPane.NO_OPTION) {
                System.exit(0);
            }
        }
        public static void playGame () {
     
        String firstGuess = "", secondGuess = "", thirdGuess = "";
        int red, blue, green, yellow;
     
        String inputfirstGuess = JOptionPane.showInputDialog("Enter your first guess: (Red, Green, Blue, Yellow):");
        String inputsecondGuess = JOptionPane.showInputDialog("Enter your second guess: (Red, Green, Blue, Yellow):");
        String inputthirdGuess = JOptionPane.showInputDialog("Enter your third guess: (Red, Green, Blue, Yellow):");        
     
        int computerSelection = (int) ((Math.random() * 4) + 1);
     
            if (firstGuess.equalsIgnoreCase("red")) {
                firstGuess = "red";
                }
            else if (firstGuess.equalsIgnoreCase("green")){
                firstGuess = "green";
            }
            else if (firstGuess.equalsIgnoreCase("blue")){
                firstGuess = "blue";
            }
            else if (firstGuess.equalsIgnoreCase("yellow")){
                firstGuess = "yellow";
            }
     
     
            if (secondGuess.equalsIgnoreCase("red")) {
                secondGuess = "red";
                }
            else if (secondGuess.equalsIgnoreCase("green")){
                secondGuess = "green";
            }
            else if (secondGuess.equalsIgnoreCase("blue")){
                secondGuess = "blue";
            }
            else if (secondGuess.equalsIgnoreCase("yellow")){
                secondGuess = "yellow";
            }
     
     
            if (thirdGuess.equalsIgnoreCase("red")) {
                thirdGuess = "red";
                }
            else if (thirdGuess.equalsIgnoreCase("green")){
                thirdGuess = "green";
            }
            else if (thirdGuess.equalsIgnoreCase("blue")){
                thirdGuess = "blue";
            }
            else if (thirdGuess.equalsIgnoreCase("yellow")){
                thirdGuess = "yellow";
            }
          }
       }


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game

    You are asking us to code your "Mastermind" clone for you. What do you specifically need help with?

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game

    Well, I am trying to incorporate with the Math.random() function that the computer will randomly select a colour and if the user selection ex. blue matches the computer's selection with blue then it will input the correct number of colours guessed and the correct position they are in. Then the program will keep playing until the user gets it correctly. I don't know if that makes sense but all I am saying is I want to add the Math.random() function into the program for all cases.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Guessing Game

    Can you explain what problems you are having using the Math.random() method?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    8
    My Mood
    Starving
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Guessing Game

    Are you in Mr. Fesko's class as well?

Similar Threads

  1. Guessing game help
    By np657 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 12th, 2012, 07:39 AM
  2. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 02:30 PM
  3. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 02:50 PM
  4. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 09:46 PM
  5. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 12:41 PM