Results 1 to 20 of 20
Thread: what am i doing wrong here??
- 04-15-2009, 01:50 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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
- 04-15-2009, 02:09 AM #2
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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.
- 04-15-2009, 02:18 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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
- 04-15-2009, 02:20 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
the broken code is in red and bold writing. the code continues, but i had to shorten it.
thanks for any help again :)
- 04-15-2009, 03:17 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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.
- 04-15-2009, 08:50 AM #6
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
do you think '1' == 1? no, they are differenceJava Code:if (choice == cpuNUM1 && choice2 == cpuNUM2 && choice3 == cpuNUM3)
char '1' ... if you force it to be int... is 49
- 04-15-2009, 11:30 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 25
- Rep Power
- 0
The problem here is your trying to see if a character is equal to an integer...
I know that its messy but it'll work.Java Code:char n = '4'; int i = 4; if (n == ("" + i).charAt(0)) { System.out.println("Equal!"); }
- 04-15-2009, 02:52 PM #8
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-16-2009, 12:08 AM #9
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
THANKS SO MUCH, it worked, ive never used that sort of code before, but you guys know what your doing
thanks again
- 04-16-2009, 12:12 AM #10
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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;
now i want to delcare it as static char yes;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........ }
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 :)
- 04-16-2009, 12:21 AM #11
You're welcome...
Glad it worked. On the curious side... do you know what is happening in the code that hawaiian_robots suggested?:
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.Java Code:if (n == ("" + i).charAt(0))
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-16-2009, 12:27 AM #12
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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.
- 04-16-2009, 01:30 AM #13
go ahead and try it and see if it works
Luck,Java Code:(guess == 'Y')//probably be a good idea to use the toUpperCase() or toLowerCase() methods with the guess variable { c.println ("hi"); }
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-16-2009, 01:35 AM #14
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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")))
- 04-16-2009, 02:02 AM #15
You mean something like:
Luck,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 (); }
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-16-2009, 02:08 AM #16
Member
- Join Date
- Mar 2009
- Posts
- 25
- Rep Power
- 0
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...
- 04-16-2009, 02:16 AM #17
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 04-16-2009, 02:36 AM #18
Member
- Join Date
- Mar 2009
- Posts
- 25
- Rep Power
- 0
Yeah I just thought I better explain the code fully. And the downsides of using such a method.
- 04-16-2009, 03:37 AM #19
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
you may want something like this for your loop...
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.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)
Good Luck,
Singing BoyoIf 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!
- 04-16-2009, 03:54 AM #20
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
what wrong
By pro85 in forum New To JavaReplies: 3Last Post: 02-09-2009, 01:07 PM -
What did i do wrong
By jpnym15 in forum New To JavaReplies: 8Last Post: 11-17-2008, 10:07 AM -
what's wrong in here!!!
By Annatar in forum New To JavaReplies: 8Last Post: 11-14-2008, 02:55 AM -
right or wrong
By jot321 in forum New To JavaReplies: 7Last Post: 09-25-2008, 11:45 AM -
I am Doing Something Wrong But Don't Know What?
By BHCluster in forum New To JavaReplies: 3Last Post: 04-16-2008, 01:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks