Basic React Babel ES6 Fiddle
by Ved prakash
HTML
<script src="https://fb.me/react-dom-0.14.3.js"></script>
<div id="container">
</div>
CSS
#game-layout {
width: 360px;
height: 245px;
margin: 60px auto 0 auto;
background-color: whitesmoke;
padding: 15px 5px 218px 10px;
border-radius: 5px;
border-bottom: 5px solid #42729B;
}
.move-index{
background-color: grey;
}
td {
height: 40px;
border-bottom: 3px solid grey;
border-radius: 10px;
text-align: center;
line-height: 40px;
cursor: pointer;
background-color: #ecf0f1;
color: #2c3e50;
margin: 4px;
}
td {
float: left;
width: 75px;
}
td:hover{
font-size: 28px;
}
.replay-btn {
margin-top:40px
}
Babel + JSX
class Game extends React.Component {
constructor() {
super();
this.state = {
nextMovePosition: [2, 2],
gameNumbers: this.initialData(),
totalClicks: 0
};
this.maxBound = this.state.gameNumbers.length - 1; // boundary condition
this.minBound = 0; // boundary condition
this.isValidMoveIndex = false;
}
//Initail gameNumbers value
initialData() {
return [
[2, 1, 6, 11],
[9, 13, 5, 8],
[4, 3, "", 15],
[10, 14, 12, 7]
]
};
checkRowShift(row, col) {
let {nextMovePosition, gameNumbers, totalClicks} = this.state;
const i = nextMovePosition[0];
const j = nextMovePosition[1];
//Horizontal row shift
if (row === i) {
nextMovePosition = [row, col];
gameNumbers[i].splice(j, 1);
gameNumbers[row].splice(col, 0, "");
this.setState({gameNumbers: gameNumbers, nextMovePosition: nextMovePosition, totalClicks: totalClicks + 1});
}
//Vertical row shift
else if (col === j) {
let tempArrForSwap = [];
nextMovePosition = [row, col];
let index = 0;
while (index < gameNumbers.length) {
tempArrForSwap[index] = gameNumbers[index][col];
index++;
}
if (row < i) {
tempArrForSwap.splice(row, 0, '');
tempArrForSwap.splice(i + 1, 1);
} else {
tempArrForSwap.splice(row + 1, 0, '');
tempArrForSwap.splice(i, 1);
}
index = 0;
while (index < gameNumbers.length) {
gameNumbers[index][col] = tempArrForSwap[index];
index++;
}
this.setState({gameNumbers: gameNumbers, nextMovePosition: nextMovePosition, totalClicks: totalClicks + 1});
}
...