diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/App.tsx | 37 | ||||
-rw-r--r-- | src/components/Box.tsx | 41 | ||||
-rw-r--r-- | src/components/Footer.tsx | 32 | ||||
-rw-r--r-- | src/components/Grid.tsx | 94 | ||||
-rw-r--r-- | src/components/ScoreBoard.tsx | 36 | ||||
-rw-r--r-- | src/components/style.css | 93 |
6 files changed, 333 insertions, 0 deletions
diff --git a/src/components/App.tsx b/src/components/App.tsx new file mode 100644 index 0000000..19d1e8c --- /dev/null +++ b/src/components/App.tsx @@ -0,0 +1,37 @@ +/* + * Tic Tac Toe - Minimalistic Tic Tac Toe + * Copyright (C) 2021 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +import React, { useState } from 'react'; +import ScoreBoard from './ScoreBoard'; +import Grid from './Grid'; +import Footer from './Footer'; +import './style.css'; + +const App: React.FC = () => { + const [scoreX, setScoreX] = useState<number>(0); + const [scoreO, setScoreO] = useState<number>(0); + return ( + <> + <ScoreBoard scoreX={scoreX} scoreO={scoreO}/> + <Grid scoreX={scoreX} setScoreX={setScoreX} scoreO={scoreO} setScoreO={setScoreO}/> + <Footer/> + </> + ); +} + +export default App; diff --git a/src/components/Box.tsx b/src/components/Box.tsx new file mode 100644 index 0000000..abe8b66 --- /dev/null +++ b/src/components/Box.tsx @@ -0,0 +1,41 @@ +/* + * Tic Tac Toe - Minimalistic Tic Tac Toe + * Copyright (C) 2021 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +import React from 'react'; +import './style.css'; + +interface Props { + sign: number + index: number + setSign: (index: number) => void +} + +const Box: React.FC<Props> = (props) => { + const handleClick = () => { + props.sign === 2 && + props.setSign(props.index) + } + + return ( + <div className={"Box"} onClick={handleClick}> + {props.sign < 2 ? (props.sign === 1 ? "X" : "O") : ""} + </div> + ); +} + +export default Box; diff --git a/src/components/Footer.tsx b/src/components/Footer.tsx new file mode 100644 index 0000000..3bcaa2a --- /dev/null +++ b/src/components/Footer.tsx @@ -0,0 +1,32 @@ +/* + * Tic Tac Toe - Minimalistic Tic Tac Toe + * Copyright (C) 2021 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +import React from 'react'; +import './style.css'; + +const Footer: React.FC = () => { + return ( + <div className={"Footer"}> + <p><a href="https://github.com/MikunoNaka/tic-tac-toe">Tic Tac Toe</a> Copyright (C) 2021 Vidhu Kant Sharma</p> + <p>This program comes with ABSOLUTELY NO WARRANTY; for details refer to <a href="https://www.gnu.org/licenses/gpl-3.0.en.html">GNU GPL Licence.</a></p> + <p>This is free software, and you are welcome to redistribute it under certain conditions; Refer to <a href="https://www.gnu.org/licenses/gpl-3.0.en.html">GNU GPL Licence</a> for details</p> + </div> + ); +} + +export default Footer; diff --git a/src/components/Grid.tsx b/src/components/Grid.tsx new file mode 100644 index 0000000..9ac881d --- /dev/null +++ b/src/components/Grid.tsx @@ -0,0 +1,94 @@ +/* + * Tic Tac Toe - Minimalistic Tic Tac Toe + * Copyright (C) 2021 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +import React, { useState, useEffect, Dispatch, SetStateAction } from 'react'; +import Box from './Box'; +import './style.css'; + +interface Props { + scoreX: number + scoreO: number + setScoreX: Dispatch<SetStateAction<number>> + setScoreO: Dispatch<SetStateAction<number>> +} + +const Grid: React.FC<Props> = (props) => { + // 0 is O, 1 is X, 2 is blank + const [board, setBoard] = useState<number[]>([2,2,2,2,2,2,2,2,2]); + const [turn, setTurn] = useState<number>(1); + const [winner, setWinner] = useState<number>(2) + + const getBoard = (index: number) => { + setBoard(board.slice(0, index).concat(turn).concat(board.slice(index+1, 9))); + setTurn(turn === 1 ? 0 : 1) + } + + const allEqual = (arr: number[]) => arr.includes(2) ? false : arr.every(v => v === arr[0]) + + const getRow = (r: number) => board.slice(r * 3, (r * 3) + 3); + + const getCol: any = (c: number, arr=board) => arr.length <= 3 ? arr[c] : [arr[c]].concat(getCol(c, arr.slice(3))) + + const getLeftDiagonal: any = (i: number = 0) => + i < 3 ? [getRow(i)[i]].concat(getLeftDiagonal(i+1)) : []; + + const getRightDiagonal: any = (i: number = 2) => + i >= 0 ? [getRow(2-i)[i]].concat(getRightDiagonal(i-1)) : []; + + const checkHorMatch: any = (rowNum: number = 0) => + rowNum <= 2 && (allEqual(getRow(rowNum)) ? setWinner(turn === 1 ? 0 : 1) : checkHorMatch(rowNum+1)) + + const checkVerMatch: any = (colNum: number = 0) => + colNum <= 2 && (allEqual(getCol(colNum)) ? setWinner(turn === 1 ? 0 : 1) : checkVerMatch(colNum+1)) + + const checkDiagonalMatch: any = () => { + (allEqual(getLeftDiagonal()) || allEqual(getRightDiagonal())) && setWinner(turn === 1 ? 0 : 1) + } + + const endGame = (winner: number) => { + const gameWinner = winner < 2 ? (winner === 1 ? "X" : "O") : "Draw"; + alert(`WINNER: ${gameWinner}`) + gameWinner === "Draw" || gameWinner === "X" + ? props.setScoreX(props.scoreX + 1) + : props.setScoreO(props.scoreO + 1) + // clean up + setBoard([2,2,2,2,2,2,2,2,2]); + setWinner(2); + } + + useEffect(() => { + checkHorMatch(); + checkVerMatch(); + checkDiagonalMatch(); + winner < 2 && endGame(winner); + board.includes(2) || endGame(2); + }) + + return ( + <div className="Grid"> + { + board.map( + (i, index) => + <Box key={index} index={index} sign={i} setSign={getBoard}/> + ) + } + </div> + ); +} + +export default Grid; diff --git a/src/components/ScoreBoard.tsx b/src/components/ScoreBoard.tsx new file mode 100644 index 0000000..6d51deb --- /dev/null +++ b/src/components/ScoreBoard.tsx @@ -0,0 +1,36 @@ +/* + * Tic Tac Toe - Minimalistic Tic Tac Toe + * Copyright (C) 2021 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +import React from 'react'; +import './style.css'; + +interface Props { + scoreX: number + scoreO: number +} + +const ScoreBoard: React.FC<Props> = (props) => { + return ( + <div className={"ScoreBoard"}> + <span>X: {props.scoreX}</span> + <span>O: {props.scoreO}</span> + </div> + ); +} + +export default ScoreBoard; diff --git a/src/components/style.css b/src/components/style.css new file mode 100644 index 0000000..4dac3c2 --- /dev/null +++ b/src/components/style.css @@ -0,0 +1,93 @@ +/* + * Tic Tac Toe - Minimalistic Tic Tac Toe + * Copyright (C) 2021 Vidhu Kant Sharma + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +body, #root { + margin: 0px; + height: 100vh; + width: 100vw; + display: flex; + flex-direction: column; + justify-content: space-between; + align-items: center; + background-color: #232627; + color: white; +} + +.ScoreBoard { + display: flex; + justify-content: space-between; + font-size: 3rem; + width: 20rem; + margin-bottom: 0.7rem; + margin-top: 1rem; +} + +.Grid { + display: grid; + grid-template-columns: 9rem 9rem 9rem; + grid-template-rows: 9rem 9rem 9rem; + grid-row-gap: 1.5rem; + grid-column-gap: 1.5rem; +} + +.Box { + border: 1px solid #5b76b7; + display: flex; + justify-content: center; + align-items: center; + height: 100%; + width: 100%; + font-size: 7rem; +} + +.Footer { + width: 100%; + padding: 0.2rem 0; + background-color: #0f0f0f; + display: flex; + flex-direction: column; + align-items: center; + text-align: center; +} + +.Footer p { + color: #a75cb8; + margin: 0.2rem; + width: 90%; +} + +.Footer a { + color: white; +} + +@media only screen and (max-width: 600px) { + .Grid { + grid-template-columns: 6rem 6rem 6rem; + grid-template-rows: 6rem 6rem 6rem; + grid-row-gap: 1.5rem; + grid-column-gap: 1.5rem; + } + .Box { + font-size: 6rem; + } + .ScoreBoard { + font-size: 3.5rem; + width: 16rem; + padding-top: 1rem; + } +} |