# Mastermind!

• 12-08-2010, 11:17 AM
MishkaRX
Mastermind!
So I want to develop this Mastermind game, it's quite complete. It works with numbers instead of pegs. Yet I have to tweak it a little bit.

See I want to extend the program to allow repeated numbers in the correct sequence and in the candidate sequences. Instead of being 1234, I want to make it so it can be 1122 for example.

The main difficulty in this case is the calculation of the score
after a guess of the codebreaker.The implementation must also test if the program works properly with repeated numbers in the sequences.

This is al a bit complicated, could you guys lend me a hand?

Code:

```package practica3; import java.util.*; public class FirstPart {         public static void main(String[] args) {                 /*for (int i=0;i<20;i++){//Condition is true so it executes the instructions                         int x=generateRandomNumber(9);                         System.out.print(x + " ");                 }                 */                 /*  int []s;                 int []r;                 r=generateRandomSequence(4,8);                 s=readSequence(4,8);                 printSequence(s);                 printSequence(r);                 checkSequence(s,r);*/                 play();         }         public static int generateRandomNumber (int n){                 int r;                 r=(int) (1+(n-1)*Math.random());//{1,2,3...n-1}                 return r;         }         public static int [] generateRandomSequence (int l, int n){                 int [] sequence=new int [l];                 boolean repeated;                 int x;                 for(int i=0;i<l ;i++){                         do{                                 repeated=false;                                 x=generateRandomNumber(n+1);                                 for(int j=0;j<i && !repeated;j++){                                         if(x==sequence[j])                                                 repeated=true;                                 }                         } while(repeated==true);                         if(repeated==false){                                 sequence[i]= x;                         }                 }                 return sequence;         }                  public static void printSequence(int [] sequence){                 System.out.print("[ ");                 for(int i=0;i<sequence.length;i++){                         System.out.print(sequence[i]+" ");                 }                 System.out.print("]");         }         public static int [] readSequence (int l, int n){                 Scanner sc=new Scanner (System.in);                 System.out.println("Enter a sequence with values in {1,..., 8} ('s' to end)");                 int []sequence =new int [l];                 String seq=sc.next();                 if(seq.equals("s")||seq.equals("S")){                         System.out.println("Game over");                         System.exit(-1);                 }                 if(seq.length() != l) {                         System.out.println("The length is not correct");                         sequence[0] = 0;                 } else{                         for (int i=0;i<l;i++){                                                                sequence[i]=(int)(seq.charAt(i)- 48);                                 if(sequence[i]>n){                                         System.out.println("The value "+ sequence[i] + " is out of range");                                         sequence[0] = 0;                                                                                 }                                                         }                         boolean repeated = false;                         for (int i=0;i<l && !repeated ;i++){                                 for(int j=0;j<i;j++)                                         if (sequence[i]==sequence[j]){                                                 System.out.println("Repeated values are not allowed");                                                 repeated=true;                                                 sequence[0] = 0;                                         }                         }                 }                 return sequence;         }         public static void checkSequence(int[]cand, int []correct){                 int whites=0;                 int blacks=0;                 for(int i=0;i<correct.length;i++){                                 if(correct[i]==cand[i])                                         whites++;                         }                 for(int i=0;i<correct.length;i++){                 for (int j=0;j<correct.length;j++){                         if(correct[i]==cand[j])                                 blacks++;                 }                 }                 blacks=blacks-whites;                 if (whites==4){                         System.out.println("Correct code");                         System.out.println("The codebraker wins");                         System.out.println("The sequence is:");                         printSequence(correct);                         System.exit(-1);                 }                 else {                         System.out.println("\n" +whites+ " numbers are correct in value and position");                         System.out.println(blacks+ " numbers are correct in value");                         System.out.println("Sequence is not correct");                 }         }         public static void play (){                 int []correct;                 int []candidate;                 correct=generateRandomSequence(4,8);                 printSequence(correct);                 for (int i=1;i<=10;i++){                         System.out.println("Turn "+ i);                         candidate=readSequence(4,8);                         if(candidate[0]==0)                                         i--;                         else                                 checkSequence(candidate,correct);                         }                                                 System.out.println("Maximum number of turns reached. End of the game!");                 System.out.println("The correct sequence is:");                 printSequence(correct);                 System.out.println("Codemaker wins!");                 System.exit(-1);         }                 }```
• 12-08-2010, 11:34 AM
Eranga
If I'm get you correctly, what you want to do is allow the repeated numbers in a sequence, but always the incremental way. Am I correct?
• 12-08-2010, 10:32 PM
MishkaRX
Quote:

Originally Posted by Eranga
If I'm get you correctly, what you want to do is allow the repeated numbers in a sequence, but always the incremental way. Am I correct?

Not at all incremental, actually just random, but allowed to be repeated.

Like this for example: 1626

Completely random.
• 12-09-2010, 03:45 AM
Eranga
So, then it's easy.
• 12-09-2010, 03:49 AM
Eranga
Do you want to generate this by digits?
• 12-09-2010, 08:32 AM
MishkaRX
Quote:

Originally Posted by Eranga
Do you want to generate this by digits?

Yes indeed.
• 12-09-2010, 09:02 AM
JosAH
Quote:

Originally Posted by MishkaRX
The main difficulty in this case is the calculation of the score
after a guess of the codebreaker.

You have to define what the score is; e.g. suppose the secret code is 1234 and the code breaker guesses 1111. What would be the score? One peg correct and in the right place? Or one peg correct, in the right place plus three pegs correct, not in the right place?

kind regards,

Jos
• 12-13-2010, 12:38 PM
MishkaRX
I don't quite understand.
• 12-14-2010, 02:11 AM
Eranga
How did you match the user guess and the application generated value and define the score?
• 12-14-2010, 01:41 PM
MishkaRX
Allowing it to read repeated values got it a bit confused.

For example if the sequence is:

Code:

`5 2 3 7`
And the candidate sequence is:

Quote:

2 5 3 3
It says:

Quote:

1 numbers are correct in value and position
3 numbers are correct in value
Which is false, it should say:

Quote:

1 numbers are correct in value and position.
2 numbers correct in value.
Where is the extra correct value coming from?

Also if the sequence is:

Code:

`7 2 1 8`
And the candidate sequence is:

Quote:

2222
It says:

Quote:

1 numbers are correct in value and position
3 numbers are correct in value
Which is completely wrong. It has me quite confused as to how to "patch-up" the code to avoid these errors.
• 12-14-2010, 02:30 PM
gcalvin
Take one pass to count up digits that are correct in both value and position. Then take a second pass to count up digits that are correct only in value, but make sure to ignore all positions that were correct in your first pass.

-Gary-
• 12-14-2010, 02:39 PM
gcalvin
Also, in your second pass, take your guess digits one by one and remove matches from your code as you get matches.

Example:
Code:

``` CODE:  7212  GUESS: 2220 First pass:  Position 0 - no match  Position 1 - match  Position 2 - no match  Position 3 - no match Second pass:  Position 0 - no match on position 0             - ignore position 1             - no match on position 2             - match on position 3  Ignore position 1 (because it was matched in the first pass)  Position 2 - no match on position 0             - ignore position 1             - no match on position 2             - ignore position 3 (because it has been matched already)  Position 3 - no match on position 0             - ignore position 1             - no match on position 2             - ignore position 3 (because it has been matched already)```
Hope that helps.

-Gary-