Winning Triples

To program a way to detect a win, we need to determine what counts as a win. Since there are only 8 ways to win we could define them explicitly in our program. We'll store each possible winning triple as a list of three position numbers (a triple).

For example, this winning triple could be represented as because the win is in the 3rd, 5th, and 7th positions:

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!

Checking One Way to Win

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?"

Fill-in the block that takes one possible winning triple (a triple like list{1, 2, 3}) as input and reports a list of what is in those three positions (X, O, or Empty). For example:

Hint: Use a HOF with for this problem

Checking All the Ways to Win

You've just built a block that will check one triple to show if either player has chosen all three of those squares. Now, you'll build a block, to perform the same check for all of the triples that make a win. Then, you'll use that block to check if any of the winning triples actually contain a space that could be a winning move for the computer player. For example, when we input the board shown above we get the following result:

Hint: Use a HOF with and for this problem