Page 4: Making the Computer Play Strategically

Unit 5, Lab 6, Page 4

MF: Project still needs to be wrapped up and updated online

On this page, you’ll implement the Tic-Tac-Toe strategy rules to make the computer play strategically.

You’ll set up a next move for computer block so you can broadcast to all the clones the position where the computer will move. As before, only the clone with the matching position number will make the move.

The code for next move for computer will implement the three strategy rules:

  1. If it isn’t open already, open your U5L6-TicTacToe project.
  2. The English description of Rules 1 and 2 is “If … do …” To turn that into code using the if block, you’ll need a predicate to put in the hexagonal input slot of if. Write can player () win now? using the winning square for player block you wrote on the previous page.
  3. Now, create the next move for computer block that implements the strategy rules using these two new blocks, and use it as the input to broadcast instead of best empty square.
  4. Play several games against the program. Fix any bugs. Make sure computer is using three rules.
  1. Modify the program so that the computer can play either X or O. When the game starts, ask the human player to choose X or O.
  2. Create a way for the player to decide whether they want to play against another human or against the computer.
  1. Play against the computer, and find a strategy that lets you win the game. (This isn’t trivial; just the computer rules you already have are pretty good.)
  2. Program more rules to make the computer a better player. This will involve looking more than one move ahead. There are various ways to do it, and what follows is just one suggestion.

    1. It’s not good enough just to look for a winning move for yourself two moves from now. If you can see such a move, so can your opponent, who’ll move to block you. So what you have to find is a fork: two triples in which you have one appearance and your opponent has none, and which have a free square in common. This is much easier to see with a picture:
      board with fork

      Player X opened the game with the standard opening move, in the center. Player O responded poorly, on the top edge, and is therefore about to lose the game. Player X played in the top left corner. Player O had to respond in the bottom right corner in order to block an immediate win for player X. It’s player X’s turn. There is no winning combination with two Xs, nor with two Os. But player X can find two winning combinations, the ones marked with the red lines, both of which have one X and two free squares, with one of the free squares in common. In the picture, the common square is 4, the left edge square, and so that’s where Player X should move.

    2. Find all the triples (that is, report a list of triples) in which the computer has one square and the other two are empty, and store them in a variable called singles.
    3. Make one big list of all the letters and numbers from the triples:
      script variable(atoms); set (atoms) to (combine with(append()()) items of (singles))
    4. “Atom” is a technical term for a value that isn’t a list. A list contains a bunch of items, just as a molecule contains a bunch of atoms.

    5. Now find a number that appears more than once in atoms.
    6. You solved a related problem in Unit 5 Lab 1 Page 7: Removing Duplicates.