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

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

13 9 6
94,240 points

Related questions

2 votes
1 answer 151 views
151 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, 2020 Gavin 15.3k points
0 votes
1 answer 2 views
2 views
Problem: I just need someone to steer me in the right direction. Point out errors I have so I can try and fix them. I'm new to c++
asked 5 days ago neeraj 9.5k points
0 votes
0 answers 7 views
7 views
Problem: anyone who know this problem please help? tic tac toe minimax java source code
asked Mar 25 Rohit kr 15k points
1 vote
1 answer 6 views
6 views
Problem: How to make a java program to make a simple tic-tac-toe game ?
asked Mar 16 Ashty 5.7k points
0 votes
1 answer 2 views
2 views
Problem: My code runs, but it doesn't print out anything on the board. I'm pretty sure I have done a couple of things wrong, and if someone could point me in the right direction, that would be great. Thanks for any help!
asked 5 days ago neeraj 9.5k points
1 vote
1 answer 3 views
3 views
Problem: Need help with tic tac toe program optimization. Tic tac toe program c++.
asked Mar 25 PkGuy 13.1k points
0 votes
1 answer 7 views
7 views
Problem: I'm having problems during my learning as I mentioned in my question above, I try to find on web search but I can't get proper information, can you help me to continue my project?
asked Mar 21 shamimkhan 15.1k points
0 votes
1 answer 2 views
2 views
Problem: how to Solve this: error: tic tac toe game in python HELP! I need help understanding this: tic tac toe game in python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 13 Mashhoodch 13k points
0 votes
1 answer 23 views
23 views
Problem: Hello everyone, here I leave my code made in C ++ of the famous game Tic-Tac-Toe . The truth is that I am still learning this language on my own and I would like you to give me your opinions that I should edit or improve my code, also that I would like to achieve ... '7', '8', '9'}}; menu (numbers, Player, Name, Name_2); // Function where the menu and game options are located return 0; }
asked Dec 4, 2020 sasha 16.2k points
0 votes
1 answer 39 views
39 views
How to implement the Tik-tac-toe game in C++?
asked Oct 10, 2020 sikandar 2.4k points