Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me, My professor asked to have a line by line explanation of this co

ID: 3885688 • Letter: P

Question

Please help me, My professor asked to have a line by line explanation of this code for tic tac toe.

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoe.java;

import java.util.Scanner;

public class TicTacToeJava {
  
public static Scanner sc = new Scanner(System.in); /**
* @param args the command line arguments
*/
public static void main(String[] args) {
  
final int SIZE = 3;
char[][] board = new char[SIZE][SIZE]; // game board

resetBoard(board); // initialize the board (with ' ' for all cells)

// First, welcome message and display the board.
System.out.println("===== WELCOME TO THE TIC-TAC-TOE GAME!! ===== ");
showBoard(board);

// Then ask the user which symbol (x or o) he/she wants to play.
System.out.print(" Which symbol do you want to play, "x" or "o"? ");
char userSymbol = sc.next().toLowerCase().charAt(0);
char compSymbol = (userSymbol == 'x') ? 'o' : 'x';

// Also ask whether or not the user wants to go first.
System.out.println();
System.out.print(" Do you want to go first (y/n)? ");
char ans = sc.next().toLowerCase().charAt(0);

int turn; // 0 -- the user, 1 -- the computer
int remainCount = SIZE * SIZE; // empty cell count

// THE VERY FIRST MOVE.
if (ans == 'y') {
turn = 0;
userPlay(board, userSymbol); // user puts his/her first tic
}
else {
turn = 1;
compPlay(board, compSymbol); // computer puts its first tic
}
// Show the board, and decrement the count of remaining cells.
showBoard(board);
remainCount--;

// Play the game until either one wins.
boolean done = false;
int winner = -1; // 0 -- the user, 1 -- the computer, -1 -- draw

while (!done && remainCount > 0) {
// If there is a winner at this time, set the winner and the done flag to true.
done = isGameWon(board, turn, userSymbol, compSymbol); // Did the turn won?

if (done)
winner = turn; // the one who made the last move won the game
else {
// No winner yet. Find the next turn and play.
turn = (turn + 1 ) % 2;

if (turn == 0)
userPlay(board, userSymbol);
else
compPlay(board, compSymbol);

// Show the board after one tic, and decrement the rem count.
showBoard(board);
remainCount--;
}
}

// Winner is found. Declare the winner.
if (winner == 0)
System.out.println(" ** YOU WON. CONGRATULATIONS!! **");
else if (winner == 1)
System.out.println(" ** YOU LOST.. Maybe next time :) **");
else
System.out.println(" ** DRAW... **");

}

public static void resetBoard(char[][] brd)
{
for (int i = 0; i < brd.length; i++)
for (int j = 0; j < brd[0].length; j++)
brd[i][j] = ' ';
}

public static void showBoard(char[][] brd)
{
int numRow = brd.length;
int numCol = brd[0].length;

System.out.println();

// First write the column header
System.out.print(" ");
for (int i = 0; i < numCol; i++)
System.out.print(i + " ");
System.out.print(' ');

System.out.println(); // blank line after the header

// The write the table
for (int i = 0; i < numRow; i++) {
System.out.print(i + " ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("|");
System.out.print(" " + brd[i][j] + " ");
}

System.out.println();

if (i != (numRow - 1)) {
// separator line
System.out.print(" ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("+");
System.out.print("---");
}
System.out.println();
}
}
System.out.println();
}

public static void userPlay(char[][] brd, char usym)
{
System.out.print(" Enter the row and column indices: ");
int rowIndex = sc.nextInt();
int colIndex = sc.nextInt();

while (brd[rowIndex][colIndex] != ' ') {
System.out.print(" !! The cell is already taken. Enter the row and column indices: ");
rowIndex = sc.nextInt();
colIndex = sc.nextInt();
}

brd[rowIndex][colIndex] = usym;
}

public static void compPlay(char[][] brd, char csym)
{
// Find the first empty cell and put a tic there.
for (int i = 0; i < brd.length; i++) {
for (int j = 0; j < brd[0].length; j++) {
if (brd[i][j] == ' ') { // empty cell
brd[i][j] = csym;
return;
}
}
}
}

public static boolean isGameWon(char[][] brd, int turn, char usym, char csym)
{
char sym;
if (turn == 0)
sym = usym;
else
sym = csym;

int i, j;
boolean win = false;

// Check win by a row
for (i = 0; i < brd.length && !win; i++) {
for (j = 0; j < brd[0].length; j++) {
if (brd[i][j] != sym)
break;
}
if (j == brd[0].length)
win = true;
}

// Check win by a column
for (j = 0; j < brd[0].length && !win; j++) {
for (i = 0; i < brd.length; i++) {
if (brd[i][j] != sym)
break;
}
if (i == brd.length)
win = true;
}

// Check win by a diagonal (1)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][i] != sym)
break;
}
if (i == brd.length)
win = true;
}

// Check win by a diagonal (2)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][brd.length - 1 - i] != sym)
break;
}
if (i == brd.length)
win = true;
}

// Finally return win
return win;
}
}
  

Explanation / Answer

Added comments:


import java.util.Scanner;
public class TicTacToeJava {
  
public static Scanner sc = new Scanner(System.in); /**
* @param args the command line arguments
*/
//main method with command arguments
public static void main(String[] args) {
// SIZE variable initial value is 3.
final int SIZE = 3;
// 2d char array with default as size as 3
char[][] board = new char[SIZE][SIZE]; // game board
resetBoard(board); // initialize the board (with ' ' for all cells)
// First, welcome message and display the board.
System.out.println("===== WELCOME TO THE TIC-TAC-TOE GAME!! ===== ");
//showBoard method call, with board parameters as 3
showBoard(board);
// Then ask the user which symbol (x or o) he/she wants to play.
System.out.print(" Which symbol do you want to play, "x" or "o"? ");
//character symbol from user input, logic with one character.converting that to lower case.
char userSymbol = sc.next().toLowerCase().charAt(0);
//Comparing the user entered symbol with parameter x and assigning the values.
char compSymbol = (userSymbol == 'x') ? 'o' : 'x';
// Also ask whether or not the user wants to go first.
System.out.println();
System.out.print(" Do you want to go first (y/n)? ");
char ans = sc.next().toLowerCase().charAt(0);
int turn; // 0 -- the user, 1 -- the computer
int remainCount = SIZE * SIZE; // empty cell count
// THE VERY FIRST MOVE.
if (ans == 'y') {
// assign turn value as zero, if the answer is y. that is from user input.
turn = 0;
// method call for userPlay with board and userSymbol as input parameters.
userPlay(board, userSymbol); // user puts his/her first tic
}
else {
// assign turn value as one, if the answer is non y. that is from user input.
turn = 1;
// method call for compPlay with board and compSymbol as input parameters.
compPlay(board, compSymbol); // computer puts its first tic
}
// Show the board, and decrement the count of remaining cells.
showBoard(board);
remainCount--;
// Play the game until either one wins.
boolean done = false;
int winner = -1; // 0 -- the user, 1 -- the computer, -1 -- draw
while (!done && remainCount > 0) {
// If there is a winner at this time, set the winner and the done flag to true.
done = isGameWon(board, turn, userSymbol, compSymbol); // Did the turn won?
if (done)
winner = turn; // the one who made the last move won the game
else {
// No winner yet. Find the next turn and play.
turn = (turn + 1 ) % 2;
if (turn == 0)
// method call for userPlay with board and userSymbol as inputs.
userPlay(board, userSymbol);
else
//// method call for compPlay with board and userSymbol as inputs.
compPlay(board, compSymbol);
// Show the board after one tic, and decrement the rem count.
showBoard(board);
// decrement with one remainCoun.
remainCount--;
}
}
// Winner is found. Declare the winner.
if (winner == 0)
System.out.println(" ** YOU WON. CONGRATULATIONS!! **");
else if (winner == 1)
System.out.println(" ** YOU LOST.. Maybe next time :) **");
else
System.out.println(" ** DRAW... **");
}
// This method will reset the board, by assigning the empty values.
public static void resetBoard(char[][] brd)
{
for (int i = 0; i < brd.length; i++)
for (int j = 0; j < brd[0].length; j++)
brd[i][j] = ' '; //assigning the empty values.
}
// This method will show the Board.
  
public static void showBoard(char[][] brd)
{
int numRow = brd.length;
int numCol = brd[0].length;
System.out.println();
// First write the column header
System.out.print(" ");
for (int i = 0; i < numCol; i++)
System.out.print(i + " ");
System.out.print(' ');
System.out.println(); // blank line after the header
// The write the table
for (int i = 0; i < numRow; i++) {
System.out.print(i + " ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("|");
System.out.print(" " + brd[i][j] + " ");
}
System.out.println();
if (i != (numRow - 1)) {
// separator line
System.out.print(" ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("+");
System.out.print("---");
}
System.out.println();
}
}
System.out.println();
}
  
// This method with do the user play.taking the inputs from user.
public static void userPlay(char[][] brd, char usym)
{
System.out.print(" Enter the row and column indices: ");
int rowIndex = sc.nextInt(); // row index from user input.
int colIndex = sc.nextInt(); // column index from user input.
while (brd[rowIndex][colIndex] != ' ') {
System.out.print(" !! The cell is already taken. Enter the row and column indices: ");
rowIndex = sc.nextInt();
colIndex = sc.nextInt();
}
brd[rowIndex][colIndex] = usym;
}
  
//com play from option user play.
public static void compPlay(char[][] brd, char csym)
{
// Find the first empty cell and put a tic there.
for (int i = 0; i < brd.length; i++) { // loop check for the board size.
for (int j = 0; j < brd[0].length; j++) { // loop check for the board size with 0th element.
if (brd[i][j] == ' ') { // empty cell
brd[i][j] = csym;
return;
}
}
}
}
  
// is game won check for the user options.
// isGameWon method wth board, turn, usym, and csym as inputs.
public static boolean isGameWon(char[][] brd, int turn, char usym, char csym)
{
char sym;
if (turn == 0) // Check turn is 0 or not.
sym = usym;
else
sym = csym;
int i, j; // i, j elements declaration
boolean win = false;
// Check win by a row
for (i = 0; i < brd.length && !win; i++) { // loop with board size.
for (j = 0; j < brd[0].length; j++) {
if (brd[i][j] != sym)
break;
}
if (j == brd[0].length)
win = true;
}
// Check win by a column
for (j = 0; j < brd[0].length && !win; j++) {
for (i = 0; i < brd.length; i++) {
if (brd[i][j] != sym)
break;
}
if (i == brd.length)
win = true;
}
// Check win by a diagonal (1)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][i] != sym)
break;
}
if (i == brd.length)
win = true;
}
// Check win by a diagonal (2)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][brd.length - 1 - i] != sym)
break;
}
if (i == brd.length)
win = true;
}
// Finally return win
return win;
}
}
  


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote