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…
- Play a couple of games of Tic-Tac-Toe with your partner on paper.
- 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.”
- What’s the second most important rule?
-
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.
- Make a block .
- Look inside the 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.
-
A possible winning triple for O on the next move has ____ Os and ____ Xs.
Now write . It should report the first triple it finds that contains a place where the player could win on the next move.
-
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. -
What does
winning triple
report if there is no winning triple? - Now write the block. Make sure it works even if there is no winning square.
-
Play part of a game, test
winning square
for both players, and fix any bugs.
You’re going to start implementing “If I can win…” and “If the other player can win…” rules, starting by detecting those situations.
On the next page you’ll use winning square
to finish giving the computer a better strategy.