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 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:
- Rule 1: If I (the computer, player O) can win, do so.
- Rule 2: If my opponent (the human, player X) can win, block them.
- Final rule: Otherwise, just pick the best empty square.
- If it isn’t open already, open your U5L6-TicTacToe project.
-
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 ofif
. Write using the block you wrote on the previous page. -
Now, create the block that implements the strategy rules using these two new blocks, and use it as the input to
broadcast
instead ofbest empty square
. - Play several games against the program. Fix any bugs. Make sure computer is using three rules.
Click for a hint about implementing the strategy rules.
Use your block too.
- 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.
- Create a way for the player to decide whether they want to play against another human or against the computer.
- 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.)
-
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.
-
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:
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.
- 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.
-
Make one big list of all the letters and numbers from the triples:
-
Now find a number that appears more than once in
atoms
.
“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.
You solved a related problem in Unit 5 Lab 1 Page 7: Removing Duplicates.
-