How do I save changes to a matrix in a while loop?

7 Ansichten (letzte 30 Tage)
David
David am 6 Dez. 2024
Kommentiert: David am 6 Dez. 2024
I have a score board for a rock paper scissors game. I have to give an output of a 1, 0 or -1 based on whether it was win, tie, or loss and based on that update the score board accordingly. Ive done that but im not sure how to keep the scoreboard data between new runs.
Here's the code
clc;clear all
% Setup
playAgain=1;
while playAgain
disp ('Welcome to Rock, Paper, Scissors: Input 1 for Rock, 2 for Paper, and 3 for Scissors')
x=input('What would you like to play? ');
% Player and Computer Choices
while x < 1 == 1
if x < 1
disp('Not A Valid Input, Please Choose Again');
x=input('What would you like to play? ');
end
end
while x > 3 == 1
if x > 3
disp('Not A Valid Input, Please Choose Again');
x=input('What would you like to play? ');
end
end
y=randi([1 3],1,1);
options={'Rock' 'Paper' 'Scissors'};
fprintf('You Chose %s\n',options{x})
options={'Rock' 'Paper' 'Scissors'};
fprintf('The Computer Chose %s\n',options{y})
[score,attempts] = determinewinner(x,y);
fprintf('Number of Attempts: %d\n',attempts)
fprintf('Player Wins: %d\n Ties: %d\n Computer Wins: %d\n', score)
playAgain=askToPlayAgain();
end
% Functions
function [score,attempts] = determinewinner(x,y)
attempts=0;
win=false;
while ~win
attempts=attempts+1;
if [x;y]==[1;3]
disp("You Win!")
win = true;
z=1;
elseif [x;y]==[1;2]
disp("You Lose!")
win = true;
z=-1;
elseif [x;y]==[1;1]
disp("It's A Tie!")
win = true;
z=0;
end
if [x;y]==[2;1]
disp("You Win!")
win = true;
z=1;
elseif [x;y]==[2;3]
disp("You Lose!")
win = true;
z=-1;
elseif [x;y]==[2;2]
disp("It's A Tie!")
win = true;
z=0;
end
if [x;y]==[3;2]
z=1;
disp("You Win!")
win = true;
elseif [x;y]==[3;1]
disp("You Lose!")
z=-1;
win = true;
elseif [x;y]==[3;3]
disp("It's A Tie!")
z=0;
win = true;
end
end
basescore=[0 0 0];
if z==1
score=basescore+[1 0 0];
elseif z==0
score=basescore+[0 1 0];
elseif z==-1
score=basescore+[0 0 1];
end
end
function playAgain=askToPlayAgain()
response=input("Play Again? (Y/N): ","s");
playAgain=isequal(response, "Y");
end

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 6 Dez. 2024
overall_score = [0 0 0];
before the while loop.
overall_score = overall_score + score;
fprintf('Overall Player Wins: %d\n Ties: %d\n Computer Wins: %d\n', overall_score)
before asking to play again.

Weitere Antworten (1)

Subhajyoti
Subhajyoti am 6 Dez. 2024
Bearbeitet: Subhajyoti am 6 Dez. 2024
Hi @David,
In the given code snippet, variable 'attempts' is re-initialised to '0' every time the function is called. Also, variable 'win' is always set to 'true'. Hence, the loop 'while ~win {}' is running only once and the scoreboard is not updating correctly.
You can it as a global variable, and update from the function everytime.
function [score,attempts] = determinewinner(x,y)
global attempts
...
end
Refer to the following MathWorks Documentation to know about 'global' valiables in MATLAB:

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by