XNA Game – Rotation – Knowing when you’re wrong – Part 6

Sometimes in programming you realise that you have made a wrong turn somewhere. Often the first signs are the fact that changing one part of the code breaks everything or doing what should be the simplest thing becomes very difficult. I realised that my rotation game source code had taken one of those turns.

I was modelling the board literally. As described in my previous post.  Each row was modelled as an array of squares.  The top row had one item in the array, the next row had three items in it’s array etc.  The problem with modelling the board in this way became apparent when I got to trying to write the code to rotate letters or move the user’s selection around.

The trouble with modelling the board this way as that what should be simple movement on the board ie moving up one square, is very tricky.  To calculate the new row and column index to move the selection up one square is a matter of recalculating both of the row and column indexes due to their being a different number of squares in each array that represents a row or column.

To get around this problem I’ve decided to rewrite the code so that the board is represented as a square of squares ie each row and column has the same number of squares in.  Then I can model the selectable squares that make up the actual usuable board by setting a property on those squares.  This way, although I’ve got a small amount of redundant objects, the algorithm for moving around the board is simplified massively.  To move the selection up one is simply a matter of taking one from the row index (the column index will stay the same), simples!

Although this was about an hour or so’s work to do the rewrite, I am sure that it will be well worth it in the long run, especially when I come to tackle the algorithm around rotation.  Dread to think about that with the way it was modelled before.

The message that comes out of this episode is that if you find yourself fighting against your code, don’t be afraid to stop and work out why.  Then if you can go back and change it.  In the long run you will be glad that you did.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s