Programming In C++
Spring 2000


Homework #2 - Object-Oriented Tic-Tac-Toe Game

Due Date: Feb 14th (by midnigth on the 14th)

Email submissions to cpp-submit@cs.rpi.edu

This assignment involves modifying (or rewriting) your tic-tac-toe playing program to be object-oriented. Instead of functions, you must develop some classes. You must develop class definitions according to the interface specifications listed below.

Class Interface Specification

Here is a description of the objects (classes) you must define and the methods your objects must support. When testing your code we will use a main program that declares variables of each class type and calls the methods. Some sample test code that plays 2 copies of your player objects is available here: Sample Test Code

class board

A board object contains information about the state of a game of tic-tac-toe. You can represent this information within a board object any way you like (you can use a 2-d array as in HW1, or whatever else you might think of), but you must provide the following methods:
Method prototype Description
board(void); constructor. This must initialize the board to an empty game.
void print(void); This method prints out the current game to stdout. Feel free to use the code provided with HW1 to accomplish this.
int getval(int r, int c); A function that returns the current value of the element of the board at row r and column c. If the row or column specified are not legal (for example if the row is 24) the return value of this method should be -1. If the row and column are valid, the return value must be either 0, 1 or 2.
int setval(int r, int c, int newval); This function changes the value of the board element at row r and column c to the value newval. The return value should be newval if the move is a legal move, or -1 if the move is not legal.

An illegal move must be detected by this method. If newval has any value other than 1 or 2, the move is not legal. If the row or column are invalid the move is not legal. If the current value of the element at the row and column is not 0 the move is not legal.


class player

The player will contain methods that can be used to make a move in a game of tic-tac-toe. Within your class definition you can do anything you want (as far as defining data members and methods), but you must support the following methods:
Method prototype Description
player(int playernum); constructor that takes a single argument that specifies whether the object is player 1 or player 2 (playernum will be a 1 or a 2).
bool makemove(board &b); This method changes the board object so that one of the zeros in the board is changed to the player's number (the player makes a move). If a move is not possible the method returns false, otherwise the board should be updated and the method returns true.
bool has_won( board b); A boolean function that returns true if the player has won, otherwise returns false.
bool has_lost( board b); A boolean function that returns true if the player has lost the game, otherwise returns false.



Test Code

Sample Test Code is available that plays your player against itself, and that plays against a human. You should make sure that your objects work with this test code!

Submission

You should create a single file that contains the definition of both the board and player classes and send the file as an attachment via email to:

cpp-submit@cs.rpi.edu

You must include the homework number on the subject line (anything like "subject: HW2" will work great).

Please use the above email address to submit - this email address is routed to a program that collects your submission and will send you back a receipt right away. If you submit to any other email address we will ask you to resubmit!

You may submit up to 10 times, we will grade the last submission received.

Send email to cpp@cs.rpi.edu if you have questions or trouble emailing your submission.