React 三连棋后续完善

    xiaoxiao2022-07-03  199

    React 三连棋后续完善

    React官方文档入门教程中的三连棋,根据文档提供改进建议完善游戏功能。

    改进功能:

    在游戏历史记录列表显示每一步棋的坐标,格式为 (列号, 行号)。在历史记录列表中加粗显示当前选择的项目。使用两个循环来渲染出棋盘的格子,而不是在代码里写死(hardcode)。添加一个可以升序或降序显示历史记录的按钮。每当有人获胜时,高亮显示连成一线的 3 颗棋子。当无人获胜时,显示一个平局的消息。

    直接贴上代码:

    index.js

    import React from 'react' import ReactDOM from 'react-dom' import './index.css' class Square extends React.Component { render() { return ( <button className="square" onClick={() => this.props.onClick()}> {this.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((itemx,x) => ( <div className="board-row" key={x}> { Array(3).fill(null).map((itemy,y) => ( this.renderSquare(3 * x + y) )) } </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 { winnerName: squares[a], winIndex: [a, b, c] } } } return null; } class Game extends React.Component { constructor(props) { super(props); this.state = { history: [{ squares: Array(9).fill(null) }], xIsNext: true, stepNumber: 0, 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(); if (calculateWinner(squares) || squares[i]) { return; } squares[i] = this.state.xIsNext ? 'X' : 'O'; this.setState({ history: history.concat([{ squares: squares, lastIndex: i }]), xIsNext: !this.state.xIsNext, stepNumber: history.length }); } jumpTo(step) { for (let i = 0; i < 9; i++) { document.getElementsByClassName('square')[i].style = ""; } this.setState({ stepNumber: step, xIsNext: (step % 2) === 0 }) } order() { this.setState({ sort: !this.state.sort }) } 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 + ' 最后落棋点(列号,行号):(' + parseInt(step.lastIndex / 3) + ', ' + step.lastIndex % 3 + ')': 'Go to game start'; return ( <li key={move}> <button onClick={() => this.jumpTo(move)} className={move === this.state.stepNumber ? 'bold' : ''}>{desc}</button> </li> ) }); let status; if (winner) { status = 'Winner: ' + winner.winnerName; for (let i of winner.winIndex) { document.getElementsByClassName('square')[i].style = "background: lightblue; color: #fff;"; } } else { if (this.state.history.length > 9) { status = 'No player win! It ends in a draw!'; } else { status = 'Next player: ' + (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.order()}> {this.state.sort ? '倒序' : '正序'} </button> <ol>{this.state.sort ? moves : moves.reverse()}</ol> </div> </div> ); } } ReactDOM.render( <Game />, document.getElementById('root') );

    index.css

    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; } button.bold { font-weight: bold; }

    最终效果:

    最新回复(0)