I need help with my code for Odd Vs Even

So I am trying to make an Odd vs Even game and I'm having a hard time getting the graph to come out correctly, I know that I have to have a loop so that it can get the correct amount of data points to get a graph, but im just stuck on how to get that. FYI im supposed to have 10,000 games.
%% OvE Part1
%% Description
% Charles Combs
% Students are making a code to make two computers play odds and evens
% together, Player A is either even or Odd, and Player B would be the
% oppiste of player A. When one player gets there score, then they get a
% point while the other player gets none. This game continues for 10,000
% turns. After that all the scores are added up, and the player with the
% largest score wins.
clearvars
clc
%% Steps for Odd vs Even
%A
%B
%A throw one;
%B does the same
%A_throw=rand(?)
%B_throw=rand(?)
%add up outcome of both rand outputs
%If outcome is divisible by 2, A score increases by 1, and B score is increased by 0
%Else If A score increases by 0, and B score increases by 1
%Plot the difference in A_score and B_score.
%% Initial Conditions
scoreA = 0; % odd
scoreB = 0; % even
games = 0;
%% Game Started
while(games < 1)
playerA = randi(2);
playerB = randi(2);
if(playerA ~= playerB)
games = games+1;
end
if playerA == 1
if playerB == 1
scoreB = scoreB +1;
elseif playerB == 2
scoreA = scoreA +1;
end
elseif playerA == 2
if playerB == 1
scoreA = scoreA +1;
elseif playerB == 2
scoreB = scoreB +1;
end
end
end
%% Results of Game
fprintf("Score of Player B is %d/n", scoreB)
fprintf("Score of Player A is %d/n", scoreA)
%% New Value for Plotting
diff_i = scoreA - scoreB;
%% Plotting this Bad Boy
histogram(diff_i, games)
title('Difference in Scores vs # of games');
xlabel('Throws');
ylabel('ScoreA - ScoreB')
%% Who Won?
if scoreA > scoreB
fprintf("Player A won!")
elseif scoreA < scoreB
fprintf("Player B won!")
else
fprintf("OMG!! It's a draw!")
end

 Accepted Answer

playerA=randi(2,1,10000);
playerB=randi(2,1,10000);
scoreA=or(and(playerA==1,playerB==2),and(playerA==2,playerB==2));
scoreB=~scoreA;
diff_i = cumsum(scoreA) - cumsum(scoreB);
plot(diff_i);

More Answers (1)

You only iterate until the random selection is different once .
You are not recording the scores as you go, so you have nothing to plot.
A_won = zeros(1,MaxGames);
B_won = zeros(1,MaxGames);
For game K, increment A_won(K) or B_won(K) .
Afterwards you can plot cumsum(A_won) vs cumsum(B_won)

1 Comment

By the way, you do not have to do as much work on the random assignements of player.
playerA = randi(2);
playerB = 3-playerA;
If A got 1 then B gets 3-1 -> 2. If A got 2 then B gets 3-2 -> 1. Always different, no need for tests.

Sign in to comment.

Categories

Find more on Number games in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!