For loop in if/elseif statements which compares multiple characters

1 view (last 30 days)
I have a project with school, in which I have to make a script in which I can plot possible darttrows on a dartboard. I have already found a way to plot it, As a small example, here below. The problem is that a dart board has 62 different scores and I have to emulate when someone wants to trow 1 time, 2 time or 3 time. Which means that the loop can take for over 372 comparisons. Can I use For loops for this to shorten the script.
if trows == 1
if (t1 == p1)% The t1 is already asked. They will answer in a dartscore, for example Bulleye is B and triple 20 is T20, P1 is the position of the score, in this case it is single 1, thus S1)
p1x = [458];
p1y = [429 354];
plot(p1x, p1y, 'b*', 'LineWidth', 2, 'MarkerSize', 15)
elseif (t1 == p2)
p2x = [428];
p2y = [429 354];
plot(p2x, p2y, 'b*', 'LineWidth', 2, 'MarkerSize', 15)
elseif (t1 == p3)
p3x = [458];
p3y = [429 354];
plot(p3x, p3y, 'b*', 'LineWidth', 2, 'MarkerSize', 15)
elseif (t1 == p4)
p4x = [458];
p4y = [429 354];
plot(p4x, p4y, 'b*', 'LineWidth', 2, 'MarkerSize', 15)
  4 Comments
Selsa31
Selsa31 on 24 Mar 2019
Ow, and I also ask them how many trows they need to trow, it could be t1 one time, and (t1,t2 &t3 ) the other
Walter Roberson
Walter Roberson on 24 Mar 2019
I suggest you input the values as character vector, such as if you use
%sector 1: bull's eye 'e'
%sector 2: bull 'b'
%sector 3: single (inner) 's or 'p'
%sector 4: triple 't'
%sector 5: single (outer) 's' or 'p'
%sector 6: double 'd'
t1 = input('enter first throw: ', 's');
Then
if isempty(t1)
error
end
%default is single if the user only inputs a number
if ~ismember(lower(t1(1)), ['1':'9', 'sdtpbe'])
error
end
if rand < 1/3
sector = 3;
else
sector = 5;
end
if ismember(lower(t1(1)), 'sp') %'s', and 'p' is alias for it
t1 = t1(2:end);
elseif ismember(lower(t1(1)), 'd')
sector = 6;
t1 = t1(2:end);
elseif ismember(lower(t1(1)), 't')
sector = 4;
t1 = t1(2:end)
elseif ... take care of bull and bull's eye
end
%now sector is set appropriately and t1 has characters representing a number, or possibly empty for 'b' (bull) or 'e' (bulls eye)

Sign in to comment.

Accepted Answer

TADA
TADA on 24 Mar 2019
The simplest way to simulate N dart throws is to use a for loop from 1 to n and simulate each throw inside the loop:
N = randi(3);
for i = 1:N
% simulate dart throw here
end
there are other ways to do this without the loop (not using if/elseif blocks) but thats for later...
as for your numerous if statements, I think you are going about it the wrong way
a dart board is a circle divided into 82 sections. the simplest way to describe a section on a circle is using polar coordinates, i.e using an angle (theta) and a radius (r).
each score (1 through 20) is represented by a range of theta, and the inner/outer bullseye, and the multipliers (x1, x2, x3) are controlled by the radius.
Now you can randomize the polar coordinates and score each dart according to the coordinates it hit, instead of doing it the opposite way like you suggested. that should reduce the number of control statements significantly.
matlab also lets you plot polar axes, have a look here, which again makes life easier as you don't have to calculate the carthesian coordinates of each section in the dartboard
all of this is good because less code == less bugs
  2 Comments
Selsa31
Selsa31 on 24 Mar 2019
Thanks for your input Tada, i forgot to mention that I ask before this, what score they want to throw, so I have to plot the position they want to throw, Do you have any input about that?
TADA
TADA on 24 Mar 2019
and I'm guessing you don't want to ask for coordinates...
You can make a map of the sections on the board, something of that sort:
% radius scores mapped like that: [lower, upper, score, multiplier]
rScore = [0 0.1 50 0;... % inner bulls eye
0.1 0.2 25 0;... % outer bulls eye
0.2 0.5 0 1;... % inner x1 multiplier
0.5 0.6 0 3;... % x3 multiplier
0.6 0.9 0 1;... % outer x1 multiplier
0.9 1 0 2]; % x2 multiplier
% theta scores mapped like that: [lower, upper, score]
thScores = [(bsxfun(@plus, [-9 9], (0:18:351)') * pi()/180) [6 13 4 18 1 20 5 12 9 14 11 8 16 7 19 3 17 2 15 10]'];
now you have the sections mapped by angle and radius.
You can ask the user to input score and multiplier and search for those in the map....
it's a bit off i have to admit...

Sign in to comment.

More Answers (0)

Categories

Find more on Surfaces, Volumes, and Polygons 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!