2016-10-01 21:49:15 +02:00
|
|
|
import React from 'react';
|
|
|
|
|
import Grid from './grid';
|
|
|
|
|
import GridField from './grid-field';
|
|
|
|
|
import UserControl from '../user/user-control';
|
|
|
|
|
|
|
|
|
|
class GridControl extends React.Component {
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
|
|
|
|
|
var grid = new Grid();
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
grid: grid.state.grid,
|
2016-10-12 22:18:10 +02:00
|
|
|
updatedFieldCache: [],
|
2016-10-11 22:11:21 +02:00
|
|
|
lastClicked: {
|
|
|
|
|
red: null,
|
|
|
|
|
blue: null
|
|
|
|
|
}
|
2016-10-01 21:49:15 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
refString(row, col) {
|
|
|
|
|
return 'gridField_' + row + '_' + col;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkMine(field, i, j) {
|
|
|
|
|
return typeof field[i] !== 'undefined' && typeof field[i][j] !== 'undefined' && field[i][j] !== 'm';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkNeighbourItem(row, col) {
|
|
|
|
|
if (this.checkMine(this.state.grid, row, col)) {
|
|
|
|
|
var currentField = this.refs[this.refString(row, col)];
|
|
|
|
|
|
|
|
|
|
currentField.setState({
|
2016-10-11 22:11:21 +02:00
|
|
|
currentImage: this.state.grid[row][col],
|
2016-10-01 21:49:15 +02:00
|
|
|
currentObj: this.state.grid[row][col],
|
|
|
|
|
active: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
2016-10-12 22:18:10 +02:00
|
|
|
* It must be cached because the GridField.state not updated until
|
|
|
|
|
* all showAppropriateFields() method runned out!!
|
2016-10-01 21:49:15 +02:00
|
|
|
*/
|
2016-10-12 22:18:10 +02:00
|
|
|
if (this.state.updatedFieldCache.indexOf(this.refString(row, col)) < 0 && !currentField.state.active) {
|
|
|
|
|
this.state.updatedFieldCache.push(this.refString(row, col));
|
|
|
|
|
|
|
|
|
|
if (this.state.grid[row][col] === 0) {
|
|
|
|
|
return {
|
|
|
|
|
row: row,
|
|
|
|
|
col: col
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-10-01 21:49:15 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-10-12 22:18:10 +02:00
|
|
|
|
2016-10-01 21:49:15 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkNeighbours(row, col) {
|
|
|
|
|
var anotherFields = [];
|
|
|
|
|
|
|
|
|
|
anotherFields.push(this.checkNeighbourItem(row - 1, col));
|
|
|
|
|
anotherFields.push(this.checkNeighbourItem(row - 1, col - 1));
|
|
|
|
|
anotherFields.push(this.checkNeighbourItem(row - 1, col + 1));
|
|
|
|
|
anotherFields.push(this.checkNeighbourItem(row, col - 1));
|
|
|
|
|
anotherFields.push(this.checkNeighbourItem(row, col + 1));
|
|
|
|
|
anotherFields.push(this.checkNeighbourItem(row + 1, col));
|
|
|
|
|
anotherFields.push(this.checkNeighbourItem(row + 1, col + 1));
|
|
|
|
|
anotherFields.push(this.checkNeighbourItem(row + 1, col - 1));
|
|
|
|
|
|
|
|
|
|
return anotherFields;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showAppropriateFields(currentField, row, col) {
|
|
|
|
|
currentField.setState({
|
|
|
|
|
currentObj: this.state.grid[row][col],
|
|
|
|
|
active: true
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-12 22:18:10 +02:00
|
|
|
if (this.state.updatedFieldCache.indexOf(this.refString(row, col)) < 0) {
|
|
|
|
|
this.state.updatedFieldCache.push(this.refString(row, col));
|
2016-10-01 21:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.state.grid[row][col] === 0) {
|
|
|
|
|
var neighbours = this.checkNeighbours(row, col);
|
|
|
|
|
|
|
|
|
|
neighbours
|
|
|
|
|
.filter((i) => {
|
|
|
|
|
return i !== false;
|
|
|
|
|
})
|
|
|
|
|
.forEach((element, index, array) => {
|
|
|
|
|
var currentField = this.refs[this.refString(element.row, element.col)];
|
|
|
|
|
this.showAppropriateFields(currentField, element.row, element.col);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Player control method
|
|
|
|
|
* @param currentObject
|
2016-10-11 22:11:21 +02:00
|
|
|
* @param x
|
|
|
|
|
* @param y
|
2016-10-01 21:49:15 +02:00
|
|
|
*/
|
2016-10-11 22:11:21 +02:00
|
|
|
handleGridField(currentObject, x, y) {
|
2016-10-01 21:49:15 +02:00
|
|
|
var userControl = this.refs.userControl,
|
2016-10-11 22:11:21 +02:00
|
|
|
gridFieldControl = this.refs[this.refString(x, y)],
|
2016-10-01 21:49:15 +02:00
|
|
|
activePlayer = userControl.state.activePlayer ? 'blue' : 'red';
|
|
|
|
|
|
2016-10-11 22:11:21 +02:00
|
|
|
/** update last cliked grid field */
|
|
|
|
|
if (this.state.lastClicked[activePlayer] !== null) {
|
|
|
|
|
this.refs[this.refString(this.state.lastClicked[activePlayer][0], this.state.lastClicked[activePlayer][1])].setState({
|
|
|
|
|
lastClickedRed: false,
|
|
|
|
|
lastClickedBlue: false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!gridFieldControl.state.active) {
|
|
|
|
|
this.state.lastClicked[activePlayer] = [x, y];
|
|
|
|
|
|
|
|
|
|
/** If you found mine */
|
|
|
|
|
if (currentObject === 'm') {
|
|
|
|
|
userControl.setState({
|
|
|
|
|
mines: userControl.state.mines - 1
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
userControl.refs[activePlayer].setState({
|
|
|
|
|
mines: userControl.refs[activePlayer].state.mines + 1
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.state.lastClicked[activePlayer] = [x, y];
|
|
|
|
|
|
|
|
|
|
gridFieldControl.setState({
|
|
|
|
|
currentImage: gridFieldControl.state.icons.root + gridFieldControl.state.icons.flag[activePlayer]
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
userControl.state.activePlayer = userControl.state.activePlayer ? 0 : 1;
|
|
|
|
|
|
|
|
|
|
/** It is a number */
|
|
|
|
|
if (!isNaN(currentObject)) {
|
|
|
|
|
gridFieldControl.setState({
|
|
|
|
|
currentImage: currentObject
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** set-up last clicked */
|
|
|
|
|
gridFieldControl.setState({
|
|
|
|
|
lastClickedRed: activePlayer === 'red',
|
|
|
|
|
lastClickedBlue: activePlayer === 'blue'
|
2016-10-01 21:49:15 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Most important event!!
|
|
|
|
|
* @param coords
|
|
|
|
|
*/
|
|
|
|
|
onClick(coords) {
|
|
|
|
|
var currentField = this.refs[this.refString(coords[0], coords[1])];
|
|
|
|
|
this.showAppropriateFields(currentField, coords[0], coords[1]);
|
2016-10-11 22:11:21 +02:00
|
|
|
this.handleGridField(this.state.grid[coords[0]][coords[1]], coords[0], coords[1]);
|
2016-10-01 21:49:15 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-12 22:18:10 +02:00
|
|
|
renderGridFields() {
|
2016-10-01 21:49:15 +02:00
|
|
|
var grid = [];
|
|
|
|
|
|
|
|
|
|
for (var i = 0, j = this.state.grid.length; i < j; i++) {
|
|
|
|
|
for (var k = 0, l = this.state.grid[i].length; k < l; k++) {
|
|
|
|
|
grid.push(
|
|
|
|
|
<GridField row={i}
|
|
|
|
|
col={k}
|
|
|
|
|
ref={this.refString(i, k)}
|
|
|
|
|
key={i + k * Math.random() * 0.5}
|
|
|
|
|
onClick={this.onClick.bind(this, [i, k])}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return grid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
return (
|
|
|
|
|
<div className="game-wrapper">
|
|
|
|
|
<div className="users">
|
|
|
|
|
<UserControl ref="userControl" blue="Olcsó János" red="Eszet Lenke"/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="grid">
|
2016-10-12 22:18:10 +02:00
|
|
|
{this.renderGridFields()}
|
2016-10-01 21:49:15 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default GridControl;
|