카운트 a
카운트 b
버튼 각각 누르고 Count를 클릭하면 1씩 증가하는 코드 만들기.
import React, {useState} from 'react';
import Board3 from './Board3';
import calculateWinner from './calculateWinner';
import '../App.css';
function Game4 (props) {
const [history, setHistory] = useState([{
squares : Array(9).fill(null)}]);
const [stepNumber, setStepnumber] = useState(0);
const [xIsNext, setXIsnext] = useState(true);
const boardSize = 6;
// let aPlayer = 0;
// let bPlayer = 0;
let aPlayer = 0;
let bPlayer = 0;
let [PB, setPB] = useState('')
let [countA, setCountA] = useState(0)
let [countB, setCountB] = useState(0)
const PlusButton = () => {
if (PB === 'A') {
setCountA(countA += 1)
} else {
setCountB(countB += 1)
}
}
const handleClick = (i) => {
const historycopy = history.slice(0, stepNumber + 1);
const current = historycopy[historycopy.length - 1];
const squares = current.squares.slice();
if (xIsNext) {
aPlayer += 1;
} else {
bPlayer += 1;
}
if (calculateWinner(squares, boardSize) || squares[i]) {
return;
}
squares[i] = xIsNext ? "X" : "O";
setHistory(historycopy.concat([{squares: squares}]));
setXIsnext(!xIsNext);
setStepnumber(history.length);
}
const jumpTo = (step) => {
setStepnumber(step);
setXIsnext((step % 2) === 0);
}
const current = history[stepNumber];
const winner = calculateWinner(current.squares, boardSize);
const count = 9-history.length
const moves = history.map((step, move) => {
const desc = move ?
'Go to move #' + move :
'Go to game start';
return (
<li key={move}>
<button onClick={() => jumpTo(move)}>{desc}</button>
</li>
);
});
let status;
if (winner) {
status = 'Winner' + winner;
} else {
status = 'Next player: ' + (xIsNext ? 'X' : 'O');
}
return (
<div className="game">
<div className="game-board">
<Board3
squares={current.squares}
onClick={i => handleClick(i)}
size={boardSize}
count = {count}
// countsA={countA}
// countsB={countB}
/>
</div>
<div className="game-info">
<div>{status}</div>
<ol>{moves}</ol>
</div>
<div className='game-score'>
<table>
<thead>
<tr><td>X</td><td>O</td></tr>
</thead>
<tbody>
<tr><td>{aPlayer}</td><td>{bPlayer}</td></tr>
</tbody>
</table>
</div>
<div>
<div>
<button onClick={()=>setPB(PB = 'A')}>A Plus</button>
<button onClick={()=>setPB(PB = 'B')}>B Plus</button>
<div></div>
<button onClick={()=>PlusButton()}>Count</button>
<button onClick={()=>PlusButton()}>Count</button>
<h1>A: {countA} B: {countB}</h1>
</div>
</div>
</div>
);
}
export default Game4;
'Framework > react' 카테고리의 다른 글
react css 글씨체 두께 다르게 적용하기. 글씨체 두껍게 하기. (0) | 2025.03.14 |
---|---|
[리액트 네이버 레이아웃 잡기] Target container is not a DOM element. at exports.createRoot 에러 해결 (0) | 2025.02.06 |
[리액트]클래스형 컴포넌트를 함수형 컴포넌트로 변환하기. (0) | 2024.06.20 |
[리액트] 클래스형 컴포넌트 함수형 컴포넌트로 변환하기. Assignment to constant variable. (0) | 2024.06.20 |
[리액트 틱택토 게임 응용하기] part 1_2 말의 갯수 8개로 제한하기 (0) | 2024.06.17 |