Borrar filtros
Borrar filtros

How do I stop an input from being entered twice?

2 visualizaciones (últimos 30 días)
Matthew Smyth
Matthew Smyth el 19 de Abr. de 2022
Respondida: Manas el 5 de Oct. de 2023
I'm working on a game of hangman for matlab, I am unsure how to stop the same input from being entered twice or stop it increasing the count for correct index.
My code is:
words=readWords('dictionary_long.txt');
wordNum= rand(1,1);
wordNum=9887*wordNum;
wordNum=ceil(wordNum);
guessingWord=words(wordNum);
guessingWord=char(guessingWord);
while length(guessingWord)<=4
wordNum= rand(1,1);
wordNum=9887*wordNum;
wordNum=ceil(wordNum);
guessingWord=words(wordNum);
guessingWord=char(guessingWord);
end
Guess = [];
UserGuess=[];
incorrectCount=0;
lettersLeft=length(guessingWord);
guessesLeft=8;
guessCount=0;
correctCount=0;
for i=1:length(guessingWord)
Guess=[Guess '_'];
end
Guess1=input('Please enter your first guess: ', 's');
for i=1:length(Guess)
if Guess1==guessingWord(i)
Guess(i)=Guess1;
lettersLeft=lettersLeft-1;
correctCount=correctCount+1;
else
incorrectCount=incorrectCount+1;
end
end
if incorrectCount==length(guessingWord);
guessesLeft=guessesLeft-1;
fprintf('Your letter is not in the word!\n');
else
fprintf('You have made a correct guess!\n')
end
disp(Guess);
NewGuess=0;
while ~(guessesLeft <= 0) || ~(correctCount == length(guessingWord-1))
incorrectCount1=0;
currentNum=0;
NewGuess=input('Please enter your guess: ','s');
for i=1:length(Guess)
if NewGuess==guessingWord(i)
Guess(i)=NewGuess;
lettersLeft=lettersLeft-1;
currentNum=currentNum+1;
correctCount=correctCount+1;
else
incorrectCount1=incorrectCount1+1;
end
end
if currentNum==0
fprintf('Your letter is not in the word!\n');
guessesLeft=guessesLeft-1;
else
fprintf('You have made a correct guess!\n');
end
disp(Guess);
end
if guessesLeft==0
fprintf('You didnt win! Not all letters were guessed.\n');
else
fprintf('You won! You correctly guessed all the letters!\n');
end

Respuestas (1)

Manas
Manas el 5 de Oct. de 2023
Hello Matthew Smyth,
I understand that you want to develop a MATLAB program that can simulate the game of hangman. Particularly, you are facing an issue preventing the same input from being entered twice or stopping the correct-guesses counter from incrementing again for an already verified input.
After inspecting your code, I identified a few areas in the code that can be modified.
  • This if-else block will always execute the else block as the condition can never be true.
if incorrectCount==length(guessingWord);
guessesLeft=guessesLeft-1;
fprintf('Your letter is not in the word!\n');
else
fprintf('You have made a correct guess!\n')
end
  • Consider combining the first guess prompt with the while loop to make the code look cleaner easier to understand.
  • The looping conditions seem to be incorrect.
  • The second half of the while condition seems to be incorrect even according to your current logic
while ~(guessesLeft <= 0) || ~(correctCount == length(guessingWord-1))
Coming back to the main issue of mitigating the incrementation of the "correctCount" counter for the same input, a truth-table (boolean array) can be considered. Initially a truth-table of size "guessingWord" could be initialized with all indexes set as false. Once a correct guess is made, that index can be flipped to true so that correctCount" counter is not updated again for that particular guess.
Here is a sample implementation that combines all of the above suggestions
IsGuessed = false(1, length(guessingWord))
while ~(guessesLeft <= 0) || ~(correctCount == length(guessingWord))
incorrectCount=0;
UserGuess=input('Please enter your guess: ','s');
for i=1:length(guessingWord)
if (UserGuess==guessingWord(i) && IsGuessed(i) == false)
Guess(i)=UserGuess;
lettersLeft=lettersLeft-1;
correctCount=correctCount+1;
else
incorrectCount=incorrectCount+1;
end
end
if incorrectCount==1
fprintf('Your letter is not in the word!\n');
guessesLeft=guessesLeft-1;
else
fprintf('You have made a correct guess!\n');
end
disp(Guess);
end
Again, this is only a sample implementation. Please feel free to make changes to it as per your requirements.
Hope this helps!

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by