Excellent explanation! This inspired me to write my own version, and it seems to work (most of the time). However, I am able to win sometimes.
The below images detail how I won (I am X):
Here is my log:
What I believe the problem is that it does not correctly choose which of the maximum scores to use, such as on the last AI turn where it chooses 3 rather than 8.