MATLAB Answers

How do I graph game scores over time?

16 views (last 30 days)
Kylie Ellis
Kylie Ellis on 6 Mar 2020
Commented: Kylie Ellis on 12 Mar 2020
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');

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 6 Mar 2020
Edited: KSSV on 6 Mar 2020
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');

  3 Comments

Kylie Ellis
Kylie Ellis on 6 Mar 2020
THANK YOU SO VERY MUCH!!!
Prashanthgoud DOMAKONDA
Prashanthgoud DOMAKONDA on 12 Mar 2020
is this game for rock paper scissor non zero sum game?
Kylie Ellis
Kylie Ellis on 12 Mar 2020
I genuinely have no idea. Sorry.

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by