in java, Write a class ( and a client class to test it) that encapsulates a tic-
ID: 660756 • Letter: I
Question
in java,
Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. At any point, a cell of that table chould be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the tic-tac-toe board.
Your default constructor should instantiate the array so that it represents an empty board.
You should include the following methods:
A method, returning a bollean, simulating a play with three parameters as follows: If the first parameter is true, then X is playing; otherwise, O is playing. The other two parameters represent what cell on the board is being played. If the play is legal, that is, the cell is a legal cell on the board and is empty, then the method should update the array and return true; otherwise, the array should not be updated and the method should return false.
A method returning how many valid plays have been made so far.
A method checking if a player has won based on the contents of the board; this method takes no parameter. It returns X if the "X player" has won, O if the "O player" has won, T if the game was a tie. A player wins if he or she has placed an X (or an O) in all cells in a row, all cells in a column, or all cells in one of the two diagnals.
Thank you!
Explanation / Answer
public class TicTacToe { private char[][] board; private char currentPlayerMark; public TicTacToe() { board = new char[3][3]; currentPlayerMark = 'x'; initializeBoard(); } // Set/Reset the board back to all empty values. public void initializeBoard() { // Loop through rows for (int i = 0; i < 3; i++) { // Loop through columns for (int j = 0; j < 3; j++) { board[i][j] = '-'; } } }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.