How do I record data within a loop?

1 view (last 30 days)
Kyle
Kyle on 20 Jul 2014
Answered: Geoff Hayes on 21 Jul 2014
I am using the following while loop to track how often one player beats another player in a game:
%Duel Simulator
HHP = HeroHealth;
OHP = OppHealth;
PlayerDead = 0;
AccDefault = 0;
AccDefault2 = 0;
while PlayerDead==0
AccDefault = rand;
if OHP > 0;
if AccDefault > HeroAccuracy;
OppHealth;
else
OHP = OHP-randi([1,HeroMax]);
end
% fprintf('Opponent Health %g\n',OHP)
else
PlayerDead = 1;
end
AccDefault2 = rand;
if HHP > 0;
if AccDefault2 > OppAccuracy;
HeroHealth;
else
HHP = HHP-randi([1,OppMax]);
end
% fprintf('Hero Health %g\n',HHP)
else
PlayerDead = 2;
end
end
if PlayerDead==1
fprintf('Hero Won\n')
else
fprintf('Opp Won\n')
end
When I run that code by itself it tells me if "Hero Won" or if "Opponent Won". However, when I attempt to run it within another loop to simulate 50,100,etc fights something goes wrong within the code and it displays, for example, 50 lines of "Hero Won", which is wrong. It is also unable to track how many times "PlayerDead" has equalled 1 and simply tells me only one fight was tracked, for example, hero won 1/50 + opp won 0/50.
I know that it is my coding that is wrong, obviously, and would be very pleased if someone could explain where I am going wrong.
Thank you in advance, Kyle.

Answers (1)

Geoff Hayes
Geoff Hayes on 21 Jul 2014
Kyle - without seeing your full code, especially where the for loop for the 50,100,etc. repeated duels, it is not easy to point to a certain line of code and say "there is the problem". A guess would be that one or more local variables are not being reset prior to each new duel. For example, if player one wins the first duel and PlayerDead is not reset to zero (from one) then the above while loop is ignored and the if statement will always evaluate PlayerDead==1 to be true and so print out the same statement 50 times. Or, since you are using random number in your above duel, it could be that you are resetting the random number generator with the same seed at each of the duels...and so the same set of random numbers will be produced for each duel, and the hero will always win.
As for tracking the number of times that the hero has won, you need to add a counter for that. So something like
heroWinCount = 0;
% iterate over each of the duels
for k=1:50
% your duel code
% reset all local variables
% do the duel while loop
% display outcome and record hero win count
if PlayerDead==1
fprintf('Hero Won\n')
heroWinCount = heroWinCount + 1;
else
fprintf('Opp Won\n')
end
end
Try adding the above counter and reviewing your code closely to see how/when the duel for loop begins (checking for the resetting of local variables and the maybe resetting of the random number generator).

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!