Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 18th, 2013, 04:33 AM
 rhyno626
Registered User

Join Date: May 2013
Posts: 2
Time spent in forums: 23 m
Reputation 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
May 18th, 2013, 04:58 AM
 jack13580
Contributing User

Join Date: Aug 2012
Posts: 185
Time spent in forums: 1 Week 16 h 36 m
Reputation Power: 76
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
May 18th, 2013, 05:31 AM
 rhyno626
Registered User

Join Date: May 2013
Posts: 2
Time spent in forums: 23 m
Reputation 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
May 18th, 2013, 09:12 AM
 bullet
Java Junkie

Join Date: Feb 2004
Location: Mobile, Alabama
Posts: 3,974
Time spent in forums: 1 Month 2 Weeks 1 Day 8 h 1 m 16 sec
Reputation Power: 1283
Are you familar with ?:

#5
May 18th, 2013, 02:14 PM
 jack13580
Contributing User

Join Date: Aug 2012
Posts: 185
Time spent in forums: 1 Week 16 h 36 m
Reputation Power: 76
Well, if your not allowed to use arrays you won't be able to make that shorter

 Viewing: Dev Shed Forums > Programming Languages > Java Help > Help with a loop