grid.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /** @type {Array.<Array.<number>>|null} */
  10. this.word = null;
  11. }
  12. /** @const */
  13. Definition.RIGHT_HORIZONTAL = 1;
  14. /** @const */
  15. Definition.RIGHT_VERTICAL = 2;
  16. /** @const */
  17. Definition.BOTTOM_HORIZONTAL = 3;
  18. /** @const */
  19. Definition.BOTTOM_VERTICAL = 4;
  20. /**
  21. * @constructor
  22. **/
  23. function Cell() {
  24. /** @type {boolean} */
  25. this.isBlack = false;
  26. /** @type {Array.<Definition>} */
  27. this.definitions = null;
  28. }
  29. Cell.prototype.update = function(data) {
  30. if (data["type"] === null) {
  31. this.isBlack = true;
  32. } else if (data["definitions"] !== undefined) {
  33. this.definitions = [];
  34. data["definitions"].forEach(function(definition) {
  35. this.definitions.push(new Definition(definition));
  36. }.bind(this));
  37. } else {
  38. // Grid letter
  39. // TODO return version
  40. }
  41. return 0;
  42. };
  43. /**
  44. * @constructor
  45. **/
  46. function Grid(data) {
  47. /** @type {string} */
  48. this.title = data["title"] || "";
  49. /** @type {number} */
  50. this.difficulty = data["difficulty"];
  51. /** @type {number} */
  52. this.width = data["w"];
  53. /** @type {number} */
  54. this.height = data["h"];
  55. this.words = [];
  56. this.grid = [];
  57. for (var i =0; i < this.width; i++) {
  58. this.grid[i] = [];
  59. for (var j =0; j < this.height; j++) {
  60. this.grid[i][j] = new Cell();
  61. }
  62. }
  63. }
  64. Grid.prototype.computeWord = function(x, y, dx, dy) {
  65. if (!this.grid[x +dx] || !this.grid[x +dx][y +dy] || this.grid[x +dx][y +dy].definitions || this.grid[x +dx][y +dy].isBlack) {
  66. return [[x, y]];
  67. }
  68. var word = this.computeWord(x +dx, y +dy, dx, dy);
  69. word.unshift([x, y]);
  70. return word;
  71. };
  72. Grid.prototype.getWord = function(x, y) {
  73. var words = [];
  74. this.words.forEach(function(word) {
  75. for (var i =0, nbLetters =word.length; i < nbLetters; i++) {
  76. if (word[i][0] == x && word[i][1] == y) {
  77. words.push(word);
  78. break;
  79. }
  80. }
  81. });
  82. return words;
  83. };
  84. /**
  85. * @return {number|null}
  86. **/
  87. Grid.prototype.update = function(data) {
  88. var maxVersion = null
  89. ,grid = this.grid
  90. ,topologyUpdated = false;
  91. data.forEach(function(cellData) {
  92. var updateResult = grid[cellData["x"]][cellData["y"]].update(cellData);
  93. maxVersion = Math.max(maxVersion || 0, updateResult);
  94. if (updateResult === 0) {
  95. topologyUpdated = true;
  96. }
  97. });
  98. if (topologyUpdated) {
  99. var words = [];
  100. for (var i =0; i < this.width; i++) {
  101. for (var j =0; j < this.height; j++) {
  102. if (grid[i][j].definitions) {
  103. grid[i][j].definitions.forEach(function(definition) {
  104. var word;
  105. switch (definition.direction) {
  106. case Definition.RIGHT_VERTICAL:
  107. word = this.computeWord(i +1, j, 0, 1);
  108. break;
  109. case Definition.RIGHT_HORIZONTAL:
  110. word = this.computeWord(i +1, j, 1, 0);
  111. break;
  112. case Definition.BOTTOM_VERTICAL:
  113. word = this.computeWord(i, j +1, 0, 1);
  114. break;
  115. case Definition.BOTTOM_HORIZONTAL:
  116. word = this.computeWord(i, j +1, 1, 0);
  117. break;
  118. }
  119. words.push(word);
  120. definition.word = word;
  121. }.bind(this));
  122. }
  123. }
  124. }
  125. this.words = words;
  126. }
  127. return maxVersion;
  128. };