gameLogic.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. var BLOCK_DIM = 3;
  5. var BLOCKS_PER_SIDE = 3;
  6. var DIM = BLOCKS_PER_SIDE * BLOCK_DIM;
  7. var numbers; // The main array for the numbers on the Sudoku game board
  8. var numbersCopy; // A working copy of the main array
  9. var randOrder; // An array for helping in the randomization
  10. var solutionFound = 0; // A helper variable for checking of the unique solution
  11. /* Tries to set the given number to the given index of the main array.
  12. * Returns 1 if it was possible, 0 otherwise. If it wasn't possible
  13. * this function will start the collision animations.
  14. */
  15. function setNumberByUser(index, value)
  16. {
  17. var row = Math.floor(index/DIM);
  18. var column = index%DIM;
  19. var rowCollision = testRow(index, row, value);
  20. var columnCollision = testColumn(index, column, value);
  21. var blockCollision = testBlock(index, row, column, value);
  22. if (numbers[index] || value)
  23. moves++;
  24. if (!rowCollision && !columnCollision && !blockCollision) {
  25. if (!numbers[index] && value)
  26. empties--;
  27. if (numbers[index] && !value)
  28. empties++;
  29. numbers[index] = value;
  30. return 1;
  31. }
  32. else {
  33. var indices = new Array();
  34. if (rowCollision)
  35. indices.push(rowCollision-1);
  36. if (columnCollision)
  37. indices.push(columnCollision-1);
  38. if (blockCollision)
  39. indices.push(blockCollision-1);
  40. startCollisionAnimations(indices);
  41. return 0;
  42. }
  43. }
  44. /* Tries to set the given number to the given index.
  45. * Returns 1 if it was possible, 0 otherwise.
  46. *
  47. * @param useCopy, tells whether we use the main array or a copy of it
  48. */
  49. function setNumber(index, value, useCopy)
  50. {
  51. var row = Math.floor(index/DIM);
  52. var column = index%DIM;
  53. if (!testRow(index, row, value, useCopy) &&
  54. !testColumn(index, column, value, useCopy) &&
  55. !testBlock(index, row, column, value, useCopy)) {
  56. if (useCopy)
  57. numbersCopy[index] = value;
  58. else
  59. numbers[index] = value;
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. /*
  65. * Tests if there already is this number in the same row.
  66. * If there is, this returns the numbers index plus one and
  67. * 0 otherwise.
  68. *
  69. * @param useCopy, tells whether we use the main array or a copy of it
  70. */
  71. function testRow(index, row, value, useCopy)
  72. {
  73. var min = row*DIM;
  74. var max = row*DIM + DIM;
  75. for (var i = min; i < max; i++)
  76. if (useCopy) {
  77. if (numbersCopy[i])
  78. if (i != index && numbersCopy[i] == value)
  79. return i+1;
  80. }
  81. else {
  82. if (numbers[i])
  83. if (i != index && numbers[i] == value)
  84. return i+1;
  85. }
  86. return 0;
  87. }
  88. /*
  89. * Tests if there already is this number in the same column.
  90. * If there is, this returns the numbers index plus one and
  91. * 0 otherwise.
  92. *
  93. * @param useCopy, tells whether we use the main array or a copy of it
  94. */
  95. function testColumn(index, column, value, useCopy)
  96. {
  97. var max = DIM*DIM;
  98. for (var i = column; i <= max; i += DIM)
  99. if (useCopy) {
  100. if (numbersCopy[i])
  101. if (i != index && numbersCopy[i] == value)
  102. return i+1;
  103. }
  104. else {
  105. if (numbers[i])
  106. if (i != index && numbers[i] == value)
  107. return i+1;
  108. }
  109. return 0;
  110. }
  111. /*
  112. * Tests if there already is this number in the same block.
  113. * If there is, this returns the numbers index plus one and
  114. * 0 otherwise.
  115. *
  116. * @param useCopy, tells whether we use the main array or a copy of it
  117. */
  118. function testBlock(index, row, column, value, useCopy)
  119. {
  120. var blockRow = Math.floor(row/BLOCK_DIM);
  121. var blockColumn = Math.floor(column/BLOCK_DIM);
  122. var min = blockRow*BLOCK_DIM*DIM + blockColumn*BLOCK_DIM;
  123. var max = min+BLOCK_DIM;
  124. var idx;
  125. for (var x = min; x < max; x++)
  126. for (var y = 0; y < 3; y++) {
  127. idx = x + y*DIM;
  128. if (useCopy) {
  129. if (numbersCopy[idx])
  130. if (idx != index && numbersCopy[idx] == value)
  131. return idx+1;
  132. }
  133. else {
  134. if (numbers[idx])
  135. if (idx != index && numbers[idx] == value)
  136. return idx+1;
  137. }
  138. }
  139. return 0;
  140. }
  141. /*
  142. * Creates an array with numbers 1-9 in it, ordered randomly.
  143. */
  144. function fillRandOrder()
  145. {
  146. randOrder = new Array();
  147. var isSet = 0;
  148. var rand;
  149. for (var i = 0; i < DIM; i++) {
  150. while (!isSet) {
  151. rand = Math.floor(Math.random()*DIM)+1;
  152. for (var j = 0; j < DIM; j++)
  153. if (randOrder[j] && (rand == randOrder[j]))
  154. break;
  155. if (j == DIM) {
  156. randOrder[i] = rand;
  157. isSet = 1;
  158. }
  159. }
  160. isSet = 0;
  161. }
  162. }
  163. /* Generates a random full sudoku board by using recursive
  164. * backtracking method.
  165. */
  166. function fillCell(index)
  167. {
  168. if (index == DIM*DIM)
  169. return 1;
  170. for (var i = 0; i < 9; i++) {
  171. if (setNumber(index, randOrder[i], 0) && fillCell(index+1))
  172. return 1;
  173. numbers[index] = 0;
  174. }
  175. return 0;
  176. }
  177. /* Checks whether the puzzle has an unique solution.
  178. * Returns 0 if it does, 1 otherwise. The algorithm is mainly
  179. * the same as is fillCell().
  180. */
  181. function checkUniqueness(index)
  182. {
  183. if (index == DIM*DIM) {
  184. if (solutionFound) {
  185. solutionFound = 0;
  186. return 1;
  187. }
  188. solutionFound = 1;
  189. return 0;
  190. }
  191. if (numbersCopy[index]) {
  192. if (checkUniqueness(index+1))
  193. return 1;
  194. }
  195. else
  196. for (var i = 1; i <= 9; i++)
  197. if (setNumber(index, i, 1)) {
  198. if (checkUniqueness(index+1))
  199. return 1;
  200. numbersCopy[index] = 0;
  201. }
  202. return 0;
  203. }
  204. /* Removes numbers from the board until a desired puzzle is obtained.
  205. * Checks after every deletion that the puzzle still has an unique solution.
  206. */
  207. function removeCells()
  208. {
  209. var temp;
  210. var rand;
  211. while (empties < 45) {
  212. rand = Math.floor(Math.random()*DIM*DIM);
  213. temp = numbers[rand];
  214. if (temp) {
  215. numbers[rand] = 0;
  216. numbersCopy = numbers.slice(0);
  217. if (checkUniqueness(0))
  218. numbers[rand] = temp;
  219. else
  220. empties++;
  221. }
  222. }
  223. }
  224. /*
  225. * Generates a new Sudoku puzzle.
  226. */
  227. function generatePuzzle()
  228. {
  229. numbers = new Array();
  230. fillRandOrder();
  231. fillCell(0);
  232. empties = 0;
  233. removeCells();
  234. }
  235. /*
  236. * This enables the puzzle generating in another thread.
  237. */
  238. WorkerScript.onMessage = function(message) {
  239. generatePuzzle(message.rands);
  240. WorkerScript.sendMessage( {board: numbers, boardEmpties: empties} );
  241. }