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!