Monday, January 23, 2012

Homework 2: MasterMindz



For this homework you will implement a variation of the Mastermind board game, where we are using digits instead of colors and changing the rules a little bit. In this game your program will generate a random 4-digit number that the user then has to guess (use Math.random() to generate a random number). After the user makes a guess your program will tell him:

  1. how many of the digits in his guess are correct, that is, they have the same value and in the same position as in the secret,
  2. how many digits in his guess are not found in the secret.

Below is a sample run. Notice that the program is telling the user the secret. This is useful for debugging. The program you turn in should also tell the user the secret so as to make it easier for us to grade it.
Guess the 4-digit number I am thinking of.
(the secret is 9712)
Your guess:1245
1245 has 0 digits in the correct position.
1245 has 2 digits that are not found anywhere in the secret number.
Your guess:9999
9999 has 1 digits in the correct position.
9999 has 0 digits that are not found anywhere in the secret number.
Your guess:5555
5555 has 0 digits in the correct position.
5555 has 4 digits that are not found anywhere in the secret number.
Your guess:9779
9779 has 2 digits in the correct position.
9779 has 0 digits that are not found anywhere in the secret number.
Your guess:9721
9721 has 2 digits in the correct position.
9721 has 0 digits that are not found anywhere in the secret number.
Your guess:1234
1234 has 0 digits in the correct position.
1234 has 2 digits that are not found anywhere in the secret number.
Your guess:9712
9712 has 4 digits in the correct position.
9712 has 0 digits that are not found anywhere in the secret number.
Congratulations! You guessed correctly.


This homework is due Monday, Jann 30 @noon in the dropbox.cse.sc.edu

1 comment:

Jose Vidal said...

Here is a good hint. You can use the String.contains() method to check if the secret contains a particular character.