Problem 43562. Mastermind
We are playing the boardgame mastermind https://en.wikipedia.org/wiki/Mastermind_(board_game). The game master has created a 4-character code for you to guess, with characters ranging from "1" to "6" (e.g. code = [2 1 3 4]).
The first guess (this will be [1 1 2 2]) is already done and the game master has given you following feedback:
- The number of correct characters in your guess, in this case 1 because character "1" at index 2 is correct.
- The number of correct characters, but in a wrong place. In this case 1 because you guessed character "2", but it is in the wrong place.
Now, your function has to do the next guesses. Each time your function is called, the input will contain the code and feedback of all your previous guesses, the output is a copy of the input with your new code added (refer to the example code).
It is shown you could solve the code in only 4 additional guesses (refer to the wiki page), you will however get 11 according to the rules of Mastermind.
Good luck!
Solution Stats
Problem Comments
-
2 Comments
If the hidden code is [2 4 4 2] and the attempt is [1 2 3 5], then the test gives rightNumRightPlace = 0, rightNumWrongPlace = 2. I my head, rightNumWrongPlace should be 1. Have I misunderstood? .
I rate difficult-level this challenge because in your test case is not working correctly. Sometimes you pass [3right place 1wrong place] which is not possible. You should replace the way you calculate the RightNumWrongPlace.
This may fix things (it's also vectorized)
function [r w]=tellMe(sol, gues)
% this function calculates the score of the pegs given a solution
1x4 and a matrix of guesses nx4.
r = sum( sol==gues,2);
s = size(gues,1);
rows = repmat( (1:s)',1,4);
w = sum(min( accumarray(sol',1,[6 1])', accumarray( [rows(:) gues(:)],1,[s 6])),2)-r;
end
Solution Comments
Show commentsProblem Recent Solvers14
Suggested Problems
-
4486 Solvers
-
1236 Solvers
-
208 Solvers
-
Calculate Euler's phi function
89 Solvers
-
Find a subset that divides the vector into equal halves
387 Solvers
More from this Author25
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!