Skip to content

Instantly share code, notes, and snippets.

@VivienAdnot
Last active April 13, 2018 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VivienAdnot/6e6e863f43dfe0b1c58a9960d33761de to your computer and use it in GitHub Desktop.
Save VivienAdnot/6e6e863f43dfe0b1c58a9960d33761de to your computer and use it in GitHub Desktop.
const buildMap = (bombPositions) => {
let positions = Array.from({length: 10}, (value, x) => {
return Array.from({length: 10}, (value, y) => {
return {
position: {x, y},
value: (isPositionBomb(bombPositions, {x, y})
? 'B'
: getNeighborBombPositions(bombPositions, {x, y}).length
),
visibility: 'hidden' // visible, marked
};
});
});
return positions;
};
class Board extends Component {
constructor(props) {
super(props);
this.bombPositions = buildBombPositions();
this.state = {
boardMap: buildMap(this.bombPositions),
gameStatus: 'playing' //won, lost
};
}
}
onSquareLeftClick = (position) => {
this.setState((prevState) => {
prevState.boardMap[position.x][position.y].visibility = 'visible';
return {
boardMap: prevState.boardMap
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment