Coders Packet

Tic-tac-toe using Java AWT and Swing

By Shreya Shukla

Tic-tac-toe is a classic 2 player game, played on a 3x3 grid. Java AWT and Swing help to make this game user-friendly.

Screenshot of the game

This tic-tac-toe game is a 2-player only game. 3 classes together implement this game. These are:

        1) Board class: This class implements the GUI elements of this game and manages them with the help of event handlers.

        2) Game class: This class enacts the rules of this game and performs operations on the data structure that stores the input "X" or "O".

        3) TicTacToe class: This class contains the entry point function that creates the JFrame object and adds the Board object to it.

 

About Board class:

       Features of the Board class:

                1) The Board class uses GridLayout layout manager for displaying the 3x3 grid on the screen.

                2) initializeButtons(): This method creates the 9 buttons and adds them to the frame. This method also registers all the buttons as action event listeners. This method ensures that a button is clicked only once, so the data structure cannot be updated. This method also ensures that the data structure is filled correctly. After every move, this method calls the gameOver() method to check for winner or draw condition.

                3) gameOver(): This method checks for winner and draw, and displays the appropriate message with the help of a dialog box.

                4) resetTheButtons(): This method resets the 9 buttons and sets their text to empty strings so a new game can be played. This method also calls the reset button of the Game class to reset the data structure's elements as empty strings.

 

The Game class:

       Features of the Game class:

                1) playerMark is an instance variable that stores the string values "X" or "O".

                2) Setter and getter methods are used to manipulate and access the values of playerMark.

                3) setButtonText(): This method sets the text of the button clicked, specified by the string value passed to it, at the index value passed to it in the data structure.

                4) checkDraw(): This method checks for the draw condition when all the buttons have been clicked.

                5) checkForWinner(): This method checks if the player passed to this method has fulfilled any of the winning conditions i.e., won the game.

                6) checkRows(): This method checks if the player passed to this method has fulfilled the row condition to win this game.

                7) checkColumns(): This method checks if the player passed to this method has fulfilled the column condition to win this game.

                8) checkDiagonals(): This method checks if the player passed to this method has fulfilled the diagonal condition to win this game.

                9) reset(): This method resets all the elements of this data structure as empty strings so that a new game can be played.

 

The TicTacToe class:

       Features of TicTacToe class:

                1) main(): This method creates the JFrame object, sets its size, title, visibility, default close operation, and location on the screen, and creates and adds the Board object to the JFrame object. 

 

The flow of the game:

       1) Player X clicks any one of the 9 buttons.

       2) Player O clicks any one of the remaining 8 buttons.

       3) The players take alternate turns and when one of them fulfills the winning condition, the game ends with a dialog box displaying the desired message.

       4) If none of the players fulfill the winning condition after all the buttons have been clicked, the game ends with a draw, and the appropriate message is displayed with the help of a dialog box. 

 

Algorithm:
       1) Initialize the game board with a 3x3 grid representing the tic-tac-toe board.
       2) Generally, player X goes first.
       3) Start the game loop. Do:
             a) Display the present state of the game board.
             b) The next player chooses a cell to place "X" or "O".
             c) Ensure that the selected cell is empty. This is called the validity of the move condition.
             d) if(validity of the move condition) then perform step 3(e).
                 Else proceed from step 3(b) again.
             e) Place "X" or "O" on the selected cell of the game board.
             f) Check for winning conditions after each move. These are either only 3 Xs or only 3 Os that should fill any of the three rows, three columns, or any of the two diagonals.
             g) If (winning condition) end the game;
                 Else repeat the steps from 3(a) after alternating the players;
             h) If ((all cells have been filled) && (winning condition)) end the game;
                 Else repeat the steps from 3(a);
       4) Display the end state of the game board and display the winner, if any, or draw.
 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Shreya Shukla (Shreya35)

Download packets of source code on Coders Packet