grid.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @constructor
  3. **/
  4. function Definition(data) {
  5. /** @type {Array.<string>} */
  6. this.text = data["text"];
  7. /** @type {number} */
  8. this.direction = data["pos"];
  9. }
  10. /** @const */
  11. Definition.RIGHT_HORIZONTAL = 1;
  12. /** @const */
  13. Definition.RIGHT_VERTICAL = 2;
  14. /** @const */
  15. Definition.BOTTOM_HORIZONTAL = 3;
  16. /** @const */
  17. Definition.BOTTOM_VERTICAL = 4;
  18. /**
  19. * @constructor
  20. **/
  21. function Cell() {
  22. /** @type {boolean} */
  23. this.isBlack = false;
  24. /** @type {Array.<Definition>} */
  25. this.definitions = null;
  26. }
  27. Cell.prototype.update = function(data) {
  28. if (data["type"] === null) {
  29. this.isBlack = true;
  30. } else if (data["definitions"] !== undefined) {
  31. this.definitions = [];
  32. data["definitions"].forEach(function(definition) {
  33. this.definitions.push(new Definition(definition));
  34. }.bind(this));
  35. } else {
  36. // Grid letter
  37. // TODO return version
  38. }
  39. return 0;
  40. };
  41. /**
  42. * @constructor
  43. **/
  44. function Grid(data) {
  45. /** @type {string} */
  46. this.title = data["title"] || "";
  47. /** @type {number} */
  48. this.difficulty = data["difficulty"];
  49. /** @type {number} */
  50. this.width = data["w"];
  51. /** @type {number} */
  52. this.height = data["h"];
  53. this.grid = [];
  54. for (var i =0; i < this.width; i++) {
  55. this.grid[i] = [];
  56. for (var j =0; j < this.height; j++) {
  57. this.grid[i][j] = new Cell();
  58. }
  59. }
  60. }
  61. /**
  62. * @return {number|null}
  63. **/
  64. Grid.prototype.update = function(data) {
  65. var maxVersion = null
  66. ,grid = this.grid;
  67. data.forEach(function(cellData) {
  68. maxVersion = Math.max(maxVersion || 0, grid[cellData["x"]][cellData["y"]].update(cellData));
  69. });
  70. return maxVersion;
  71. };