Due: Monday, Sept 11th by 11:59 PM

Getting Started

Download CS201_Assign01.zip and import it into your Eclipse workspace (File → Import → General → Existing projects into workspace → Archive file.)

You should see a project called CS201_Assign01 in the Package Explorer.

You can run the program interactively by right-clicking on TicTacToe.java and choosing Run As → Java Application.

Your Task

In this assignment, you will complete the implementation of a Tic-Tac-Toe program.

Example Session

Here is an example session (user input in bold):

   |   |   
---|---|---
   |   |   
---|---|---
   |   |   

Player X's turn:
Enter row (0-2): 1
Enter column (0-2): 1

   |   |   
---|---|---
   | X |   
---|---|---
   |   |   

Player O's turn:
Enter row (0-2): 0
Enter column (0-2): 0

 O |   |   
---|---|---
   | X |   
---|---|---
   |   |   

Player X's turn:
Enter row (0-2): 4
Enter column (0-2): 2
Invalid move, try again

 O |   |   
---|---|---
   | X |   
---|---|---
   |   |   

Player X's turn:
Enter row (0-2): 1
Enter column (0-2): 1
Invalid move, try again

 O |   |   
---|---|---
   | X |   
---|---|---
   |   |   

Player X's turn:
Enter row (0-2): 0
Enter column (0-2): 1

 O | X |   
---|---|---
   | X |   
---|---|---
   |   |   

Player O's turn:
Enter row (0-2): 2
Enter column (0-2): 1

 O | X |   
---|---|---
   | X |   
---|---|---
   | O |   

Player X's turn:
Enter row (0-2): 

Requirements and Specifications

The main method is provided for you. You won’t need to change it.

Your task is to complete the static methods described below. Static methods are like C functions, so this assignment will be very much like writing a C program.

The game board is represented by a 3x3 array of int values:

int[][] board = new int[3][3];

Note that each element of the array will contain the value 0 initially.

The first dimension of the array is the rows of the board, and the second dimension is the columns. Each array element represents a location on the game board. The values of the elements are interpreted as follows:

The PLAYER_X and PLAYER_O constants are defined with the values 1 and 2, respectively. You should use these constants as apporpriate in the program.

Make sure that your program’s output matches the output shown above as closely as possible. In particular, make sure that all prompts and messages are spelled correctly.

Static methods

These are the static methods you must implement.

Note that the bodies of some of the methods have the code

throw new UnsupportedOperationException("not implemented yet");

As you implement each method, simply remove this code.

Also note that arrays in Java are passed by reference (just like arrays in C), so a static method can modify an array it receives via a parameter.

public static void printBoard(int[][] board)
This method prints a representation of the board.
public static int getRow(Scanner keyboard)
public static int getCol(Scanner keyboard)
These methods should prompt the user to enter row and column values, reading them with the Scanner passed as the parameter. The value entered by the user should be returned as the result. Note that these methods don't need to check the value entered by the user for validity.
public static boolean isLegalMove(int[][] board, int row, int col)
This method checks the specified row and column to see if they constitute a legal move. A move is legal if the row and column are each in the range 0-2 (inclusive) and if the board contains a blank space (0 value) at that position. The method should return true if the move is legal, and false if the move is not legal.
public static void placeMarker(int[][] board, int row, int col, int player)
This method should place the given player's marker on the board at the specified row and column.
private static boolean playerWins(int[][] board, int player)
This method should return true if the specified player has won the game by getting three pieces in a row in a row, column, or diagonal, and false if the player has not won the game.
private static boolean isDraw(int[][] board)
This method should return true if all spaces on the board are occupied and false if there is at least one empty space on the board.

Testing

You can run JUnit tests by right-clicking TicTacToeTest.java (in the junit source folder) and choosing Run As → JUnit test. If you see a green bar, then all of the tests have passed.

Note that not every method will be tested, and it is possible that the tests don’t test every possible situation. I highly encourage you to add your own tests!

Grading

The assignment will be graded as follows:

Submitting

When you are done, submit the lab to the Marmoset server using one of the methods below.

From Eclipse

If you have the Simple Marmoset Uploader Plugin installed, select the project (CS201_Assign01) in the package explorer and then press the blue up arrow button in the toolbar. Enter your Marmoset username and password when prompted.

This is the recommended way to submit your work.

From a web browser

Save the project (CS201_Assign01) to a zip file by right-clicking it and choosing

Export…→Archive File

Upload the saved zip file to the Marmoset server as assign01. The server URL is

https://cs.ycp.edu/marmoset/

Use this method only if there is some reason why you can’t use the plugin.