On this page:
Intro
Recall
The Grid
Updating the Grid
Winning at Tic Tac Toe
6.12

Lab 22: Focus in a Grid

Intro

You’ll work in this lab with your lab partners.

The two of you will work as a team to solve problems. At any time, one of you will be the Head and the other will be the Hands. The Head does the thinking and the Hands does the typing. Hands type only what the Head tells them to, but you’re free to discuss any issues that pop up. You should switch off during the lab to make sure each of you get practice problem solving, dealing with syntax, and getting finger exercises on the keyboard.

You should start this lab with this project skeleton.

Recall

In Monday’s lab (Lab 21: Focus with Zippers) we created a FocusZipper, a data structure that allowed us to focus on particular elements inside of a list. Today, we’ll consider what functionality a FocusZipper of FocusZippers enables.

In the file Zipper.java, we’ve given you a fully implemented FocusZipper. We’ll use this implementation to build a Grid, in which we can freely move up, down, left, or right. This grid will let us easily implement the game TicTacToe! You can run the game at any time to test the functionality you’ve implemented so far.

The Grid

A FocusZipper<X> allows us to focus on a particular element of type X inside of a list. When X is a FocusZipper as well, we have two different directions in which we can move the focus. We’ll have the inner zippers represent the rows of the grid; the outer zipper will move between those rows.

To move up and down between rows, we need to move left and right in the outer zipper.

Ex 1: Implement the Grid.up() and Grid.down() methods.

To move left and right inside a row, we need to move left and right in the inner zippers. Note: this is tricker than it seems, make sure that if you move right, then down, the position is where you expect in the lower zipper.

Ex 2: Implement the Grid.left() and Grid.right() methods.

What happens if you use the methods leftCycle and rightCycle to implement the grid functionality? Which do you prefer during a game of TicTacToe?

Updating the Grid

A key feature that zippers provide is a way to functionally update a data structure. By creating a new zipper with a new element in focus, we’ve effectively created a new list with a single element changed.

In order to implement TicTacToe, we need to be able to change the focused square to an X or O.

Ex 3: Design the method FocusZipper.updateFocus(f). It should take an arbitrary function and given the focused element x, returns a new zipper where the focused element is f(x).

Winning at Tic Tac Toe

Now we’ve got all the pieces we need to play TicTacToe, except it’s impossible to win! The method TicTacToe.gameOver() should test to see if either X or O has won, but it’s incomplete.

Ex 4: Implement TicTacToe.gameOver() inside the file Lab22.java. Hint: use SquareVisitors to do case analysis on the squares of the grid, and any form of iteration you want to test all the necessary cases. Feel free to add any operations you need to the Listof class, including nth, which may be useful.