• Register
1 vote
21 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!

12 7 7
15,250 points

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

13 9 6
94,240 points

Related questions

2 votes
1 answer 26 views
26 views
Problem: Hello, Kodlogs I am looking for a code that makes a tic-tac-toe game board of two-player. I am searching for the codes for a long time over the internet but didn&rsquo;t get codes that I understand easily. Thanks for your effort guys. I must recognize your help. Thanks again
asked Mar 31 Gavin 15.3k points
0 votes
1 answer 8 views
8 views
How to implement the Tik-tac-toe game in C++?
asked Oct 10 sikandar 2.4k points
2 votes
1 answer 15 views
15 views
Problem: Hello programming people I am very new to the world of programming, and I started with the language Java. Right now I am working with the array. I understand what array is and why it&rsquo;s important in programming. But the problem is I only can handle a 1D- ... far. Could any of you please help me out to write a program that prints a 2D array. I must appreciate your kind help. Thanks
asked Mar 13 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
How to dynamically allocate a 2d array in c? What are the different ways to achieve allocation?
asked Oct 9 sikandar 2.4k points
0 votes
1 answer 9 views
9 views
I have a function that I want to take, as a parameter, a variable size 2D array. So far I have this: void myFunction (double ** myArray) { myArray [x] [y] = 5; etc... } And I have showed an array in my code: double anArray [10] [10]; However, ... changes made to my Function should alter the state of an Array. If I understand correctly, I just want to pass as argument a pointer to a 2D array.
asked Aug 8 game 4.3k points