Page 3: Developing a Gameplay Strategy

Unit 5, Lab 6, Page 3

On this page, you’ll define the strategy that the computer will use by investigating the strategies that humans use.

You’ve already built into your project the most basic Tic-Tac-Toe strategy rule: the computer will move to the best empty square on its turn. But there are other important elements to a good strategy…

  1. Play a couple of games of Tic-Tac-Toe with your partner on paper.
  2. Discuss your strategy with your partner, and write out a list of if/then rules that you both use to play (for example, “if such-and-such is the case, then play here”).

The strategy rules for Tic-Tac-Toe have different degrees of priority. For example, the most important rule is “if I can win on this turn, then play in the winning square.”

  1. What’s the second most important rule?
  2. Talk with Your Partner Think through how the computer can determine if a player can win on their next turn. The won? block works by looking for a triple in which all three slots are X or all three slots are O. Describe what the slots of a triple will contain if player O can win on their next move by filling that triple’s last square.

These three rules (if you can win on this move, do so; if the other player can win on their next move, block them; and otherwise just pick the best empty square) are part of most human Tic-Tac-Toe strategies. But the best players develop additional rules that come after the first two but before the last. You can explore some of these rules in the Take It Further problems at the end of the lab.

    You’re going to start implementing “If I can win…” and “If the other player can win…” rules, starting by detecting those situations.

  1. Make a block number of (X) in (list{X,5,X}) reporting 2.
  2. Look inside the won? (X) block that you wrote in Unit 3. Remind yourself how it finds a triple that has three in a row of the desired X or O.
  3. Click if you need a hint.

    A possible winning triple for O on the next move has ____ Os and ____ Xs.

    Now write winning triple for player (). It should report the first triple it finds that contains a place where the player could win on the next move.
    winning triple for player reporting {3, O, O}

  4. Play part of a game, and then test winning triple for both inputs (X and O). Play a little more, and then test them both again. Fix any bugs.
  5. What does winning triple report if there is no winning triple?
  6. Now write the winning square for player () block. Make sure it works even if there is no winning square.
  7. Play part of a game, test winning square for both players, and fix any bugs.
  8. Save your work

On the next page you’ll use winning square to finish giving the computer a better strategy.