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.
map (join( )(s)) over (list {cat, dog, bird}) reporting {cats, dogs, birds}

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.

  1. Click green flag button to start a new game. Play one game, deliberately letting x win.
  2. Then build this expression, and see what it reports:
    map (item ( ) of (board)) over (list{1, 2, 3})

    How does map work here?

    This map expression works the same way as the join example above, but people sometimes find it a little more complex to think about, because it’s natural to expect map (something) over (board), 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), so map 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.
    picture of: watcher of board variable showing {O, O, X, 4, X, 6, X, 8, 9}; and Tic Tac Toe game with X, X, O in top row; empty, X, empty in middle row; and X, empty, empty in bottom row map (item ( ) of (board)) over (list{1, 2, 3}) reporting {O, O, X}

  3. Talk with Your Partner What does the result of that expression tell you about the state of the game?
  4. If {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.
  5. Use this idea to make a status of triple ( ) block that takes one possible winning triple (a triple like list{1, 2, 3}) 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 list{1, 2, 3}, or False otherwise.

    You learned how to specify the list input type (list input type: rectangle with two smaller orange rectangles inside) on Unit 2 Lab 2 Page 2: Planning a Quiz App.

  6. Save Your Work 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.
Tic Tac Toe game where Player O has won down the middle column. In row one, there is X, O, and X. In row two, there is X, O, and an empty square. In row three, there is an empty square, O, and an empty square. The status of all winning triples block is reporting a list of lists (a table). There are three columns and eight rows, and each cell contains either a X, an O, or a number. There is text to the right of each row listing the corresponding triple and where it appears on the board (such as 1, 5, 9 being the top-left to bottom-right diagonal). The triple in which Player O has won this game (2, 5, 8) shows O, O, O in the table. That is the only row with either all O or all X.

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.

  1. Use 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 possible winning triples as a list of lists, as shown above.
  2. Click for a hint on how to use map.

    When you use map think:

    1. What is the function you are performing? That goes in the gray ring.
    2. What data are you performing that function on? That is the list you are mapping over.

    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?

    Click for another hint.

    ( ) contains ( )

  3. Now make a block won? ( ) that takes the letter X or O as input, and reports true if and only if that player has won the game.
  4. Save Your Work

  5. Modify your program so that when a player wins the game, the program notifies the players.
  6. 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.