15-121 Homework 2: TicTacToe - Due 9/25 (revised) at midnight
Download and unzip hw2-code.zip, which contains a
modified version of the TicTacToe.java file that we developed in class
on Tuesday. All the code you write for this assignment should be added to
Note: You will be graded in part on your coding style. Your code
should be easy to read, well organized, and concise. You should avoid
Background: The Game
In this lab, you'll be implementing TicTacToe, allowing for two human players
to play against each other (play against your friends and family for valuable
prizes!) as well as allowing for a human to play against the computer (albeit
a pretty stupid computer!).
We all know how to play TicTacToe (or noughts and crosses - bloody British).
Two players, designated by X and O respectively, alternately place their
symbol on an open cell in a 3 by 3 square matrix until either one player has 3
marks in a row (the winner) or no more vacant cells are available (a draw).
Make sure that you can have two humans play against each other before you
attempt the machine vs. human part!
You will write additional methods in the TicTacToe class to implement the
game. I will provide a rough outline below, but feel free to add additional
methods as you require. I have provided return types and parameter lists but
these are "serving suggestions" in that you can feel free to change them if
you choose to approach the method differently than I did. Remember, there are
LOTS of ways to do things correctly!
Oh, and your code should be substantively different from everyone
else's in the class. And please do not Google for "Java TicTacToe code".
With all that said, here's an outline of the solution I wrote:
This method is the only method called by main. You may not call any
other methods from main! This method asks the user if it is to be a
two-player game or a man-vs-machine game and then loops until either one
player wins or the players draw (no more moves). It should announce the
result as soon as a player wins or there is a draw.
void humanMove(boolean player1)
Prompts the player for a row and column in which to place their mark (X or
O). Must call validMove to ensure that the input provided is 1) on
the board and 2) not already occupied. Must loop until the player provides
a valid move! Once a valid location is provided, calls addMark to
add the X or O in the specified location.
boolean validMove(int row, int col)
Verifies that the row and col are within bounds and, if so, that
the cell at that location is blank. Returns true if the location is in bounds
and blank, false otherwise.
Returns true if there are no valid moves left to be made; false otherwise.
boolean winningBoard(boolean player1)
Returns true if the player has three in a row, either across, up and down,
or diagonally; false otherwise.
Letting the computer play and BONUS POINTS
Once you have the two-human player game working, you should modify your code
to allow a person to play against the machine. Only work on this part once
you have the two-human version working!
I assumed that when it was human vs. machine that the human would always go
first, so the machine is always player 2 (O). This method must generate a
legal move for the machine. There are a number of ways to do this. The
easiest way (and a decent baseline) is to search for the first empty space on
the board and use that as the machine's move. You must at
least get the baseline method to work! Once you have the baseline working,
you can improve things for bonus, listed below.
5 points bonus - the machine picks a random
legal position. There are a couple ways to do this, so think before you
10 points bonus - the machine implements a
reasonable winning strategy. What's a winning strategy? Your first
goal would be to play a winning move if one exists; next, you should block a
position that would cause a win for the human player on his/her next play. If
there is no need to block, a (potentially) winning strategy would be to make a
move that gives the machine (O) two cells in a row, column, or diagonal.
The Haroun-negotiated bonus points follow. Attempt at your own risk!
15 points bonus - the machine implements a
(legal) strategy that always results in a draw when played by
20 points bonus - you implement a graphical user
interface that allows the human player(s) to click on a cell and have it
display the ongoing progress of the game.
Note: you should only attempt the bonus once you have all the other
methods working correctly!
Submitting Your Work
When you have completed this assignment and tested your code
thoroughly, create a .zip file with your work
(including TicTacToe.java). Name the zip file "your-last-name".zip and
email it to me firstname.lastname@example.org.
Make sure to keep a copy of your work just in case something goes wrong!