Clear Filters
Clear Filters

I need help using an array in my code for hangman game

2 views (last 30 days)
Okay, so PLEASE bare with me here. I'm very new to Matlab (literally learning it today), and I'm just trying to learn the basics. All I know is a little bit of Java/C++, so I'm having a hard time adjusting to this. What I'm trying to do is make a hangman game without the plotting/graph of man getting hung. I'm randomly selecting a color and then asking the user to guess the letters. I'd like to place the user inputted letter into an element in an array, so that when I use "strfind" in my for loop I can display previous user inputted characters. The problem I was having is this: Let's say the color is blue, so the program inserts _ _ _ _ and asks the user to choose a letter and then the user would choose 'b' let's say so then the output would be "b _ _ _" - now the problem I was having is the next step; let's say the user inputs "l" now - the program would output "___ l _ _" and not retain the "b" from the previous. Therefore, I thought to put the user input into an array that is being searched into a for loop, so that whenever the user inputs anything it will search all the elements in the array to see if any previous characters were made. Sorry if that's confusing; I'm trying my best to explain. And again, please bare with me, as I really don't know what I'm doing and I have NO IDEA how arrays are handled in Matlab (the only concept of arrays I have is from Java/C++) The code is below:
a = 8;
numWrong = 0;
index = '0';
again = 'Y';
count = 1;
disp('Lets Play!')
while(again == 'Y')
p = randperm(a);
for i=1:8
r = p(i);
end;
disp(r);
switch(r)
case 1
word = 'blue';
wordCount = 4;
case 2
word = 'green';
wordCount = 5;
case 3
word = 'red';
wordCount = 3;
case 4
word = 'black';
wordCount = 5;
case 5
word = 'yellow';
wordCount = 6;
case 6
word = 'orange';
wordCount = 6;
case 7
word = 'purple';
wordCount = 6;
case 8
word = 'gray';
wordCount = 4;
end;
for i= 1: wordCount
disp('___');
end;
while(numWrong < 6)
disp(['You have ' num2str(6-numWrong) ' guesses']);
letter = input('Please choose a character a-z: ','s');
character{count} = letter; %trying to place letter into array "character" in element specified by the count
count = count + 1;
isFound = false;
for i=1: wordCount
for k=1: wordCount
letter = character(k); %trying to put info from array back into variable, so I can use it for strfind
if(strfind(word,letter) == i) %this is where I find errors.
disp(character(k));
isFound = true;
else
disp('___');
end;
end;
end;
if(isFound == false)
numWrong = numWrong + 1;
end;
num = num + 1;
end;
if(numWrong == 6)
disp('GAME OVER, better luck next time!');
end;
again = input('Would you like to play again? (Y/N): ','s');
end;

Answers (1)

rocketboyjka
rocketboyjka on 24 Jun 2016
Edited: rocketboyjka on 24 Jun 2016
Try:
letter = char(character(k))
This should fix the error you described. Also, I think the preceding line might be using the wrong limits for the for loop. Should it read:
for k=1:length(character)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!