Thursday, August 26, 2010

Algorithms are fun

I've been working on a persnickety (yes, I actually just used that word) little algorithmic issue for a new game I'm working on. The game is kind like Phlinx and Bubble Town, so there is a little projectile that flies up toward the board and, upon matching three or more, causes the pieces to explode and disappear.

That matching bit was fun in and of itself, but I got that one sorted out. The challenge now is the line-of-sight check for spotting where the projectile is allowed to land.

The problem is a little tougher because the board is staggered (Row 0 starts at pixel 45 and Row 1 starts at pixel 90...for example), so you can't just simply check on an array basis. You use the array, of course, but you have to kind of treat it a bit differently.

For example, consider:

[ ][ ][O][ ]
[ ][ ][X][ ]

[P]

Since the row offsets match, we can immediately see that the projectile (P) can not get to the open position (O) because there is a piece (X) in the way.

But, if we stagger the board:

[ ][ ][O][ ]
[ ][ ][X][ ]

[P]

...and if we imagine an line drawn from P to O, then the piece CAN hit that spot.

But how far can it go?

[ ][ ][ ][O]
[ ][ ][ ][O]
[ ][ ][O][ ]
[ ][ ][X][ ]

[P]

...looking at this board, an imagining that line draw up, the projectile can find its way all the way to the top of the board here.

Those are just a couple of the issues associated with this. Either way, it's a fun challenge (so far), but I'm certainly finding myself brooding more than coding at times. :)

No comments:

Post a Comment