I am doing in eclipse can you send me the code Thank You Finish reading Deitel,
ID: 3939708 • Letter: I
Question
I am doing in eclipse can you send me the code
Thank You
Explanation / Answer
import java.util.Scanner; //** import package**//
public Class TicTacToe Game
{
private enum Tile
{
X, O, EMPTY;
}
public static void main(String[] args)
{
//** initializing 2 dimensional array of enum type **//
Tile[][] board = new Tile[3][3];
for (int m=0; m<board.length; m++)
for (int n=0; n<board.length; n++)
board[m][n] = Tile.EMPTY;
//**print an empty board as a 2imensionald array,each tile set as "EMPTY" **//
printBoard(board);
int row, column;
int countEmpty=0;
//**countEmpty count, if it is lessthan 1,board is full and game is over **//
for (int m=0; m<board.length; m++)
for (int n=0; n<board.length; n++)
if (board[m][n] == Tile.EMPTY)
countEmpty++;
while (countEmpty > 0)
{
//**Player O enters the row **//
System.out.println("Player O's turn.");
System.out.println("Player O: Enter row (0, 1, or 2):");
Scanner stdin1 = new Scanner(System.in);
row = stdin1.nextInt();
//**Player O enters the column**//
System.out.println("Player O: Enter columnumn (0, 1, or 2):");
Scanner stdin2 = new Scanner(System.in);
column = stdin2.nextInt();
//**If tile is empty, it is valid move, and 'O' is placed on the spot**//
if (board[row][column] == Tile.EMPTY)
{
board[row][column] = Tile.O;
//** MOVE FOR O ***** **//
printOBoard(board, row, column);
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
//**If tile is not empty, it is not a valid move and Player O is prompted to try again.**//
else
{
System.out.println("Space already occupied. Please choose another.");
System.out.println("Player O's turn.");
//** Player 0 enters the row coordinate **//
System.out.println("Player O: Enter row (0, 1, or 2):");
stdin1 = new Scanner(System.in);
row = stdin1.nextInt();
//**Player O enters the columnumn**//
System.out.println("Player O: Enter columnumn (0, 1, or 2):");
stdin2 = new Scanner(System.in);
column = stdin2.nextInt();
//** ERROR MOVE FOR O********* **//
board[row][column] = Tile.O;
printOBoard(board, row, column);
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
//** Player X enters row **//
System.out.println("Player X's turn.");
System.out.println("Player X: Enter row (0, 1, or 2):");
Scanner stdin3 = new Scanner(System.in);
row = stdin3.nextInt();
//** Player X enters columnumn**//
System.out.println("Player X: Enter columnumn (0, 1, or 2):");
Scanner stdin4 = new Scanner(System.in);
column = stdin4.nextInt();
if (board[row][column] == Tile.EMPTY)
{
board[row][column] = Tile.X;
printXBoard(board, row, column);
//** MOVE FOR X ***** **//
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
else
{
System.out.print(" choose another,SPACE is occupied ");
System.out.println("Player O turn.");
System.out.println("Player O: Enter row (0, 1, or 2):");
stdin3 = new Scanner(System.in);
row = stdin3.nextInt();
//**Player O enters columnumn**//
System.out.println("Player O: Enter columnumn (0, 1, or 2):");
stdin4 = new Scanner(System.in);
column = stdin4.nextInt();
board[row][column] = Tile.O;
//** ERROR MOVE FOR X ******** **//
printXBoard(board, row, column);
checkWin(board);
if (checkWin(board) == 2)
;
else
break;
}
//**After both players move, we check if the board is full.**//
countEmpty = 0;
for (int m=0; m<board.length; m++)
for (int n=0; n<board.length; n++)
if (board[m][n] == Tile.EMPTY)
countEmpty++;
}
}
//**printBoard prints grid of EMPTY's and returns nothing**//
private static void printBoard(Tile board[][])
{
System.out.println("_________");
System.out.println("| | | |");
for (int m=0; m<board.length; m++)
{
for (int n=0; n<board.length; n++)
{
System.out.printf("| " + board[m][n] + " ");
}
System.out.println("|");
System.out.println("| | | |");
System.out.println("________");
if (i<2)
System.out.println("| | | |");
}
return;
}
//**printXBoard prints grid modified with addition of X after Player X's turn**//
private static void printXBoard(Tile board[][], int curRow, int curcolumn)
{
System.out.println("_________");
System.out.println("| | | |");
for (int m=0; m<board.length; m++)
{
for (int n=0; n<board.length; n++)
{
if (m == curRow && n == curcolumn)
board[m][n] = Tile.X;
else
;
System.out.printf("| " + board[m][n] + " ");
}
System.out.println("|");
System.out.println("| | | |");
System.out.println("________");
if (i<2)
System.out.print("| | | |");
}
return;
}
//**printOBoard prints grid modified with addition of X after Player X's turn**//
private static void printOBoard(Tile board[][], int curRow, int curcolumn)
{
System.out.println(" _________");
System.out.println("| | | |");
for (int m=0; m<board.length; m++)
{
for (int n=0; n<board.length; n++)
{
if (m == curRow && n == curcolumn)
board[m][n] = Tile.O;
else
;
System.out.println("| " + board[m][n] + " ");
}
System.out.print("|");
System.out.println("| | | |");
System.out.println("________");
if (i<2)
System.out.println("| | | |");
}
return;
}
//**checkWin checks all possible winning combinations for the both players.**//
private static int checkWin(Tile board[][])
{
if (board[0][0] == Tile.X && board[0][1] == Tile.X && board[0][2] == Tile.X)
{
System.out.println("Player X wins!");
return 1;
}
else if (board[0][0] == Tile.X && board[1][1] == Tile.X && board[2][2] == Tile.X)
{
System.out.println("Player X wins!");
return 1;
}
else if (board[0][0] == Tile.X && board[1][0] == Tile.X && board[2][0] == Tile.X)
{
System.out.println("Player X wins!");
return 1;
}
else if (board[1][0] == Tile.X && board[1][1] == Tile.X && board[1][2] == Tile.X)
{
System.out.println("Player X wins!");
return 1;
}
else if (board[2][0] == Tile.X && board[2][1] == Tile.X && board[2][2] == Tile.X)
{
System.out.println("Player X wins!");
return 1;
}
else if (board[0][1] == Tile.X && board[1][1] == Tile.X && board[2][1] == Tile.X)
{
System.out.println("Player X wins!");
return 1;
}
else if (board[0][2] == Tile.X && board[1][2] == Tile.X && board[2][2] == Tile.X)
{
System.out.println("Player X wins!");
return 1;
}
else if (board[2][0] == Tile.X && board[1][1] == Tile.X && board[0][2] == Tile.X)
{
System.out.println("Player X wins!");
return 1;
}
//**check if player O win**//
else if (board[0][0] == Tile.O && board[0][1] == Tile.O && board[0][2] == Tile.O)
{
System.out.println("Player O wins!");
return 0;
}
else if (board[0][0] == Tile.O && board[1][1] == Tile.O && board[2][2] == Tile.O)
{
System.out.println("Player O wins!");
return 0;
}
else if (board[0][0] == Tile.O && board[1][0] == Tile.O && board[2][0] == Tile.O)
{
System.out.println("Player O wins!");
return 0;
}
else if (board[1][0] == Tile.O && board[1][1] == Tile.O && board[1][2] == Tile.O)
{
System.out.println("Player O wins!");
return 0;
}
else if (board[2][0] == Tile.O && board[2][1] == Tile.O && board[2][2] == Tile.O)
{
System.out.println("Player O wins!");
return 0;
}
else if (board[0][1] == Tile.O && board[1][1] == Tile.O && board[2][1] == Tile.O)
{
System.out.println("Player O wins!");
return 0;
}
else if (board[0][2] == Tile.O && board[1][2] == Tile.O && board[2][2] == Tile.O)
{
System.out.println("Player O wins!");
return 0;
}
else if (board[2][0] == Tile.O && board[1][1] == Tile.O && board[0][2] == Tile.O)
{
System.out.println("Player O wins!");
return 0;
}
else
return 2;
}
}
//**thank you**//
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.