grid.js 4.0 KB

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