Word Guessing game code, with a bit of a twist. (matlab)

2 views (last 30 days)
I'm writing a program about guessing a 4 letter word. I have to propose a word for the computer to guess and then display the amount of correct letters guessed (per guess) and how many letters were in the correct position of the guess and the number of guesses.
e.g. if the word I select is 'tank' and the computer guesses 'bank' the display should read: 'There are 3 correctly guessed letters, 3 characters are in the right position. In the end it should say 'you've guessed the right word, it took 4 attempts'.
The twist part is that it has to work systematically and not just random guesses. If possible, otherwise I'd settle for assistance on just the random guessing. Any help would be appreciated.
  1 Comment
Guillaume
Guillaume on 27 Sep 2016
T-man1011 comment originally posted as an answer moved here:
Thanks for the help guys, I'm a bit stuck on how to make the computer guess what the word is, basically it has to guess a randomly generated 4 character string (A-Z), I just don't know how to go about it. Do I have to make a function or is there already one that exists?
If I have to make one can someone please let me know how to go about that.
Cheers

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 26 Sep 2016
Start with something like this:
correctWord = 'tank';
guessedWord = 'bank';
locationsThatAreCorrect = correctWord == guessedWord
numPositionsCorrect = sum(locationsThatAreCorrect)
You'll see
locationsThatAreCorrect =
0 1 1 1
numPositionsCorrect =
3

Walter Roberson
Walter Roberson on 26 Sep 2016
https://www.mathworks.com/matlabcentral/fileexchange/11798-mastermind-

Guillaume
Guillaume on 27 Sep 2016
With regards to your latest comment:
char('A' + randi([0 25]))
will give you a random uppercase letter. It is trivial to extend that to four letters. Another option, which you'll probably need at some point:
searchspace = 'A':'Z'; %this will have to be reduced at some point
letter = searchspace(randi([1 numel(searchspace)]))

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!