Results 1 to 3 of 3
  1. #1
    Ebayer is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default MasterMind Assignement , Use of String Class

    Hi,

    im trying to solve this last bit of mastermind assignment im on from my book and ive been stuck on this for a while now.
    i have to solve this using string class according to assigment.
    the part im troubled with is the part were im supposed to compare the user input against the computer generated code.
    if your familiar with mastermind game you might have used colors instead of numerical code like i did.
    but the principal remains the same.
    the game will generate a 4 digit code wich i convert to string , same is done for user input string.
    then i compare the 2 string and any number that match will gain the player a cow for each correct guess, e.g
    Generated code : 1234
    user input: 1278

    this will result in 2 right guesses and give 2 cows.
    the player will also recieve 1 sheep for each number he guessed wich was present in the generated code but not at the exact location.
    this is the part im strugling with.
    can anyone please shed some light on this for me?
    any help would be appriciated.
    i just need some pointers not the solution.
    heres my code so far;


    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    class  Master {
    public static void main(String[] args){
    	
    	System.out.println("MASTERMIND v.1");                //  Startup dialouge.
    	System.out.println("I want you to guess the secret code.");
    	System.out.println("NOTE! The code consists of 4 digits.");
    	System.out.println("Please input your guess");
    	
    	int cows = 0;
     	int sheeps = 0;
    	int input;
    	 
    	
    	
    	
    	int code = (int)(Math.random() * 9999); // create a random four digit number.
    	
    	
    	
    	
    	String codeString = Integer.toString(code); // convert Random code to String.
    	
    	
    	 Scanner temp = new Scanner (System.in); // initiate code for input.
         input = temp.nextInt();
         
         String inputString = Integer.toString(input); // convert input  to String.
         
     
         
        
        
         
        if ( inputString.charAt(0) == codeString.charAt(0)){
            cows++;}
        else{
            	//good to go.
            }
        	
        if ( inputString.charAt(1) == codeString.charAt(1)){
            cows++;}
        else{
            	//good to go.
            } 
        	
        if ( inputString.charAt(2) == codeString.charAt(2)){
            cows++;}
        else{
            	//good to go.
            }
        	
        if ( inputString.charAt(3) == codeString.charAt(3)){
            cows++;}
        else{
            	//good to go.
            }
        
        
        System.out.println(cows+"cows");    
        System.out.println(sheeps+"sheeps");
        
    }
    }

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    After searching for exact matches, you'll want to search for those present elsewhere. However, you don't want anything showing up twice, so how about setting each character to null (in a copy of the string, if you want to preserve the original) after it returns a cow?

  3. #3
    Ebayer is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    thanks for your reply.
    really apriciate it , ill try it out as soon as i get the time.

Similar Threads

  1. Mastermind!
    By MishkaRX in forum New To Java
    Replies: 11
    Last Post: 12-14-2010, 01:39 PM
  2. MasterMind Problem!!
    By papinhio in forum New To Java
    Replies: 4
    Last Post: 07-09-2010, 03:51 PM
  3. My own string class
    By viperlasson in forum New To Java
    Replies: 8
    Last Post: 01-25-2010, 05:55 PM
  4. how to make mastermind game
    By javabeginer in forum New To Java
    Replies: 10
    Last Post: 04-14-2009, 02:11 AM
  5. assignement change the java screensaver
    By anotsu in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 12:28 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
  •