Advertisement
Guest User

Untitled

a guest
May 30th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. /*
  2. 0
  3. 1 1
  4. 1 1 1
  5. 1 1 1 1
  6. 1 1 1 1 1
  7.  
  8. |
  9. v
  10.  
  11. 1
  12. 0 0
  13. 0 0 0
  14. 0 0 0 0
  15. 0 0 0 0 0
  16.  
  17.  
  18. xx.
  19. xxx
  20. .xx
  21.  
  22. */
  23. function countBits (n) {
  24. n -= (n >> 1) & 0x55555555;
  25. n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
  26. return ((n + (n >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
  27. }
  28.  
  29. function Field(size) {
  30. var n = size*(size+1)/2;
  31. this.size = size;
  32. this.data = (1 << n) - 1;
  33. this.set(0, 0, 0);
  34. this.length = n - 1;
  35. }
  36. Field.prototype = {
  37. _toBit: function(x, y) {
  38. if (x < 0) return -1; // throw new RangeError("x should be non-negative");
  39. if (y < 0) return -1; // throw new RangeError("y should be non-negative");
  40. if (x >= this.size) return -1; // throw new RangeError("x should be less than size");
  41. if (y >= this.size) return -1; // throw new RangeError("y should be less than size");
  42. if (x > y) return -1; // throw new RangeError("position should be within triangle");
  43. return y*(y+1)/2 + x;
  44. },
  45. get: function (x, y) {
  46. var bit = this._toBit(x, y);
  47. if (bit === -1) return -1;
  48. var mask = 1 << bit;
  49. return +!!(this.data & mask);
  50. },
  51. set: function (x, y, v) {
  52. var mask = 1 << this._toBit(x, y);
  53. if (v) {
  54. this.data |= mask;
  55. } else {
  56. this.data &= ~mask;
  57. }
  58. },
  59. store: function () {
  60. return this.data;
  61. },
  62. load: function (data) {
  63. this.data = data;
  64. this.length = countBits(data);
  65. },
  66. check: function (x, y, dx, dy) {
  67. return (
  68. this.get(x+dx, y+dy) === 1 &&
  69. this.get(x+dx*2, y+dy*2) === 0
  70. );
  71. },
  72. move: function (x, y, dx, dy) {
  73. this.set(x, y, 0);
  74. this.set(x+dx, y+dy, 0);
  75. this.set(x+dx*2, y+dy*2, 1);
  76. this.length--;
  77. return true;
  78. },
  79. tryMove: function (x, y, dx, dy) {
  80. return this.check.apply(this, arguments) &&
  81. this.move.apply(this, arguments);
  82. },
  83. toString: function () {
  84. var x, y, i, s, a = [];
  85. for (y = 0; y < this.size; y++) {
  86. s = "";
  87. for (x = this.size; x >= y; x--) s += " ";
  88. for (x = 0; x <= y; x++) s += this.get(x, y) + " ";
  89. a.push(s);
  90. }
  91. return a.join("\n");
  92. }
  93. };
  94.  
  95.  
  96. var F = new Field(5),
  97. d = [[-1,0],[-1,-1],[0,-1],[1,0],[1,1],[0,1]];
  98. (function r (field) {
  99. var x, y, i, moved = false;
  100. F.load(field);
  101. if (F.length === 1 && F.get(0, 0) === 1) return true;
  102. for (y = 0; y < F.size; y++) {
  103. for (x = 0; x <= y; x++) {
  104. if (F.get(x, y) !== 1) continue;
  105. for (i = 0; i < 6; i++) {
  106. if (!F.tryMove(x, y, d[i][0], d[i][1])) continue;
  107. moved = true;
  108. if (r(F.store())) {
  109. F.load(field);
  110. console.log(F + '');
  111. return true;
  112. }
  113. F.load(field);
  114. }}}
  115. if (!moved) return false; // no more moved;
  116. }(F.store()));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement