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

I need a Little help with the Below code which runs a tic tac toe game, Ok my pr

ID: 3538584 • Letter: I

Question

I need a Little help with the Below code which runs a tic tac toe game,

Ok my problem is I do not know how to implement my hasPlayerWon method to

to the main method to print out X or O has won the game

The second thing I need to do is stop the loop for when either player has won

Thanks for the help.

import java.util.Scanner;

public class TicTacToe
{
public static void main(String[] args)
{
    Scanner input= new Scanner(System.in);
    char[][] board={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
   
    char player = 'X';
   
    while(true)
    {
      System.out.println(player + "'s move. Please enter the row:");
      int row =input.nextInt();
      System.out.println(player + " Please enter the column:");
      int column =input.nextInt();
     
      board[row-1][column-1] = player;
     
      printBoard(board);
     
   
     
     
      if (player == 'X')
         player = 'O';
      else
        player = 'X';
     
}
}

public static boolean hasPlayerWon(char player, char[][] board)
{

if ((board[0][0]==player&&board[0][1]==player&&board[0][2]==player||
       board[1][0]==player&&board[1][1]==player&&board[1][2]==player||
       board[2][0]==player&&board[2][1]==player&&board[2][2]==player||
       board[0][0]==player&&board[1][1]==player&&board[2][2]==player||
       board[2][0]==player&&board[1][1]==player&&board[0][2]==player||
       board[0][0]==player&&board[1][0]==player&&board[2][0]==player||
       board[0][2]==player&&board[1][2]==player&&board[2][2]==player||
       board[0][3]==player&&board[1][3]==player&&board[2][3]==player))
       
        return true;
       else
        return false;
      }

public static void printBoard(char[][] board)
{
for(int row=0; row<3; row++)
{
    for (int column =0; column < 3; column++)
{
System.out.print(" " + board[row][column] + " ");
if(column !=2)
    System.out.print("|");
}
System.out.println();
if (row !=2)
    System.out.println("---+---+---");
}
}
}

Explanation / Answer

please rate - thanks


I changed your method a bit, ask if you have any questions


import java.util.Scanner;

public class TicTacToe
{
public static void main(String[] args)
{
    Scanner input= new Scanner(System.in);
    char[][] board={{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
  
    char player = 'X',done=' ';
  
    while(done==' ')
    {
      System.out.println(player + "'s move. Please enter the row:");
      int row =input.nextInt();
      System.out.println(player + " Please enter the column:");
      int column =input.nextInt();
    
      board[row-1][column-1] = player;
    
      printBoard(board);
      done = hasPlayerWon(board);      
        if(done== ' ')     
           if (player == 'X')
                 player = 'O';
           else
                 player = 'X';
       
    
}
if(done=='t')
      System.out.println("the game is a tie");
else
    System.out.println("The winner is "+done);
}

public static char hasPlayerWon(char board[][])
{int i,j;
for(i=0; i<3; i++)
    if(board[i][0]==board[i][1]&&board[i][0]==board[i][2])    //check rows
             return board[i][0];
for(i=0; i<3; i++)
    if(board[0][i]==board[1][i]&&board[0][i]==board[2][i])       //check columns
            return board[0][i];

if(board[0][0]==board[1][1]&&board[1][1]==board[2][2])     //check 1st diagnol
       return board[0][0];
if(board[0][2]==board[1][1]&&board[1][1]==board[2][0])     //check other diagnol
       return board[0][2];
for(i=0;i<3;i++)                                   //check if board is filled?
    for(j=0;j<3;j++)
         if(board[i][j]==' ')                       //if not, game continue
              return ' ';       
return 't';                                            //if all filled, it's a tie
}

public static void printBoard(char[][] board)
{
for(int row=0; row<3; row++)
{
    for (int column =0; column < 3; column++)
{
System.out.print(" " + board[row][column] + " ");
if(column !=2)
    System.out.print("|");
}
System.out.println();
if (row !=2)
    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