Friday, February 25, 2011

Modeling Movement

OK, so I was going back and forth about how to implement the sticks.

In the pilot version of the program, I just had an array of 0 to 11 bools. Not so bad, until I tried to implement moving. Here's a valid move:

._ _
|_ _|
._|_


._
|_|_|
._|_

I model this as 12 numbered positions:

01
234
56

789

JK

(J = 10, K = 11 -- A and B are already spoken for, see below.)

Here, stick 1 moves to position 3.

I was starting to implement this procedurally, but it turned into an ugly set of if-thens pretty quick, so I thought, how about a class with the bool for the stick and then the corners that the sticks belong to:

A B C
D E F

G H I

So, 0 and 2 belong to A, 0, 1 and 3 belong to B, 1 and 4 belong to C. The values for the corners would be calculated when a stick was added or moved and then referred to.

The problem with this is that it maintains, as a property, something that changes and something that depends only on outside info.

So, I think I might have been right with my original approach -- which corner a stick belong to is a fact about the board, not the sticks. What I need is a member of Sticks that does the look-up in a clear way.

Unfortunately, I don't see how to make something that isn't kind of ugly. Here are my notes:

0 2 A
0 1 3 B

1 4 C

2 5 7 D

3 5 6 8 E

4 6 9 F

7 10 G

8 10 11 H

9 11 I

Param1, Param2

does Param1 belong to A?

false: continue

true: Does Param2 belong to A?

- false: continue

- true: return true


This procedure, as a loop, would be repeated for B to I.

Although the number of items is small, this is a computationally difficult problem. This is a multiple to multiple relationship: A has two sticks, 0 and 2, but 0 has two corners, A and B. I can't just use a hash table and do a unique look-up on anything.

And if I implement a solution along the procedural lines I outlined in my quasi-code, it won't be scalable. ( I thought of being able to add more squares in a later version. A good design would let me do this.)

So, the blog plan is:
Tomorrow, I'll start re-implementing the array-based board I used in the pilot project. Once that is done, with testing code in place, I'll just write the method I described for figuring out whether a move is a possible move.

No comments:

Post a Comment