react学习之完善官网游戏教程
1. 在游戏历史记录列表显示每一步棋的坐标,格式为 (列号, 行号)。
2.在历史记录列表中加粗显示当前选择的项目。
3.使用两个循环来渲染出棋盘的格子,而不是在代码里写死(hardcode)。
4.添加一个可以升序或降序显示历史记录的按钮。
5.每当有人获胜时,高亮显示连成一线的 3 颗棋子。
6.当无人获胜时,显示一个平局的消息。
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
function Square(props) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}
class Board extends React.Component {
renderSquare(i) {
return (
<Square
key={i}
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}
render() {
return (
<div>
{/* 使用两个循环来渲染出棋盘的格子 */}
{Array(3)
.fill(null)
.map((item, x) => (
<div className="board-row" key={x}>
{Array(3)
.fill(null)
.map((item, y) => this.renderSquare(3 * x + y))}
</div>
))}
</div>
);
}
}
class Game extends React.Component {
constructor(props) {
super(props);
this.state = {
history: [
{
squares: Array(9).fill(null),
},
],
stepNumber: 0,
xIsNext: true,
sort: true, //默认顺序
};
}
handleClick(i) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[history.length - 1];
const squares = current.squares.slice();
// 定义一个坐标数组
const coordinates = [
"(1,1)",
"(2,1)",
"(3,1)",
"(1,2)",
"(2,2)",
"(3,2)",
"(1,3)",
"(2,3)",
"(3,3)",
];
if (calculateWinner(squares) || squares[i]) {
return;
}
squares[i] = this.state.xIsNext ? "X" : "O";
this.setState({
history: history.concat([{ squares, coordinate: coordinates[i] }]), //在历史记录里添加每一步棋的坐标
stepNumber: history.length,
xIsNext: !this.state.xIsNext,
});
}
jumpTo(step) {
for(let i = 0; i < 9; i++){
document.querySelectorAll('.square')[i].style = '' //每次点击清空上一次的样式
}
this.setState({
stepNumber: step,
xIsNext: step % 2 === 0,
});
}
render() {
const history = this.state.history;
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
const moves = history.map((step, move) => {
const desc = move
? "Go to move #" + move + ",坐标:" + step.coordinate //渲染当前坐标,在游戏历史记录列表显示每一步棋的坐标,格式为 (列号, 行号)
: "Go to game start";
return (
<li key={move}>
<button
onClick={() => this.jumpTo(move)}
style={{
fontWeight: move === this.state.stepNumber ? "900" : "600", //在历史记录列表中加粗显示当前选择的项目
}}
>
{desc}
</button>
</li>
);
});
let status;
if (winner) {
status = "Winner" + winner.name;
winner.index.map(item=>document.querySelectorAll('.square')[item].style = "background:black;color: white;") // 每当有人获胜时,高亮显示连成一线的 3 颗棋子
} else if(history.length > 9){
status = 'Draw' // 当无人获胜时,显示一个平局的消息
}else {
status = "Next playe: " + (this.state.xIsNext ? "X" : "O");
}
return (
<div className="game">
<div className="game-board">
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
{/* 添加升序或降序显示历史记录的按钮 */}
<button onClick={() => this.setState({ sort: !this.state.sort })}>
{this.state.sort ? "倒序" : "正序"}
</button>
<ol>{this.state.sort ? moves : moves.reverse()}</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 {
name:squares[a],
index:[a,b,c]
};
}
}
return null;
}
// ========================================
ReactDOM.render(<Game />, document.getElementById("root"));
线上地址