Winning Triples

Since there are only 8 ways to win we could define them explicitly in our program. We'll call each row, column, and diagonal a triple. The program will need a list of all the winning triples. This is the sort of thing—an unchanging value that's needed throughout the program—that can reasonably be put in a global variable.

list of winning combinations

Why is this a good way to think of things? Well now, "can I win on the next move" means "is there a triple in which two of the squares have my letter and the third square is empty?" This solves the problem we thought of at the end of answering Question 1.

Asking and answering questions beforehand allowed us to clearly define what we're going to do before we get started. We gained valuable insights that affected our design, and will make things go smoothly for us!

Finding Triples

tic-tac-toe board

So we've decided that we'll represent the board as a list whose items are in the order of the numbers above. If we have the board state above with X's and O's, our list looks like this:

example board as triples

The list of winning triples above will help us answer all the questions our strategy program will have to ask. Remember, "can I win on the next move" now means "is there a triple in which two of the squares have my letter and the third square is empty?" So you're going to translate each board position into a list based on wins in which each number is replaced by the status of that square: X, O, or the number (empty):

example board as triples

The picture doesn't show all of the result, just the first five triples. Make sure that each triple in the list reported by find triples corresponds to a triple in wins, with the square numbers replaced by what's in each square in the sample position. For example, the first triple in wins is 1 2 3, and the first triple in the result is 1 O X.

Don't start implementing just yet-- head the next page first.