Outlab 5: Tic-Tac-Toe
Due: Monday 31 Jul at 10 pm
Practice everything we have learned this semester, to include
This is a lab that allows you to work with 1 partner. A group of two is the maximum.
You will build an application that allows a person to play tic-tac-toe
against the computer. Your application will print out the board,
ask the user to select a spot, update the spot selected, and make
a move on its own. This will continue until the game ends (3
sequential spots having the same mark in the same row, column,
diagonal, or if no more moves are possible due to the lack of
vacant spots). Your application will also do limited error
checking to ensure occupied spots won't be overwritten with new
- Create an Outlab5 project and type this
into a Driver. You won't need to change the Driver.
- Read through the Driver and understand the flow of how this
program will execute.
- Here are some specifications and assumptions for you:
- The board will always be a standard 3-by-3 tic-tac-toe
board. No need to have your code generalizable to other sizes.
- No player is allowed to mark an already marked spot. If the
human tries to do that, an error message must be printed and
the player is to be prompted for another spot until they enter
a valid one (however many times it takes).
- No other error checking on user input needs to occur. Assume
that the user will only type in row and columns that are valid
- The computer move method will randomly select a spot for the
computer to place its mark. The spot to be marked must not
already be occupied (if it is, just randomly select a new
- The printWinner() method does not need to say which player
(computer or human) won, just the mark that won (X or O).
- Here is output when the computer
wins, human wins, there is a tie, and where occupied
spots are attempted.
- You won't be able to give this lab a good honest effort until
we talk about multi-dimensional arrays on July 20th. Before then
you can still map out how you expect objects to interact.
- This is a hard lab and will take many hours. It is worth 10%
of your total class grade. Take it seriously. It WILL take you a
lot of time. My solution has close to 300 lines of code.
- You will probably jump back and forth between the two classes,
but to get you started, kick things off with the Board class.
Think you need something in the Board class to represent the
actual board? The point of the constructor is to give things an
initial condition. Can you think of a good default initial
condition for a tic-tac-toe board? What should the spots be
filled with? Do you mean literally nothing (null), or do you
mean an empty space (" ")?
- After you make the Board constructor (and maybe an instance
variable...), work on the print method. It will be hard and will
take you some time, but will be good practice dealing with 2-d
arrays in preparation for the rest of the assignment.
- After you have a 2-d array working and printing, think about
the rest of the assignment. In English, what do you need to
check to see if the game is over? I check four different things
(1 - Do the rows have a winner? 2 - Do the columns have a
winner? ...). Perhaps each thing you check, should be its own
- Start with plain English when building your methods: How
should the move methods operate? Generate (randomly or through
input) row and column values. If that spot is empty, place the
mark. If not...
- My Player class has only two non-constructor methods, and they
are both called from the Driver. My Board class has nine
non-constructor methods and some are only called from insde the
Board class, some are called from the Driver, and some are
called from the Player class. It is important to be organized
when building this program.
- Think about some lessons from previous labs. Which class
should be responsible for reading input when the human wants to
make a move? Which class should be responsible for changing the
board? Perhaps I need a method in the board class that will
enable Players to check whether or not a space is vacant, or
maybe another method that will enable a Player to add a mark to
Make the computer player smarter. Don't make it randomly select a spot, but give it some strategy. If you do this, upload a text document with your submission explaining the strategy you implemented. You can also think of your own extension, but if you do, you may want to convince the instructor that it is complicated (or feasible) enough.
By Monday 31 Jul. at 10:00 pm, submit the files Player.java and
Board.java into the appropriate D2L dropbox folder. DO NOT SUBMIT .class
Grading - 20 points
- 4 points - A 2-d array is used to represent the board and the
board can be printed appropriately.
- 1 point - The array in the Board class is private, thus
requiring all questions about occupancy or changes to be done
via methods in the Board class
- 2 points - The humanMove() method interacts with the user and
reads in input appropriately.
- 2 points - The computerMove() method
acts as described above.
- 2 points - The Players are unable to overwrite an existing
spot with a new value.
- 4 points - The game ends when there is a winner in a row,
column, diagonal, or tie due to full board.
- 1 point - The output is clean and similar to the sample
- 1 point - The winner, or a tie, is printed.
- 3 points - Good programming principles are followed (comments,
minimal unnecessary code, appropriate delegation of