BadBumble [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=25906 Going back in For Next Loop <p>Hello,<br /> I'm a noob at programming and need some help please.<br /> I'm try to write a for next loop that randomly fills an 2D array (my game grid), but I also want it to check that if that cell already has been used, then knock the counter back -1 and tries again. So in this way, at the end of the loop there should be a set number of cells filled.<br /> My code works great except that when it detaches a duplicate cell, it doesn't roll the counter back.</p> <p>So say at loop count 50, there's a duplicate cell, it doesn't alter the cell (which is correct), but it doesn't roll the loop counter back, it just continues.<br /> Here's my code. I was hoping that the i-=1 would do the trick, but it doesn't.</p> <p>function make_grid()<br /> for i=1,level*100+1 do<br /> y=flr(rnd(gridheight-6))<br /> x=flr(rnd(gridwidth-6))<br /> --is the space empty? then add<br /> --a bomb. if not rollback counter<br /> if grid[1][y+3][x+1]==0 then<br /> grid[1][y+3][x+1]=2<br /> else<br /> i-=1<br /> end<br /> end<br /> end</p> <p>I've had print statements in there print the value of I as it went along, but the I-=1 doesn't effect the loop counter I<br /> Any ideas and thanks in advance.</p> https://www.lexaloffle.com/bbs/?tid=30040 https://www.lexaloffle.com/bbs/?tid=30040 Thu, 05 Oct 2017 19:42:53 UTC