ST EK List:
No EK's found

Detecting Wins and Ties

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.

Storing All the Ways to Win

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 list{3, 5, 7} because the win is in the 3rd, 5th, and 7th positions:
diagonal win bottom left to top right

  1. Talk with Your Partner
    1. How many winning triples are possible?
    2. Write down each of the possible winning triples.

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.

  1. Open your project U3L2-TicTacToe if it isn't open already.
  2. Save Your Work
  3. Create a reporter that outputs a list of all possible winning triples. It will look something like this:
    winning triples partial block definition showing a list with at least two lists inside it: {1,2,3} and {2,5,8}

Checking One Way to Win

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.
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 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.
    picture of: watcher of board variable showing {X, O, Empty, Empty, X, Empty, Empty, Empty, O}; and Tic Tac Toe game with X in upper left and center and O in upper middle and lower right map (item ( ) of (board)) over (list{1, 2, 3}) reporting {X, Empty, Empty}

  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 Empty).
    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 1: Processing Each Item in a List.
  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

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.
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 Empty. 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 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 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.

Checking for Ties

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

  1. Develop a way to determine if the game has tied, and if so, make the program notify the players.
  2. Play a couple games to test the tie-checking feature, and fix any bugs.
  3. You can work on this project more in Unit 5 Optional Project: Tic-Tac-Toe.

  4. Why does the order of the tests (for wins vs. for ties) matter? Talk with Your Partner
  5. Save. You will use this later.
  1. There are many small improvements you could make. For one thing, when a player wins, you might want to prevent any further moves. What else might you want to change or improve?
Tough Stuff
  1. Your program can be smarter about detecting ties. If there is only one empty square on the board, and filling that square with the player whose turn is next wouldn't cause a win, then the game is already tied. The challenge here is that you'll have to test for a win on a board that isn't the current board.
  2. Instead of just having the sprite say something like "X wins," it would be more satisfying to the players if you draw a thick line through the three squares that generate a win. This is a little tricky because by the time you know there's a win, you no longer know which triple triggered it. Make sure your code stays readable to another programmer.