Results 1 to 12 of 12

Thread: Mastermind!

  1. #1
    MishkaRX is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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?

  3. #3
    MishkaRX is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    MishkaRX is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Do you want to generate this by digits?
    Yes indeed.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by MishkaRX View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    MishkaRX is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    I don't quite understand.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    MishkaRX is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    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.

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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. #12
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

Similar Threads

  1. MasterMind Problem!!
    By papinhio in forum New To Java
    Replies: 4
    Last Post: 07-09-2010, 03:51 PM
  2. how to make mastermind game
    By javabeginer in forum New To Java
    Replies: 10
    Last Post: 04-14-2009, 02:11 AM

Posting Permissions

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