I made some edits to include the word "possible" more. I'm not sure I didn't make it more confusing. Need to look again another day. --MF, 6/13/18
On this page, you'll program your Tic Tac Toe project to detect and report wins and ties.
To program a way to detect a win, we need to determine what counts as a win. We'll store each possible winning triple as a list of three position numbers (a triple). For example, this winning triple could be represented as because the win is in the 3rd, 5th, and 7th positions:
Next we'll create a list to store all of the possible winning triples so we can check it against the current state of the game stored by board.
We need a way to find out if a winning triple has actually happened. We can use map
to check each position in one triple to see if either player has won with that triple.
You learned about map
in Unit 3 Lab 1 Page 4: Transforming Every List Item.
Map
applies the function in the gray ring to each item in the input list, and it reports a list of all the results.
Recall that the blank input slot in the function inside the gray ring is where one list item goes every time the function is performed.
map
work here?
This map
expression works the same way as the join
example above, but it's a little more complex to think about. As before, map
inserts each list item into the blank space in the function inside the gray ring, and it reports a list of the results.
But here, the function in the gray ring is item ( ) of (
board)
, so map
checks items 1, 2, and 3 of the list stored in the board variable, and it reports a list of what is in those three positions (X, O, or Empty).
If you can't see what's inside the board variable, be sure its box is checked in the Variables palette and expand its watcher to see its list items as in the picture below.
{1, 2, 3}
isn't the triple in which x won the game, replace the list in the map
expression with the winning triple, and click the expression again.status of triple
block with the winning triple. Fix any bugs.
You've just built a block that will check one triple to show if either player has chosen all three of those squares. Now, you'll build a block, status of all winning triples
to perform the same check for all of the triples that make a win. Then, you'll use that block to check if any of the winning triples actually contain three moves from the same player.
For example below, Player O has won this game, and the status of all winning triples
block contains the status of each triple. The computer can use this block to check for a winning triple that has either all X or all O.
map
together with some of the other blocks you have made to build the status of all winning triples
block. It should report the status of all of the winning triples as a list of lists, as shown above.map
.
map
think:
won?
.
Put it in words: What is it that you want to check?
Are there any blocks that might be helpful for that?
true
if and only if that player has won the game. The definition of a tie game is that there are no more empty squares and neither player has won.
This hint won't work when we change "Empty" to numbers. -bh
I don't want to do that until we need it--in U5. --MF, 6/8/18
One reason to do it from the beginning is that as it is now, the student has to remember where in the program it assumes "Empty," otherwise the project won't work. --bh
You can work on this project more in Unit 5 Optional Project: Tic-Tac-Toe.