Variables doesn't update after running the code.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, I'm new to matlab and I'm trying to make a game that randomly generate a 2D location in a 9x9 grid and the user has to guess it. I realized that when i runs the code, the workspace is always empty. Also when i guessed the answer, it should pop up a sentence says 'Its correct.' I tried all the combination (1,1) to (9,9) but non of them are the answer.
%Generate a one in the a random position in matrix as the answer
random_answer()
%Ask the user to guess the location
guess_column = input('Please guess the column (1~9):\n');
guess_row = input('Please guess the row (1~9):\n');
%Print the location user enetered
fprintf('Your guess is\n');
guess = [guess_column,guess_row];
disp(guess);
%Verify the guess and the answer
while guess~=answer
fprintf('Your answer is wrong\n')
update_guess_column = input('Please guess the column again.(1~9):\n');
update_guess_row = input('Please guess the row again.(1~9):\n');
guess_column = update_guess_column;
guess_row = update_guess_row;
end
fprintf('It's right!!')
function random_answer()
answer_column = randi([1 9],1,1);
answer_row = randi([1 9],1,1);
answer = [answer_column,answer_row];
assignin('base','answer_column',answer_column)
assignin('base','answer_row',answer_row)
assignin('base','answer',answer)
end
1 Kommentar
Stephen23
am 24 Apr. 2022
Rather than making data magically jump between workspaces using ASSIGNIN, the simpler and much more efficient approach to passing data is to use input/output arguments. You should use ouput arguments.
Akzeptierte Antwort
Voss
am 24 Apr. 2022
This line:
fprintf('It's right!!')
contains a syntax error. So, instead of that sentence being printed, you will see an error message on the command line when that line is run. To include a single-quote in a character vector, use two single-quotes, like this:
fprintf('It''s right!!') % print: It's right!!
Another problem is with the while condition:
while guess~=answer
Since guess and answer are 1x2 vectors, guess~=answer is a 1x2 vector. A vector used as a conditional in while (or if) evaluates to true only if the condition is true for all elements of the vector, so the condition guess~=answer will be considered true only when neither element of guess is equal to the corresponding element of answer. So if the user gets only the row or column right, that condition will be false (i.e., considered a correct guess), when it should be true (i.e, considered an incorrect guess). To illustrate this:
answer = [2 5];
guess = [1 1];
if guess~=answer
disp('not right');
else
disp('right');
end
guess = [2 1];
if guess~=answer
disp('not right');
else
disp('right');
end
guess = [1 5];
if guess~=answer
disp('not right');
else
disp('right');
end
guess = [2 5];
if guess~=answer
disp('not right');
else
disp('right');
end
Instead of using a vector condition you can use isequal to compare vectors or any to see if any element is not equal:
answer = [2 5];
guess = [2 1];
if any(guess~=answer)
disp('not right');
else
disp('right');
end
Finally, you need to update the variable guess inside the while loop:
while guess~=answer
fprintf('Your answer is wrong\n')
update_guess_column = input('Please guess the column again.(1~9):\n');
update_guess_row = input('Please guess the row again.(1~9):\n');
guess_column = update_guess_column;
guess_row = update_guess_row;
guess = [guess_column,guess_row]; % update the variable guess
end
2 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!