Skip to content

This project is a web-based implementation of the timeless Tic-Tac-Toe game, featuring HTML structure, CSS styling for an appealing interface, & JavaScript for interactive gameplay logic.

Notifications You must be signed in to change notification settings

neeru24/Tic-Tac-Toe-

Repository files navigation

Tic-Tac-Toe Game

This is a web-based Tic-Tac-Toe game implemented using HTML, CSS, and JavaScript. It allows players to take turns placing their symbols (X or O) on a 3x3 grid. The game detects when a player has won or if the game ends in a draw.

Technologies Used

HTML: Provides the structure of the web page.

CSS: Styles the elements and adds visual effects.

JavaScript: Implements the game logic and interactivity.

Code Explanation

HTML Structure:

The HTML provides the basic structure of the Tic-Tac-Toe game.
It includes a container (div of class game) with nine buttons representing the game board.
There's also a message container (<div of class msg-container hide) initially hidden, which will display the winner message and a "New Game" button when necessary.
Two additional buttons outside the game container are provided for resetting the game and starting a new game.


CSS Styling:

CSS styles are applied to elements to define their appearance and layout.
Styles define the background color, font, size, and other visual aspects of the game elements.
Selectors like .box, .msg-container, .hide, .game, and button IDs (#reset-btn, #new-btn) are used to target specific elements and apply styling rules.


JavaScript Logic

Initialization :

Event listeners are assigned to each box/button to handle click events.
Variables are initialized to keep track of the game state, such as turnO to determine whose turn it is.
An array winPatterns is defined to represent the winning combinations on the game board.

Click Event Handling :

When a box/button is clicked, an event listener triggers a function.
Depending on whose turn it is (turnO), the clicked box displays either an "X" or "O".
The turnO variable is toggled to switch between players after each move.
The clicked box is disabled to prevent further interaction.

Checking for a Winner:

After each move, a function (checkWinner()) is called to determine if there's a winner.
The function iterates through the winPatterns array to check if any of the winning combinations have been achieved.
If a winning combination is found and all the boxes in that combination have the same value (either "X" or "O"), the game declares a winner and displays a message.
The winning player's symbol is also displayed in the message.

Resetting the Game:

Two buttons (#reset-btn and #new-btn) allow players to reset the game or start a new game.
When clicked, these buttons trigger a function (resetGame()) that resets the game state.
The game board is cleared, all boxes/buttons are enabled, and the message container is hidden.

Overall Functionality:

The code provides the functionality for a basic Tic-Tac-Toe game where players can take turns placing their symbols ("X" or "O") on a 3x3 grid.
It detects when a player has won or if the game ends in a draw.
Players can reset the game or start a new game at any time using the provided buttons.

Contribution

Contributions are welcome! If you have any suggestions for improvements then feel free to submit a pull request.

Output

Output


Output


Output

About

This project is a web-based implementation of the timeless Tic-Tac-Toe game, featuring HTML structure, CSS styling for an appealing interface, & JavaScript for interactive gameplay logic.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published