Question: Write a JAVA program that handles a 2-dimensional array representing a 3x3 tic-tac-toe board. In the game, players take turns writing an X or an

Write a JAVA program that handles a 2-dimensional array representing a 3x3 tic-tac-toe board. In the game, players take turns writing an X or an O and trying to get three in a row. Write three methods, one to solve for a horizontal win, one to solve for a vertical win, and one to solve for a diagonal win. A win occurs when three symbols for a player are in a line (horizontal, vertical, or diagonal).

Create four 2-d (3x3) arrays, one with a horizontal win for O, one with a vertical win for X, one with a diagonal win for O, and the last being a cat game (no player wins). It is tempting to populate the arrays with a X and O characters but consider representing these values with int values instead which makes calculating a win much easier. Consider a zero value as an empty cell. You do not have to write code to input the values, just define the four arrays using initializer lists (see example below).

For example: an array with { {1, 0, -1}, {1, -1, 0}, {1, -1, 0} } has a vertical win in the first column for X, where 1 represents an X, a -1 represents an O, and 0 represents an empty cell.

Write the main() method to call each solver for a board until one returns a win; specify which player won and the orientation (horizontal, vertical, diagonal) of the win. If none return a win, output that the board is a cat game.

Make the code as minimal as possible

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 Databases Questions!