Javascript: Mini Space Invaders I'm pretty new to code golfing, so I gave one of my first shots to a space invader clone called "Invas!on". It's a little less than 500 bytes, fully ASCII generated, and covers moving around and shooting at the aliens. There is also a little point system.

My specific question is about generating enemy patterns. I want to generate something like this:

...x.x.x.x.x.x...          
..x.x.x.x.x.x.x..        
...x.x.x.x.x.x...


...and so on.

Currently, I'm using this code:

for(b=[],j=2;j&lt;136;j+=2)b.push(j),j==14&amp;&amp;(j=41)||j==55&amp;&amp;(j=80)||j==94&amp;&amp;(j=121)


It's just a loop that pushes enemy positions into an array b. Positions are every two spaces on a line, with a varying margin on both sides.

What is the shortest way to do this?
58 : -20 bytes(25%) from OP

for(b=[],j=2;j&lt;136;j+=j==14||j==94?29:j==55?27:2)b.push(j)


This should do the same thing as your code, but without all the ugly parentheses and explicit assignments. Since all you're really doing is adding each time, all you have to do is check how much to add.

I should note that I have no idea where you got the numbers from in the original code. Since it doesn't seem to match the pattern you gave, I assume the pattern is just an example (and the actual lines are longer), or there's something going on in your other code (not shown here). Either way, I matched up my positions with yours.



For a fixed pattern like this, though, it  might be shorter to just encode it in a bitmap-style string or bitmasked integer. Doing it loop-style allows you to vary it a bit easier, but if your pattern is constant it's something to be aware of. For instance, your shown pattern has 51 positions, which are either "on" or "off". This means it needs 51 bits for a straight bitmask, which can fit in a single number:

000101010101010000010101010101010000010101010101000 = 0xAAA0AAA82AA8


So, if you can unpack that into an array efficiently, you may be able to score some more bytes. Unfortunately, I'm no Javascript whiz, so I couldn't get it shorter this way, leaving it at 61 bytes. Others may be able to improve upon this:

for(b=[],j=0,m=0xAAA0AAA82AA8;m&gt;0;m&gt;&gt;&gt;=1,j++)if(m&amp;1)b.push(j)


So for this exact pattern, the first method is shorter (for me). If the pattern wasn't so evenly spaced (allowing simple additions), the second would most likely end up shorter.