Variables doesn't update after running the code.

조회 수: 3 (최근 30일)
David  Liang
David Liang 2022년 4월 24일
댓글: Voss 2022년 4월 25일
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
Stephen23
Stephen23 2022년 4월 24일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 4월 24일
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
not right
guess = [2 1];
if guess~=answer
disp('not right');
else
disp('right');
end
right
guess = [1 5];
if guess~=answer
disp('not right');
else
disp('right');
end
right
guess = [2 5];
if guess~=answer
disp('not right');
else
disp('right');
end
right
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
not right
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
David  Liang
David Liang 2022년 4월 25일
Thank you so muchh!
Voss
Voss 2022년 4월 25일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Board games에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by