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

**URL:** <https://forum.kirupa.com/t/can-anyone-guide-me-to-make-a-tic-tac-toe-in-java-without-showing-too-much-code/670796>\
**Category:** programming\
**Created:** [November 7, 2024, 3:03am UTC](https://forum.kirupa.com/t/can-anyone-guide-me-to-make-a-tic-tac-toe-in-java-without-showing-too-much-code/670796 "2024-11-07T03:03:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oslon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/oslon/32/24631_2.png) [@oslon](https://forum.kirupa.com/u/oslon)\
**Post date:** [November 7, 2024, 3:03am UTC](https://forum.kirupa.com/t/can-anyone-guide-me-to-make-a-tic-tac-toe-in-java-without-showing-too-much-code/670796/1 "2024-11-07T03:03:27Z")

</div>

Just a function call I mean. For example:

```auto
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.

---

<div class="post-metadata">

**Author:** ![oslon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/oslon/32/24631_2.png) [@oslon](https://forum.kirupa.com/u/oslon)\
**Post date:** [November 7, 2024, 7:50am UTC](https://forum.kirupa.com/t/can-anyone-guide-me-to-make-a-tic-tac-toe-in-java-without-showing-too-much-code/670796/2 "2024-11-07T07:50:48Z")

</div>

```auto
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.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [November 7, 2024, 6:45pm UTC](https://forum.kirupa.com/t/can-anyone-guide-me-to-make-a-tic-tac-toe-in-java-without-showing-too-much-code/670796/3 "2024-11-07T18:45:49Z")

</div>

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 🙂
