MasterMind Assignement , Use of String Class

• 03-13-2011, 08:44 PM
Ebayer
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;

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");     } }```
• 03-13-2011, 11:50 PM
Iron Lion
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?
• 03-14-2011, 07:23 PM
Ebayer