Question

In: Computer Science

Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...

Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code.

- Selecting a word from a dictionary (a text file)

- Reading a single letter from the user

- Building a character array showing the letters matched so far

- 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.

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Download dctionary:https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt

Code:

fid=fopen('dict.txt');
% set linenum to the desired line number that you want to import
linenum = randi(370103);
% use '%s' if you want to read in the entire line or use '%f' if you want to read only the first numeric value
word = textscan(fid,'%s',1,'delimiter','\n', 'HeaderLines',linenum-1);
B=convertStringsToChars(string(word));
len = strlength(B);
guessedCorrectly=char(95+zeros(1,len));
guessedIncorrectly=[];
countCorrect=0;
countInCorrect=0;
while true
c=input('Please guess next character :','s');
c=char(c(1,1));
if ismember(c,guessedIncorrectly) || ismember(c,guessedCorrectly)
disp("Already Given earlier")
else
if ismember(c,B)
for i=[1:len]
if B(i)==c
guessedCorrectly(i)=c;
end
end
disp(guessedCorrectly)
countCorrect=countCorrect+1;
else
countInCorrect=countInCorrect+1;
guessedIncorrectly=[guessedIncorrectly c];
end
end
disp(["Incorrect Guesses :",guessedIncorrectly]);
if ismember('_',guessedCorrectly)
continue
else
break
end
end
fprintf("Total guesses :%d\n",(countCorrect+countInCorrect))
fprintf("Total guessed correctly :%d\n",countCorrect)
fprintf("Total guessed incorrectly :%d\n",countInCorrect)

Output:


Related Solutions

MATLAB is program Respond fast please a quiz Create a MATLAB script. Using nested for loops,...
MATLAB is program Respond fast please a quiz Create a MATLAB script. Using nested for loops, evaluate the multivariable function: z = sin ⁡ ( x ) cos ⁡ ( y ) for x between -4 and 4 in steps of 1 y between -2 and 2 in steps of 1 Display the matrix z Cut and paste the following into a word doc and submit to this question. Your script code Your input Your output
create a function in matlab that sums two m x n matrices using nested loops, then...
create a function in matlab that sums two m x n matrices using nested loops, then returns result into a new matrix. Use nesed for loops to add matrices piece by piece. Basically means, dont program simply A+B Function should perform error check to make sure both matrices have same number of rows/ columns.
How would I create a Hangman game that chooses  a random word and the player needs to...
How would I create a Hangman game that chooses  a random word and the player needs to guess it, letter by letter using  JavaScript and jQuery to allow user interaction. The content and the style of the page must be updated based on user’s input.
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes as input a matrix, and as output returns true if at least one entry in the matrix is strictly greater than 8 and returns false otherwise.
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
C++ In this lab you will be using nested for loops to print out stars in...
C++ In this lab you will be using nested for loops to print out stars in a Diamond pattern such as this: * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** * For example , if number of rows=8 then the above pattern is derived. You are to take the input for the number of lines(rows) from a file named "input_diamond" and output the pattern into both the terminal and an output file named "output_diamond".
For this assignment, you will create a command-line version of the game ​Hangman. You should work...
For this assignment, you will create a command-line version of the game ​Hangman. You should work in a group of two on the project and not view Hangman code of other students or found on-line. Submit this project-- your .java file-- here on Canvas. For this assignment you will research on StringBuilder Class (​Use this link​) and use it in your code. Functional requirements (rubric) ● Your game should have a list of at least ten phrases of your choosing...
Create a Hangman game using C++ by QT creator. please separate each code that will insert...
Create a Hangman game using C++ by QT creator. please separate each code that will insert in hider files and cpp files and use an accurate screeshoots if needed. Thanks in advance.
Code the game of Rock, Paper, Scissors between a human player and the computer. You can...
Code the game of Rock, Paper, Scissors between a human player and the computer. You can check out the game on Wikipedia if you are not familiar with it. Create a 4 option menu with the human player choices, plus the option of exiting the game. Randomly determine the computer’s choice (although a great deal of AI research has gone in to determining the best computer move). • Loop the game until the human player exits. • Count the number...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game that involves betting on the sum of two dice. The player will start out with some initial total amount of money. During each round, the player can bet some money that the sum of the two dice will be equal to a certain number. If the player wins the bet, that player adds the amount of the bet to his or her current total....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT