Question: For part 1 you need to create two classes: TicTacToe Main The Main class will only need one method: main. In main you must instantiate

For part 1 you need to create two classes:
TicTacToe
Main
The Main class will only need one method: main. In main you must instantiate a local variable of type TicTacToe and call the printBoard method followed by the runTests method.
The TicTacToe class should have a constructor and four methods: printBoard(), validMove(), gameStatus(), and runTests().
TicTacToe should start off with one private instance variable, board, which is a two-dimensional array of CellStates (with 3 rows and 3 columns). In the TicTacToe constructor, make sure to assign one of the values in the board to CellState.X and one to CellState.O (purely for testing purposes).
CellState is an enum that has three values: X, O, and EMPTY. I recommend defining CellState inside TicTacToe.java.
Write a method named validMove in TicTacToe. This method may or may not take two integer inputs representing a row and a column (It depends on whether you choose to make these local or instance variables). The method will return a boolean value (true or false). True should be returned if the row and column values are in-bounds and index an empty location on the board. Otherwise return False. I request that you let the user use 1,2,3 as valid row and column inputs and then subtract 1 from these values before your code performs the actual indexing.
(Note that the board does not need passed into the method because it is an instance variable belonging to the TicTacToe object.)
Write a method named gameStatus in TicTacToe. This method will take no input and will return a value of type GameState. GameState is a new enum that you should implement. GameState should have three values: WIN, DRAW, CONTINUE.
The gameStatus method should check the board to see if either player X or player O has won (three in a row), or if the game should continue (if there are empty spaces remaining on the board), or the game is a draw (the entire board is filled up but no one has won).
You are encouraged to create private helper methods to organize gameStatus. For example, you might write separate methods that check rows, columns, and diagonals for a win and take as input a CellState.X or CellState.O to check for a win for one player or the other.
Lastly, you must write a runTests method in TicTacToe and call this method from Main.java. Write custom code in runTests to test, and print the results of, all of the following conditions in order to make sure your latest code is written correctly:
validMove returns false when one (or both) of its inputs is out of bounds on the low end
validMove returns false when one (or both) of its inputs is out of bounds on the high end
validMove returns false when its inputs index a cell in the board that is not empty
validMove returns true when its inputs are in bounds and refer to an empty board cell
gameStatus returns WIN when a player has three in a row
gameStatus returns DRAW when the board is full and there are no three-in-a-rows
gameStatus returns CONTINUE when at least one cell is empty and there are no three-in-a-rows
You have permission to add other methods that may be useful. For instance, many students choose to write a short getCellText method that takes a CellState as input and returns one of three Strings: X,O, or , which is useful since you dont want to print EMPTY when displaying your board.
UML Diagram for TicTacToe
There are options for how each persons UML looks. Maybe you will also detect who the winner is in the gameStatus method and save that information in an instance variable named winner. Maybe you will use instance variables rowChoice and colChoice to save the chosen moves. Its up to you. There are other ways to organize your TicTacToe game and some of them are probably better (in terms of writing readable, efficient, minimalist code).
TicTacToe
- board : CellState[][]
- winner : CellState
- rowChoice : int
- colChoice : int
<> TicTacToe
+ printBoard() : void
- gameStatus() : GameState
- validMove() : boolean
+ runTests() : void
Compilation and Execution
I will test your program as follows (assume both TicTacToe.java and Main.java are in the default folder)
javac *.java
java Main

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!