|
| 1 | +import React from 'react/addons'; |
| 2 | + |
| 3 | +import Api from '../api/api.js'; |
| 4 | +/** |
| 5 | + * Returns a random integer between min (inclusive) and max (inclusive) |
| 6 | + * Using Math.round() will give you a non-uniform distribution! |
| 7 | + */ |
| 8 | +function getRandomInt(min, max) { |
| 9 | + return Math.floor(Math.random() * (max - min + 1)) + min; |
| 10 | +} |
| 11 | + |
| 12 | +class Block extends React.Component { |
| 13 | + constructor(props) { |
| 14 | + super(props); |
| 15 | + this.handleClick = this.handleClick.bind(this); |
| 16 | + } |
| 17 | + |
| 18 | + render() { |
| 19 | + var cx = React.addons.classSet; |
| 20 | + var classes = cx({ |
| 21 | + 'block': true, |
| 22 | + 'block__visited': this.props.status === 0 || this.props.status === 9, |
| 23 | + 'block__dead': this.props.status === -2 |
| 24 | + }); |
| 25 | + |
| 26 | + return( |
| 27 | + <td className={classes} onClick={this.handleClick}></td> |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + handleClick() { |
| 32 | + var msg = "data:open:[idx=" + this.props.index + "]:[help=9]"; |
| 33 | + Api.send(msg); |
| 34 | + } |
| 35 | +}; |
| 36 | +Block.propTypes = { status: React.PropTypes.number, index: React.PropTypes.number }; |
| 37 | +Block.defaultProps = { status: 0, index: 0 }; |
| 38 | + |
| 39 | +class Blocks extends React.Component { |
| 40 | + constructor(props) { |
| 41 | + super(props); |
| 42 | + } |
| 43 | + |
| 44 | + render() { |
| 45 | + var nodes = []; |
| 46 | + var size = this.props.size; |
| 47 | + |
| 48 | + /* construct blocks */ |
| 49 | + for (var i = 0; i < size; i++) { |
| 50 | + var _blocks = []; |
| 51 | + |
| 52 | + for (var j = 0; j < size; j++) { |
| 53 | + var index = i * size + j; |
| 54 | + _blocks.push(<Block key={'block-' + index} status={this.props.mines[index]} index={index} />); |
| 55 | + } |
| 56 | + |
| 57 | + nodes.push((<tr key={'blocks-' + i}>{_blocks}</tr>)); |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <table className="blocks"> |
| 62 | + <tbody> |
| 63 | + {nodes} |
| 64 | + </tbody> |
| 65 | + </table> |
| 66 | + ); |
| 67 | + } |
| 68 | +}; |
| 69 | +Blocks.propTypes = { size: React.PropTypes.number, mines: React.PropTypes.array }; |
| 70 | +Blocks.defaultProps = { size: 7, mines: [] } |
| 71 | + |
| 72 | +export default Blocks; |
0 commit comments