Assignment

A3: Tic Tac Toe Game

Due Date Tuesday, July 19, 2005 @ 4:00pm (CSL time)
Last Mod Date


Links

Announcements

Objectives

Description

Background

In this assignment, you are going to design, code, and test a program that implements the Tic Tac Toe Game. Tic Tac Toe is a game that requires two players who alternately place X's and O's upon a 3x3 playing board as shown in Figure 1 below. Players alternate roles in placing their marks in any unoccupied box (cell) on the board. The first player to complete three of its marks in (1) a horizontal row, (2) a vertical column, or (3) in either of the two cross diagonals wins the game.

Requirement

Your assignment is to write a program that allows (1) a human to play against the computer, or (2) a human to play against another human. The program MUST allow users to choose between these two options. The following describes the requirements of each option.

Option (1) (human vs. computer):

Option (2) (human vs. human):

The board boxes will be numbered from 1 to 9 as shown in Figure 2 below. Each players MUST select its unoccupied box by entering the number that corresponds to that box.

The game MUST end when (1) all the boxes are marked, or (2) one of the players wins. When the game ends, the program MUST print out the outcome of the game. After every move the program MUST print the board with all the marks up to that point. Your program MUST also prevent players from choosing wrong boxes. That is, if a human player selects a box that is already marked, the program MUST re-prompt the human player to renter a valid box number. Similarly, your program MUST prevent human players from choosing wrong options and wrong marks. The program MUST continue prompting the human player to enter a valid entry so long as the entry is wrong.

Sample outcome of the program

The program outcome MUST look like the following:

Specification

Coding

The program MUST have two classes: TicTacToeMain.java and Board.java. The TicTacToeMain.java class will contain the main method of the program. It is in this class where all user input and interaction with the program is handled. The Board class is an instantiated class which maintains the state of the board, keeps track of the marks used by players, allows marks to be placed on the board, allows the computer to select its smart move, and checks for end of game. In the following, we provide detailed description of both classes.

Board class

Constructor

sy1 and sy2 are the marks chosen from (X or O) to be used by computer and player 1 (option 1), or player 1 and player 2 (option 2) respectively.

Methods

This method places the mark sy into the box number boxNum in the board and then displays the board. Refer to sample of the outcome above for output formatting. The method must return False without doing anything if the box numbered boxNum is already occupied. Otherwise it returns True.

This method selects a unoccupied box using the Selection Algorithm given below and then returns the number of the selected box.

Selection Algorithm

Given the board with its marked boxes, the algorithm selects a box for the computer to place its mark as follows.

  1. If any of the rows, columns, or diagonals contain two of the computer's mark and an unoccupied box, then return the number of the unoccupied box
  2. Else, if any of the rows, columns, or diagonals contain two of the human player's mark and an unoccupied box,  then return the number of the unoccupied box
  3. Else, pick a random unoccupied box and return its box number

This method checks whether the game ends. The game ends when the board is all marked, or one player wins. This method will be called by the main method of the TicTacToeMain class after each move to check for the end of game. The method MUST return an integer which indicates the status of the game when it ends. This returned value will be used by the main method to display information accordingly.

TicTacToeMain class

This class contains the main method. It prompts the user for entering the number of players (option 1 or 2), the symbols to be used by the players, creates a Board object. The Board object will be used through its methods to maintain the board, make the moves, and check of end of games. The main method also should contain code that allows players to alternately enter their selected boxes.

Generating Documentation

In this assignment you will be generating documentation for the Board class from the Javadoc comments that you wrote. The following are instructions for using Eclipse to generate the Javadoc web pages.

Make sure you write comments in proper Javadoc format for all of your methods before doing this step.

Select the "Project" menu item in the Menu bar. From the "Project" menu select "Generate Javadoc..". A dialog window should appear. If it doesn't, try the "File" menu to the "Export" menu item and select the javadoc icon.

Enter S:\java\bin\javadoc.exe in the field labeled "Javadoc command".

Make sure the "Public" radio button is selected. Make sure the files you are generating documentation for are selected. You may have to click on the + box next to your project name to expand the listing and see all the files so that you can make sure they are all selected. Note the location in which the documentation will be generated; it's the field labeled "Destination" in the dialog window. Press the "Finish" button and the documentation should be generated.

Viewing the Documentation

You can use any web browser to view the documentation you create. Most web browsers have an "Open File" menu item. Select this item and navigate to the location you noted in the "Destination" field of the "Generate Javadoc" dialog window. There will be many files at this location; select the file index.html. You will see a page much like the Java API pages, but the classes listed will be the ones you have written. You can click on the links and look at the documentation for your individual classes. If you select one of the files named after one of your classes, say Board.html, you will see only the documentation for that class.

Javadocs

Documentation for Java source code is almost always provided in a special javadoc format. This documentation is automatically generated from the source code comments, which MUST follow a special format.

We will be using classes from the Java API in every assignment. The documentation for every class in the Java API can be found at http://java.sun.com/j2se/1.5.0/docs/api.

Requirements

This section outlines the major requirements of the assignment. Your solution to the assignment MUST meet each of these requirements. Be sure to read the announcements frequently and ask questions if you need clarification.

  1. Include name, login, and section information at the top of all assignment files.
  2. Include javadoc comments for every portion of the public interface of every class.
  3. Use symbolic names wherever they are appropriate.
  4. Make sure that only one Scanner object is created during execution of your TicTacToe program.
  5. Use visibility modifiers appropriately; instance fields should be private and all methods of your public interface should be public.
  6. Use the final modifier appropriately and capitalize all final variables.
  7. Submit your answers to the questions in a text-only file (no Word documents) named questions.txt.
  8. Use the handin program to hand in all work electronically.

Questions

This section lists several questions that should deepen your understanding of the assignment. Create a text-only file named questions.txt and answer these questions.

Be sure to include the descriptive information listed below as well as the answers to each question.

Assignment Number: 
Assignment Name:
Date Completed:

Partner 1 Name:
Partner 1 Login:
Partner 1 TA:

Partner 2 Name:
Partner 2 Login:
Partner 2 TA:
  1. Are any of the methods that you implemented in your assignment private? Why would one need to have private methods? What is the difference between private methods and public methods?
  2. What is the meaning of the keyword final? Give an example, if any, from your assignment where you used the keyword final?
  3. What does it mean for an field of a class to be static? When would one need to have static data members? Give an example from your assignment where you needed to use static data members?
  4. What does it mean to have a static method? When would one need to have static method? Give an example from your assignment where you needed to use static method?
  5. Why are all the methods of the Math class static?
  6. As required, you MUST have only one Scanner object that can be used by all objects/classes of your program. How did you meet such requirement?
  7. What type of loop did you use to impement the Selection Algorithm? What type of loop did you use to ensure that the user does not enter invalid input? Provide a second alternative (different from the one you used in the assignment) which performs the same task? In this question, you are required to provide a pseudo code showing that you could implement the same thing with different types of loops. Hint: while loop instead of do/while loop.
  8. Can one use the switch statement instead of the while loop?Justify?

Hints

Handin

Use the Handin program to hand in your work. Students working in pairs will only submit one copy of all program files, except the README file described below.

Do not hand in .doc, .metadata, *.class, or *.jar files.
The Handin program is described in the Eclipse Tutorial.

The required files for this assignment are:

Two program files:

One javadoc file (should be in the same directory as index.html):

One text file of answers to the questions.

Pair Programming Students MUST also submit a README file.

All students working in pairs MUST read the Pair Programming Document and submit a README file. Each student working in a pair, MUST handin this file to their own handin directory. Copy, complete and handin the file that is linked here.