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...
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...
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...
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 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...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT