Results 1 to 4 of 4
  1. #1
    TriSBR is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Mastermind Java Code

    I need a code for mastermind which allows the user to guess 4 letters from a-f, the player has 10 turns. It will tell the player if they get exact letter and position matches and partial matches for correct letter and wrong position. Here's what I have so far, but doesnt seem right. Need to use private String generateSecretCode(). THANKS!
    Java Code:
    import java.util.ArrayList;
    import java.util.Arrays;
    
    
    public class Assignment5Code {
    
    	/**Jim Petersen
    	 * 11/3/11
    	 * The purpose of this program is to create a game called Mastermind using arrays.
    	 * @param args
    	 */
    	private String generateSecretCode() {
    	
    		char[] correctCode = {'a', 'e', 'b', 'd' };
    		
    ArrayList<String> varName = Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F" } );
    String firstLetter = lettersList.remove((int)(Math.random() * 6));
    String secongLetter = lettersList.remove((int)(Math.random() * 5));
    String thirdLetter = lettersList.remove((int)(Math.random() * 4));
    String fourthLetter = lettersList.remove((int)(Math.random() * 3));
    String secretCode = firstLetter + secondLetter + thirdLetter + fourthLetter;
    return secretCode; 
    	}
    
    private String getGuess() {
    }
    
    private int getExactMatches(String secretCode, String guess) {}
    
    public void start() {
    	
    int numberOfTurns = 0;
    System.out.println("You have a maximum of 10 chances to correctly guess a sequence of four non-repeating letters ranging from a to f.");
    String secretCode = generateSecretCode();
    varName.get(1);
    System.out.println("Enter four different letters, seperated by a space");
    String guess = getGuess();
    String exactMatches = getExactMatches(secretCode, guess);
    String partialMatches = getPartialMatches(secretCode, guess); 
    System.out.println("Exact (correct letter and position): " + exactMatches);
    System.out.println("Partial (correct letter, incorrect position): " + partialMatches);
    numberOfTurns--;
    if (numberOfTurns == 0) {
    System.out.println("You lost. The correct code was: " + secretCode);
    
    if(exactMatches > 3) {
    System.out.println("You won!")
    numberOfTurns *= 0;
    }
    }
    Last edited by Norm; 11-03-2011 at 06:04 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Mastermind Java Code

    but doesnt seem right
    Does it work the way you want it to? If not, please explain what the problem is.

    numberOfTurns *= 0;
    What is the above line of code supposed to do?
    Last edited by Norm; 11-03-2011 at 06:08 PM.

  3. #3
    TriSBR is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Mastermind Java Code

    Figured it out myself. I'm good.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Mastermind Java Code

    When you get really good, you won't need to post a question for others to look at.

Similar Threads

  1. Mastermind game problem
    By TheQuad in forum New To Java
    Replies: 1
    Last Post: 04-16-2011, 05:29 PM
  2. problem mastermind
    By Alexander_003 in forum AWT / Swing
    Replies: 4
    Last Post: 03-16-2011, 05:08 PM
  3. Mastermind!
    By MishkaRX in forum New To Java
    Replies: 11
    Last Post: 12-14-2010, 01:39 PM
  4. MasterMind Problem!!
    By papinhio in forum New To Java
    Replies: 4
    Last Post: 07-09-2010, 03:51 PM
  5. 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
  •