From e87497332196dce646e0df206f7eded14f776024 Mon Sep 17 00:00:00 2001 From: juknow <124476542+juknow@users.noreply.github.com> Date: Mon, 3 Apr 2023 21:49:19 +0900 Subject: [PATCH] react assignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit devkor 4회차 리액트 스터디 과제 --- src/index.css | 50 +++++++++++++++++++++++++ src/index.js | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 src/index.css create mode 100644 src/index.js diff --git a/src/index.css b/src/index.css new file mode 100644 index 0000000..58c1a5d --- /dev/null +++ b/src/index.css @@ -0,0 +1,50 @@ +body { + font: 14px "Century Gothic", Futura, sans-serif; + margin: 20px; + } + + ol, ul { + padding-left: 30px; + } + + .board-row:after { + clear: both; + content: ""; + display: table; + } + + .status { + margin-bottom: 10px; + } + + .square { + background: #fff; + border: 1px solid #999; + float: left; + font-size: 24px; + font-weight: bold; + line-height: 34px; + height: 34px; + margin-right: -1px; + margin-top: -1px; + padding: 0; + text-align: center; + width: 34px; + } + + .square:focus { + outline: none; + } + + .kbd-navigation .square:focus { + background: #ddd; + } + + .game { + display: flex; + flex-direction: row; + } + + .game-info { + margin-left: 20px; + } \ No newline at end of file diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..bac6569 --- /dev/null +++ b/src/index.js @@ -0,0 +1,102 @@ +import React, { useState } from 'react'; +import ReactDOM from 'react-dom/client'; +import './index.css'; + +const Square = ({value,onClick}) =>{ + return ( + + ); +} + +const Board = () => { + const hasWinner = () => { + const lines = [ + [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 < lines.length; ++i) { + const [f, s, t] = lines[i]; + if(squares[f] != '' && squares[f] == squares[s] && squares[f] == squares[t]) return squares[f] + } + return false; + } + + const [squares, setSquares] = useState(Array(9).fill('')) + const [next, setNext] = useState('X'); + const handleClick = (i) => { + if(hasWinner() || squares[i] != '') return; + const sq = squares.slice() + sq[i] = next; + if(next === 'X') setNext('O') + else setNext('X') + + setSquares(sq) + } + + const renderSquare = (i) => { + return {handleClick(i)}}/>; + } + let status = 'Next player: ' + next; + if(hasWinner()) { + status = 'Winner: ' + hasWinner(); + } else if (squares.every((square) => square !== '')) { + status = 'Draw'; + } + + const handleRestart = () => { + setSquares(Array(9).fill('')); + setNext('X'); + } + + + return ( +
+
{status}
+
+ {renderSquare(0)} + {renderSquare(1)} + {renderSquare(2)} +
+
+ {renderSquare(3)} + {renderSquare(4)} + {renderSquare(5)} +
+
+ {renderSquare(6)} + {renderSquare(7)} + {renderSquare(8)} +
+ {hasWinner() || squares.every((square) => square !== '') ? ( + + ) : null} +
+ ); +} + +const Game = () => { + return ( +
+
+ +
+
+
{/* status */}
+
    {/* TODO */}
+
+
+ ); +} + +// ======================================== + +const root = ReactDOM.createRoot(document.getElementById("root")); +root.render(); \ No newline at end of file