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

4 ビュー (過去 30 日間)
David
David 2024 年 12 月 6 日
コメント済み: David 2024 年 12 月 6 日
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

採用された回答

Walter Roberson
Walter Roberson 2024 年 12 月 6 日
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.
  1 件のコメント
David
David 2024 年 12 月 6 日
This worked perfectly, thank you!

サインインしてコメントする。

その他の回答 (1 件)

Subhajyoti
Subhajyoti 2024 年 12 月 6 日
編集済み: Subhajyoti 2024 年 12 月 6 日
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:

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by