A modern, responsive Tic-Tac-Toe game built using React, TypeScript, and Tailwind CSS. The game provides an intuitive interface with winner detection, draw condition handling, and easy state management with hooks. It also supports game reset functionality.
- Tic-Tac-Toe Game: React, TypeScript, and Tailwind CSS
- Contributing
- License
Try the live version of the game here.
- Winner and Draw Detection: Detects when a player wins or when the game ends in a draw.
- Responsive Design: The layout adjusts smoothly to mobile, tablet, and desktop screen sizes using Tailwind's responsive utilities.
- Modular Code: Reusable components for the board and individual squares, following best practices for separation of concerns.
- Restart Game Functionality: Reset the game with a click to start a new match.
This project was built with the following technologies:
- React: A JavaScript library for building user interfaces.
- TypeScript: A superset of JavaScript that adds static types.
- Tailwind CSS: A utility-first CSS framework for designing responsive and clean UI.
- Vite (or Create React App): Build tool that offers fast development and optimized builds.
Follow these steps to set up the project on your local machine:
Ensure you have Node.js and npm or yarn installed on your system. You can download Node.js here.
git clone https://github.com/JasrajChouhan/tic-tac-toe-react-ts.git
cd tic-tac-toe-react-ts
Run the following command to install the required dependencies:
For npm:
npm install
For yarn:
yarn install
- Playing the Game
- The game alternates turns between Player X and Player O.
- When a player wins, the game displays the 4.winner.
- If no one wins and the board is full, the game will declare a draw.
- Press the "Restart" button to reset the game board and start a new round.
.
├── src
│ ├── components
│ │ ├── Board.tsx # Renders the game board (3x3 grid)
│ │ ├── BoardSquare.tsx # Represents individual squares on the board
│ │ └── BoardContainer.tsx # Handles state management and game logic
│ ├── App.tsx # Root component that renders the game
│ ├── index.tsx # Entry point of the application
│ └── styles
│ └── index.css # Tailwind CSS imports and custom styles
├── public
│ ├── index.html # Main HTML template
├── package.json # Project dependencies and scripts
└── tailwind.config.js # Tailwind CSS configuration file
- squares: An array representing the state of the 9 squares on the board.
- xIsNext: A boolean indicating whether Player X is the next to play.
- status: A string indicating the current status of the game (winner, draw, or the next player).
calculateWinner(squares: Array<string | null>):
Checks if any player has matched a row, column, or diagonal, returning the winning player if found.
handleClick(index: number):
Handles the click event for a square. Updates the state based on the player's move and calls calculateWinner to check for a win.
The game detects a draw when all squares are filled and no winner is found. This is done by checking if all the squares have a value and the winner is null.
const isDraw = squares.every(square => square !== null) && !winner;
- The game layout is styled using Tailwind CSS to be fully responsive. The grid system (grid-cols-3) and spacing (gap-4) ensure the game board scales correctly on different screen sizes.
- Tailwind's mobile-first utilities help adjust the UI seamlessly for mobile, tablet, and desktop.
1.Fork the repository.
2.Create a feature branch (git checkout -b feature-branch
).
3.Make your changes and commit them (git commit -m "Add new feature"
).
4.Push the changes to your fork (git push origin feature-branch
).
5.Create a pull request
describing the changes.
- Demo: A section where you can add a link to the live demo of the project.
- Features: Lists the main features of the project, including winner and draw detection, responsive design, and modular structure.
- Technologies: Lists the key technologies used in the project like React, TypeScript, and Tailwind CSS.
- Installation: Step-by-step guide for cloning the repository, installing dependencies, and running the application locally.
- Usage: A simple guide to play the game, including the logic for detecting the winner or a draw.
- Project Structure: Provides a tree of the project folder structure, with explanations for key components.
- State Management: Explains how
useState
is used to manage the game state. - Game Logic: Details the logic for calculating the winner and handling clicks on the squares.
- Responsive Design: Mentions how Tailwind CSS utilities are used to make the game responsive.
- Contributing: Instructions for contributing to the project.
- License: Specifies that the project is licensed under the MIT license.
This complete markdown file will help developers and contributors understand your project, set it up locally, and contribute effectively.