Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can anyone help me with my code?

0 votes
asked Nov 30, 2018 by STUDENTINNEEDOFHELP
import java.util.Scanner;

public class TicTacToe

{

  char[][] testGrid;

  public void PrintGrid ()

  {

    for (int row = 0; row < testGrid.length; row++)

      {

//System.out.print(row+ "_");

for (int col = 0; col < testGrid.length; col++)

  {

    //print _

    //testGrid[row][col] = '_';

    //System.out.print(col+ "_");

    System.out.print (testGrid[row][col] + " ");

  }

System.out.println ();

      }

  }

  public TicTacToe ()

  {

    Scanner input = new Scanner (System.in);

    System.out.print ("Please enter the board size 'n': ");

    int boardSize = input.nextInt ();

    //char[][] testGrid = new char[ boardSize ][ boardSize];

    testGrid = new char[boardSize][boardSize];

    for (int row = 0; row < testGrid.length; row++)

      {

for (int col = 0; col < testGrid.length; col++)

  {

    testGrid[row][col] = '_'; //print an _

  }

      }

    boolean win = false;

    char currentPlayer = 'X'; //currentPlayer is shown as either X or O

    while (win == false)

      {

//promt the user for coordinates for X and O

System.out.println ("Please enter row for " + currentPlayer);

int row = input.nextInt ();

System.out.println ("Please enter column for " + currentPlayer);

int col = input.nextInt ();

testGrid[row][col] = currentPlayer;

//System.out.println("_" +testGrid[row][col]);

//horizontal wins

int counter = 0;

for (int r = 0; r < testGrid.length; r++)

  {

    counter = 0;

    for (int c = 0; c < testGrid.length; c++)

      {

if (testGrid[r][c] == currentPlayer)

  counter = counter + 1;

      }

    //are there enough X's?

    //  if so, the game is over

    if (counter == testGrid.length)

      {

System.out.println (currentPlayer + " has won!");

win = true;

      }

  }

/*

   for(int c = 0; c<testGrid.length; c++)

   {

   if(testGrid[r][c] == 'O')

   counter = counter + 1;

   }

   if(counter == testGrid.length)

   {

   System.out.println("O has won!");

   }

*/

//diagonal wins

counter = 0;

for (int i = 0; i < boardSize; i++)

  {

    if (testGrid[i][i] == currentPlayer)

      {

counter = counter + 1;

      }

  }

if (counter == testGrid.length)

  {

    System.out.println (currentPlayer + "has won!");

    win = true;

  }

//ties

PrintGrid ();

if (currentPlayer == 'X')

  currentPlayer = 'O';

else

  currentPlayer = 'X';

//System.out.println("_" +testGrid[row][col]);

      }

  }

  public static void main (String[]args)

  {

    new TicTacToe ();

  }

}

1 Answer

0 votes
answered Nov 30, 2018 by anonymous
import java.util.Scanner;

public class TicTacToe {
    
    private static final char EMPTYCHAR = '_';

    private char[][] testGrid;
    private Scanner input;

    public TicTacToe(Scanner scanner) {
        input = scanner;
        int boardSize = 0;

        do {
            System.out.print("Please enter the board size 'n': ");
            boardSize = input.nextInt();
        } while (boardSize < 3);
        
        testGrid = new char[boardSize][boardSize];
        for (int row = 0; row < testGrid.length; row++) {
            for (int col = 0; col < testGrid.length; col++) {
                testGrid[row][col] = EMPTYCHAR;
            }
        }
    }
    
    private void PrintGrid() {
        for (int row = 0; row < testGrid.length; row++) {
            for (int col = 0; col < testGrid.length; col++) {
                System.out.print(testGrid[row][col] + " ");
            }
            System.out.println();
        }
    }

    private boolean gameFinished(char currentPlayer) {
        int counter;
        
        //horizontal wins
        for (int r = 0; r < testGrid.length; r++) {
            counter = 0;
            for (int c = 0; c < testGrid.length; c++) {
                if (testGrid[r][c] == currentPlayer) {
                    counter++;
                }
            }
            if (counter == testGrid.length) {
                System.out.println(currentPlayer + " has won!");
                return true;
            }
        }
        
        //vertical wins
        for (int c = 0; c < testGrid.length; c++) {
            counter = 0;
            for (int r = 0; r < testGrid.length; r++) {
                if (testGrid[r][c] == currentPlayer) {
                    counter++;
                }
            }
            if (counter == testGrid.length) {
                System.out.println(currentPlayer + " has won!");
                return true;
            }
        }

        //diagonal win 1
        counter = 0;
        for (int i = 0; i < testGrid.length; i++) {
            if (testGrid[i][i] == currentPlayer) {
                counter++;
            }
        }
        if (counter == testGrid.length) {
            System.out.println(currentPlayer + " has won!");
            return true;
        }
        
        //diagonal win 2
        counter = 0;
        for (int i = 0; i < testGrid.length; i++) {
            if (testGrid[i][testGrid.length-1-i] == currentPlayer) {
                counter++;
            }
        }
        if (counter == testGrid.length) {
            System.out.println(currentPlayer + " has won!");
            return true;
        }
        
        //check if there are available actions
        for (int r = 0; r < testGrid.length; r++) {
            for (int c = 0; c < testGrid.length; c++) {
                if (testGrid[r][c] == EMPTYCHAR) {
                    return false;
                }
            }
        }
        
        System.out.println("The game has ended with a draw!");
        
        return true;
    }
    
    private boolean validateAction(int row, int col) {
        if ((row >= testGrid.length) || (row < 0)) return false;
        if ((col >= testGrid.length) || (col < 0)) return false;
        if (testGrid[row][col] != EMPTYCHAR) return false;
        return true;
    }

    public void play() {

        int row, col;
        boolean validAction;
        char currentPlayer = 'X'; //currentPlayer is shown as either X or O

        do
        {
            //promt the user for coordinates for X and O
            do {
                System.out.print("Please enter row for " + currentPlayer + " : ");
                row = input.nextInt();
                System.out.print("Please enter column for " + currentPlayer + " : ");
                col = input.nextInt();
                validAction = validateAction(row,col);
                if (!validAction) {
                    System.out.println("Invalid action! Please enter new action!");
                }
            } while (!validAction);

            testGrid[row][col] = currentPlayer;

            if (currentPlayer == 'X') {
                currentPlayer = 'O';
            } else {
                currentPlayer = 'X';
            }

            PrintGrid();
        } while (!gameFinished(testGrid[row][col]));
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        TicTacToe t = new TicTacToe(input);
        t.play();
    }
}
commented Nov 30, 2018 by STUDENTINNEEDOFHELP
Thank you so so so much! Can you possibly also do it in a boolean? Its ok if not im just wondering.
commented Nov 30, 2018 by anonymous
What exactly do you want to be boolean?
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...