Shortening a loop [Archive] - Team17 Forum

PDA

View Full Version : Shortening a loop

Xinos
27 Apr 2007, 12:42
I've got a rather uggly bit of code here and I'd like to clean up. Right now it's hard coded and would have to be made longer and longer if I add more cards. I've tried to get this loop work nicely but there's too much going on so I get lost and confused allong the way ;P
function sortCards() {
//Associates 10 picture frames with the different cards.
for (i=0; i<20; i++) {
if (pictureNum[i] == 1 || pictureNum[i] == 2) {
frame = 2;
} else if (pictureNum[i] == 3 || pictureNum[i] == 4) {
frame = 3;
} else if (pictureNum[i] == 5 || pictureNum[i] == 6) {
frame = 4;
} else if (pictureNum[i] == 7 || pictureNum[i] == 8) {
frame = 5;
} else if (pictureNum[i] == 9 || pictureNum[i] == 10) {
frame = 6;
} else if (pictureNum[i] == 11 || pictureNum[i] == 12) {
frame = 7;
} else if (pictureNum[i] == 13 || pictureNum[i] == 14) {
frame = 8;
} else if (pictureNum[i] == 15 || pictureNum[i] == 16) {
frame = 9;
} else if (pictureNum[i] == 17 || pictureNum[i] == 18) {
frame = 10;
} else if (pictureNum[i] == 19 || pictureNum[i] == 20) {
frame = 11;
}
_root["card"+i].pictureNum = frame;
}
}

AndrewTaylor
27 Apr 2007, 14:22
That's ActionScript, isn't it? That has Math functions, right?
for (i=0; i<20; i++)
_root["card"+i].pictureNum = Math.ciel(pictureNum[i]/2)+1;
That seems like it should work to me.

Xinos
27 Apr 2007, 16:29
But of course! I knew it would be far more simple than I could imagine =D
I was trying all sorts of crazyness with up to 5 'for loops' within each other doing wierd things untill i got stuck and realised it would never work..

bloopy
28 Apr 2007, 15:49
ciel
*ceiling :p

Xinos
1 May 2007, 15:38
It's finnished! Check out mah memory game!
http://xinos.planet-eggplant.com/spel/Memory.html

*Splinter*
1 May 2007, 15:50
It's finnished! Check out mah memory game!
http://xinos.planet-eggplant.com/spel/Memory.html
Not bad, but every try counts as 2, rather than 1

vBulletin® v3.8.6, Copyright ©2000-2013, Jelsoft Enterprises Ltd.