Giving a Score to a Nine Dots Puzzle
by Ted Kaehler


In the previous chapter of the Nine Dots Puzzle, we wrote code to find the Intersection of Two Lines.  Now we need to give a score to a proposed solution to the puzzle.  We represent a solution as a mixed Array of X-Y pairs that represent dot positions and line intersections.
[[0,2], [1,2], [2,2], a LineIntersection,
[2,1], [2,0], a LineIntersection,
[1,0], [0,0], a LineIntersection,
[0,1], [1,1]]
Let's make an object to hold one attempted solution to the Nine Dots Puzzle.  It has slots for the array of dots and intersections, a numeric score, a position counter in the array. 
Object subclass: #NineDotsTry
instanceVariableNames: 'dotsInOrder score whichPos'
classVariableNames: ''
poolDictionaries: ''
category: 'Active-Essay'
The first test of a potential solution is to see if each segment has at least two points.  If it has three points, find out if they are co-linear. 

Let's make the same function called "colinear" work on each of the segments.  Each time we call colinear, the instance variable whichPos will be the index of the thing before the first dot in the segment.
colinear
"Find the next segment. Check if at least 2 points.
Check if 3 are in line. Leave whichPos at next intersection."

| start end diff |
start _ whichPos +1.
[(whichPos _ whichPos +1) > dotsInOrder size ifTrue: [false]
ifFalse: [(dotsInOrder at: whichPos) isPoint "not a separator"]
] whileTrue.
end _ whichPos-1.
"whichPos points at the next intersection"
end - start < 1 ifTrue: [^ false]. "only one point in segment"
end - start = 1 ifTrue: [score _ score + 10. ^ true].
 "two points are always colinear"
diff _ (dotsInOrder at: start+1) - (dotsInOrder at: start).
"in the 9 dots puzzle, if a segment has 3 points, the gaps
between them are the same size. This is not true for an
arbitrary bunch of dots."
start+1 to: end-1 do: [:ii |
((dotsInOrder at: ii+1) - (dotsInOrder at: ii)) = diff
ifFalse: [^ false]].
score _ score + 10.
^ true
Remember the index of the first dot in "start".  Step through array until a non-dot is encountered.  The index of the last dot in the segment is "end".  Check the number of dots.  One dot gets a score of 0, while two dots get a score of 10.

If there are three dots, compute the vector between them.  Add the vector to the second dot and see if that is the location of the third dot.  (We assume that in this puzzle they will be evenly spaced.)  Give 10 points if the dots are co-linear.









add index and age when needed for evolution.