-
-
Notifications
You must be signed in to change notification settings - Fork 934
Tic Tac Toe Game #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Tic Tac Toe Game #208
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e798e9f
changes
tejinder-sharma d2bf8ae
final changes
tejinder-sharma 378b208
removed unwanted file
tejinder-sharma df1a7df
cover image changed
tejinder-sharma f46c69b
Final changes
tejinder-sharma 9bc7cc4
Made winning squares of same color
tejinder-sharma 19ba6b3
Used flex on button too, beacuse mobile view is not great without flex.
tejinder-sharma 6091094
Merge branch 'main' into main
tejinder-sharma 36f431c
Resoved CSS conflicts
tejinder-sharma e7da554
Merge branch 'main' of https://github.com/tejinder-sharma/react-play
tejinder-sharma 8295aa6
Updated readme
tejinder-sharma 967d45c
Made code more readable by using destructuring.
tejinder-sharma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Tic Tac Toe Game | ||
|
|
||
| It is a Tic Tac Toe Game to made with React. | ||
|
|
||
|
|
||
| ## What will you learn? | ||
|
|
||
| - Functional Components | ||
| - Event handling for user interaction. | ||
| - Update of states. | ||
| - How to reuse the components.(e.g we reused our game square button many times). | ||
| - How to reset the states. | ||
|
|
||
|
|
||
| The file `TicTacToeGAme` is the main file along with the css. | ||
|
|
||
| If you want to implement your learnings about states and useEffect hooks try implementing it on your own! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { getPlayById } from 'meta/play-meta-util'; | ||
|
|
||
| import PlayHeader from 'common/playlists/PlayHeader'; | ||
|
|
||
| import React from "react"; | ||
|
|
||
| import Game from "./components/Game.jsx" | ||
|
|
||
| import "./styles/tic-tac-toe-tj.css"; | ||
|
|
||
| function TicTacToeGame(props) { | ||
| // Do not remove the below lines. | ||
| // The following code is to fetch the current play from the URL | ||
| const { id } = props; | ||
| const play = getPlayById(id); | ||
|
|
||
| // Your Code Start below. | ||
| return ( | ||
| <> | ||
| <div className="play-details"> | ||
tejinder-sharma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <PlayHeader play={play} /> | ||
| <div className="play-details-body tic-tac-toe-tj"> | ||
| {/* Your Code Starts Here */} | ||
| <Game /> | ||
| {/* Your Code Ends Here */} | ||
| </div> | ||
| </div> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default TicTacToeGame; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import React, { useState } from "react"; | ||
|
|
||
| function Square(props) { | ||
| return ( | ||
| <button className={`squareButton ${props.winningSquare?.includes(props.index) && "bg-yellow-500"}`} onClick={props.onClick}> | ||
| {props.value} | ||
| </button> | ||
| ); | ||
| } | ||
| function Restart({ onClick }) { | ||
| return ( | ||
| <button className="jumpButton" onClick={onClick}> | ||
| Play again | ||
| </button> | ||
| ); | ||
| } | ||
|
|
||
| function Game() { | ||
| const [squares, setSquares] = useState(Array(9).fill(null)); | ||
| const [isXNext, setIsXNext] = useState(true); | ||
|
|
||
| const nextSymbol = isXNext ? "X" : "O"; | ||
|
|
||
| const winner = calculateWinner(squares); | ||
|
|
||
|
|
||
| function renderSquare(i) { | ||
| return ( | ||
| <Square | ||
| key={i} | ||
| index={i} | ||
| winningSquare={winner ? winner.line : null} | ||
| value={squares[i]} | ||
| onClick={() => { | ||
| const nextSquares = squares.slice(); | ||
| if (squares[i] != null || winner != null) { | ||
| return; | ||
| } | ||
| nextSquares[i] = nextSymbol; | ||
| setSquares(nextSquares); | ||
| setIsXNext(!isXNext); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
| function getStatus() { | ||
| if (winner) { | ||
| return `Winner: ${winner.player}`; | ||
| } else if (isBoardFull(squares)) { | ||
| return "Draw!"; | ||
| } else { | ||
| return `Next player: ${nextSymbol}`; | ||
| } | ||
| } | ||
| function renderRestartButton() { | ||
| return ( | ||
| <Restart | ||
| onClick={() => { | ||
| setSquares(Array(9).fill(null)); | ||
| setIsXNext(true); | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
| function renderSquares(n) { | ||
| let squares = []; | ||
| for (let i = n; i < n + 3; i++) { | ||
| squares.push(renderSquare(i)); | ||
| } | ||
| return squares; | ||
| } | ||
|
|
||
| function renderRow(i) { | ||
| return <div className="row ">{renderSquares(i)}</div>; | ||
| } | ||
|
|
||
| return ( | ||
| <div className="container"> | ||
| <header className="header">Tic Tac Toe Game</header> | ||
| <div className="boardMain"> | ||
| {renderRow(0)} | ||
| {renderRow(3)} | ||
| {renderRow(6)} | ||
| </div> | ||
| <div className="status">{getStatus()}</div> | ||
| <div>{renderRestartButton()}</div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function calculateWinner(squares) { | ||
| const possibleLines = [ | ||
| [0, 1, 2], | ||
| [3, 4, 5], | ||
| [6, 7, 8], | ||
| [0, 3, 6], | ||
| [1, 4, 7], | ||
| [2, 5, 8], | ||
| [0, 4, 8], | ||
| [2, 4, 6], | ||
| ]; | ||
| for (let i = 0; i < possibleLines.length; i++) { | ||
| const [a, b, c] = possibleLines[i]; | ||
| if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { | ||
| return {player:squares[a], line:[a, b, c]}; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function isBoardFull(squares) { | ||
| for (let i = 0; i < squares.length; i++) { | ||
| if (squares[i] == null) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| export default Game; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| .tic-tac-toe-tj .container{ | ||
| text-align: center; | ||
| } | ||
|
|
||
|
|
||
| .tic-tac-toe-tj .squareButton{ | ||
| width: 5rem; | ||
| height: 5rem; | ||
| margin: 1px; | ||
| background-color: #EC4899; | ||
|
|
||
| display: inline-flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
|
|
||
| border: none; | ||
| font: bold; | ||
| font-size: 32px; | ||
| color: white; | ||
| } | ||
| .tic-tac-toe-tj .squareButton:hover{ | ||
| background-color: #10B981; | ||
| } | ||
| .tic-tac-toe-tj .row{ | ||
| display: flex; | ||
| } | ||
| .tic-tac-toe-tj .jumpButton{ | ||
| padding: 0.5rem 1rem; | ||
| background-color: #10B981; | ||
|
|
||
| display: inline-flex; | ||
| justify-content: center; | ||
| align-items: center; | ||
|
|
||
| border: none; | ||
| color: white; | ||
| font-size: 18px; | ||
| border-radius: 4px; | ||
| margin-top: 1rem; | ||
| } | ||
| .tic-tac-toe-tj .bg-yellow-500{ | ||
| background-color: #f59e0b; | ||
| } | ||
| .tic-tac-toe-tj .jumpButton:hover{ | ||
| background-color: #EC4899; | ||
| } | ||
| .tic-tac-toe-tj .list{ | ||
| margin-top: 1rem; | ||
| } | ||
| .tic-tac-toe-tj .status{ | ||
| margin-top: 1rem; | ||
| font-size: 24px; | ||
| } | ||
| .tic-tac-toe-tj .header{ | ||
| width: 100%; | ||
| padding: 16px 8px; | ||
| color: white; | ||
| font-size: 24px; | ||
|
|
||
| display: grid; | ||
| place-content: center; | ||
|
|
||
| background-color: #010326; | ||
| margin-bottom: 2rem; | ||
| } | ||
|
|
||
| .tic-tac-toe-tj .boardMain{ | ||
| display: grid; | ||
| place-content: center; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.