Results 1 to 3 of 3
- 03-13-2011, 07:44 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
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"); } }
- 03-13-2011, 10:50 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
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, 06:23 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Mastermind!
By MishkaRX in forum New To JavaReplies: 11Last Post: 12-14-2010, 01:39 PM -
MasterMind Problem!!
By papinhio in forum New To JavaReplies: 4Last Post: 07-09-2010, 03:51 PM -
My own string class
By viperlasson in forum New To JavaReplies: 8Last Post: 01-25-2010, 05:55 PM -
how to make mastermind game
By javabeginer in forum New To JavaReplies: 10Last Post: 04-14-2009, 02:11 AM -
assignement change the java screensaver
By anotsu in forum New To JavaReplies: 2Last Post: 03-07-2008, 12:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks