Can anyone guide me to make a tic-tac-toe in java without showing too much code?

Just a function call I mean. For example:

initializeBoard();
player1turn();
printBoard()

My confusin is with function parameters what should I keep to make it work. That’s why. The design of program is an issue for me, writing the code isn’t.

1 Like
import java.util.Scanner;

public class Example {
    public static void main(String[] args) {
        boolean gameOver = false;
        // initialize board
        char[][] board = new char[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                board[i][j] = ' ';
            }
        }
        Scanner input = new Scanner(System.in);
        char p1 = 'X';
        char p2 = 'O';
        int turn = 0;
        int row, col;
        char aVal = ' ';
        do {
            if (turn % 2 == 0) {
                System.out.println("Enter the row number to insert X");
                row = input.nextInt();
                System.out.println("Enter the column number to insert X");
                col = input.nextInt();
                aVal = 'X';

            } else {
                System.out.println("Enter the row number to insert O");
                row = input.nextInt();
                System.out.println("Enter the column number to insert O");
                col = input.nextInt();
                aVal = 'O';
            }
            displayBoard(board, row, col, aVal);
            gameOver = (diagonalWin(aVal, board) || rowWin(aVal, board) || colWin(aVal, board)) ;


            turn++;
        } while (!gameOver);
        System.out.println(aVal + " has won the game");
    }

    public static boolean colWin(char val, char[][] board) {
        return ((board[0][0] == val && board[1][0] == val && board[2][0] == val) || (board[0][1] == val && board[1][1] == val && board[2][1] == val) || (board[0][2] == val && board[1][2] == val && board[2][2] == val));

    }

    public static boolean rowWin(char val, char[][] board) {
        return ((board[0][0] == val && board[0][1] == val && board[0][2] == val) || (board[1][0] == val && board[1][1] == val && board[1][2] == val) || (board[2][0] == val && board[2][1] == val && board[2][2] == val));
    }

    public static boolean diagonalWin(char val, char[][] board) {
        if ((board[0][0] == val && board[1][1] == val && board[2][2] == val) ||
                (board[0][2] == val && board[1][1] == val && board[2][0] == val)) {
            return true;
        }
        return false;
    }

    public static void displayBoard(char[][] arr, int x, int y, char ch) {
        arr[x][y] = ch;
        System.out.println("-------");
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print("|" + arr[i][j]);
            }
            System.out.print("|");
            System.out.println();
            System.out.print("-------");
            System.out.println();
        }
    }


}

Did the code.

1 Like

Have you gotten into building UIs in Java? That may seem difficult at first, but it will (in the long run) make building apps like this much easier :slight_smile:

1 Like