Page 3: Checking for Ties

Unit 3, Lab 3, Page 3

On this page, you’ll check for a tie game, that is, whether there are no more empty squares but neither player has won.

  1. Develop a way to determine if the game has tied, and if so, make the program notify the players.

    Snap! has a higher-order function find first item () in 'list input slot' that works similarly to keep, but it reports only the first item that’s found, so it can be faster. Find first is equivalent to item (1) of (keep).

    Find first is a higher-order function like keep, map, and combine because it takes a function (a predicate) as input.

    Click for a hint about using find first to check for ties.

    find first item (is () a number?) in ()

  2. Play a couple of games to test the tie-checking feature, and fix any bugs.
  3. You can work on this project more in Unit 5 Lab 6: Tic-Tac-Toe with a Computer Player.

  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.