Skip to content

Instantly share code, notes, and snippets.

@gunnarig
Created March 4, 2019 09:02
Show Gist options
  • Save gunnarig/e7d2fd7ac74d980513644803d99cae54 to your computer and use it in GitHub Desktop.
Save gunnarig/e7d2fd7ac74d980513644803d99cae54 to your computer and use it in GitHub Desktop.
fdf
var board =
[['1', '1', '1', '4', '5', '6', '7', '8', '9'],
['2', '2', '2', '3', '.', '.', '9', '.', '.'],
['3', '3', '3', '9', '.', '.', '.', '.', '.'],
['.', '7', '1', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.', '8'],
['8', '.', '.', '.', '5', '.', '6', '.', '4'],
['.', '1', '.', '2', '.', '.', '.', '9', '.'],
['2', '.', '.', '.', '.', '4', '.', '.', '.'],
['.', '6', '9', '.', '.', '.', '.', '7', '.']];
var indexBoard =
[['00', '01', '02', '03', '04', '05', '06', '07', '08'],
['10', '11', '12', '13', '14', '15', '16', '17', '18'],
['20', '21', '22', '23', '24', '25', '26', '27', '28'],
['30', '31', '32', '33', '34', '35', '36', '37', '38'],
['40', '41', '42', '43', '44', '45', '46', '47', '48'],
['50', '51', '52', '53', '54', '55', '56', '57', '58'],
['60', '61', '62', '63', '64', '65', '66', '67', '68'],
['70', '71', '72', '73', '74', '75', '76', '77', '78'],
['80', '81', '82', '83', '84', '85', '86', '87', '88']];
var Count = 0;
var secondCount = 0;
var rowCheck = false;
var colCheck = false;
var unlock = false;
function validateAll(start = indexBoard[secondCount][Count]) {
let k = 0;
let i = 0;
let j = 0;
var target = document.getElementById("cell" + k + j).value;
var notlegal = document.getElementById("cell" + k + j).className;
var secondTarget = document.getElementById("cell" + i + k).value;
var index = document.getElementById("cell" + start).value;
var noval = document.getElementById("cell" + start).className;
if (start === indexBoard[8][8]) {
Count = 0;
secondCount = 0;
return true
} else if (Count === 8) {
oneCount = 8;
Count += -8;
secondCount += 1;
var goat = 1;
console.log(validateAll(indexBoard[secondCount-1][oneCount]))
return validateAll(indexBoard[secondCount+parseInt(goat)][oneCount]);
} else if (noval == "NoEditValue") {
Count += 1;
return validateAll(indexBoard[secondCount][Count]);
} else if (noval == "redValue") {
Count += 1;
return validateAll(indexBoard[secondCount][Count]);
} else if (index == "") {
document.getElementById("cell" + start).className = "yellowValue";
Count += 1;
return validateAll(indexBoard[secondCount][Count]);
} else if (target == index && start !== String(String(Count) + String(secondCount))) {
Count += 1;
return validateAll(indexBoard[secondCount][Count])
} else if (rowCheck == false) {
for (k = 0; k <= 2; k++) {
for (i = 0; i <= 2; i++) {
} if (index === target) {
document.getElementById("cell" + k + j).className = "redValue";
unlock = true;
} else {
Count += 1;
return validateAll(indexBoard[secondCount][Count])
}
}
} else if (unlock == true && colCheck == false) {
for (k = 0; k <= 8; k += 3) {
for (i = 0; i <= 8; i += 3) {//target("cell" + k + j).value
} if (index === target && notlegal !== "NoEditValue") { //index("cell" + start).value
//indexin á að verða rauður
document.getElementById("cell" + k + j).className = "redValue";
colCheck = true;
rowCheck = false;
} else {
Count += 1;
return validateAll(indexBoard[secondCount][Count])
}
}
} else {
Count += 1;
return validateAll(indexBoard[secondCount][Count])
}
}
function checkBox() {
let cellArray = [];
for (i = 0; i <= 8; i++) {
console.log(i);
console.log(document.getElementById("cell0" + i));
var res = document.getElementById("cell0" + i).value;
cellArray.push(res);
}
console.log(cellArray);
validator(cellArray);
}
function validator(arr) {
var boardArray = board[0];
for (i = 0; i <= 8; i++) {
if (arr[i] != boardArray[i]) {
console.log("FUCK TURTLES ");
return false;
}
}
console.log("I LIKE TURTLes ")
return true
}
function checkRow() {
let rowArray = [];
let k = 0;
let i = 0;
for (k = 0; k <= 2; k++) {
for (i = 0; i <= 2; i++) {
console.log(document.getElementById("cell" + k + i));
var res = document.getElementById("cell" + k + i).value;
rowArray.push(res)
}
}
console.log(rowArray);
rowValidator(rowArray);
}
function checkColumn() {
let rowArray = [];
let k = 0;
let i = 0;
for (k = 0; k <= 8; k += 3) {
for (i = 0; i <= 8; i += 3) {
console.log(document.getElementById("cell" + k + i));
var res = document.getElementById("cell" + k + i).value;
rowArray.push(res)
}
}
console.log(rowArray);
rowValidator(rowArray);
}
function rowValidator(arr) {
var boardArray = []
let k = 0;
let i = 0;
for (k = 0; k <= 2; k++) {
for (i = 0; i <= 2; i++)
boardArray.push(board[k][i]); {
}
}
console.log(arr)
console.log(boardArray)
twoValidator(arr, boardArray)
}
function twoValidator(arr, arr2) {
for (i = 0; i <= 8; i++) {
if (arr[i] != arr2[i]) {
console.log("I dislike stuff ");
return false;
}
}
console.log("im happy ")
return true
}
//===[ Fetch request with Axios ]==================================================================
//Send get Request to fetch a new gameboard at given game level.
function fetchSudoku() {
//set up variables
var gameLevel = document.getElementById("Difficulty").value;
//Database URL
var url = 'https://veff213-sudoku.herokuapp.com/api/v1/sudoku';
//Request new board from database: Send gameLevel as parameter
axios.post(url, { difficulty: gameLevel })
.then(function (response) {
//If request is succesful then process data.
//Send data to console for Debug
console.log("Success: ", response.data);
// Calls a method that creates the sudoku table and starts the game loop.
// initSudokuBoard(response.data.board.boxes);
initSudokuBoard(response.data);
fetchID(response.data);
})
.catch(function (error) {
//If request is succesful: Print error
console.log(error);
//If error perhaps here is a good place to call saved boards of correct difficulty
})
// This code is always executed, independent of whether the request succeeds or fails
.then(function () {
});
}
//=================================================================================================
//=================================================================================================
//===[ Create New Sudoku Board ]===================================================================
function fetchID(data) {
console.log(data.board._id)
document.getElementById("board_ID").innerHTML = "<p><h5> Board ID: " + data.board._id + "</p></h5>";
}
// build HTML table data from an array (one or two dimensional)
// takes in cleaned data from database
function initSudokuBoard(fetchData) {
sudokuArr = fetchData.board.boxes;
console.log("fetchData: ", fetchData);
//Set up a 2D loop. Make an input string targeting the correct div id
for (var y = 0; y < sudokuArr.length; y++) {
for (var x = 0; x < sudokuArr[y].length; x++) {
html_str = '';
pos_id = '';
//Check if array returns integer or not.
//If integer then make read only and set class to NoEditValue
//If not integer then make box blank and set class to editValue
if (Number.isInteger(parseInt(sudokuArr[y][x]))) {
console.log("Is integer: ", sudokuArr[y][x])
html_str = '<input id="cell' + y + x + '"' +
' type="number" class="NoEditValue" name="subcell" value="' +
sudokuArr[y][x] + '" readonly>';
} else {
console.log("Is NOT integer: ", sudokuArr[y][x])
html_str = '<input id="cell' + y + x + '"' +
' type="number" maxlength="1" min="1" max="9" class="editValue" name="subcell" value="' +
sudokuArr[y][x].replace(/[.]/g, "") + '">';
}
pos_id += y;
pos_id += x;
document.getElementById(pos_id).innerHTML = html_str;
}
}
}
//=================================================================================================
//=================================================================================================
//===[ Hardcoded Sudoku pussle arrays ]============================================================
// var easy = [[5,6,4,.,.,3,2,.,1],
// [8,7,2,.,1,.,3,9,.],
// [3,9,1,.,.,.,.,.,5],
// [4,2,9,6,5,7,3,1,8],
// [.,.,8,2,3,1,9,4,7],
// [7,1,3,8,4,9,5,2,6],
// [.,.,6,.,3,5,8,4,2],
// [4,2,3,7,8,9,1,.,.],
// [.,5,8,2,6,4,9,3,7]]
// var medium = [[8,7,.,.,4,.,6,2,5],
// [4,5,.,.,2,.,.,1,.],
// [2,1,.,8,5,.,.,9,.],
// [7,6,.,5,.,4,.,8,.],
// [9,3,1,8,6,2,5,.,7],
// [5,4,8,3,.,1,9,6,2],
// [2,.,7,9,5,8,4,.,6],
// [.,9,4,6,7,3,2,.,5],
// [.,.,5,1,.,4,.,.,.]]
// var hard = [[4,.,.,9,.,.,.,.,.],
// [.,.,.,.,4,.,.,.,.],
// [5,3,9,6,.,1,7,.,4],
// [.,9,6,.,4,7,.,.,.],
// [.,7,8,5,.,2,1,9,6],
// [2,5,3,9,1,6,8,4,7],
// [.,.,1,.,8,4,2,.,.],
// [.,8,.,.,.,.,.,5,4]]
//=================================================================================================
//=================================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment