https://ko.react.dev/learn/tutorial-tic-tac-toe
https://ko.react.dev/learn/tutorial-tic-tac-toe - 리액트 공식 사이트에. 틱택톡 예제 - 여기에 추가 예제도 있음. 그것도 가능하면 도전해볼 것.
https://nextjs.org/learn/react-foundations → 이건 넥스트 js 예제. 이건 좀 더 어려워. 이것도 해볼 것.
import { useState } from 'react';
function Square({value, onSquareClick}) {
// const [value, setValue] = useState(null);
// function handleClick() {
// const nextSquares = squares.slice();
// nextSquares[0] = "X";
// setSquares(nextSquares);
//setValue('X');
//console.log('clicked!');
return (
<button
className="square"
// onclick={handleClick}
onClick={onSquareClick}
>
{value}
</button>
);
}
// export default function Board() {
//const [xIsNext, setXisNext] = useState(true);
// const [squares, setSquares] = useState(Array(9).fill(null));
function Board({ xIsNext, squares, onPlay}) {
function handleClick(i) {
if (calculateWinner(squares) || squares[i]) {
return;
}
const nextSquares = squares.slice();
if (xIsNext) {
nextSquares[i] = "X";
} else {
nextSquares[i] = "O";
}
onPlay(nextSquares);
//setSquares(nextSquares);
// setXisNext(!xIsNext);
}
const winner = calculateWinner(squares);
let status;
if (winner) {
status = "Winner: " + winner;
} else {
status = "Next player: " + (xIsNext ? "X" : "O");
}
return (
<>
<div className="status">{status}</div>
<div className="board-row">
<Square value={squares[0]} onSquareClick={() => handleClick(0)} />
<Square value={squares[1]} onSquareClick={() => handleClick(1)}/>
<Square value={squares[2]} onSquareClick={() => handleClick(2)}/>
</div>
<div className="board-row">
<Square value={squares[3]} onSquareClick={() => handleClick(3)}/>
<Square value={squares[4]} onSquareClick={() => handleClick(4)}/>
<Square value={squares[5]} onSquareClick={() => handleClick(5)}/>
</div>
<div className="board-row">
<Square value={squares[6]} onSquareClick={() => handleClick(6)}/>
<Square value={squares[7]} onSquareClick={() => handleClick(7)}/>
<Square value={squares[8]} onSquareClick={() => handleClick(8)}/>
</div>
</>
);
}
export default function Game() {
// const [xIsNext, setXIsNext] = useState(true);
const [history, setHistory] = useState([Array(9).fill(null)]);
const [currentMove, setCurrentMove] = useState(0);
// const currentSquares = history[history.length - 1];
const xIsNext = currentMove % 2 === 0;
const currentSquares = history[currentMove];
function handlePlay(nextSquares) {
const nextHistory = [...history.slice(0, currentMove + 1), nextSquares];
setHistory(nextHistory);
setCurrentMove(nextHistory.length - 1);
// setXIsNext(!xIsNext);
}
function jumpTo(nextMove) {
// TODO
setCurrentMove(nextMove);
// setXIsNext(nextMove % 2 === 0);
}
const moves = history.map((squares, move) => {
let description;
if (move > 0) {
description = 'Go to move #' + move;
} else {
description = 'Go to game start';
}
return (
<li key={move}>
<button onClick={() => jumpTo(move)}>{description}</button>
</li>
);
}); // 여기 부분에 왜 가로에 오류가 안 없어지지???
return (
<div className="game">
<div className="game-board">
<Board xIsNext={xIsNext} squares={currentSquares} onPlay={handlePlay} />
</div>
<div className="game-info">
<ol>{/*TODO*/}</ol>
</div>
</div>
);
}
function calculateWinner(squares) {
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 [a, b, c] = lines[i];
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
return squares[a];
}
}
return null;
}
제공해주신 코드는 리액트(React)를 사용하여 틱택토(Tic-Tac-Toe) 게임을 구현한 것으로 보입니다. 문의하신 주요 개념들과 코드의 나머지 부분에 대해 자세히 설명해 드릴게요.
제공해주신 틱택토 코드는 모던 리액트 개발의 핵심 원칙을 잘 보여줍니다.
useState (Hook)useState는 함수형 컴포넌트에서 **상태(state)**를 관리하게 해주는 리액트 훅(Hook)입니다. 상태는 시간에 따라 변하고, 이 변화가 발생할 때마다 컴포넌트가 다시 렌더링(re-render)되어 사용자 인터페이스(UI)가 업데이트됩니다.const [value, setValue] = useState(initialValue);
value: 현재 상태 값입니다.setValue: 상태를 업데이트하는 함수입니다. 이 함수를 호출해야만 리액트가 컴포넌트를 다시 렌더링합니다.initialValue: 상태의 초기 값입니다.