前言
最近学习微前端,正好接触到了React.js,索性就系统完整地学习一遍。和当初学习Vue.js相同,先将官网的教程看一遍,在结合项目完整地做一个系统出来,从而达到融会贯通地目的。
三连棋完整功能代码
大家看了React.js的官网教程的话,就会发现和其他教程不同,React.js第一节课就是教会我们如何写一个游戏——三连棋,目的是让我接触和感受React.js中的组件、状态等一些概念,同时不失趣味性。官网已经有了绝大部分的游戏代码,但是也留下了几个小任务让我们去自主完成,从而检验我们的学习情况。话不多说,上完整代码: index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
function Square(props) {
return (
<button className={'square' + (props.isActive?' active':'') + (props.highlight?' highlight':'')} onClick={props.onClick}>
{props.value}
</button>
)
}
class Board extends React.Component {
renderSquare(i) {
return (
<Square
highlight={this.props.highlight[0] === i || this.props.highlight[1] === i || this.props.highlight[2] === i}
key={i.toString()}
isActive={this.props.isActive === i}
value={this.props.squares[i]}
onClick={()=>this.props.onClick(i)}
/>
)
}
render() {
let boardRow = []
for(let i = 0;i < 3;i++){
let square = []
for(let j = 0;j < 3;j++){
square.push(this.renderSquare(i*3 + j))
}
boardRow.push(
<div key={i} className="board-row">{square}</div>
)
}
return (
<div>
{boardRow}
</div>
);
}
}
class Game extends React.Component {
constructor(props){
super(props)
this.state = {
history:[{
squares:Array(9).fill(null),
position:null,
}],
stepNumber:0,
xIsNext:true,
acs: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,
position:i
}]),
stepNumber:history.length,
xIsNext: !this.state.xIsNext,
});
}
jumpTo(step){
this.setState({
stepNumber: step,
xIsNext: (step % 2) === 0,
})
}
changeSort(acs){
this.setState({
acs:!acs
})
}
render() {
const history = this.state.history
const current = history[this.state.stepNumber]
const winnerInfo = calculateWinner(current.squares)
let acs = this.state.acs
let sort = []
sort.push(
<button key={acs} onClick={()=>{this.changeSort(acs)}}>{acs?'升序':'降序'}</button>
)
const moves = history.map((step,move)=>{
if(!acs){
move = history.length - 1 - move
}
const desc = move ? 'Go to move #' + move : 'Go to game start'
let position = history[move].position
position = calculatePosition(position)
const positionDesc = move ? `position: (${position[0]},${position[1]})` : ''
return (
<li key={move}>
<button onClick={()=>{this.jumpTo(move)}}>{desc}</button>
<span>{positionDesc}</span>
</li>
)
})
let isActive = current.position
let info = [null,null,null]
let status
if(winnerInfo){
status = 'Winner: ' + winnerInfo.winner
info = winnerInfo.info
}
else{
if(this.state.stepNumber === 9){
status = 'Draw!'
}
else{
status = 'Next player: ' + (this.state.xIsNext?'X':'O');
}
}
return (
<div className="game">
<div className="game-board">
<Board
highlight={info}
isActive={isActive}
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{status}</div>
<div>{sort}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
}
ReactDOM.render(
<Game />,
document.getElementById('root')
);
function calculatePosition(index) {
const positions = [
[1,1],[1,2],[1,3],
[2,1],[2,2],[2,3],
[3,1],[3,2],[3,3],
]
return positions[index]
}
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 {
winner: squares[a],
info: lines[i]
}
}
}
return null;
}
index.css:
body {
font: 14px 'Century Gothic', Futura, sans-serif;
margin: 20px;
}
ol,
ul {
padding-left: 30px;
}
.game-info button {
background: aqua;
padding: 10px;
border: 0;
margin: 10px;
}
.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;
}
.active {
border: 3px solid #000;
line-height: 27px;
}
.highlight {
background: aqua;
}
.kbd-navigation .square:focus {
background: #ddd;
}
.game {
display: flex;
flex-direction: row;
}
.game-info {
margin-left: 20px;
}
建议大家在学习后自己完成一遍这几个任务,同时也可以参考对照我的文章,相互学习进步。如果能够对我的代码提些宝贵的意见的话,那我必将不胜荣幸。
|