# Help creating a Masterminds game.

• 11-08-2009, 08:35 AM
viper98
Help creating a Masterminds game.
ok, *should be* simple little game. The program randomly chooses 3 numbers between 1&4 then assigns either an R, G, B, or Y to those 3 variables. Then it ask's for a guess and tells the user how many colors they guessed right and how many of their guesses are in the right position.

My problem is when it assigns 2 of the same letters to 2 of the variables. For example if the 3 were RBB. When i give it the input of, for example, RBR it should say i got 2 colors right and 2 in the right position. But instead it says there is one color, and 2 positions. I hope ya'll can figure out the program and what i am trying to do. This was a project for my Java I class and rendomizing it was extra credit. Its past the due date, but i just hate starting it and not figuring it out, thats just me, have to figure out everything :D.

So if im on the completely wrong track with trying to use "flag" boolean's. Tell me. Anything that could help me out with this would be greatly appreciated. But keep in mind ive only been programming for about 2 months now.

Thank you for any help!

MY code:

Code:

```import java.util.Scanner; public class MastermindsEXTRACREDIT {         public static void main(String[] args)         {         Scanner keyboard = new Scanner(System.in);         char s1='r';         char s2='y';         char s3='b';         int rand1 = (int)(4*Math.random()+1);         int rand2 = (int)(4*Math.random()+1);         int rand3 = (int)(4*Math.random()+1);         if (rand1==1)                 s1='r';         if (rand1==2)                 s1='g';         if (rand1==3)                 s1='y';         if (rand1==4)                 s1='b';         //s1^         if (rand2==1)                 s2='r';         if (rand2==2)                 s2='g';         if (rand2==3)                 s2='y';         if (rand2==4)                 s2='b';         //s2^         if (rand3==1)                 s3='r';         if (rand3==2)                 s3='g';         if (rand3==3)                 s3='y';         if (rand3==4)                 s3='b';         //s3^                 boolean flag=true;         boolean colorflag1=true;         boolean colorflag2=true;         boolean colorflag3=true;         int position=0, color=0;         while (flag==true)         {                                                 System.out.print(s1+" ");                 System.out.print(s2+" ");                 System.out.println(s3+" ");                 System.out.println("Whats your guess?");                 String guess1 = keyboard.next();                 char g1 = guess1.charAt(0);                 char g2 = guess1.charAt(1);                 char g3 = guess1.charAt(2);                         if (s1==g1&&s2==g2&&s3==g3)                 {                         System.out.println("You guessed it! Good job!");                         flag=false;                 }                 else                 {                         if (s1==g1)                                 position++;                         if (s2==g2)                                 position++;                         if (s3==g3)                                 position++;                         //Correct position^                         if (g1==s1)                         {                                 color++;                                 colorflag1=false;                         }                         if (g1==s2&&colorflag1==true)                         {                                 color++;                                 colorflag1=false;                         }                         if (g1==s3&&colorflag1==true)                         {                                 color++;                                 colorflag1=false;                         }                         //FIRST^                         if (g2==s1&&colorflag1==true)                         {                                 color++;                                 colorflag2=false;                         }                         if (g2==s2&&colorflag2==true&&colorflag1==true)                         {                                 color++;                                 colorflag2=false;                         }                         if (g2==s3&&colorflag2==true&&colorflag1==true)                         {                                 color++;                                 colorflag2=false;                         }                         //SECOND^                         if (g3==s1&&colorflag1==true&&colorflag2==true)                         {                                 color++;                                 colorflag3=false;                         }                         if (g3==s2&&colorflag3==true&&colorflag1==true&&colorflag2==true)                         {                                 color++;                                 colorflag3=false;                         }                         if (g3==s3&&colorflag3==true&&colorflag1==true&&colorflag2==true)                         {                                 color++;                                 colorflag3=false;                         }                                                 }                 if (flag==true)                         {                         System.out.println(color+" colors correct");                         System.out.println(position+" positions correct");                         position=0;                         color=0;                         colorflag1=true;                         colorflag2=true;                         colorflag3=true;                         }         }         } }```
Example of where the output is not what i want it to be. (the 3 letters above "whats your guess?" are the 3 ones im trying to guess, its a "cheat" my teacher asked for so he knows what the randoms are)

Code:

```b y y Whats your guess? bbb 1 colors correct 1 positions correct b y y Whats your guess? yyy 1 colors correct 2 positions correct b y y Whats your guess? byb 1 colors correct 2 positions correct b y y Whats your guess?```
• 11-08-2009, 09:26 AM
CodesAway
For setting your colors, why don't you use an array that contains the four values and retrieve it based on index? Or, create an enum type with the four colors, and add a method that returns a random color?

You can change this code

Code:

```char s1='r'; char s2='y'; char s3='b'; int rand1 = (int)(4*Math.random()+1); int rand2 = (int)(4*Math.random()+1); int rand3 = (int)(4*Math.random()+1); if (rand1==1)         s1='r'; if (rand1==2)         s1='g'; if (rand1==3)         s1='y'; if (rand1==4)         s1='b'; //s1^ if (rand2==1)         s2='r'; if (rand2==2)         s2='g'; if (rand2==3)         s2='y'; if (rand2==4)         s2='b'; //s2^ if (rand3==1)         s3='r'; if (rand3==2)         s3='g'; if (rand3==3)         s3='y'; if (rand3==4)         s3='b'; //s3^```
Into this cleaner, but equivalent form.

Code:

```char[] colors = { 'r', 'g', 'y', 'b' }; int numOfColors = colors.length; char s1 = colors[(int) (numOfColors * Math.random())]; char s2 = colors[(int) (numOfColors * Math.random())]; char s3 = colors[(int) (numOfColors * Math.random())];```

I would presume the rest of your code could be refactored as well.

Also, as a suggestion, don't use numbers in your code. As you see above, instead of using 4, I created a variable numOfColors. "Magic numbers" like these are confusing when you read code, because the question becomes "what is the 4 for?" Now, it's obvious that the four is the number of colors. Also, using this method, you can add more colors later and still use the same exact code. Note, instead of using colors.length in each place, I decided to create a variable, because numOfColors seems more descriptive than colors.length.
• 11-08-2009, 07:24 PM
viper98
ok, thank you for that. Much simpler. The way my teacher teaches thing, we hadnt learned that yet. I was using the on method of randomizing that we had learned. But i do get what you did there. Thank you, but the real issue in my program is inside the while loop.
• 11-09-2009, 04:10 AM
viper98
no more help?
• 11-09-2009, 05:13 AM
CodesAway
Sorry, I was out all day. Can you describe what the while loop does (in English). The code looks much more complicated than I would expect it to be, so I'm hoping that in understanding what the loop does, I can help you toward a solution, while yielding cleaner code in the process.

I'm familiar with the rules of mastermind, so this is what I'm expecting the loop to do.

1) The user inputs their "guess"
2) The "guess" is compared to the "answer"
2a) If the correct color is in the correct position, then increase the "position" counter
2b) For the remaining pegs, if the peg's color matches ANY of the remaining pegs in the "answer", increase the "color" counter.

For this, I referenced the wikipedia page for Mastermind, under "gameplay and rules", Mastermind - gameplay and rules.