Thread: Help creating a Masterminds game.

1. Member
Join Date
Nov 2009
Posts
3
Rep Power
0

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:

Java 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+" ");

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)

Java Code:
```b y y
bbb
1 colors correct
1 positions correct
b y y
yyy
1 colors correct
2 positions correct
b y y
byb
1 colors correct
2 positions correct
b y y

2. 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

Java 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.

Java 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.
Last edited by CodesAway; 11-08-2009 at 09:31 AM.

3. Member
Join Date
Nov 2009
Posts
3
Rep Power
0
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.

4. Member
Join Date
Nov 2009
Posts
3
Rep Power
0
no more help?

5. 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.

For the "guess" "RBR", The first and second peg's color is the same in both, so the "position" counter is two. For the remaining peg (the third one), even though a red appears in the "answer", it is already accounted by a "position" peg, so it is not given a "color" peg. The sum of position and color pegs is the total number of correct pegs. There are only two correct pegs, because if there were three, then the "answer" would have to be some combination of the "guess", which it is not.

For this guess, the "position" counter would be 2 and the "color" counter would be 0.

I'm confused why you expect two for the "color" counter. For each peg, it's either 1) the right color, in the correct position, 2) the right color, in the wrong position, or 3) wrong color. The first is the "position" counter. The second is the "color" counter. The third is a "blank" (no counter). A single peg fits in one (and only one) of these three categories.

If the solution was 2 and 2, that means that two pegs are the right color, in the right position, and two pegs are the right color, in the wrong position. This CANNOT happen with only three pegs. So, your purposed solution doesn't follow the rules of mastermind.

Posting Permissions

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