Programming the Algorithm

Each fractal game board was drawn by recursing the border shape by a certain depth. It finds the midpoints of all the vertices and then calls the triangle routine on the three triangles created by the midpoints. In the following picture these three triangles are the bigger red, blue, and green triangles. Then each one of these triangles is divided into three smaller triangles, one red, one green, one blue. This process is repeated until the recursion depth is reached.

You can also see from that picture's coloring scheme that the recursion defines the algorithm to map a dot into a target triangle. If you look at the target in the picture, you can see that first it is in a red triangle, and then a green triangle within that, and then a blue triangle. Thus the sequence to map to it would be the reverse of that: blue, green, red.

When the background game-board is drawn, each triangle object, remembers where it is placed in the big triangle. So the target triangle in the above example contains the information: blue, green, red. That is, each time a recursive call is made the triangle object remembers which third of the triangle that call was made in, and stores it in an array. This makes checking to see if the red dot is in the triangle fairly easy. The choices that the player makes are stored in an array as well. Then the two array are compared. If they are equal, the red dot is in the target triangle.

The only other part missing is that the red dot must first be within the border of the biggest triangle. This is because the biggeset triangle maps to one of the smaller triangles within it. So the program checks this, and once the dot is in the big triangle it compares the arrays.

The method of programming the algorithm is the same in all three game boards. Except the shapes recursed are different.

To Index Next Page