1. Member Join Date
Dec 2010
Posts
5
Rep Power
0

## 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?

Java 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);
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;
} 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;

}

}

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;
}

}
}

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);
if(candidate==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);
}

}```  Reply With Quote

2. ## 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?  Reply With Quote

3. Member Join Date
Dec 2010
Posts
5
Rep Power
0

##  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.  Reply With Quote

4. ## So, then it's easy.  Reply With Quote

5. ## Do you want to generate this by digits?  Reply With Quote

6. Member Join Date
Dec 2010
Posts
5
Rep Power
0

##  Originally Posted by Eranga Do you want to generate this by digits?
Yes indeed.  Reply With Quote

7. ##  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  Reply With Quote

8. Member Join Date
Dec 2010
Posts
5
Rep Power
0

## I don't quite understand.  Reply With Quote

9. ## How did you match the user guess and the application generated value and define the score?  Reply With Quote

10. Member Join Date
Dec 2010
Posts
5
Rep Power
0

## Allowing it to read repeated values got it a bit confused.

For example if the sequence is:

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

2 5 3 3
It says:

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

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:

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

2222
It says:

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.  Reply With Quote

11. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
10

## 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-  Reply With Quote

12. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
10

## Also, in your second pass, take your guess digits one by one and remove matches from your code as you get matches.

Example:
Java 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-  Reply With Quote

#### Posting Permissions

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