How do I graph game scores over time?

3 次查看(过去 30 天)
How can I graph scores over time when the scores are determined in an if-else function? (Plot the score on the y axis and the round # on the x axis)
I have to code a rock-paper-scissors game where it is a computer playing itself, using the random function (randi). Every time I try to plot it, nothing shows up. The following graph is what it should look like (just an example).
This is my code that I have set up at the moment:
scoreA=0; %score for playerA
scoreB=0; %score for playerB
score={1,2,3}
i=0
for i=(0:9999)
playerA=score{randi(length(score))}
playerB=score{randi(length(score))}
if ((playerA==2)&&(playerB==2)) %tie 2=2
disp('No points.')
scoreA=scoreA-0
scoreB=scoreB-0
i=i+1
elseif ((playerA==2)&&(playerB==1)) %2>1
disp('+1 point to playerA!')
scoreA=scoreA+1
i=i+1
elseif ((playerA==3)&&(playerB==2)) %3>2
disp('+1 point to playerA!')
scoreA=scoreA+1
i=i+1
elseif ((playerA==1)&&(playerB==3)) %1>3
disp('+1 point to playerA!')
scoreA=scoreA+1
i=i+1
elseif ((playerA==1)&&(playerB==2)) %1<2
disp('+1 point to playerB!')
scoreB=scoreB+1
i=i+1
elseif ((playerA==3)&&(playerB==1)) %3<1
disp('+1 point to playerB!')
scoreB=scoreB+1
i=i+1
elseif ((playerA==2)&&(playerB==3)) %2<3
disp('+1 point to playerB!')
scoreB=scoreB+1
i=i+1
elseif ((playerA==1)&&(playerB==1)) %tie 1=1
disp('No points.')
scoreA=scoreA-0
scoreB=scoreB-0
i=i+1
elseif ((playerA==3)&&(playerB==3)) %tie 3=3
disp('No points.')
scoreA=scoreA-0
scoreB=scoreB-0
i=i+1
end
end
% End Game Results
disp('END GAME SCORES')
disp('Score for playerA:')
disp(scoreA)
disp('Score for playerB:')
disp(scoreB)
if (scoreA > scoreB)
disp('playerA won the game!')
elseif (scoreA < scoreB)
disp('playerB won the game!')
elseif (scoreA == scoreB)
disp('Game ends in a tie!')
end
%% Score over time figure
% Plot the score on the y axis and the round # on the x axis
plot(-,-);
grid on;
title('Score over Time');
ylabel('Score');
xlabel('Round Number');

采纳的回答

KSSV
KSSV 2020-3-6
编辑:KSSV 2020-3-6
You need to save the result into a vector and plot at the end. This is included in the below code.
scoreA=0; %score for playerA
scoreB=0; %score for playerB
score={1,2,3}
i=0
R = zeros([],1) ;
for i=(0:9999)
playerA=score{randi(length(score))}
playerB=score{randi(length(score))}
if ((playerA==2)&&(playerB==2)) %tie 2=2
disp('No points.')
scoreA=scoreA-0
scoreB=scoreB-0
i=i+1
elseif ((playerA==2)&&(playerB==1)) %2>1
disp('+1 point to playerA!')
scoreA=scoreA+1
i=i+1
elseif ((playerA==3)&&(playerB==2)) %3>2
disp('+1 point to playerA!')
scoreA=scoreA+1
i=i+1
elseif ((playerA==1)&&(playerB==3)) %1>3
disp('+1 point to playerA!')
scoreA=scoreA+1
i=i+1
elseif ((playerA==1)&&(playerB==2)) %1<2
disp('+1 point to playerB!')
scoreB=scoreB+1
i=i+1
elseif ((playerA==3)&&(playerB==1)) %3<1
disp('+1 point to playerB!')
scoreB=scoreB+1
i=i+1
elseif ((playerA==2)&&(playerB==3)) %2<3
disp('+1 point to playerB!')
scoreB=scoreB+1
i=i+1
elseif ((playerA==1)&&(playerB==1)) %tie 1=1
disp('No points.')
scoreA=scoreA-0
scoreB=scoreB-0
i=i+1
elseif ((playerA==3)&&(playerB==3)) %tie 3=3
disp('No points.')
scoreA=scoreA-0
scoreB=scoreB-0
i=i+1
end
R(i)= scoreA-scoreB ;
end
% End Game Results
disp('END GAME SCORES')
disp('Score for playerA:')
disp(scoreA)
disp('Score for playerB:')
disp(scoreB)
if (scoreA > scoreB)
disp('playerA won the game!')
elseif (scoreA < scoreB)
disp('playerB won the game!')
elseif (scoreA == scoreB)
disp('Game ends in a tie!')
end
%% Score over time figure
% Plot the score on the y axis and the round # on the x axis
plot(R,'b');
grid on;
title('Score over Time');
ylabel('Score');
xlabel('Round Number');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by