|
-
March 5th, 2010, 12:11 PM
#5
Re: Mastermind java game (non object-oriented version)
 Originally Posted by keang
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!
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|