Results 1 to 20 of 20
  1. #1
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default what am i doing wrong here??

    well im trying to design a game, and everything works right, except for this part. ive already declared everything, and i dont get any errors, its just that "you win" isint comming up. its in a method, and the whole game works, its just not comming up.

    Java Code:
    public static void guess1 ()
        {
            choice = c.getChar ();
            //ALL COMBINATIONS STARTING WITH 1
            if (choice == '1')
            {
                c.setColor (Color.red);
                c.fillRoundRect (10, 130, 50, 50, 10, 10);
    
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
    
            }
            //ALL COMBINATIONS STARTING WITH 2
            if (choice == '2')
            {
                c.setColor (Color.blue);
                c.fillRoundRect (10, 130, 50, 50, 10, 10);
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
            }
            //ALL COMBINATIONS STARTING WITH 3
            if (choice == '3')
            {
                c.setColor (Color.green);
                c.fillRoundRect (10, 130, 50, 50, 10, 10);
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
            }
    [B][B][B]        if (choice == cpuNUM1 && choice2 == cpuNUM2 && choice3 == cpuNUM4)
            {
                c.println ("you win");
    
            }
            else
            {
                guess2 ();
            }
        }[/B][/B][/B]

    the bolded text is the problem. if i have the computer generate a random number, and i type in the number that it generates, it should show up that i win, instead, it just goes to guess2 ();

    what am i doing wrong here? im using RTP HSA template. the program runs fine, its just not comming up that im winning.

    i could upload the full file if its needed

    any help would be great thanks

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    your conditional statement "choice == cpuNUM1 && choice2 == cpuNUM2 && choice3 == cpuNUM4" is being evaluated as false, obviously. since you don't define cpuNUM1/2/4 (what happened to 3?) in this code, can't help you there really.

  3. #3
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    sorry, i guess i typed it in wrong when i was adding it to the site. here is the full code. ive declared everything. each cpuNUM is a random number (between 1 and 3) generated and guess should equal cpuNUM1, guess2 should equal cpuNUM2. and guess3 should equal cpuNUM3, therefore printing the statement "you win" but instead just goes to the next one. it has to be something with that statment as you said, but it looks right to me. the code seems long, but its just the same code repeated, and im only putting this portion of code that im habing problem with in the first method (named guess1)

    Java Code:
    // The "Mastermind" class.
    import java.awt.*;
    import hsa.Console;
    
    public class Mastermind2
    {
        static Console c;           // The output console
        static int cpuNUM1, cpuNUM2, cpuNUM3, guess1, guess2, guess3, guess4, guess5, guess6, number, counter, h1, h2, h3, h4, h5, h6;
        static int result;
        static int replay;
        static char guess, choice, choice2, choice3;
        public static void main (String[] args)
        {
            c = new Console ();
    
            cpuNUM1 = (int) (Math.random () * 3 + 1);
            cpuNUM2 = (int) (Math.random () * 3 + 1);
            cpuNUM3 = (int) (Math.random () * 3 + 1);
            c.print (cpuNUM1);
            c.print (cpuNUM2);
            c.print (cpuNUM3);
    
            c.setFont (new Font ("Times New Roman", 1, 15));
            c.drawString ("You have THREE different colour cubes to choose from", 0, 30);
            c.drawString ("Enter Your Guess [1] [2] [3] YOU HAVE 6 GUESSES!", 0, 120);
            c.drawRect (0, 445, 280, 20);
            c.drawString ("", 0, 465);
            c.drawString ("RED = 1", 5, 460);
            c.drawString ("BLUE = 2", 100, 460);
            c.drawString ("Green  = 3", 200, 460);
            c.drawString ("type in any combination of 1, 2, 3 to try and guess the computers order of Colours", 0, 481);
            c.drawString ("EG: 123, 321 etc", 0, 500);
    
            //while (counter < 3)
            //{
            //number ();
            //}
            c.setFont (new Font ("Times New Roman", 1, 60));
            //red cube
            c.setColor (Color.red);
            c.fillRoundRect (10, 50, 50, 50, 10, 10);
            //blue cube
            c.setColor (Color.blue);
            c.fillRoundRect (70, 50, 50, 50, 10, 10);
            //green cube
            c.setColor (Color.green);
            c.fillRoundRect (130, 50, 50, 50, 10, 10);
            //
            c.setColor (Color.white);
            c.drawString ("1", 20, 95);
            c.drawString ("2", 82, 95);
            c.drawString ("3", 144, 95);
            guess1 ();
    
    
    
            // Place your program here.  'c' is the output console
        } // main method
    
    
        public static void guess1 ()
        {
            choice = c.getChar ();
            //ALL COMBINATIONS STARTING WITH 1
            if (choice == '1')
            {
                c.setColor (Color.red);
                c.fillRoundRect (10, 130, 50, 50, 10, 10);
    
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
    
            }
            //ALL COMBINATIONS STARTING WITH 2
            if (choice == '2')
            {
                c.setColor (Color.blue);
                c.fillRoundRect (10, 130, 50, 50, 10, 10);
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
            }
            //ALL COMBINATIONS STARTING WITH 3
            if (choice == '3')
            {
                c.setColor (Color.green);
                c.fillRoundRect (10, 130, 50, 50, 10, 10);
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 130, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 130, 50, 50, 10, 10);
                    }
    
                }
            }
    
            [B][FONT="Arial Black"][COLOR="Red"]if (choice == cpuNUM1 && choice2 == cpuNUM2 && choice3 == cpuNUM3)
            {
                c.println ("you win");
    
            }
            else
            {
                guess2 ();
            }[/COLOR][/FONT][/B]
        }
    
    
        public static void guess2 ()
        {
            choice = c.getChar ();
            //ALL COMBINATIONS STARTING WITH 1
            if (choice == '1')
            {
                c.setColor (Color.red);
                c.fillRoundRect (10, 182, 50, 50, 10, 10);
    
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 182, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    guess3 ();
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 182, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    guess3 ();
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 182, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    guess3 ();
                }
    
            }
            //ALL COMBINATIONS STARTING WITH 2
            if (choice == '2')
            {
                c.setColor (Color.blue);
                c.fillRoundRect (10, 182, 50, 50, 10, 10);
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 182, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    guess3 ();
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 182, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    guess3 ();
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 182, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    guess3 ();
                }
            }
            //ALL COMBINATIONS STARTING WITH 3
            if (choice == '3')
            {
                c.setColor (Color.green);
                c.fillRoundRect (10, 182, 50, 50, 10, 10);
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 182, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    guess3 ();
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 182, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    guess3 ();
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 182, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 182, 50, 50, 10, 10);
                    }
                    guess3 ();
                }
            }
    
        }
    
    
        public static void guess3 ()
        {
            choice = c.getChar ();
            //ALL COMBINATIONS STARTING WITH 1
            if (choice == '1')
            {
                c.setColor (Color.red);
                c.fillRoundRect (10, 234, 50, 50, 10, 10);
    
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 234, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    guess4 ();
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 234, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    guess4 ();
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 234, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    guess4 ();
                }
    
            }
            //ALL COMBINATIONS STARTING WITH 2
            if (choice == '2')
            {
                c.setColor (Color.blue);
                c.fillRoundRect (10, 234, 50, 50, 10, 10);
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 234, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    guess4 ();
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 234, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    guess4 ();
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 234, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    guess4 ();
                }
            }
            //ALL COMBINATIONS STARTING WITH 3
            if (choice == '3')
            {
                c.setColor (Color.green);
                c.fillRoundRect (10, 234, 50, 50, 10, 10);
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 234, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    guess4 ();
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 234, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    guess4 ();
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 234, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 234, 50, 50, 10, 10);
                    }
                    guess4 ();
                }
            }
    
        }
    
    
        public static void guess4 ()
        {
            choice = c.getChar ();
            //ALL COMBINATIONS STARTING WITH 1
            if (choice == '1')
            {
                c.setColor (Color.red);
                c.fillRoundRect (10, 286, 50, 50, 10, 10);
    
                choice2 = c.getChar ();
                //all combinations with middle digit of 1
                if (choice2 == '1')
                {
                    c.setColor (Color.red);
                    c.fillRoundRect (65, 286, 50, 50, 10, 10);
    
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 286, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 286, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 286, 50, 50, 10, 10);
                    }
                    guess5 ();
                }
                //all combinations with middle digit of 2
                if (choice2 == '2')
                {
                    c.setColor (Color.blue);
                    c.fillRoundRect (65, 286, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 286, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 286, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 286, 50, 50, 10, 10);
                    }
                    guess5 ();
                }
    
                //all combinations with middle digit of 3
                if (choice2 == '3')
                {
                    c.setColor (Color.green);
                    c.fillRoundRect (65, 286, 50, 50, 10, 10);
                    choice3 = c.getChar ();
                    //ending with 1
                    if (choice3 == '1')
                    {
                        c.setColor (Color.red);
                        c.fillRoundRect (120, 286, 50, 50, 10, 10);
                    }
                    //ending with 2
                    if (choice3 == '2')
                    {
                        c.setColor (Color.blue);
                        c.fillRoundRect (120, 286, 50, 50, 10, 10);
                    }
                    //ending with 3
                    if (choice3 == '3')
                    {
                        c.setColor (Color.green);
                        c.fillRoundRect (120, 286, 50, 50, 10, 10);
                    }
                    guess5 (); //ETC
                }
    mind3 class
    
    
    // Random1 class

  4. #4
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    the broken code is in red and bold writing. the code continues, but i had to shorten it.

    thanks for any help again :)

  5. #5
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    when i say c.println (cpuNUM + choice);

    depending on what i put in , it is always 50, 51, or 54... why??? im only adding a maximum of 3 + 3... im confused, if anyone can help that would be great.

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Java Code:
    if (choice == cpuNUM1 && choice2 == cpuNUM2 && choice3 == cpuNUM3)
    do you think '1' == 1? no, they are difference

    char '1' ... if you force it to be int... is 49

  7. #7
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    The problem here is your trying to see if a character is equal to an integer...

    Java Code:
    char n = '4';
    int i = 4;
    if (n == ("" + i).charAt(0)) {
       System.out.println("Equal!");
    }
    I know that its messy but it'll work.

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Convert char to int

    ... and another way to do it (and maybe less messier) would be:
    Java Code:
    char n = '5';
    int i = 5;
    if (i == Integer.parseInt(Character.toString(n))) {
       System.out.println("Equal!");
    }

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    THANKS SO MUCH, it worked, ive never used that sort of code before, but you guys know what your doing

    thanks again

  10. #10
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    i have one last question...

    im creating a while loop for the game to loop, and usually i always have to hit enter to continue. heres my code;

    Java Code:
    c.println ("do you want to play mastermind (y)es or (n)o
      reply = c.readLine ();
            c.clear ();
            while ((reply.equals ("y")))
            {
           CODE ETC........
            }
    now i want to delcare it as static char yes;

    yes = c.getChar ();

    and i want the user to hit y without hitting enter. the top code works fine, but is it possible to declare char as "y" in a while loop? what would i put in the while loop at the beginning??

    thanks again for the help :)

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default You're welcome...

    Glad it worked. On the curious side... do you know what is happening in the code that hawaiian_robots suggested?:
    Java Code:
    if (n == ("" + i).charAt(0))
    As for what you want to do with the "Y". I don't know of a way of inputing data without hitting enter on the command prompt (you would have to do something funky like capture/intercept the keyboard strokes). Maybe somebody else has a suggestion for that.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    i actually didnt try that one, but there is a way to have it without hitting enter, ive done it for my whole code

    for me, i declared this and did this:

    static char guess, choice, choice2, choice3;

    guess = c.getChar ();

    if (guess ==1)
    {
    c.println ("hi");
    }

    and thats instead of doing the c.readINT. if the user hits 1, it goes right to the if statement, without hitting enter

    now i want to incorporate that into the while loop, but dont know how to write it, or if char can be a letter.
    Last edited by tornbacchus; 04-16-2009 at 12:31 AM.

  13. #13
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    go ahead and try it and see if it works
    Java Code:
    (guess == 'Y')//probably be a good idea to use the toUpperCase() or toLowerCase() methods with the guess variable
    {
    c.println ("hi");
    }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  14. #14
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    o i forgot to add the ' ' on on the site, but do you know how to incorporate that into a while loop? if you do that would be great

    while ((reply.equals ("y")))

  15. #15
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    You mean something like:

    Java Code:
    char guess = 'y';
    
    while (guess.toLowerCase() == 'y')
            {
             CODE ETC........
             ...
             system.out.println("Do you want to play again: y or n?")
             guess = c.getChar ();
            }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  16. #16
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post
    Glad it worked. On the curious side... do you know what is happening in the code that hawaiian_robots suggested?:
    Java Code:
    if (n == ("" + i).charAt(0))
    CJSL
    All strings in are an instance of the string class. So what I did was parse the int into a string ("" + 1) then get the first element of the string (charAt(0)). This won't work if you type in a negative number or number greater then 9 (it will only ever get the first character, so "30" will be '3' and -1 will be '-'). It was purely a hack method cause I just couldn't remember where the string parser was LOL.

    In hindsight you probably shouldn't do it that way...

  17. #17
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Hack: a short, rasping dry cough

    yeah, it's a hack... for testing an idea (and not remembering the correct methods to use), it's OK, but it would be frowned opon by proffesionals and some teachers. BTW... my question was directed to the OP... was just wondering if he/she understood it.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  18. #18
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    Yeah I just thought I better explain the code fully. And the downsides of using such a method.

  19. #19
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    you may want something like this for your loop...

    Java Code:
    char cont = 'y';
    //any code you need
    while(cont == 'y' || cont == 'Y'){
    //game code
         int validate = 0;
         //loops until a valid answer to the question "Play Again?" is entered
         while(validate == 0){
              c.println("Play Again?");
              cont = c.getChar();
              //ensures that y, Y, n, or N were entered
              if(cont != 'y' && cont != 'n' && cont 'Y' && cont != 'N')
                   c.println("ERROR: Illegal answer");
              else
                   //causes the loop to exit
                   validate = 1;
         }//exit validate loop
    }//exit game loop
    System.exit(0)
    This is a loop for determining whether the user wishes to continue. Assuming your getChar() method works as I believe it does, this should work, as a char is a single character which can be a number, letter, or symbol.

    Good Luck,
    Singing Boyo
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  20. #20
    tornbacchus is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    alright that works great almost, ive never used System.exit, do i have to declare it or anything?? it starts up fine, but if i say n or N, it still starts. im trying to work it out, but thanks for that answer, it helped me alot

Similar Threads

  1. what wrong
    By pro85 in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 01:07 PM
  2. What did i do wrong
    By jpnym15 in forum New To Java
    Replies: 8
    Last Post: 11-17-2008, 10:07 AM
  3. what's wrong in here!!!
    By Annatar in forum New To Java
    Replies: 8
    Last Post: 11-14-2008, 02:55 AM
  4. right or wrong
    By jot321 in forum New To Java
    Replies: 7
    Last Post: 09-25-2008, 11:45 AM
  5. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 01:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •