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

