├── Images └── demo.png ├── src ├── img │ └── bg.jpg ├── setupTests.js ├── App.test.js ├── App.js ├── components │ ├── Square.js │ ├── Board.js │ └── Game.js ├── index.css ├── reportWebVitals.js ├── index.js ├── logo.svg └── App.css ├── public ├── favicon.png ├── robots.txt ├── manifest.json └── index.html ├── package.json ├── LICENSE └── README.md /Images/demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mridul0703/Tic-Tac-Toe/HEAD/Images/demo.png -------------------------------------------------------------------------------- /src/img/bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mridul0703/Tic-Tac-Toe/HEAD/src/img/bg.jpg -------------------------------------------------------------------------------- /public/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mridul0703/Tic-Tac-Toe/HEAD/public/favicon.png -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import { render, screen } from '@testing-library/react'; 2 | import App from './App'; 3 | 4 | test('renders learn react link', () => { 5 | render(); 6 | const linkElement = screen.getByText(/learn react/i); 7 | expect(linkElement).toBeInTheDocument(); 8 | }); 9 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | // src/App.js 2 | import React from 'react'; 3 | import './App.css'; 4 | import Game from './components/Game'; 5 | 6 | function App() { 7 | return ( 8 |
9 | 10 | 11 |
12 | ); 13 | } 14 | 15 | export default App; 16 | -------------------------------------------------------------------------------- /src/components/Square.js: -------------------------------------------------------------------------------- 1 | // src/components/Square.js 2 | import React from 'react'; 3 | 4 | 5 | const Square = ({ value, onClick, isWinningSquare }) => { 6 | return ( 7 | 10 | ); 11 | }; 12 | 13 | export default Square; 14 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/reportWebVitals.js: -------------------------------------------------------------------------------- 1 | const reportWebVitals = onPerfEntry => { 2 | if (onPerfEntry && onPerfEntry instanceof Function) { 3 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 4 | getCLS(onPerfEntry); 5 | getFID(onPerfEntry); 6 | getFCP(onPerfEntry); 7 | getLCP(onPerfEntry); 8 | getTTFB(onPerfEntry); 9 | }); 10 | } 11 | }; 12 | 13 | export default reportWebVitals; 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom/client'; 3 | import './index.css'; 4 | import App from './App'; 5 | import reportWebVitals from './reportWebVitals'; 6 | 7 | const root = ReactDOM.createRoot(document.getElementById('root')); 8 | root.render( 9 | 10 | 11 | 12 | ); 13 | 14 | // If you want to start measuring performance in your app, pass a function 15 | // to log results (for example: reportWebVitals(console.log)) 16 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 17 | reportWebVitals(); 18 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Mridul M Kumar | Tic-Tac-Toe", 3 | "short_name": "Tic-Tac-Toe", 4 | "description": "Self Developed Tic-Tac-Toe game built with React.js", 5 | "start_url": "https://mridul0703.vercel.app/", 6 | "display": "standalone", 7 | "background_color": "#ffffff", 8 | "theme_color": "#000000", 9 | "icons": [ 10 | { 11 | "src": "https://github.com/mridul0703/Portfolio/blob/main/public/favicon.png", 12 | "sizes": "192x192", 13 | "type": "image/png" 14 | }, 15 | { 16 | "src": "https://github.com/mridul0703/Portfolio/blob/main/public/favicon.png", 17 | "sizes": "512x512", 18 | "type": "image/png" 19 | } 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tic-tac-toe", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.17.0", 7 | "@testing-library/react": "^13.4.0", 8 | "@testing-library/user-event": "^13.5.0", 9 | "react": "^18.3.1", 10 | "react-dom": "^18.3.1", 11 | "react-scripts": "5.0.1", 12 | "web-vitals": "^2.1.4" 13 | }, 14 | "scripts": { 15 | "start": "react-scripts start", 16 | "build": "react-scripts build", 17 | "test": "react-scripts test", 18 | "eject": "react-scripts eject" 19 | }, 20 | "eslintConfig": { 21 | "extends": [ 22 | "react-app", 23 | "react-app/jest" 24 | ] 25 | }, 26 | "browserslist": { 27 | "production": [ 28 | ">0.2%", 29 | "not dead", 30 | "not op_mini all" 31 | ], 32 | "development": [ 33 | "last 1 chrome version", 34 | "last 1 firefox version", 35 | "last 1 safari version" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/components/Board.js: -------------------------------------------------------------------------------- 1 | // src/components/Board.js 2 | import React from 'react'; 3 | import Square from './Square'; 4 | 5 | const Board = ({ squares, onClick, winningLine }) => { 6 | const renderSquare = (i) => { 7 | const isWinningSquare = winningLine.includes(i); 8 | return ( 9 | onClick(i)} 12 | isWinningSquare={isWinningSquare} 13 | /> 14 | ); 15 | }; 16 | 17 | return ( 18 |
19 |
20 | {renderSquare(0)} 21 | {renderSquare(1)} 22 | {renderSquare(2)} 23 |
24 |
25 | {renderSquare(3)} 26 | {renderSquare(4)} 27 | {renderSquare(5)} 28 |
29 |
30 | {renderSquare(6)} 31 | {renderSquare(7)} 32 | {renderSquare(8)} 33 |
34 |
35 | ); 36 | }; 37 | 38 | export default Board; 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Mridul M Kumar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | Tic-Tac-Toe Game - 3 | Tic Tac Toe

4 |

Welcome to my Tic-Tac-Toe game!

5 |

6 | 7 |
8 | Demo 9 |
10 | 11 | 12 | ## Table of Contents 13 | 14 | - [Introduction](#introduction) 15 | - [Technologies Used](#technologies-used) 16 | - [Features](#features) 17 | - [License](#license) 18 | 19 | ## Introduction 20 | 21 | This project is a simple yet fun Tic-Tac-Toe game built with React.js. Enjoy playing the game and explore the code to see how it's made! 22 | 23 | ## Technologies Used 24 | 25 | This Tic-Tac-Toe game was built using the following technologies: 26 | 27 | - React.js 28 | - JavaScript 29 | - CSS3 30 | - HTML 31 | - VsCode 32 | - Vercel 33 | 34 | ## Features 35 | 36 | - Responsive design 37 | - Interactive gameplay 38 | - Restart game functionality 39 | 40 | ## License 41 | 42 | This project is licensed under the [MIT License](LICENSE). 43 | 44 | ## Contact 45 | 46 | Feel free to reach out to me at [mridulmkumar07@gmail.com](mailto:mridulmkumar07@gmail.com) or visit my website mridul.app for more information. 47 | 48 | ## Show your support 49 | 50 | Give a ⭐ if you like this game! 51 | -------------------------------------------------------------------------------- /src/components/Game.js: -------------------------------------------------------------------------------- 1 | // src/components/Game.js 2 | import React, { useState } from 'react'; 3 | import Board from './Board'; 4 | 5 | const Game = () => { 6 | const [history, setHistory] = useState([{ squares: Array(9).fill(null) }]); 7 | const [stepNumber, setStepNumber] = useState(0); 8 | const [xIsNext, setXIsNext] = useState(true); 9 | 10 | const handleClick = (i) => { 11 | const newHistory = history.slice(0, stepNumber + 1); 12 | const current = newHistory[newHistory.length - 1]; 13 | const squares = current.squares.slice(); 14 | const winnerData = calculateWinner(squares); 15 | if (winnerData || squares[i]) { 16 | return; 17 | } 18 | squares[i] = xIsNext ? 'X' : 'O'; 19 | setHistory(newHistory.concat([{ squares: squares }])); 20 | setStepNumber(newHistory.length); 21 | setXIsNext(!xIsNext); 22 | }; 23 | 24 | const restartGame = () => { 25 | setHistory([{ squares: Array(9).fill(null) }]); 26 | setStepNumber(0); 27 | setXIsNext(true); 28 | }; 29 | 30 | const current = history[stepNumber]; 31 | const winnerData = calculateWinner(current.squares); 32 | const winner = winnerData ? winnerData.winner : null; 33 | const winningLine = winnerData ? winnerData.line : []; 34 | 35 | let status; 36 | if (winner) { 37 | status = 'Winner: ' + winner; 38 | } else { 39 | status = 'Next player: ' + (xIsNext ? 'X' : 'O'); 40 | } 41 | 42 | return ( 43 |
44 |

Tic-Tac-Toe

45 |
46 | handleClick(i)} winningLine={winningLine} /> 47 |
48 |
49 |
{status}
50 | 51 |
52 |
53 | ); 54 | }; 55 | 56 | const calculateWinner = (squares) => { 57 | const lines = [ 58 | [0, 1, 2], 59 | [3, 4, 5], 60 | [6, 7, 8], 61 | [0, 3, 6], 62 | [1, 4, 7], 63 | [2, 5, 8], 64 | [0, 4, 8], 65 | [2, 4, 6], 66 | ]; 67 | for (let i = 0; i < lines.length; i++) { 68 | const [a, b, c] = lines[i]; 69 | if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { 70 | return { winner: squares[a], line: lines[i] }; 71 | } 72 | } 73 | return null; 74 | }; 75 | 76 | export default Game; 77 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | /* src/App.css */ 2 | .App { 3 | display: flex; 4 | flex-direction: column; 5 | justify-content: center; 6 | align-items: center; 7 | min-height: 100vh; 8 | background-image: url("./img/bg.jpg"); 9 | background-position: top; 10 | background-repeat: no-repeat; 11 | background-size: cover; 12 | font-family: 'Arial', sans-serif; 13 | } 14 | 15 | h1 { 16 | font-size: 3.5em; 17 | margin-bottom: 20px; 18 | color: white; 19 | text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5); 20 | } 21 | 22 | .game { 23 | display: flex; 24 | flex-direction: column; 25 | align-items: center; 26 | background-color: rgba(255, 255, 255, 0.3); 27 | padding: 20px; 28 | border-radius: 10px; 29 | box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); 30 | width: 80%; 31 | max-width: 400px; /* Adjust max-width as needed */ 32 | height: auto; 33 | } 34 | 35 | .game-info { 36 | margin-top: 20px; 37 | text-align: center; 38 | } 39 | 40 | .stats { 41 | background-color: #4361EE; /* Purple */ 42 | color: #ffffff; 43 | padding: 10px 20px; 44 | border-radius: 5px; 45 | margin-bottom: 20px; 46 | font-size: 25px; 47 | font-weight: bold; 48 | box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); 49 | } 50 | 51 | .restart-button { 52 | background-color: #b74aff; /* Pink */ 53 | color: white; 54 | border: none; 55 | padding: 10px 20px; 56 | font-size: 20px; 57 | border-radius: 5px; 58 | cursor: pointer; 59 | transition: background-color 0.3s; 60 | } 61 | 62 | .restart-button:hover { 63 | background-color: #9900ff; /* Darker Pink */ 64 | } 65 | 66 | .board-row { 67 | display: flex; 68 | } 69 | 70 | .square { 71 | width: 120px; 72 | height: 120px; 73 | opacity: 0.6; 74 | border: 2px solid #000000; 75 | font-size: 50px; 76 | font-weight: bold; 77 | display: flex; 78 | justify-content: center; 79 | align-items: center; 80 | cursor: pointer; 81 | transition: background-color 0.3s, transform 0.2s; 82 | } 83 | 84 | .square:hover { 85 | background-color: #CFA3EA; /* Blue */ 86 | transform: scale(1.1); 87 | } 88 | 89 | .winning-square { 90 | background-color: #b34ae0; /* Purple */ 91 | color: #ffffff; 92 | } 93 | 94 | @media (max-width: 600px) { 95 | .square { 96 | width: 100px; 97 | height: 100px; 98 | font-size: 35px; 99 | } 100 | 101 | .game { 102 | width: 90%; 103 | padding: 10px; 104 | } 105 | 106 | .stats { 107 | font-size: 16px; 108 | } 109 | 110 | .restart-button { 111 | padding: 8px 16px; 112 | font-size: 14px; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Mridul | Tic-Tac-Toe 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 60 | 61 | 62 | 63 |
64 | 65 | 66 | --------------------------------------------------------------------------------