Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var playTable = new Array(), row = 0, col = 0, rowPosit, colPosit, playElementValue = 0, sorted = 0, rP, cP;
- //creates a 2dimensional array, filled wit zero-s; OK
- function createPlayField() {
- for (row = 0; row < 4; row++) {
- playTable[row] = new Array();
- for (col = 0; col < 4; col++) {
- playTable[row][col] = 0;
- }
- }
- }
- //Generates the 2dimensional table; OK
- function generateTable() {
- for ( var elementCount = 0 ; elementCount < 16 ; elementCount++) {
- console.log("Count: ", elementCount + 1);
- //generates the random position and checks if there is a number there already
- do {
- rowPosit = Math.floor((Math.random() * 4) + 1) - 1;
- colPosit = Math.floor((Math.random() * 4) + 1) - 1;
- } while (playTable[rowPosit][colPosit] !== 0);
- console.log("R:", rowPosit, "C:", colPosit);
- //do {
- // playElementValue = Math.floor((Math.random() * 4) + 1);
- //} while (checkRowCol() === -1);
- playElementValue = Math.floor((Math.random() * 4) + 1);
- playTable[rowPosit][colPosit] = playElementValue;
- console.log("check in the end of the cycle. num:", playTable[rowPosit][colPosit]);
- }
- }
- //checks the 2x2 square for matching number;
- function checkSquare() {
- //checks the square of the number
- if( rowPosit <= 1 && colPosit <= 1) {
- rP = rowPosit, cP = colPosit;
- if( rP === 1 && cP === 1) {
- rP--, cP--;
- }
- } else if( rowPosit <= 1 && colPosit === 3 || colPosit === 2 ) {
- rP = rowPosit, cP = colPosit;
- if( rowPosit === 1 || colPosit === 3) {
- rP--, cP--;
- }
- } else if( rowPosit === 3 || rowPosit === 2 && colPosit <= 1 ) {
- rP = rowPosit, cP = colPosit;
- if( rowPosit === 3 && colPosit === 1) {
- rP--, cP--;
- }
- } else if( rowPosit === 3 || rowPosit === 2 && colPosit === 3 || colPosit === 2 ) {
- rP = rowPosit, cP = colPosit;
- if( rowPosit === 3 && colPosit === 3) {
- rP--, cP--;
- }
- }
- console.log(rP, cP);
- console.log("kube before the cicle");
- var colHelper;
- for ( var rowHelper = rP ; rowHelper <= rP ; rowHelper++) {
- for ( colHelper = cP ; colHelper <= cP ; colHelper++) {
- console.log(playTable[rowHelper][colHelper])
- if (playTable[rowHelper][colHelper] === playElementValue
- && rowHelper !== rowPosit
- && colHelper !== colPosit) {
- console.log("SQUARE check");
- sorted = 1;
- return -1;
- }
- }
- }
- }
- //checks the cols and rowsfor matching numbers
- function checkRowCol() {
- for( var R = 0 ; R < 4 ; R++ ) {
- if( playTable[R][colPosit] === playElementValue && R !== rowPosit) {
- console.log("COL check");
- return -1;
- }
- }
- for( var C = 0 ; C < 4 ; C++ ) {
- if( playTable[rowPosit][C] === playElementValue && C !== colPosit) {
- console.log("ROW check");
- return -1;
- }
- }
- }
- //system check function OK
- function printthegoddamnvalues() {
- var k = 0;
- for (var i = 0; i < 4; i++) {
- for (k = 0; k < 4; k++) {
- console.log(playTable[i][k]);
- console.log(typeof(playTable[i][k]));
- }
- }
- }
- //alters the html table OK
- function returnValues() {
- var elementValue = 0;
- for (var id = 0, inputRow = 0, inputCol = 0; id < 4; id++, inputCol++) {
- elementValue = playTable[inputRow][inputCol];
- document.getElementById(id.toString()).innerHTML = elementValue;
- }
- for (id = 4, inputRow = 1, inputCol = 0; id < 8; id++, inputCol++) {
- elementValue = playTable[inputRow][inputCol];
- document.getElementById(id.toString()).innerHTML = elementValue;
- }
- for (id = 8, inputRow = 2, inputCol = 0; id < 12; id++, inputCol++) {
- elementValue = playTable[inputRow][inputCol];
- document.getElementById(id.toString()).innerHTML = elementValue;
- }
- for (id = 12, inputRow = 3, inputCol = 0; id < 16; id++, inputCol++) {
- elementValue = playTable[inputRow][inputCol];
- document.getElementById(id.toString()).innerHTML = elementValue;
- }
- }
- //generates a 4x4 sudoku and alters the html table OK
- function play() {
- createPlayField();
- generateTable();
- //printthegoddamnvalues();
- returnValues();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement