Program 3

Home Windows C++ 241 C++ 242 Contact Info Contents

Program 3 - January 29

Up

This assignment will be your first opportunity to develop an object-based program. It is based on assignment 6.16, on page 414 of the text, but has some differences which are intended to make it both easier and a better vehicle for learning proper design an object that will be useful in solving a variety of problems.

Tic Tac Toe

There are a variety of programs one might right based on the game of tic-tac-toe. You could simulate various winning strategies and evaluate which is best. You could simply display moves made by two human players after validating that they are legal. You could display a board as text or in a graphics window.

All of these programs have in common that they would benefit from the use of an object which represents a tic-tac-toe game position. For this assignment, you will design and program such an object. In order to test it, you will write a program that accepts moves alternately from two players and displays the board board position. The program should also detect that the game has been won or drawn and announce it.

You will turn in two things for this assignment, and both count toward your grade:

  1. A design for the TicTacToe object.

  2. An implementation of the TicTacToe object.

  3. A program that uses the TicTacToe object.

The first item must be turned in at the start of class. If you turn in a design, I'll give some leeway on completing the other two parts. Some time will be allowed in class to work on this program after we discuss the designs.

Design

In order to design an object's internal structure, you first must determine what it looks like and does from the outside. Objects of class TicTacToe will represent the current state of a TicTacToe game. To provide an initial design, answer the following questions in writing:

  1. What questions must a program be able to ask a TicTacToe object?

  2. What operations must the object be able to perform? Give special consideration to I/O. Should the object be able to perform I/O at all? If so, can you structure the operations so that it can be used in both console and windows programs? 

  3. Decide what public functions (and possibly data members) you will implement to provide the answers and operations listed above.

Please provide your design answers in a separate document  - text format or word. Note that this contradicts instructions I gave in class saying you could place them in a comment - I prefer that you do this completely before writing any code at all.

Implementation

Develop a header (.h) file that completely describes your class. Develop an implementation file (.cpp) that contains definitions for all your functions. Note that the name of the class must be TicTacToe because we will be doing some exercises that depend on your use of this name.

You did an external design in the previous step. To implement the class, you have to decide what internal representation to give for the data. The description for exercise 6.16 tells you to use a 3x3 array with the numbers 1 and 2 representing the two players. You don't have to do it this way - and this may not be the best way. Give some thought to the internal representation that makes it easiest for you to provide the public information that the object is responsible for. The most complex internal task will be to figure out whether anyone has won the game yet, so you may want to think about how you would do that using various internal structures.

Test Program

The program that tests out your class should be placed in a separate file. In later exercises, you may be using your class with other main programs. The suggested outline for the test program is as follows:

Input the name of the first player.
Input the name of the second player.
For each move
    Ask the appropriate player to enter a move
    Have your object validate the move
        If it's Illegal, give an error message
    Enter the the players move (to the object)
    Ask the object if the game is over
        If someone won, display a message
        If it's a draw, display a message

Note that you will have to decide how to display the board. It should be updated each time a move is made and the display should make it easy for a player to indicate the next move.

Note that you are not to develop a computer program that plays tic-tac-toe for this exercise. Your class should not know how to select moves because that would violate the encapsulation of the class as we are designing it. We will develop another object in class which understands how to choose good moves, and uses the TicTacToe class to get information for that purpose.

Up

Home Windows C++ 241 C++ 242 Contact Info Contents

Copyright © 2000  Charlie Poole. All rights reserved.
Revised: July 15, 2002 - cpoole@ctc.edu