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

In a game of TicTacToe, two players take turns marking an available cell in a 3

ID: 3567560 • Letter: I

Question

In a game of TicTacToe, two players take turns marking an available cell in a 3 * 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create a program for playing TicTacToe. The program prompts two players to enter X token and O token alternately. Whenever a token is entered, the program redisplays the board on the console and determines the status of the game (win, draw, or continue).

In the main method is where you can have your game loop. This loop runs the game. But before you start your loop you need to do your initialization step ( like creating the board, any needed variables, Scanner object, etc.), Then inside a loop you should ask a player for their coordinates of where to place their piece on the board. Check for valid coordinates, check to see if there if a piece is already in that spot or not. If the spot is empty place the piece there, if it is not ask for new coordinates. You should also check for out of bounds coordinates as well ( 5,5 is not valid coordinates in tic tac toe). Once a piece is on the board, you should print it, check for a winner (if there is a winner break, and say so) , then move to the next player. Keep in mind you should also note when a draw has occurred (when the board is full and no one has won).

PAY CLOSE ATTENTION TO THE FORMATTING. UGLY TICTACTOE BOARDS WILL LOSE POINTS.

Example Output:

Enter row(1,2,3) for Player X: 1

Enter col(1,2,3) for Player X: 2

-------------

| | x | |

-------------

| | | |

-------------

| | | |

Explanation / Answer

import java.util.Scanner;

public class TicTacToe
{
   public enum Value{ EMPTY,X, O;}
   private static Value [][] board = new Value[3][3];
   public TicTacToe()
   {
   for(int i = 0 ; i <3 ;i++ )
   {
   for(int j = 0; j<3 ;j++)
  
   {
   board[i][j] = Value.EMPTY;
   }
   }
   }
   public static void main(String [] x)
   {
   TicTacToe game = new TicTacToe();
   Scanner input = new Scanner(System.in);
   int count=0;
   int row,column;
   boolean chance = true;
   while(true)
   {
   System.out.printf("Player %d's chance: Enter row column Co-ordinates seprated by space",chance?1:2);
   row = input.nextInt(); column = input.nextInt();
   Value mark = chance?Value.O:Value.X;
   count++;
   try
   {
   game.move(row,column,mark);
   }
   catch(Exception e)
   {
   System.out.println(e);
   continue;
   }
   game.displayBoard();
   if(game.check(row, column, mark))
   {
  
   System.out.println("Player 1 won");
   break;
   }
   else
   {
   chance = !chance;
   }
  
   if(count ==9)
   {
   System.out.println("Draw");
   break;
   }
  
   }
   input.close();
   }
  
   public void move(int row,int column, Value x )
   {
   if(board[row ][column] == Value.EMPTY)
   board[row][column] = x;
   else
   throw new IllegalArgumentException("Position already Filled");
  
  
   }
   private boolean check(int row , int column, Value x)
   {
   boolean check1=true , check2= true;
  
  
   if(row ==1 && column == 1)
   {
   if((board[0][0]== x && board[2][2] == x) | (board[0][2] == x && board[2][0] == x))
   return true;
   }
  
   if((row == 0 | row == 2) && (column == 0 | column == 2))
   {
  
   if(board[1][1] == x && board[2-row][2-column] == x )
   return true;
   }
  
   for(int i=0; i <3; i++)
   {
   if(board[row][i]!=x)
   {
   check1 = false;
   break;
   }
  
   }
   for(int i = 0; i<3; i++)
   {
   if(board[i][column] != x)
   {
   check2 = false;
   break;
   }
  
   }
  
   return check1|check2;
  
   }
   public void displayBoard()
   {
   for(Value[] row : board )
   {
   for(Value element : row)
   {
   System.out.print(" "+element);
   }
   System.out.println();
   }
   }
  
}

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