Welcome,
I had to make a bot for the mastermind game to solve it using AI,
well Donald Knuth’s algorithm is simple, but It takes more moves and is not efficient up to me.
So, after researching I found a paper and its code on solving mastermind with genetic algorithm.
But that paper is like a benchmark and doesn’t explain, as well as I don’t understand python
!
Hence looking for a code on other languages (my preferred language is pure js
),I found:
Link | Language | Disadvantage |
---|---|---|
LINK1 | Javascript with Jquery |
Jquery is a problem more than that the three modes are confusing (I want only Human vs AI) |
LINK2 | React js |
React js is a problem more than that see the [My problem] below |
LINK3 | Java |
Again, see the [My problem] below |
[My problem]=The way it calculates the output (black and white coins) is different
That is as you can see in the LINK1,
Output for
Guess:[2, 2, 5, 2]
Answer:[4, 5, 3, 5]
Output:-
Correct coin in, Correct position: 0
Correct coin in, Wrong position: 1
The above output is correct,
But the below output which is got for LINK2 & LINK3 code is wrong
Output for
Guess=[2, 2, 5, 2]
Answer=[4, 5, 3, 5]
Output:-
Correct coin in, Correct position: 0
Correct coin in, Wrong position: 2
I got these outputs by extracting the code part that the algorithm uses to check
Correct code, extracted from link1
prediction = [2, 2, 5, 2];
answer = [4, 5, 3, 5];
function testCode (combination, solution) {
NUM_FIELDS = solution.length;
var a = 0;
var b = 0;
var marked = [0, 0, 0, 0];
for(var i = 0; i<NUM_FIELDS; i++) {
if(combination[i] == solution[i]) {
a++;
marked[i] = 1;
}
}
for(var i = 0; i<NUM_FIELDS; i++) {
if(combination[i] != solution[i]) {
for(var j = 0; j<NUM_FIELDS; j++) {
if(i != j && 0 == marked[j] && combination[i] == solution[j]) {
b++;
marked[j] = 1;
break;
}
}
}
}
return [a, b];
}
console.log(testCode(prediction,answer));
Wrong code, extracted from link2 & link3
prediction = [2, 2, 5, 2];
answer = [4, 5, 3, 5];
function getMoveScore(input, answer) {
let correctColorLocation = 0;
let correctColorWrongLocation = 0;
// correct color correct location
for (let i = 0; i < input.length; i++) {
if (input[i] === answer[i]) {
correctColorLocation += 1;
}
}
// correct color
for (let i = 0; i < answer.length; i++) {
var inputSet = new Set(input);
if (inputSet.has(answer[i])) {
correctColorWrongLocation += 1;
}
}
correctColorWrongLocation -= correctColorLocation;
const incorrect = 4 - correctColorWrongLocation - correctColorLocation;
return [correctColorLocation,correctColorWrongLocation];
}
console.log(getMoveScore(prediction, answer));
Anyway this is not the problem, Main problem is
- Everything is ok in LINK1 but the 3 modes are confusing
- I am looking for a simple code that is console-based(not graphical),explanation&commented code is appreciated but not needed