Page 2: Detecting A Win
Unit 3, Lab 3, Page 2
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
I need to review the rest of this lab. Looks different than before. –MF, 5/31/20
PG: I haven’t thought deeply (haven’t even read carefully!) but would want to check carefully to make sure this is /developmentally/ helpful. Maybe it already is.
On this page, you’ll program your Tic-Tac-Toe project first to report the contents of one specific set of three squares that would make a win (such as the top row), and then to use that to find the contents of all possible winning triples. You’ll use that information to find out if one of the players has won the game.
Checking One Way to Win
We need a way to find out if a possible 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 2 Page 5: 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.
- Click to start a new game. Play one game, deliberately letting x win.
-
Then build this expression, and see what it reports:
This
map
expression works the same way as thejoin
example above, but people sometimes find it a little more complex to think about, because it’s natural to expect , but that’s not what the expression says. Instead, it maps over a constant list, not a variable. 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)
, somap
checks items 1, 2, and 3 of the board list, and it reports a list of what is in those three positions (each of which is X, O, or a number if the square is 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.
- What does the result of that expression tell you about the state of the game?
-
If
{1, 2, 3}
isn’t the triple in which x won the game, replace the list in themap
expression with the winning triple, and click the expression again. -
Use this idea to make a block that takes one possible winning triple (a triple like ) as input and reports a list of what is in those three positions (X, O, or a number). Then use it to write an expression in the scripting area whose value will be
True
if X won the game in the triple , orFalse
otherwise.You learned how to specify the list input type () on Unit 2 Lab 2 Page 2: Planning a Quiz App.
-
Play another game where player O wins, and test your
status of triple
block with the winning triple. Fix any bugs.
Checking All the Ways to Win
Now you’re going to use status of triple
systematically to check all the triples.
For example, Player O has won the game below, and the status of all winning triples
block finds the status of each possible triple. The computer can use this block to check for a winning triple that has either all X or all O.
The order of your triples and/or the numbering of the positions in your project might be different. This is OK as long as your project works.
-
Use
map
together with some of the other blocks you have made to build thestatus of all winning triples
block. It should report the status of all of the possible winning triples as a list of lists, as shown above. - What is the function you are performing? That goes in the gray ring.
- What data are you performing that function on? That is the list you are mapping over.
-
Now make a block that takes the letter X or O as input, and reports
true
if and only if that player has won the game. - Modify your program so that when a player wins the game, the program notifies the players.
- Play the game a few times to check that it’s working, and fix any bugs. Be sure to let each player win at least once.
-
Click for a hint on how to use map
.
When you use map
think:
Here, you want to know the status of each possible winning triple.
Click for a hint on building won?
.
Put it in words: What is it that you want to check?
Are there any blocks that might be helpful for that?