• Register
1 vote
563 views

Problem:

I’ve completed my basic java course yesterday. My instructor provided me a problem to solve as an assignment for the final test. The problem is to make a tic tac toe java code using a 2d array. I know how to use a 2d array and handle it. But I am not sure where to start the program and make a Tic-Tac toe game.
I am not looking for the whole solution, but yeah if you can provide it might help me a lot to understand the process better.
Thanks, fellow!

15.3k points

Please log in or register to answer this question.

1 Answer

2 votes

Solution:

If you’ve completed the basic java course you should have the ability to write program that make a Tic-Tac-Toe board and able to play. I am adding the code snippet below that makes the game field for Two players.

public class TictacToe {
    private static int state;
    private static char[][] board; 
    public static void main(String[] args) {
        board = new char[3][3];
        initializeArray();
         
        while(!won()) {
            move();
        }
        if(won()) {
            if(state%2 == 0) System.out.println("Player 1 won");
            else System.out.println("Player 2 won");
        }
    }
     
    public static char[][] initializeArray(){
        state = 0;
        for(int i=0; i<=2; i++) {
            for(int j=0; j<=2; j++) {
                board[i][j] = '-';
            }
        }        
        return board;
    }
     
    public static boolean isBoardFull() {
        for(int i = 0; i<=2; i++) {
            for(int j = 0; j<=2; j++) {
                if(board[i][j] == '-') return false;
            }
        }
        return true;
    }
     
     
    public static int move() {
        Scanner sc = new Scanner(System.in);
        while(!isBoardFull() && state!=9) {
            System.out.println("Player, enter a row");
            int row = sc.nextInt();
            System.out.println("Player, enter a col");
            int col = sc.nextInt();
            if(board[row][col] == 'x' || board[row][col] == 'o') {
                System.out.println("Position filled. Try again");
                row = sc.nextInt();
                col = sc.nextInt();
            }
            if(board[row][col] == '-') {
                if(state%2==0) {
                    board[row][col] = 'x';
                }
                else board[row][col] = 'o';
            }
            printBoard();
            state++;
        }
        if(draw()) System.out.println("Game ended in draw");
        sc.close();
        return state;
    }
         
    public static boolean won() {
        boolean won = false;
        if(draw()) System.out.println("Game Over - No one wins");
        for(int i=0; i<3; i++) {
            if(board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
                System.out.println("We have a winner!");
                if(board[i][0] == 'x') System.out.println("Player 1 wins");
                else System.out.println("Player 2 wins");
                won = true;
            }
        }
        for(int i=0; i<3; i++) {
            if(board[0][i] == board[1][i] && board[2][i] == board[1][i]) {
                System.out.println("We have a winner!");
                if(board[0][i] == 'x') System.out.println("Player 1 wins");
                else System.out.println("Player 2 wins");
                won = true;
            }
        }
         
        if(board[0][0]==board[1][1] && board[1][1] == board[2][2]) {
            System.out.println("We have a winner!");
            if(board[0][0] == 'x') System.out.println("Player 1 wins");
            else System.out.println("Player 2 wins");
            won = true;
        }
         
        if(board[2][0]==board[1][1] && board[0][2] == board[1][1]) {
            System.out.println("We have a winner!");
            if(board[2][0] == 'x') System.out.println("Player 1 wins");
            else System.out.println("Player 2 wins");
            won = true;
        }
             
        return won;
    }
     
     
    public static boolean draw() {
        boolean draw = true;
        for(int i=0; i<=2; i++) {
            for(int j=0; j<=2; j++) {
                if(board[i][j] == '-') draw = false;
            }
        }
        return draw;
    }
    public static void printBoard() {
         System.out.println("-------------");
         for (int i = 0; i < 3; i++) {
             System.out.print("| ");
             for (int j = 0; j < 3; j++) {
                 System.out.print(board[i][j] + " | ");
             }
             System.out.println();
             System.out.println("-------------");
         }
    }  
}

Let me know if you any boundaries to understand. Thanks

94.3k points