I am making a naive AI for the board game Mastermind where the AI guesses all 1296 combinations, below is the code that stores all combinations and checks for a match:
while (match == false) {
for (int z = 0; z < 6; z++) {
for (int x = 0; x < 6; x++) {
for (int i = 0; i < 6; i++) {
for (int k = 0; k < 6; k++) {
turn++;
a = z;
b = x;
c = i;
d = k;
compguess[0] = a;
compguess[1] = b;
compguess[2] = c;
compguess[3] = d;
combination.add(new int[] { z, x, i, k });
if (hiddenGuess[0] == compguess[0] && hiddenGuess[1] == compguess[1]
&& hiddenGuess[2] == compguess[2] && hiddenGuess[3] == compguess[3]) {
compGuesses = turn;
match = true;
}
}
}
}
}
}
The next piece of code below is where I'm getting stuck on. It changes the state (which is the different colours) fine, but now I want the AI to change the state differently when (whiteThings == 2 && blackThings == 2) this if statement is at the point where the AI has the correct colours but 2 are in the wrong place (whiteThings) and two are in the right place (blackThings)
for example the colours red,blue,yellow,orange,cyan,green are represented 0,1,2,3,4,5 accordingly
And lets pretend the hidden guess is 1000 (one blue, three reds)
the AI will guess:
0000 - first guess returns 3 black pegs blackThings
0001 - second guess returns 2 black pegs and 2 white pegs
At this point I want the AI to stop going through the list of combinations (e.g. next search would have been 0002, then 0003, so on)
instead I want the next search to be 0010, 0100, 1000 - swapping around the combination until it gets 4 blackThings
for (int i = 0; i < height; i++) {
int[] temp = combination.get(i);
for (int j = 0; j < 4; j++) {
state[i][j] = temp[j];
if (whiteThings == 2 && blackThings == 2) {
int[] twoWhiteBlack = state[numGuesses];
state[i][j] = 4;
System.out.print(twoWhiteBlack[j]);
}
colouredPegs[i][j].setBackground(choose(state[i][j]));
}
}
guess.addActionListener(this);
my problem atm is that when it executes the if statement it prints out the combination System.out.print(twoWhiteBlack[j]); but it doesn't change the state state[i][j] = 4; in my code I tried to change it to 4 just as a test (note 4 has no significance), instead of changing state to 4 my code continues with the combination state[i][j] = temp[j]; e.g. (0002, 0003, 0004, 0005)
How can I get the program to change state in the if statement? Sorry for the long post.
Aucun commentaire:
Enregistrer un commentaire