# Assignment 1 Tic-Tac-Toe

Document Sample

```					Assignment 1: Tic-Tac-Toe
CS 162
Due Date: Wednesday, January 20 by 11:59 PM via TEACH
Total Points: 100 points

http://en.wikipedia.org/wiki/File:Tic-tac-toe-game-1.svg

Recently, you've notice the cost of education has been increasing dramatically. First, there's
the mandatory laptop you had to buy for your major. Then, you didn't want to loose your
files in the event your laptop got stolen, so you bought that iPod to “back up” all the “critical
files.” And, of course, when your house goes out to see Avatar again, it's practically a
requirement to go along. And who could consider themselves a proper student without at
least one, or two, or a “reasonable number” of trips to Dutch Bros or Clodfelters. And, oh
yes, there's the cost of books too! It turns out this student lifestyle is getting quite expensive,
and you need some cash.

Fortunately, you found out that the Acme Software Company is in a fix. One of their junior
programmers won the lottery and unexpectedly moved to the Cayman Islands. They've hired
you to finished up some code for their soon-to-be-released tic-tac-toe phone app. It turns out
they have everything but the game engine itself. After wondering for a second if that
programmer was actually writing code or just filling out lottery tickets, you get to work....

Objective
You will implement a simple game engine for tic-tac-toe. You will not be implementing a user
interface or a way of actually playing the game, but instead, you will be validating your
implementation using unit tests.

Background
Tic-tac-toe is a simple game played on a 3x3 game board. One player is “X” and the other is
“O”. “X” goes first, choosing an empty square and writing an “X” mark in the chosen square.
Then “O” takes a turn, then “X”, etc. The first player to get 3 marks in a row wins: either
horizontally, vertically, or diagonally. If the board is full and neither player has won, the
game is a draw.
A common practice when creating a computer game is to create a “game engine” that
implements the underlying logic of the game. By creating the engine in its own class, the logic
for controlling the game is loosely coupled to the rest of the program (such as the user
interface.) A complex game engine would require many classes, but for our simple game of tic-
tac-toe, we will only need one class.
For the game to function properly, the game engine must be correct. A good practice is to
write unit tests to validate the functionality of the game engine. That way the engine may be
tested in an automated manner rather than requiring a user to play the game over and over
and over again.
You be be designing two classes: TicTacToeGame and TicTacToeGameTest. The first
class will contain the game engine and the second will contain the unit tests for validating the
game engine. You must name your classes and methods exactly as the assignment requires.
Before getting started, download TicTacToeGame.java from the class website. It contains
a skeleton class.

Game Engine Requirements
The game board consists of a 3x3 array of characters. The characters 'X' and 'O' (the letter
“O”, not the number “0”) are used to denote which player space a player occupies. The
character '.' means that spot is unoccupied.

Constructor
There is no explicit constructor given in the skeleton class. Instead, we'll use the default
constructor. Each field is initialized when declared, so we do not need to define our own
constructor. If you choose to, you may add your own no-argument constructor.

Printing the Board (10 pts)
You must implement a method for printing out the game board. This method will be useful
for printing out versions of the board while you are debugging, so you should implement it
first.
public void printBoard()
The game board must be printed to the console output. (Use “print” and “println”.) Feel free
to be creative with the output.

Examining a Spot on the Board (5 pts)
You must implement a method to retrieve a mark from a particular spot on the board.
public char getMark(int row, int col)
For (row, col): (0,0) is the top left corner, (0,2) is the top right, (2,0) is the bottom left, and
(2,2) is the bottom right. Remember, the mark will either be an 'X' or 'O' if the space is
occupied or '.' if the space is unoccupied.
Knowing the Who's Turn It Is (5 pts)
You must implement a method which tells you who's turn it is.
public char getTurn()
This will return 'X' if it is the first player's turn or 'O' if it is the second player's turn. If the
game is over, it should return '.' for “no player.”

Placing a Mark (15 pts)
You must implement a method for placing new marks on the board.
public void placeMark(int row, int col)
This method will place a mark for the current player at the specified place on the board. For
example, if the first player has the current turn, an 'X' will be placed on the board.
After a mark is placed on the board, your game engine should switch turns. However, if the
given row and column are already occupied, the placeMark method should make no
changes. This means nothing will be placed on the board and the turn will remain the same.

Finishing the Game (15 pts)
You must implement several methods related to the end of the game.
public boolean isOver()
public boolean isDraw()
public char getWinner()
The isOver method should return true if the game is complete and false if the game is
still in progress. The game is considered complete if a player has won or no more moves
remain.
The isDraw method should true if the game is complete and there is no winner, otherwise
it should return false.
The getWinner method should return '.' if there is no winner or the game is still in progress,
otherwise it should return 'X' or 'O' depending on which player won the game.

Coding Style, Following the Rules, etc (10 pts)
Be sure to include your name at the top of all the source files and adhere to the programming
style guidelines in Appendix A in Big Java.

Unit Test Requirements
You will need to build confidence in the correctness of your game engine. To accomplish this,
you are going to use unit tests.

Code for Unit Tests (20 pts)
Place all the code for your JUnit test cases in a class called TicTacToeGameTest. Write as
many unit tests as necessary to validate the functionality of your tic-tac-toe game engine.
Writeup (20 pts)
You need to turn in a written explanation convincing the TA why your unit tests are
complete and effective. This should address, at minimum, the follow topics:
1. What sort of cases did you test?
2. Why did you choose those cases?
3. Do you consider the cases to be a good reflection of the possible cases? Why?
4. What is your approach for validating the output?
5. How did you ensure your output validation is accurate?
You must submit your writeup as a PDF document. You can use OpenOffice.org or
Acrobat+Word to save a PDF. Alternatively, you could download and install the free
PDFCreator to create a PDF from any application.
http://sourceforge.net/projects/pdfcreator/

Example Execution
Consider the following game:

This game could be simulated as follows:
TicTacToeGame game = new TicTacToeGame();
assert(game.isOver() == false);
assert(game.getTurn() == 'X');
game.placeMark(0, 2);
game.placeMark(0, 0);
game.placeMark(2, 0);
game.placeMark(1, 1);
game.placeMark(2, 2);
game.placeMark(1, 2);
game.placeMark(2, 1);
assert(game.isOver() == true);
assert(game.isDraw() == false);
assert(game.getWinner() == 'X');
What to Turn In
Use the TEACH system to hand in TicTacToeGame.java, TicTacToeGameTest.java,
and the PDF file for your writeup. Do not hand in your *.class files. Additionally, you should
copy your *.java files to an engineering server before the due date. In the event something
goes wrong, the files can be retrieved from the engineering server and be demonstrated to
have the correct time stamp.

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 20 posted: 8/28/2011 language: English pages: 5