The actual intelligence behind artificial intelligence
The function needs to check all eight opportunities to score: 3 columns, 3 rows, and 2 diagonals. The big "aha" moment came when I realized that I couldn't just eject out of the function with a return statement the moment I discovered a chance to block, because (according to our handy prioritized strategy list), winning is of higher priority than blocking. By immediately returning the first opportunity to block I came across, I might be leaving an unsearched opportunity to win on the table.
It became clear that I would need to store a list of opportunities to block, and a list of opportunities to win. Once I search through all eight scoring scenarios, the function should return a random opportunity to win from the list (if any exist). If not, the function should return a random opportunity to block from the second list (if any existed). If there are no opportunities to block or win, the function should return null.
For example, in the...