In: Computer Science
I am having an issue with the Java program with a tic tac toe. it isn't a game. user puts in the array and it prints out this. 1. Write a method print that will take a two dimensional character array as input, and print the content of the array. This two dimensional character array represents a Tic Tac Toe game. 2. Write a main method that creates several arrays and calls the print method. Below is an example of my main method: System.out.println("First Tic Tac Toe: " ); char[][] ttt1 = { {'X','O','X'}, {'0','O','X'}, {'X','X','O'}}; print(ttt1); System.out.println("Second Tic Tac Toe: " ); char[][] ttt2 = { {'O','O','X'}, {'0','O','X'}, {'X','X','O'}}; print(ttt2); here is the instructions my java program is all messed up. Please help.
Since no information is provided regarding the format of the output, I have written the code according to my understanding only. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// TicTacToe.java
public class TicTacToe {
// required method
static void print(char board[][]) {
// looping through each row
for (int i = 0; i < board.length; i++) {
// looping through each column
for (int j = 0; j < board[0].length; j++) {
// printing current character with a field with of 2, followed
// by a white space
System.out.printf("%2c ", board[i][j]);
}
//line break
System.out.println();
}
}
public static void main(String[] args) {
System.out.println("First Tic Tac Toe: ");
char[][] ttt1 = { { 'X', 'O', 'X' }, { '0', 'O', 'X' },
{ 'X', 'X', 'O' } };
print(ttt1);
System.out.println("Second Tic Tac Toe: ");
char[][] ttt2 = { { 'O', 'O', 'X' }, { '0', 'O', 'X' },
{ 'X', 'X', 'O' } };
print(ttt2);
}
}
/*OUTPUT*/
First Tic Tac Toe:
X O X
0 O X
X X O
Second Tic Tac Toe:
O O X
0 O X
X X O