Question

Write a MATLAB code for the hangman game below you might break the problem down into:...

Write a MATLAB code for the hangman game below you might break the problem down into:

  • Selecting a word from a dictionary. ‘aardvark’
  • Reading a single letter from the user like ‘a’
  • Building a character array showing the letters matched so far like ‘aa---a--’
  • Keeping count of the number of guessed letters so far.
  • Keeping count of the number of guesses so far.
  • Writing conditional logic to see whether the game is finished or not.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

words = {'apple','football','banana','grapes','criciketer','offside','watermelon','pineapple','avacado'}; % list of words
random_index = ceil(length(words)*rand()); % select random index
random_word = words{random_index}; % selct random word
guessed_chars = {};
number_guesses = 0;
next_iteration = true;
while next_iteration % run until broken
next_iteration = false;
for i=1:length(random_word) % for each alphabet
guessed = false; % was this in guessed alphabet
for j=1:length(guessed_chars)
if random_word(i) == guessed_chars{j} % check if this psecific charactr was guessed
fprintf(random_word(i)) % show it
guessed = true;
break
end
end
if(~guessed)
fprintf('-') % put a dash if character has not been guessed yet
next_iteration = true;
end
end
if(~next_iteration)
break
end
input_char = input('\nEnter char : ','s'); % input char
guessed_chars = [guessed_chars input_char]; % add to guessed chars

number_guesses = number_guesses + 1; % count number of guesses
end
fprintf('\n Number of guesses : ')
disp(number_guesses)

Output Run

>> hangman
-------
Enter char : a
a-a-a--
Enter char : e
a-a-a--
Enter char : o
a-a-a-o
Enter char : p
a-a-a-o
Enter char : v
ava-a-o
Enter char : d
ava-ado
Enter char : c
avacado
Number of guesses : 7

>>

Add a comment
Know the answer?
Add Answer to:
Write a MATLAB code for the hangman game below you might break the problem down into:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • Consider a class that could be used to play a game of hangman. The class has...

    Consider a class that could be used to play a game of hangman. The class has the following attributes: The secret word. The disguised word, in which each unknown letter in the secret word is replaced with a question mark (?). For example, if the secret word is abracadabra and the letters a, b, and e have been guessed, the disguised word would be ab?a?a?ab?a. The number of guesses made. The number of incorrect guesses. It will have the following...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

  • Hangman is a game for two or more players. One player thinks of a word, phrase...

    Hangman is a game for two or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers, within a certain number of guesses. You have to implement this game for Single Player, Where Computer (Your Program) will display a word with all characters hidden, which the player needed to be guessed. You have to maintain a dictionary of Words. One word will be selected by your program....

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • code block and c++ only please on Programs> Prog 4: Twisted Hangman & Sylabus ule nte...

    code block and c++ only please on Programs> Prog 4: Twisted Hangman & Sylabus ule nte zza ckboard ss Notes al Reference ference [Corrected sample output and rubric still to come ..] Write a program to play hangman where the computer chooses the word and the human user tries to guess the word. The twist is that the computer starts out with a dictionary of many words. After each human guess, the computer eliminates all words containing the letters guessed...

  • can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put...

    can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put together everything you've learned to make the classic game "Hangman" in a webpage. If you are unfamiliar with the rules of Hangman, the game works like this: A gallows is drawn on a surface, and Player 1 chooses a word that player 2 must guess. Empty dashes for each letter in the word are drawn next to the gallows (so a 7-letter word means...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT