This site requires JavaScript, please enable it in your browser!
Greenfoot back
mntng
mntng wrote ...

2014/10/19

need help for assignment: loop for non-random chess pieces locations

mntng mntng

2014/10/19

#
The world is of size 8(width)*4(height). I can successfully place the chess pieces in one complete horizontal line. But how to place the pieces on every line and cover the whole world by using either "while" or "doWhile" or "for" loops? (nested loop is not allowed in this case)
NikZ NikZ

2014/10/19

#
A for inside of a for should work.
Super_Hippo Super_Hippo

2014/10/19

#
Nested loops are not allowed? =/ Great, then you can place 4 lines. So one for-loop and every time it creates one line. Or you have one loop and have a switch in it and actually place every object manually...
danpost danpost

2014/10/20

#
I would create one loop that goes from zero to 32 (one iteration per grid-square). Having 'n' as the iterator, I would then use '(n%8, n/8)' for the locations inside the loop.
davmac davmac

2014/10/21

#
Another way: you can loop from 0 to 31 to cover all squares, and maintain two variables - row and column - inside the loop (but declare them outside the loop!). During each iteration, increment the column, and if it reaches 8, reset it to 0 and increment the row.
You need to login to post a reply.