This program is a multiplayer Tic-Tac-Toe game using HTML, CSS, JavaScript, and Node.js with Socket.io for real-time communication between players. Here’s a brief explanation of how each part of the program works:
https://multiplayer-tic-tac-toe-6nt4.onrender.com
https://youtu.be/7uPLD-colPw?si=3OI1_jwWnehxlCDV
This file sets up the structure of the web page:
- Loads necessary scripts and stylesheets.
- Defines a form for users to enter their name and join the game.
- Displays elements for game info (user names, turn info) and the Tic-Tac-Toe grid.
- Includes the necessary scripts to handle the game logic.
This file styles the HTML elements:
- Sets up basic styles for the body, headings, and buttons.
- Styles the game grid and its elements.
- Includes animations and transitions for better UX.
- Uses custom fonts from Google Fonts.
This file defines font styles:
- Specifies different font weights and styles for the "Poppins" font.
- Provides classes for easy use of these font styles in the HTML.
This file sets up the server using Node.js and Socket.io:
- Creates an HTTP server and sets up Socket.io for real-time communication.
- Serves the static files (HTML, CSS, JS) from the root directory.
- Manages game logic on the server-side, including player connections, game state, and turn handling.
- Connection Handling: Listens for players connecting and disconnecting, and manages player matchmaking.[socket.on [socket.on(eventName,event)] is used to get the data sent via socket.emit[socket.emit(eventName,data)] function and similarly socket.emit is used to send data to the server (The eventName of socket.on event and socket.emit should be same to get data in the server)]
- Game State Management: Keeps track of game states (e.g., player turns, moves, game over conditions) and updates clients accordingly.
- Real-time Updates: Uses Socket.io to emit events to clients, keeping them in sync with the current game state.
This file handles the client-side logic and interaction:
- Connects to the server via Socket.io.
- Manages user interactions, such as joining a game and making moves.
- Updates the UI based on the current game state received from the server.
- Event Listeners: Sets up event listeners for form submission and grid button clicks.
- Socket Events: Listens for events from the server (
join_room,playing,gameOver) and updates the UI accordingly. - Game Logic: Implements the game logic, including checking for wins, handling turns, and updating the game state.
-
User Joins the Game:
- User enters their name and submits the form.
- Client emits a
join_roomevent with the user's name. - Server adds the user to the waiting list and matches them with another player if available.
-
Game Starts:
- When two players are matched, the server emits a
join_roomevent with game info (players, turns, etc.) to both clients. - Clients update their UI to show the game board and player info.
- When two players are matched, the server emits a
-
Playing the Game:
- Players take turns clicking on grid boxes.
- Each move emits a
playingevent with the move info to the server. - Server updates the game state and checks for win conditions or a draw.
- Server emits updated game state to both clients, who update their UI accordingly.
-
Game Over:
- If a win or draw condition is met, the server emits a
gameOverevent. - Clients display the game result and reset the game after a short delay.
- If a win or draw condition is met, the server emits a