main.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import Immutable from 'immutable';
  4. const Cell = React.createClass({
  5. render() {
  6. let classname = this.props.occupied ? "cell occupied" : "cell";
  7. return <div className={classname} style={{top:this.props.y, left:this.props.x}}></div>;
  8. },
  9. });
  10. const Board = React.createClass({
  11. //render() {
  12. // let nh = this.props.settings.height || 10,
  13. // nw = this.props.settings.width || 10,
  14. // ot = this.props.settings.offsetTop || 0,
  15. // ol = this.props.settings.offsetLeft || 0,
  16. // cells = [];
  17. // for( let i = 0; i < nh; i++ ) {
  18. // for( let j = 0; j < nw; j++ ) {
  19. // let x = (((j % nw) * 10) + ol) + 'px',
  20. // y = ((i * 10) + ot) + 'px';
  21. // cells.push( <Cell x={x} y={y} /> );
  22. // }
  23. // }
  24. // return (
  25. // <div>
  26. // {cells}
  27. // </div>
  28. // );
  29. //},
  30. render() {
  31. let ot = this.props.settings.offsetTop || 0,
  32. ol = this.props.settings.offsetLeft || 0;
  33. let cells = this.props.world.flatMap(function (row, y) {
  34. return row.map(function eachCell(cell, x) {
  35. let xc = ((x * 10) + ol) + 'px',
  36. yc = ((y * 10) + ot) + 'px',
  37. occu = ( !(x%2) && !(y%2) );
  38. console.log('occupied?', x, y, occu);
  39. return <Cell x={xc} y={yc} occupied={occu} />;
  40. });
  41. }).toJS();
  42. return <div>
  43. {cells}
  44. </div>;
  45. },
  46. });
  47. let settings = {
  48. offsetTop:40,
  49. offsetLeft:10,
  50. width:50,
  51. height:20
  52. };
  53. function generateWorld(w = 10, h = 10) {
  54. let grid = [];
  55. for( let i = 0; i < h; i++ ) {
  56. let row = [];
  57. for( let j = 0; j < w; j++ ) {
  58. row.push(0);
  59. }
  60. grid.push(row);
  61. }
  62. return Immutable.fromJS(grid);
  63. }
  64. let world = generateWorld(100, 20);
  65. ReactDOM.render(
  66. <Board settings={settings} world={world} />,
  67. document.getElementById('board')
  68. );