PG: Long and hard.
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 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:
next move for computer
block using two new blocks: can win now?
and winning square
, which aren't written yet. To start building next move for computer
, you'll make the other two blocks, but they won't really do anything. They'll just be for setting up the code for next move for computer
, and you'll make them do what they're meant to later.can win now?
report false
no matter what. Don't put code inside winning square
yet.can win now?
report false
?
That way, the computer will skip rules 1 and 2 and just default to the final rule, and so you can continue using your program as you build and test these two new blocks.
broadcast
instead of best empty square
. You can check status of all winning triples
for a triple that will allow the computer player to win on the next turn, but you will not be able to tell the position of the square where that player will move. To make that possible, you will need to initialize the spaces in the board variable with the numbers 1 to 9 instead of nine copies of the word "Empty." Then can select the position number out of the winning triple.
Now you'll begin working on the and
blocks. The
can win now?
block can just check if there is a winning square by checking the results of winning square
, so you'll code winning square
first.
You'll begin by building a block to identify the contents of the three squares on the board where the player could win on their next turn. Then,
winning square
will select the correct position out of that triple.
Click here for a hint about winning triple
.
You learned about using keep
with predicates in Unit 2 Lab 3 Page 5: Keep
ing Items from a List.
Click here for another hint about winning triple
.
winning triple
for both inputs (X and O). Play a little more, and then test them both again. Fix any bugs.winning triple
report if there is no winning triple?winning square
for both players, and fix any bugs.winning square
block.append
block in your palette, but it's in one of the Snap! libraries. Click on the file menu icon, and choose "Libraries...", then choose "List utilities" from the menu that appears.
atoms
.duplicates in
block in
Unit 5 Lab 1 Page 7: List Processing Algorithms.