CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    3

    Mastermind java game (non object-oriented version)

    hi all

    i am a student 6 weeks into learning java.

    i have been attempting to create a simple Mastermind game using java, non object oriented version. However, I have been stuck at the part where the program counts the "number of pegs that are of the correct color but placed at the wrong position". It either overlaps with the "number of pegs of the right color at the correct position" or it under-counts.

    The following is the segment of my code:

    Code:
    	public static int incorrectPosition(int row) {
    		int incorrectCount = 0;
    
    		/*
    		 * board[row][col] 
    		 * Loop for each column in the current row. 
    		 * Loop for
    		 * each value in the code. Check if column matches value but not in the
    		 * same position.
    		 */
    
    		for (int col = 0; col < SIZE; col++) {
    			for (int index = 0; index < SIZE; index++) {
    				if ((!board[row][col].equalsIgnoreCase(secretCode[col]))
    						&& board[row][col].equalsIgnoreCase(secretCode[index])) {
    					if ((col != 3)
    							&& (!board[row][index]
    									.equalsIgnoreCase(secretCode[index]))) {
    						if ((!board[row][col + 1]
    								.equalsIgnoreCase(secretCode[index]))) {
    							incorrectCount++;
    						}
    					} else if (col == 3
    							&& (!board[row][index]
    									.equalsIgnoreCase(secretCode[index]))) {
    						if (!(board[row][0].equalsIgnoreCase(secretCode[index])
    								|| board[row][1]
    										.equalsIgnoreCase(secretCode[index]) || board[row][2]
    								.equalsIgnoreCase(secretCode[index]))) {
    							incorrectCount++;
    						}
    
    					}
    
    				}
    			}
    		}
    
    		return incorrectCount;
    
    	}

    Would appreciate if someone could advice me with regards to this problem.
    Thank you!

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Mastermind java game (non object-oriented version)

    Rather than trying to solve the problem in one go break it into managable chunks.

    For example you might define an incorrect positioned peg as one that is the colour of one of the secret pegs but is not same colour as the peg in the equivilent secret code position. Now your method just needs to calls two methods to determine these facts and returns a result accordingly eg

    Code:
    for (int col = 0; col < SIZE; col++) {
        if ( isColourInCode(board[row][col]) && !isCorrectPosition(column, board[row][col]) ) 
            incorrectCount++;
        }
    Now you need to provide code for these two methods which are simpler to code and probably reusable in other parts of your code.

    • isColourInCode() just iterates over the secret code and returns true if any of the pegs are of the given colour.
    • isCorrectPosition() just checks the secret peg at the given column and returns true if the peg is the given colour.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Mar 2010
    Posts
    3

    Re: Mastermind java game (non object-oriented version)

    Quote Originally Posted by keang View Post
    Rather than trying to solve the problem in one go break it into managable chunks.

    For example you might define an incorrect positioned peg as one that is the colour of one of the secret pegs but is not same colour as the peg in the equivilent secret code position. Now your method just needs to calls two methods to determine these facts and returns a result accordingly eg

    Code:
    for (int col = 0; col < SIZE; col++) {
        if ( isColourInCode(board[row][col]) && !isCorrectPosition(column, board[row][col]) ) 
            incorrectCount++;
        }
    Now you need to provide code for these two methods which are simpler to code and probably reusable in other parts of your code.

    • isColourInCode() just iterates over the secret code and returns true if any of the pegs are of the given colour.
    • isCorrectPosition() just checks the secret peg at the given column and returns true if the peg is the given colour.
    hi, thank you for your reply!
    i have since changed the code to the following:

    Code:
    	// incorrectPosition(). checking the player's guess. if the right colour is
    	// placed at an INCORRECT position, it will be reflected here.
    	public static int incorrectPosition(int row) {
    		int incorrectCount = 0;
    
    		for (int col = 0; col < SIZE; col++) {
    			if (isColourInCode(board[row][col])
    					&& !isCorrectPosition(col, board[row][col])) {
    				incorrectCount++;
    
    			}
    		}
    
    		return incorrectCount;
    
    	}
    
    	// checking if any of the colours guessed are that in the Secret Code
    	public static boolean isColourInCode(String board) {
    		Boolean done = false;
    		for (int index = 0; index < SIZE; index++) {
    			if (board.equalsIgnoreCase(secretCode[index])) {
    				done = true;
    			}
    		}
    		return done;
    	}
    
    	// checking if the guessed colour in the given column tallies with the
    	// Secret Code at the given column
    	public static boolean isCorrectPosition(int col, String board) {
    		Boolean done = false;
    		if (board.equalsIgnoreCase(secretCode[col])) {
    			done = true;
    		}
    		return done;
    	}
    The coding is much clearer now, thank you!

    The problem I face is:
    when the computer generates RED GREEN GREEN ORANGE, i input green green green green as my guess. The result i receive is "2 correct position, 2 incorrect position".

    Is there any way to solve this error? I have been trying to solve this for 36 hours and counting =\

    Thank you for your help!

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Mastermind java game (non object-oriented version)

    The isColourInCode() method doesn't need to set a variable to true, it can just return true the first time the if statement is true.

    The way to handle your problem is to make a copy of the secret code, find all matching pegs first and remove them from the copy (or set them to a special value denoting matched). Then look for incorrect values by checking against the copy.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Mar 2010
    Posts
    3

    Talking Re: Mastermind java game (non object-oriented version)

    Quote Originally Posted by keang View Post
    The isColourInCode() method doesn't need to set a variable to true, it can just return true the first time the if statement is true.

    The way to handle your problem is to make a copy of the secret code, find all matching pegs first and remove them from the copy (or set them to a special value denoting matched). Then look for incorrect values by checking against the copy.
    Thank you!!!
    I took your advice and made amendments to my code. After playing with it for hours I've finally gotten it right. The results seem alright after 50 guesses/tries by far!

    Here is my updated code:

    Code:
    	// generates the secret code
    	public static void genSecretCode() {
    		String[] colours = { "RED", "ORANGE", "YELLOW", "GREEN", "BLUE",
    				"PURPLE" };
    
    		for (int index = 0; index < SIZE; index++) {
    			secretCode[index] = pickRand.get(colours);
    			System.out.print(secretCode[index] + " ");
    		}
    		
    		System.out.println();
    
    	}
    	
    	// duplicates the secret code
    	public static void duplicateSecretCode() {
    		for (int index = 0; index < SIZE; index ++) {
    			dupSecretCode[index] = secretCode[index];
    		}
    	}
    
    
    
    // checking for pegs of the right colour but at the incorrect position
    	public static int incorrectPosition(int row) {
    		int incorrectCount = 0;
    
    		for (int col = 0; col < SIZE; col++) {
    
    			if (isCorrectPosition(col, board[row][col])) {
    				subtractFromDuplicate(col); // eg. if the dupSecretCode is RED X
    											// X X X
    											// and player's guess is RED RED RED RED,
    											// dupSecretCode will be changed
    											// to MATCHED X X X after
    											// comparing the first RED.
    											// Hence, the other 3 reds in RED RED RED RED will
    											// not be matched against dupSecretCode's "RED" to cause a
    											// repeated result
    			}
    		}
    
    		for (int col = 0; col < SIZE; col++) {
    
    			if (isColourInCode(col, board[row][col])
    					&& (!isCorrectPosition(col, board[row][col]))) {
    				incorrectCount++;
    
    			}
    
    		}
    		
    		duplicateSecretCode(); // duplicate the secret code again for the next iteration
    		
    		return incorrectCount;
    
    	}
    
    	// if the guessed colour and Secret Code colour at the given position is the
    	// same, it is 'subtracted' and assigned a special value here
    	public static void subtractFromDuplicate(int index) {
    		dupSecretCode[index] = "MATCHED";
    
    	}
    
    	// checking if any of the colours guessed are that in the Secret Code
    	public static boolean isColourInCode(int col, String board) {
    		Boolean done = false;
    		for (int index = 0; index < SIZE; index++) {
    			if (col != index && board.equalsIgnoreCase(dupSecretCode[index]) && (!isCorrectPosition(col, board))) {
    				done = true;
    
    				subtractFromDuplicate(index); 	// eg. if the dupSecretCode is RED X X X X
    												// and player's guess is RED RED RED RED,
    												// dupSecretCode will be changed
    												// to MATCHED X X X after
    												// comparing the first RED.
    												// Hence, the other 3 reds in RED RED RED RED will
    												// not be matched against dupSecretCode's "RED" to cause a
    												// repeated result
    				break;
    
    			}
    		}
    		return done;
    	}
    
    	// checking if the guessed colour in the given column tallies with the
    	// Secret Code at the given column
    	public static boolean isCorrectPosition(int col, String board) {
    		Boolean done = false;
    		if (board.equalsIgnoreCase(secretCode[col])) {
    			done = true;
    		}
    		return done;
    	}

    Thank you so much!

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Mastermind java game (non object-oriented version)

    Thank you so much!
    My pleasure.

    Congratulations for persevering and getting it working
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Jul 2013
    Location
    n/a
    Posts
    2

    Re:

    Thanks for starting that topic.

  8. #8
    Join Date
    Jul 2013
    Posts
    1
    Quote Originally Posted by DoctorMendel View Post
    Thanks for starting that topic.
    up sto pi kil

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured