Advertisement
Guest User

Untitled

a guest
May 29th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. var playTable = new Array(), row = 0, col = 0, rowPosit, colPosit, playElementValue = 0, sorted = 0, rP, cP;
  2.  
  3. //creates a 2dimensional array, filled wit zero-s; OK
  4. function createPlayField() {
  5. for (row = 0; row < 4; row++) {
  6. playTable[row] = new Array();
  7. for (col = 0; col < 4; col++) {
  8. playTable[row][col] = 0;
  9. }
  10. }
  11. }
  12.  
  13. //Generates the 2dimensional table; OK
  14. function generateTable() {
  15. for ( var elementCount = 0 ; elementCount < 16 ; elementCount++) {
  16.  
  17. console.log("Count: ", elementCount + 1);
  18.  
  19. //generates the random position and checks if there is a number there already
  20. do {
  21. rowPosit = Math.floor((Math.random() * 4) + 1) - 1;
  22. colPosit = Math.floor((Math.random() * 4) + 1) - 1;
  23. } while (playTable[rowPosit][colPosit] !== 0);
  24.  
  25. console.log("R:", rowPosit, "C:", colPosit);
  26. //do {
  27. // playElementValue = Math.floor((Math.random() * 4) + 1);
  28. //} while (checkRowCol() === -1);
  29.  
  30. playElementValue = Math.floor((Math.random() * 4) + 1);
  31. playTable[rowPosit][colPosit] = playElementValue;
  32.  
  33. console.log("check in the end of the cycle. num:", playTable[rowPosit][colPosit]);
  34.  
  35. }
  36. }
  37.  
  38. //checks the 2x2 square for matching number;
  39. function checkSquare() {
  40. //checks the square of the number
  41. if( rowPosit <= 1 && colPosit <= 1) {
  42. rP = rowPosit, cP = colPosit;
  43. if( rP === 1 && cP === 1) {
  44. rP--, cP--;
  45. }
  46.  
  47. } else if( rowPosit <= 1 && colPosit === 3 || colPosit === 2 ) {
  48. rP = rowPosit, cP = colPosit;
  49. if( rowPosit === 1 || colPosit === 3) {
  50. rP--, cP--;
  51. }
  52.  
  53. } else if( rowPosit === 3 || rowPosit === 2 && colPosit <= 1 ) {
  54. rP = rowPosit, cP = colPosit;
  55. if( rowPosit === 3 && colPosit === 1) {
  56. rP--, cP--;
  57. }
  58.  
  59. } else if( rowPosit === 3 || rowPosit === 2 && colPosit === 3 || colPosit === 2 ) {
  60. rP = rowPosit, cP = colPosit;
  61. if( rowPosit === 3 && colPosit === 3) {
  62. rP--, cP--;
  63. }
  64. }
  65. console.log(rP, cP);
  66. console.log("kube before the cicle");
  67. var colHelper;
  68. for ( var rowHelper = rP ; rowHelper <= rP ; rowHelper++) {
  69. for ( colHelper = cP ; colHelper <= cP ; colHelper++) {
  70. console.log(playTable[rowHelper][colHelper])
  71. if (playTable[rowHelper][colHelper] === playElementValue
  72. && rowHelper !== rowPosit
  73. && colHelper !== colPosit) {
  74. console.log("SQUARE check");
  75. sorted = 1;
  76. return -1;
  77. }
  78. }
  79. }
  80. }
  81.  
  82. //checks the cols and rowsfor matching numbers
  83. function checkRowCol() {
  84. for( var R = 0 ; R < 4 ; R++ ) {
  85. if( playTable[R][colPosit] === playElementValue && R !== rowPosit) {
  86. console.log("COL check");
  87. return -1;
  88. }
  89. }
  90. for( var C = 0 ; C < 4 ; C++ ) {
  91. if( playTable[rowPosit][C] === playElementValue && C !== colPosit) {
  92. console.log("ROW check");
  93. return -1;
  94. }
  95. }
  96. }
  97.  
  98. //system check function OK
  99. function printthegoddamnvalues() {
  100. var k = 0;
  101. for (var i = 0; i < 4; i++) {
  102. for (k = 0; k < 4; k++) {
  103. console.log(playTable[i][k]);
  104. console.log(typeof(playTable[i][k]));
  105. }
  106. }
  107. }
  108.  
  109. //alters the html table OK
  110. function returnValues() {
  111. var elementValue = 0;
  112.  
  113. for (var id = 0, inputRow = 0, inputCol = 0; id < 4; id++, inputCol++) {
  114. elementValue = playTable[inputRow][inputCol];
  115. document.getElementById(id.toString()).innerHTML = elementValue;
  116. }
  117.  
  118. for (id = 4, inputRow = 1, inputCol = 0; id < 8; id++, inputCol++) {
  119. elementValue = playTable[inputRow][inputCol];
  120. document.getElementById(id.toString()).innerHTML = elementValue;
  121. }
  122.  
  123. for (id = 8, inputRow = 2, inputCol = 0; id < 12; id++, inputCol++) {
  124. elementValue = playTable[inputRow][inputCol];
  125. document.getElementById(id.toString()).innerHTML = elementValue;
  126. }
  127.  
  128. for (id = 12, inputRow = 3, inputCol = 0; id < 16; id++, inputCol++) {
  129. elementValue = playTable[inputRow][inputCol];
  130. document.getElementById(id.toString()).innerHTML = elementValue;
  131. }
  132.  
  133. }
  134.  
  135. //generates a 4x4 sudoku and alters the html table OK
  136. function play() {
  137. createPlayField();
  138. generateTable();
  139. //printthegoddamnvalues();
  140. returnValues();
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement