Question

In: Computer Science

MATLAB Program a Matlab version of Guess the Word. The word should be determined by user...

MATLAB

Program a Matlab version of Guess the Word. The word should be determined by user input.
Once the word is determined, the game should begin by displaying blanks (-) where
all the letters of the word are. The game should then continue to prompt the player to
guess a letter in the word. If the player guesses a letter that is in the word and not
already displayed, that letter should be displayed everywhere it appears in the word.
If the player guesses a letter that is not in the word or a letter that is in the word
but has been guessed before, one try should be added. This should continue until the player completes either the word or until they have reached 6 tries, at which point the game should end which the message: "You ran out of tries and lost."

Solutions

Expert Solution

MATLAB CODE:

% Get the word to guess from the user
secretWord = input('Enter the word: ', 's');
guessedWord = secretWord;
% Clear the screen to hide the word
clc;
% Encode the guessed word with '-'
for i = 1:length(secretWord)
    guessedWord(i) = '-';
end
% Number of tries
tries = 0;
while true
    % Display word
    fprintf('Word to guess: %s\n\n', guessedWord);
    letter = input('Guess a letter in the word: ', 's');
    correctGuess = false;
    for i = 1:length(secretWord)
        if letter == secretWord(i) && guessedWord(i) == '-'
            guessedWord(i) = letter;
            correctGuess = true;
        end
    end
    % If word has been guessed successfully
    if secretWord == guessedWord
        fprintf('Congrats! You Won!\n');
        break;
    end
    % If user's input letter is incorrect
    if ~correctGuess
        tries = tries + 1;
        fprintf('Incorrect guess.\n');
    end
    % If number of tries becomes 6
    if tries == 6
        fprintf('Sorry, you have reached a max of 6 tries\n');
        fprintf('You lose!\n');
        break;
    end
end

SAMPLE OUTPUT:


Related Solutions

Write a program called whilebun.py, in which the user is asked to guess the name of...
Write a program called whilebun.py, in which the user is asked to guess the name of your favorite DBVac vacuum cleaner model. Your program should: · Include a comment in the first line with your name. · Include comments describing each major section of code. · Set a variable called vacname to be equal to the name of your favorite DBVac vacuum cleaner model. · Ask the user to guess the name. Allow the user to make up to three...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!” If the user’s die is smaller, it should display, “Gising na friend,...
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
17. Write a program that generates a random number and asks the user to guess what...
17. Write a program that generates a random number and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. 18. Enhance the program that you wrote for Programming...
Consider the user-defined MATLAB function below, ht_mp_ch(). The function ht_mp_ch(), outputs the sampled version of the...
Consider the user-defined MATLAB function below, ht_mp_ch(). The function ht_mp_ch(), outputs the sampled version of the impulse response hmp(t) of the multipath fading channel as a vector. function impulse_response=ht_mp_ch(max_delay,L,decay_base,t_step) t_vector=0:t_step:max_delay; mp_tmp=0*(t_vector); path_delays=[0 sort(rand(1,L-1)*max_delay)]; impulse_positions=floor(path_delays/t_step); mp_tmp(impulse_positions+1)=exp(j*2*pi*rand(1,L)); mp_tmp=mp_tmp.*(decay_base.^(t_vector/max_delay)); impulse_response=mp_tmp/sqrt(sum(abs(mp_tmp).^2)); Explain what the variable on the left-hand side represents and justify how the right-hand side expression is formulated by adding comments to every line.
Write a user defined MATLAB program that performs power factor correction. The inputs to the MATLAB...
Write a user defined MATLAB program that performs power factor correction. The inputs to the MATLAB function should be voltage across the load (in Vrms, assume 0 phase), frequency Resistance of the load Inductance of the load power factor of the load target power factor The output of the function should be the size of the capacitor that one would need to place in parallel with the load to reach the target power factor. Please submit the code for the...
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
You are to implement a program to play “Guess an Animal.”  Your program should construct a tree...
You are to implement a program to play “Guess an Animal.”  Your program should construct a tree with a string at each node.  This tree stores the knowledge that your program currently knows.  At leaf nodes, the strings should contain the names of animals, while interior nodes should contain yes/no questions.  The program should start at the root and ask the question stored at the root.  If the user answers the question no, the program traverses the left branch of the tree, while if the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT