Lecture 10: In-class Exercise:   A Controller for Tic Tac Toe
1 A Controller for Tic Tac Toe
2 Testing
3 Notes to Keep in Mind
4 To Turn In
6.3

Lecture 10: In-class Exercise: A Controller for Tic Tac Toe

Due: Ideally by the end of class, but not later than one day after your section talks about controllers and mocks.

1 A Controller for Tic Tac Toe

The purpose of this exercise is to give you practice with implementing the Controller component of the Model-View-Controller design pattern, by means of a textual, console-based controller.

You may work freely with other students on this exercise!

In the starter code, you are given an interface representing a controller for Tic Tac Toe, with a single method, playGame(). Your task is to implement the TicTacToeController interface. Put your new controller code in the same package alongside your Tic Tac Toe model as it will depend on the model. You are also given a class Main with a main method that will allow you to test your game interactively.

You will need to create one additional class: a public class named TicTacToeConsoleController that implements TicTacToeController, with a single public constructor that takes two arguments, a Readable and an Appendable (in that order). You will fill in the fields and the method definitions as appropriate. You may also define other classes at your option as needed.

The controller will output game state and prompts to the Appendable, and read inputs from the Readable corresponding to user moves. The append() method on Appendable throws a checked exception, IOException. Your playGame() method should not throw this exception. If it occurs, your playGame() should catch it and throw an IllegalStateException.

A single move consists of two numbers, specifying the row and column of the intended move position. Board positions for these moves are numbered from 1. For example, to mark X in the upper left cell, the user would enter "1 1" at the first prompt. To mark O in the upper right cell on the second move, the user would enter "1 3". To quit a game in progress, the user can enter q or Q at any time.

The game state is the output of the model’s toString() method, followed by a carriage return (\n). The move prompt is

"Enter a move for " + model.getTurn().toString() + ":\n"

(where model is an instance of your Tic Tac Toe Model).

If a non-integer value is entered, it should be rejected with an error message. If an invalid move is entered, namely, two valid integers, but the proposed move was deemed invalid by the model, the controller should give an error message. The message text is up to you, but should end with a carriage return.

At the end of the game, the controller should output, in order on separate lines:

If the user quits, the controller should output

"Game quit! Ending game state:\n" + model.toString() + "\n"

and end the playGame() method.

2 Testing

We have supplied you with some basic JUnit tests as part of the starter code. Use these to verify that your implementation is correct. Write additional tests of your own: Assignment 3 is a testing-heavy assignment. Some of the additional cases you should implement are listed as comments in the test class you are given. Implement those tests at the least.

3 Notes to Keep in Mind

4 To Turn In

Submit your zip containing only your src and test directories to In-class Exercise 3 on the handin server. There is no self-evalulation for this exercise. Your grade will be determined by code style (70%) and JUnit correctness (30%). We will not be doing manual grading, but if you would like feedback on your design, and specifically your test coverage, contact a member of the course staff and we will review it.