import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
function Square(props) {
return (
<button
className={props.lt ? "square lightsq" : "square"}
onClick={props.onClick}
>
{props.value}
</button>
);
}
class Board extends React.Component {
renderSquare() {
const square = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
];
return square.map((sq, index) => {
return (
<div className="board-row" key={index}>
{sq.map((i) => {
let flag = -1;
if (this.props.ltArr) {
flag = this.props.ltArr.indexOf(i);
}
return (
<Square
lt={flag === -1 ? "" : "233"}
key={i}
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
})}
</div>
);
});
}
render() {
return <div>{this.renderSquare()}</div>;
}
}
class Game extends React.Component {
ltArr;
constructor(props) {
super(props);
this.state = {
history: [
{
step: 0,
squares: Array(9).fill(null),
axis: null,
},
],
historyMove: [
{
step: 0,
squares: Array(9).fill(null),
axis: null,
},
],
stepNumber: 0,
xIsNext: true,
order: true,
ltArr: null,
};
}
handleClick(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
if (this.calculateWinner(squares)) {
return;
}
if (squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? "X" : "O";
this.setState({
history: history.concat([
{
step: this.state.stepNumber + 1,
squares: squares,
axis: getAxis(i),
},
]),
historyMove: history.concat([
{
step: this.state.stepNumber + 1,
squares: squares,
axis: getAxis(i),
},
]),
stepNumber: history.length,
xIsNext: !this.state.xIsNext,
});
}
jumpTo(move) {
this.setState({
stepNumber: move,
xIsNext: move % 2 === 0,
});
}
reverseHistory() {
const history = this.state.historyMove.slice();
this.setState({
historyMove: history.reverse().slice(),
order: !this.state.order,
});
}
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]
) {
this.ltArr = [a, b, c];
return squares[a];
}
}
this.ltArr = null;
return null;
}
render() {
const history = this.state.historyMove;
const current = this.state.history[this.state.stepNumber];
const winner = this.calculateWinner(current.squares);
const moves = history.map((square, move) => {
const desc = square.step
? "回到第" + square.step + "步, 落子坐标:" + square.axis
: "回到起点";
return (
<li key={move}>
<button
className={square.step === this.state.stepNumber ? "jumpto" : ""}
onClick={() => this.jumpTo(square.step)}
>
{desc}
</button>
</li>
);
});
let status;
if (winner) {
status = winner + "赢了";
} else {
status = "玩家标记: " + (this.state.xIsNext ? "X" : "O");
}
return (
<div className="game">
<div className="game-board">
<Board
ltArr={this.ltArr}
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>
<button
onClick={() => {
this.reverseHistory();
}}
>
{this.state.order ? "升序" : "降序"}
</button>
</div>
<div>{this.state.stepNumber === 9 ? "平局" : status}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
}
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<Game />);
function getAxis(index) {
const addes = [
[0, 0],
[0, 1],
[0, 2],
[1, 0],
[1, 1],
[1, 2],
[2, 0],
[2, 1],
[2, 2],
];
return addes[index];
}