Ну типа вот, дальше допиливайте сами:
class Game extends React.Component {
state = {
panesCurrent: [],
panesDefault: [ 1, 1, 1, 0, -1, -1, -1 ],
panesWin: [ -1, -1, -1, 0, 1, 1, 1 ],
}
componentDidMount() {
this.reset();
}
componentDidUpdate() {
if (this.state.panesCurrent.every((n, i) => n === this.state.panesWin[i])) {
setTimeout(alert, 25, 'WIN');
}
}
onClick(index) {
const clicked = this.state.panesCurrent[index];
if (clicked === 0) {
return;
}
for (let i = 1; i <= 2; i++) {
const t = index + clicked * i;
if (this.state.panesCurrent[t] === 0) {
const panes = [...this.state.panesCurrent];
[ panes[index], panes[t] ] = [ panes[t], panes[index] ];
this.setState({ panesCurrent: panes });
break;
}
}
}
reset = () => {
this.setState(({ panesDefault }) => ({
panesCurrent: [...panesDefault],
}));
}
render() {
return (
<div>
<button onClick={this.reset}>reset</button>
<div className="game">
{this.state.panesCurrent.map((n, i) => (
<div
className={'pane ' + [ 'left', '', 'right' ][n + 1]}
onClick={() => this.onClick(i)}
></div>
))}
</div>
</div>
)
}
}
.game {
font-size: 5px;
}
.pane {
display: inline-block;
width: 10em;
height: 10em;
margin: 1em;
}
.pane.right::after,
.pane.left::after {
position: relative;
display: inline-flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
font-family: monospace;
font-size: 4em;
}
.pane.right::after {
content: "-->";
color: red;
border: 2px solid red;
}
.pane.left::after {
content: "<--";
color: lime;
border: 2px solid lime;
}