Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. // holds the state of your board and calculates the view layer
  2. class Board extends PureComponent {
  3. constructor( props ) {
  4. super( props );
  5.  
  6. // create pieces for however you want the game to start
  7. this.pieces = {
  8. p1: new piece( "Lumberjack" ),
  9. p2: new piece( "Lumberjack" ),
  10. p3: new piece( "Lumberjack" ),
  11. p4: new piece( "Tree" ),
  12. p5: new piece( "Tree" ),
  13. p6: new piece( "Tree" )
  14. }
  15.  
  16. // flatten for easy and speed of access
  17. const { p1, p2, p3, p4, p5, p6 } = this.pieces;
  18.  
  19. // initial state, this is what gets updated by this.setState method
  20. // to update the rendered view
  21. this.state ={
  22. squares: [
  23. [ p1, p2, p3 ],
  24. [ null, null, null ],
  25. [ p4, p5, p6 ]
  26. ]
  27. };
  28. }
  29.  
  30. // render method that produces the view
  31. render() {
  32. const { squares } = this.state;
  33. const iLength = squares.length;
  34. let i, j jLength, components = [ ];
  35.  
  36. // generate the Squares from this.state data
  37. for ( i = 0; i < iLength; i++ ) {
  38. jLength = squares[ i ].length;
  39. for ( j = 0; jLength; j++ ) {
  40. components.push( <Square piece={ squares[ i ][ j ] } /> );
  41. }
  42. }
  43.  
  44. // render the Squares containing the pieces
  45. return (
  46. <div className="board">
  47. { components }
  48. </div>
  49. );
  50. }
  51. }
  52.  
  53. // piece constructor
  54. function piece( name ) {
  55. this.name = name;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement