MATLAB: How to graph game scores over time

graphingif statementrock paper scissorsscores

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');

Best Answer

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');
Related Question