### Thread: Help with a loop

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
2
Rep Power
0

#### Help with a loop

I've been banging my head on my desk trying to figure out how to simplify the following code from my program into a working loop in order to reduce the number of lines of code i have:

if (position1 == Marker.EMPTY)
a = (char) sp;
else if (position1 == Marker.X)
a = (char) x;
else if (position1 == Marker.O)
a = (char) o;
if (position2 == Marker.EMPTY)
b = (char) sp;
else if (position2 == Marker.X)
b = (char) x;
else if (position2 == Marker.O)
b = (char) o;
if (position3 == Marker.EMPTY)
c = (char) sp;
else if (position3 == Marker.X)
c = (char) x;
else if (position3 == Marker.O)
c = (char) o;
if (position4 == Marker.EMPTY)
d = (char) sp;
else if (position4 == Marker.X)
d = (char) x;
else if (position4 == Marker.O)
d = (char) o;
if (position5 == Marker.EMPTY)
e = (char) sp;
else if (position5 == Marker.X)
e = (char) x;
else if (position5 == Marker.O)
e = (char) o;
if (position6 == Marker.EMPTY)
f = (char) sp;
else if (position6 == Marker.X)
f = (char) x;
else if (position6 == Marker.O)
f = (char) o;
if (position7 == Marker.EMPTY)
g = (char) sp;
else if (position7 == Marker.X)
g = (char) x;
else if (position7 == Marker.O)
g = (char) o;
if (position8 == Marker.EMPTY)
h = (char) sp;
else if (position8 == Marker.X)
h = (char) x;
else if (position8 == Marker.O)
h = (char) o;
if (position9 == Marker.EMPTY)
i = (char) sp;
else if (position9 == Marker.X)
i = (char) x;
else if (position9 == Marker.O)
i = (char) o;

Last edited by rhyno626; May 18th, 2013 at 04:34 AM. Reason: my brain is zapped...
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
194
Rep Power
77
to make this shorter, make position into an array so in the loop you can do position[i] and have the loop go 9 times

I'm not sure why and what you are using those variables from a - i for but you should get rid of them and make them an array for example alphabetLetter[i]

An example of what this would look like minifide is
Code:
```for (var i=0;i<8;i++){
if (position[i] == Marker.EMPTY){
alphabetLetter[i] = (char) sp;
}else if (position[i] == Marker.X){
alphabetLetter[i] = (char) x;
}else if (position[i] == Marker.O)
alphabetLetter[i] = (char) o;
}
}```

This might not work but I'm pretty sure that it will
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
2
Rep Power
0
Thank you for the quick response! Unfortunately since we haven't been formally introduced to arrays in class yet, we are not allowed to use them (yeah it's pretty ridiculous).

The instructor pointed us in the direction he wanted us to go by writing the first few lines of code identifying the constants and enums. From there I wrote the if statements above.

Any help would be greatly appreciated!!
4. Are you familar with ?:
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
194
Rep Power
77
Well, if your not allowed to use arrays you won't be able to make that shorter