Programming Assignment 5

Due March 24, before 9:30am

Program Description

In this assignment, you will develop your own player methods for the game of tic-tac-toe.  A human can play against your program by clicking the mouse to place an X on the tic-tac-toe board.  Your method would then select the next move for the computer to place an O.   The human always goes first.   Your player methods should be good enough so that the human never wins; instead, the game should end in a "draw" or "the computer wins."

What You Do

It is assumed that you are using NetBeans to develop your code for this assignment.  Create a new NetBeans project that is a "General" "Java Application". Name your NetBeans project program5.

Download the source for the Main.java,
TicTacToe.java  and MyPlayer.java classes, and store these in the program5/src/program5 directory.

Using NetBeans,
you are to implement two methods for the class MyPlayer:
  1. public static int[] findWinningMove(int[][] board, int player)
This method searches the tic-tac-toe board to see if there is a winning move available for player. In other words, the method looks for a row, column, or diagonal on the current board where there is an open spot, and the other two spots are already taken by player. The board is a 3x3 array. This method returns an array {row,column}  of the winning move for player if there is one.  If there is no winning move, then the method returns {-1,-1}.
  1. public static int[] selectMove(int[][] board, int player)
This method selects the next move for player for the given 3x3 board.  It returns a 1-D array that gives the {row,column} location selected for the next move. If there is no empty spot, then the method returns {-1,-1}.

Here is one possible strategy for selecting the next move:
  1. Invoke the method findWinningMove to see  if there is a winning move available, and if yes, return the location {row,column} of the winning move.
  2. If not, then invoke findWinningMove to see if there is a winning move available for the opponent, and if yes, return the location {row,column} of that move, in order to block the opponent from taking that spot on the board. 
  3. If not, then check to see if the center spot is empty. If yes, return the location {1,1} of that move.
  4. If not, then see if there are any open corners on the board.  If yes, return the location {row,column} of the first open corner found.
  5. If not, then find any remaining empty spot on the board.  If there is one, then return the location {row,colmun} of that spot on the board.
  6. Else, there are no open spots on the board! Return  {-1,-1}.
These six steps outline just one possible strategy.  You are welcome to use this strategy, or to invent one of your own.

Constants

The board is represented as a 3x3 array.  Each spot on the board can have one of three possible values.  These values are static constants defined in the class TicTacToe.
  1. TicTacToe.HUMAN_PLAYER
  2. TicTacToe.COMPUTER_PLAYER
  3. TicTacToe.EMPTY 

Programming Style

You are expected to follow the CS111 / CS112 Java Coding Standard.  You will be graded on programming style. Each student must schedule two meetings with course staff to review a submitted program's style.   Two lab times will be set aside for this (refer to course schedule).  You can also arrange to have tutors review your program style during scheduled tutoring hours.   This is meant to allow students to get guidance and feedback on style, and the grading of style is separate from the grading of Program Assignment 5.

Testing

You are responsible for testing your methods.  To help you in testing, a few example tests are provided in the file Main.java.  A graphical interface is also provided that allows a human to play tic-tac-toe against your computer player. Other tests will be conducted in grading -- so test your methods carefully before submitting your code. 
     

What You Submit

Submit your  file called MyPlayer.java in a directory named 05 via gsubmit .

Under no circumstances will late assignments be accepted.

Grading Criteria



20% Submitted code compiles without errors
20% findWinningMove
returns the correct value for the cases given in Main.java
20% findWinningMove returns the correct value for other cases given at testing time.
20% selectMove returns the correct value for the cases given in Main.java
20% selectMove does not lose in games played against an automated test player given at grading time.