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

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.
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".
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....
In this exercise we will practice using nested loops. You will write s program that prompts...
In this exercise we will practice using nested loops. You will write s program that prompts the user to enter an integer between 1 and 9 and displays a pyramid of numbers, as shown in the example below. The program must validate the input given by the user and make sure that: if the user enters a non-integer the program issues an error message and requests the user provides a valid input. if the user does not enter a number...
In C 1- Use nested for loops to create an addition lookup table. Fill in an...
In C 1- Use nested for loops to create an addition lookup table. Fill in an array and print out the following table showing the sum of the row and column numbers 0-9. 0 1 2 3 4.. <- column numbers 1 2 3 4 5.. 2 3 4 5 6.. <- These nos. = Row + Column 3 4 5 6 7.. <- for example 7 = 3 + 4 4 5 6 7 8.. ... ^Row numbers 2-...
Develop a general computer program using MATLAB for the analysis of plane (2D) frames. You can...
Develop a general computer program using MATLAB for the analysis of plane (2D) frames. You can use Chapter 6 as a reference. The program should be able to: a. Analyze a frame subjected to different load types, such as joint load, member load, and support displacement; b. Analyze frames with different boundary conditions: pin, roller, and fixed support; c. Generate the results including joint displacements, member axial, shear and moment forces, and reactions
A video game player insists that the longer he plays a certain computer game, the higher...
A video game player insists that the longer he plays a certain computer game, the higher his scores are. The table shows the total number of minutes played and the high score (in thousands of points) achieved after each 5-minute interval. Use α = .01 . # Min 5 10 15 20 25 30 35 40 45 50 55 60 Score 48.0 53.3 101.9 72.5 121.5 146.0 196.1 118.5 150.5 80.7 36.0 64.8 Find the critical value. (Round to the...
In MATLAB, the script should use both for and while loops. Game Mode: Randomly generates 10...
In MATLAB, the script should use both for and while loops. Game Mode: Randomly generates 10 unique (e.g. non-repeating) multiplication problems that involve multiplying the integer selected in the previous step by an integer value between 2 and 12, inclusive, after which the following report should be displayed in the Command Window. You correctly answered __ out of __ problems (__%) involving __s. The number of correctly answered problems appears in the first blank, the total number of problems appears...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ### #12 ###123 ##1234 #12345
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT