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.
-
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 that works similarly to
keep
, but it reports only the first item that’s found, so it can be faster.Find first
is equivalent toitem (1) of (keep)
.Find first
is a higher-order function likekeep
,map
, andcombine
because it takes a function (a predicate) as input.Click for a hint about using
find first
to check for ties. - Play a couple of games to test the tie-checking feature, and fix any bugs.
- Why does the order of the tests (for wins vs. for ties) matter?
-
You can work on this project more in Unit 5 Lab 6: Tic-Tac-Toe with a Computer Player.
- 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?
- 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.
- 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.