Tic-Tac-Toe: Look for Wins

In earlier labs, you worked out how to display moves on a tic-tac-toe board and keep track of them in a variable board whose value is a list of nine values, one per square of the board, each of which can be X or O or empty if that square is unoccupied:
tic-tac-toe board and BOARD list
Now we'd like the computer to be a player in the game, and to do that, it has to be able to detect patterns in the board position that mean, for example, "I can win on the next move."

  1. How many ways are there to win at tic-tac-toe? That is, which sets of three squares count as a win? Work with your partner to make a list on paper.
  2. Load this project. We've provided a wins block that reports a list of all the winning triples:
    wins
  3. Try these: map (( )-(1)) over (list (96) (-100) (4.7)) reporting
    map (join ( ) (s)) over (list (cat) (dog) (bird)) reporting
    map (round ( )) over (list (3.14159) (87) (5.4)) reporting
    Note that each example has an empty input slot in a function (the green blocks, in these examples).
Read this block definition:
is triple (triple) a win for player (player) ? is triple {4,5,6} a win for player X?  -->  false
is triple {3,5,7} a win for player X?  -->  true
Talk with Your Partner
  1. If triple is {4,5,6} and board is as shown above, what will the value of occupied be after the set block is run?
  2. The report block says to report
    (something = 2) and (something = 0)
    Say in words what would make sense as the somethings: two of what, and zero of what, are required for a winning triple?
  3. Looking at the actual code, figure out what the keep block does. Here's a simpler example to consider:
    keep items such that (letter 1 of ( )) = e from {elephant, aardvark, ...}
  4. Write a block find winning triples for player ( ) that reports a list of all winning triples for that player, using the list reported by wins.
  1. If the computer can win on this move, it should. Otherwise, if the opponent can win on this move, the computer should block that. But if neither of those is true, at least the computer should find a place to move in a triple where it already appears once, and the opponent doesn't appear, such as {4,5,6} for X on this board. Write is triple ( ) an okay move for player ( ) ?
  1. Actually, on this board, it's not X's turn; it's O's turn. There is no winning move for O according to the block you wrote in problem 7, but there is one great square for O to choose. Which is it?
  2. Write a procedure to find such a move.