Variables doesn't update after running the code.
Afficher commentaires plus anciens
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 commentaire
Stephen23
le 24 Avr 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.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Board games dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!