0

I am trying to write a function for hangman game. The function will take a string of guessed word and a string of guessed letters. One output is a string of the board after trying each letter, another output should be a string of letters that does not match with the word (which we are calling missed letters), and the last output is the result of the game. If we get more than six letters not to match with the word the output result is 'You lose!', if the letters match before six wrong guess the output lest is 'You win!'. If the number of missed letters does not reach 6, the output should be 'You have <#> lives left.' Guessed letters will be always lower cased and guessed word can be a combination of upper and lower case. Therefore, if the word is word is 'MatLab' and guessed letters are 'aeiot' the output should be input

    word='MatLab'
letter='aeiot'

output

board=['_''a''_''_''a''_'; 
      '_''a''_''_''a''_'; 
      '_''a''_''_''a''_'; 
      '_''a''_''_''a''_'; 
      '_''a''t''_''a''_']

missed='eio'
result='You have 3 lives left.'

When I writing the function I could not figure out how can I get the boards after each iteration and the second thing is how can I get the missed letters.

Here is my function

function [boards, missed, result]=playHangman(word,letter)

 for ind=1:length(letter)
   compare=((word==letter(ind))|(word==(char(double(letter(ind)-32)))));
 end
boards=word;
boards(~compare)='_';
 missed=**??**
 if strcmp(word,boards)
   result='You win!';
 elseif strcmp(word,boards)==false||6-length(missed)==0
   result='You lose!';
 else 
   result=sprintf('You have %d lives left.',6-length(missed));
 end
end

Any help will be appreciated. Thanks

1 Answer 1

1

I'm not sure if this is what you want. board can be easily otained using ismember, and missed using setdiff:

word = 'MatLab'; %// data
letter = 'aeiot'; %// data

N = numel(letter);
board = cell(N,1);
missed = cell(N,1);
for n=1:N
    board{n} = word;
    board{n}(~ismember(lower(word),letter(1:n))); = '_' %// hide letters not hit
    missed{n} = setdiff(letter(1:n),word); %// letters not present in word
end

Result:

board = 
    '_a__a_'
    '_a__a_'
    '_a__a_'
    '_a__a_'
    '_at_a_'

missed = 
    []
    'e'
    'ei'
    'eio'
    'eio'
3
  • It is very close to what I want. We want to store all the results of board in one output. It should deals with concatenation. I know how to concatenate. However, I do not know how to do it in a loop. I was very unclear in the for and while loop class. Thanks anyway! Feb 12, 2014 at 22:43
  • @user3226108 Please see update. I'm using cell arrays to store the result of each iteration
    – Luis Mendo
    Feb 12, 2014 at 22:44
  • we are not taught cell arrays yet. That's a problem for me. However, the code works for board. And for missed we need only the missed letter. For this we can use your first code "missed=setdiff(letter,word)". Does not need to use cell array. However, I tried for different set of inputs. It gives the right missed letter, but does not maintain the order of their appearance. Feb 12, 2014 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.