How to use sudoku._assign method in Cypress

Best JavaScript code snippet using cypress

generator.js

Source:generator.js Github

copy

Full Screen

...109 // up and try again110 var rand_candidate_idx = 111 sudoku._rand_range(candidates[square].length);112 var rand_candidate = candidates[square][rand_candidate_idx];113 if(!sudoku._assign(candidates, square, rand_candidate)){114 break;115 }116 117 // Make a list of all single candidates118 var single_candidates = [];119 for(var si in SQUARES){120 var square = SQUARES[si];121 122 if(candidates[square].length == 1){123 single_candidates.push(candidates[square]);124 }125 }126 127 // If we have at least difficulty, and the unique candidate count is128 // at least 8, return the puzzle!129 if(single_candidates.length >= difficulty && 130 sudoku._strip_dups(single_candidates).length >= 8){131 var board = "";132 var givens_idxs = [];133 for(var i in SQUARES){134 var square = SQUARES[i];135 if(candidates[square].length == 1){136 board += candidates[square];137 givens_idxs.push(i);138 } else {139 board += sudoku.BLANK_CHAR;140 }141 }142 143 // If we have more than `difficulty` givens, remove some random144 // givens until we're down to exactly `difficulty`145 var nr_givens = givens_idxs.length;146 if(nr_givens > difficulty){147 givens_idxs = sudoku._shuffle(givens_idxs);148 for(var i = 0; i < nr_givens - difficulty; ++i){149 var target = parseInt(givens_idxs[i]);150 board = board.substr(0, target) + sudoku.BLANK_CHAR + 151 board.substr(target + 1);152 }153 }154 155 // Double check board is solvable156 // TODO: Make a standalone board checker. Solve is expensive.157 if(sudoku.solve(board)){158 return board;159 }160 }161 }162 163 // Give up and try a new puzzle164 return sudoku.generate(difficulty);165 };166 // Solve167 // -------------------------------------------------------------------------168 sudoku.solve = function(board, reverse){169 /* Solve a sudoku puzzle given a sudoku `board`, i.e., an 81-character 170 string of sudoku.DIGITS, 1-9, and spaces identified by '.', representing the171 squares. There must be a minimum of 17 givens. If the given board has no172 solutions, return false.173 174 Optionally set `reverse` to solve "backwards", i.e., rotate through the175 possibilities in reverse. Useful for checking if there is more than one176 solution.177 */178 179 // Assure a valid board180 var report = sudoku.validate_board(board);181 if(report !== true){182 throw report;183 }184 185 // Check number of givens is at least MIN_GIVENS186 var nr_givens = 0;187 for(var i in board){188 if(board[i] !== sudoku.BLANK_CHAR && sudoku._in(board[i], sudoku.DIGITS)){189 ++nr_givens;190 }191 }192 if(nr_givens < MIN_GIVENS){193 throw "Too few givens. Minimum givens is " + MIN_GIVENS;194 }195 // Default reverse to false196 reverse = reverse || false;197 var candidates = sudoku._get_candidates_map(board);198 var result = sudoku._search(candidates, reverse);199 200 if(result){201 var solution = "";202 for(var square in result){203 solution += result[square];204 }205 return solution;206 }207 return false;208 };209 sudoku.get_candidates = function(board){210 /* Return all possible candidatees for each square as a grid of 211 candidates, returnning `false` if a contradiction is encountered.212 213 Really just a wrapper for sudoku._get_candidates_map for programmer214 consumption.215 */216 217 // Assure a valid board218 var report = sudoku.validate_board(board);219 if(report !== true){220 throw report;221 }222 223 // Get a candidates map224 var candidates_map = sudoku._get_candidates_map(board);225 226 // If there's an error, return false227 if(!candidates_map){228 return false;229 }230 231 // Transform candidates map into grid232 var rows = [];233 var cur_row = [];234 var i = 0;235 for(var square in candidates_map){236 var candidates = candidates_map[square];237 cur_row.push(candidates);238 if(i % 9 == 8){239 rows.push(cur_row);240 cur_row = [];241 }242 ++i;243 }244 return rows;245 }246 sudoku._get_candidates_map = function(board){247 /* Get all possible candidates for each square as a map in the form248 {square: sudoku.DIGITS} using recursive constraint propagation. Return `false` 249 if a contradiction is encountered250 */251 252 // Assure a valid board253 var report = sudoku.validate_board(board);254 if(report !== true){255 throw report;256 }257 258 var candidate_map = {};259 var squares_values_map = sudoku._get_square_vals_map(board);260 261 // Start by assigning every digit as a candidate to every square262 for(var si in SQUARES){263 candidate_map[SQUARES[si]] = sudoku.DIGITS;264 }265 266 // For each non-blank square, assign its value in the candidate map and267 // propigate.268 for(var square in squares_values_map){269 var val = squares_values_map[square];270 271 if(sudoku._in(val, sudoku.DIGITS)){272 var new_candidates = sudoku._assign(candidate_map, square, val);273 274 // Fail if we can't assign val to square275 if(!new_candidates){276 return false;277 }278 }279 }280 281 return candidate_map;282 };283 sudoku._search = function(candidates, reverse){284 /* Given a map of squares -> candiates, using depth-first search, 285 recursively try all possible values until a solution is found, or false286 if no solution exists. 287 */288 289 // Return if error in previous iteration290 if(!candidates){291 return false;292 }293 294 // Default reverse to false295 reverse = reverse || false;296 297 // If only one candidate for every square, we've a solved puzzle!298 // Return the candidates map.299 var max_nr_candidates = 0;300 var max_candidates_square = null;301 for(var si in SQUARES){302 var square = SQUARES[si];303 304 var nr_candidates = candidates[square].length;305 306 if(nr_candidates > max_nr_candidates){307 max_nr_candidates = nr_candidates;308 max_candidates_square = square;309 }310 }311 if(max_nr_candidates === 1){312 return candidates;313 }314 315 // Choose the blank square with the fewest possibilities > 1316 var min_nr_candidates = 10;317 var min_candidates_square = null;318 for(si in SQUARES){319 var square = SQUARES[si];320 321 var nr_candidates = candidates[square].length;322 323 if(nr_candidates < min_nr_candidates && nr_candidates > 1){324 min_nr_candidates = nr_candidates;325 min_candidates_square = square;326 }327 }328 329 // Recursively search through each of the candidates of the square 330 // starting with the one with fewest candidates.331 332 // Rotate through the candidates forwards333 var min_candidates = candidates[min_candidates_square];334 if(!reverse){335 for(var vi in min_candidates){336 var val = min_candidates[vi];337 338 // TODO: Implement a non-rediculous deep copy function339 var candidates_copy = JSON.parse(JSON.stringify(candidates));340 var candidates_next = sudoku._search(341 sudoku._assign(candidates_copy, min_candidates_square, val)342 );343 344 if(candidates_next){345 return candidates_next;346 }347 }348 349 // Rotate through the candidates backwards350 } else {351 for(var vi = min_candidates.length - 1; vi >= 0; --vi){352 var val = min_candidates[vi];353 354 // TODO: Implement a non-rediculous deep copy function355 var candidates_copy = JSON.parse(JSON.stringify(candidates));356 var candidates_next = sudoku._search(357 sudoku._assign(candidates_copy, min_candidates_square, val), 358 reverse359 );360 361 if(candidates_next){362 return candidates_next;363 }364 }365 }366 367 // If we get through all combinations of the square with the fewest368 // candidates without finding an answer, there isn't one. Return false.369 return false;370 };371 sudoku._assign = function(candidates, square, val){372 /* Eliminate all values, *except* for `val`, from `candidates` at 373 `square` (candidates[square]), and propagate. Return the candidates map374 when finished. If a contradiciton is found, return false.375 376 WARNING: This will modify the contents of `candidates` directly.377 */378 // Grab a list of canidates without 'val'379 var other_vals = candidates[square].replace(val, "");380 // Loop through all other values and eliminate them from the candidates 381 // at the current square, and propigate. If at any point we get a 382 // contradiction, return false.383 for(var ovi in other_vals){384 var other_val = other_vals[ovi];385 var candidates_next =386 sudoku._eliminate(candidates, square, other_val);387 if(!candidates_next){388 //console.log("Contradiction found by _eliminate.");389 return false;390 }391 }392 return candidates;393 };394 sudoku._eliminate = function(candidates, square, val){395 /* Eliminate `val` from `candidates` at `square`, (candidates[square]),396 and propagate when values or places <= 2. Return updated candidates,397 unless a contradiction is detected, in which case, return false.398 399 WARNING: This will modify the contents of `candidates` directly.400 */401 // If `val` has already been eliminated from candidates[square], return402 // with candidates.403 if(!sudoku._in(val, candidates[square])){404 return candidates;405 }406 // Remove `val` from candidates[square]407 candidates[square] = candidates[square].replace(val, '');408 409 // If the square has only candidate left, eliminate that value from its 410 // peers411 var nr_candidates = candidates[square].length;412 if(nr_candidates === 1){413 var target_val = candidates[square];414 415 for(var pi in SQUARE_PEERS_MAP[square]){416 var peer = SQUARE_PEERS_MAP[square][pi];417 418 var candidates_new = 419 sudoku._eliminate(candidates, peer, target_val);420 421 if(!candidates_new){422 return false;423 }424 }425 426 // Otherwise, if the square has no candidates, we have a contradiction.427 // Return false.428 } if(nr_candidates === 0){429 return false;430 }431 432 // If a unit is reduced to only one place for a value, then assign it433 for(var ui in SQUARE_UNITS_MAP[square]){434 var unit = SQUARE_UNITS_MAP[square][ui];435 436 var val_places = [];437 for(var si in unit){438 var unit_square = unit[si];439 if(sudoku._in(val, candidates[unit_square])){440 val_places.push(unit_square);441 }442 }443 444 // If there's no place for this value, we have a contradition!445 // return false446 if(val_places.length === 0){447 return false;448 449 // Otherwise the value can only be in one place. Assign it there.450 } else if(val_places.length === 1){451 var candidates_new = 452 sudoku._assign(candidates, val_places[0], val);453 454 if(!candidates_new){455 return false;456 }457 }458 }459 460 return candidates;461 };462 463 // Square relationships464 // -------------------------------------------------------------------------465 // Squares, and their relationships with values, units, and peers.466 ...

Full Screen

Full Screen

ds.js

Source:ds.js Github

copy

Full Screen

...114 // up and try again115 var rand_candidate_idx =116 sudoku._rand_range(candidates[square].length);117 var rand_candidate = candidates[square][rand_candidate_idx];118 if(!sudoku._assign(candidates, square, rand_candidate)){119 break;120 }121122 // Make a list of all single candidates123 var single_candidates = [];124 for(var si in SQUARES){125 var square = SQUARES[si];126127 if(candidates[square].length == 1){128 single_candidates.push(candidates[square]);129 }130 }131132 // If we have at least difficulty, and the unique candidate count is133 // at least 8, return the puzzle!134 if(single_candidates.length >= difficulty &&135 sudoku._strip_dups(single_candidates).length >= 8){136 var board = "";137 var givens_idxs = [];138 for(var i in SQUARES){139 var square = SQUARES[i];140 if(candidates[square].length == 1){141 board += candidates[square];142 givens_idxs.push(i);143 } else {144 board += sudoku.BLANK_CHAR;145 }146 }147148 // If we have more than `difficulty` givens, remove some random149 // givens until we're down to exactly `difficulty`150 var nr_givens = givens_idxs.length;151 if(nr_givens > difficulty){152 givens_idxs = sudoku._shuffle(givens_idxs);153 for(var i = 0; i < nr_givens - difficulty; ++i){154 var target = parseInt(givens_idxs[i]);155 board = board.substr(0, target) + sudoku.BLANK_CHAR +156 board.substr(target + 1);157 }158 }159160 // Double check board is solvable161 // TODO: Make a standalone board checker. Solve is expensive.162 if(sudoku.solve(board)){163 return board;164 }165 }166 }167168 // Give up and try a new puzzle169 return sudoku.generate(difficulty);170 };171172 // Solve173 // -------------------------------------------------------------------------174 sudoku.solve = function(board, reverse){175 /* Solve a sudoku puzzle given a sudoku `board`, i.e., an 81-character176 string of sudoku.DIGITS, 1-9, and spaces identified by '.', representing the177 squares. There must be a minimum of 17 givens. If the given board has no178 solutions, return false.179180 Optionally set `reverse` to solve "backwards", i.e., rotate through the181 possibilities in reverse. Useful for checking if there is more than one182 solution.183 */184185 // Assure a valid board186 var report = sudoku.validate_board(board);187 if(report !== true){188 throw report;189 }190191 // Check number of givens is at least MIN_GIVENS192 var nr_givens = 0;193 for(var i in board){194 if(board[i] !== sudoku.BLANK_CHAR && sudoku._in(board[i], sudoku.DIGITS)){195 ++nr_givens;196 }197 }198 if(nr_givens < MIN_GIVENS){199 throw "Too few givens. Minimum givens is " + MIN_GIVENS;200 }201202 // Default reverse to false203 reverse = reverse || false;204205 var candidates = sudoku._get_candidates_map(board);206 var result = sudoku._search(candidates, reverse);207208 if(result){209 var solution = "";210 for(var square in result){211 solution += result[square];212 }213 return solution;214 }215 return false;216 };217218 sudoku.get_candidates = function(board){219 /* Return all possible candidatees for each square as a grid of220 candidates, returnning `false` if a contradiction is encountered.221222 Really just a wrapper for sudoku._get_candidates_map for programmer223 consumption.224 */225226 // Assure a valid board227 var report = sudoku.validate_board(board);228 if(report !== true){229 throw report;230 }231232 // Get a candidates map233 var candidates_map = sudoku._get_candidates_map(board);234235 // If there's an error, return false236 if(!candidates_map){237 return false;238 }239240 // Transform candidates map into grid241 var rows = [];242 var cur_row = [];243 var i = 0;244 for(var square in candidates_map){245 var candidates = candidates_map[square];246 cur_row.push(candidates);247 if(i % 9 == 8){248 rows.push(cur_row);249 cur_row = [];250 }251 ++i;252 }253 return rows;254 }255256 sudoku._get_candidates_map = function(board){257 /* Get all possible candidates for each square as a map in the form258 {square: sudoku.DIGITS} using recursive constraint propagation. Return `false`259 if a contradiction is encountered260 */261262 // Assure a valid board263 var report = sudoku.validate_board(board);264 if(report !== true){265 throw report;266 }267268 var candidate_map = {};269 var squares_values_map = sudoku._get_square_vals_map(board);270271 // Start by assigning every digit as a candidate to every square272 for(var si in SQUARES){273 candidate_map[SQUARES[si]] = sudoku.DIGITS;274 }275276 // For each non-blank square, assign its value in the candidate map and277 // propigate.278 for(var square in squares_values_map){279 var val = squares_values_map[square];280281 if(sudoku._in(val, sudoku.DIGITS)){282 var new_candidates = sudoku._assign(candidate_map, square, val);283284 // Fail if we can't assign val to square285 if(!new_candidates){286 return false;287 }288 }289 }290291 return candidate_map;292 };293294 sudoku._search = function(candidates, reverse){295 /* Given a map of squares -> candiates, using depth-first search,296 recursively try all possible values until a solution is found, or false297 if no solution exists.298 */299300 // Return if error in previous iteration301 if(!candidates){302 return false;303 }304305 // Default reverse to false306 reverse = reverse || false;307308 // If only one candidate for every square, we've a solved puzzle!309 // Return the candidates map.310 var max_nr_candidates = 0;311 var max_candidates_square = null;312 for(var si in SQUARES){313 var square = SQUARES[si];314315 var nr_candidates = candidates[square].length;316317 if(nr_candidates > max_nr_candidates){318 max_nr_candidates = nr_candidates;319 max_candidates_square = square;320 }321 }322 if(max_nr_candidates === 1){323 return candidates;324 }325326 // Choose the blank square with the fewest possibilities > 1327 var min_nr_candidates = 10;328 var min_candidates_square = null;329 for(si in SQUARES){330 var square = SQUARES[si];331332 var nr_candidates = candidates[square].length;333334 if(nr_candidates < min_nr_candidates && nr_candidates > 1){335 min_nr_candidates = nr_candidates;336 min_candidates_square = square;337 }338 }339340 // Recursively search through each of the candidates of the square341 // starting with the one with fewest candidates.342343 // Rotate through the candidates forwards344 var min_candidates = candidates[min_candidates_square];345 if(!reverse){346 for(var vi in min_candidates){347 var val = min_candidates[vi];348349 // TODO: Implement a non-rediculous deep copy function350 var candidates_copy = JSON.parse(JSON.stringify(candidates));351 var candidates_next = sudoku._search(352 sudoku._assign(candidates_copy, min_candidates_square, val)353 );354355 if(candidates_next){356 return candidates_next;357 }358 }359360 // Rotate through the candidates backwards361 } else {362 for(var vi = min_candidates.length - 1; vi >= 0; --vi){363 var val = min_candidates[vi];364365 // TODO: Implement a non-rediculous deep copy function366 var candidates_copy = JSON.parse(JSON.stringify(candidates));367 var candidates_next = sudoku._search(368 sudoku._assign(candidates_copy, min_candidates_square, val),369 reverse370 );371372 if(candidates_next){373 return candidates_next;374 }375 }376 }377378 // If we get through all combinations of the square with the fewest379 // candidates without finding an answer, there isn't one. Return false.380 return false;381 };382383 sudoku._assign = function(candidates, square, val){384 /* Eliminate all values, *except* for `val`, from `candidates` at385 `square` (candidates[square]), and propagate. Return the candidates map386 when finished. If a contradiciton is found, return false.387388 WARNING: This will modify the contents of `candidates` directly.389 */390391 // Grab a list of canidates without 'val'392 var other_vals = candidates[square].replace(val, "");393394 // Loop through all other values and eliminate them from the candidates395 // at the current square, and propigate. If at any point we get a396 // contradiction, return false.397 for(var ovi in other_vals){398 var other_val = other_vals[ovi];399400 var candidates_next =401 sudoku._eliminate(candidates, square, other_val);402403 if(!candidates_next){404 //console.log("Contradiction found by _eliminate.");405 return false;406 }407 }408409 return candidates;410 };411412 sudoku._eliminate = function(candidates, square, val){413 /* Eliminate `val` from `candidates` at `square`, (candidates[square]),414 and propagate when values or places <= 2. Return updated candidates,415 unless a contradiction is detected, in which case, return false.416417 WARNING: This will modify the contents of `candidates` directly.418 */419420 // If `val` has already been eliminated from candidates[square], return421 // with candidates.422 if(!sudoku._in(val, candidates[square])){423 return candidates;424 }425426 // Remove `val` from candidates[square]427 candidates[square] = candidates[square].replace(val, '');428429 // If the square has only candidate left, eliminate that value from its430 // peers431 var nr_candidates = candidates[square].length;432 if(nr_candidates === 1){433 var target_val = candidates[square];434435 for(var pi in SQUARE_PEERS_MAP[square]){436 var peer = SQUARE_PEERS_MAP[square][pi];437438 var candidates_new =439 sudoku._eliminate(candidates, peer, target_val);440441 if(!candidates_new){442 return false;443 }444 }445446 // Otherwise, if the square has no candidates, we have a contradiction.447 // Return false.448 } if(nr_candidates === 0){449 return false;450 }451452 // If a unit is reduced to only one place for a value, then assign it453 for(var ui in SQUARE_UNITS_MAP[square]){454 var unit = SQUARE_UNITS_MAP[square][ui];455456 var val_places = [];457 for(var si in unit){458 var unit_square = unit[si];459 if(sudoku._in(val, candidates[unit_square])){460 val_places.push(unit_square);461 }462 }463464 // If there's no place for this value, we have a contradition!465 // return false466 if(val_places.length === 0){467 return false;468469 // Otherwise the value can only be in one place. Assign it there.470 } else if(val_places.length === 1){471 var candidates_new =472 sudoku._assign(candidates, val_places[0], val);473474 if(!candidates_new){475 return false;476 }477 }478 }479480 return candidates;481 };482483484 // Square relationships485 // -------------------------------------------------------------------------486 // Squares, and their relationships with values, units, and peers. ...

Full Screen

Full Screen

main.js

Source:main.js Github

copy

Full Screen

...31// for (var si in shuffled_squares) {32// var square = shuffled_squares[si];33// var rand_candidate_idx = sudoku._rand_range(candidates[square].length);34// var rand_candidate = candidates[square][rand_candidate_idx];35// if (!sudoku._assign(candidates, square, rand_candidate)) {36// break;37// }38// var single_candidates = [];39// for (var si in SQUARES) {40// var square = SQUARES[si];41// if (candidates[square].length == 1) {42// single_candidates.push(candidates[square]);43// }44// }45// if (46// single_candidates.length >= no_of_squares &&47// sudoku._strip_dups(single_candidates).length >= 848// ) {49// var board = "";50// var givens_idxs = [];51// for (var i in SQUARES) {52// var square = SQUARES[i];53// if (candidates[square].length == 1) {54// board += candidates[square];55// givens_idxs.push(i);56// } else {57// board += sudoku.BLANK_CHAR;58// }59// }60// var nr_givens = givens_idxs.length;61// if (nr_givens > no_of_squares) {62// givens_idxs = sudoku._shuffle(givens_idxs);63// for (var i = 0; i < nr_givens - no_of_squares; ++i) {64// var target = parseInt(givens_idxs[i]);65// board =66// board.substr(0, target) +67// sudoku.BLANK_CHAR +68// board.substr(target + 1);69// }70// }71// if (sudoku.solve(board)) {72// return board;73// }74// }75// }76// return sudoku.generate(no_of_squares);77// };78// sudoku.solve = function (board, reverse) {79// // Assure a valid board80// var report = sudoku.validate_board(board);81// if (report !== true) {82// throw report;83// }84// var nr_givens = 0;85// for (var i in board) {86// if (board[i] !== sudoku.BLANK_CHAR && sudoku._in(board[i], sudoku.DIGITS)) {87// ++nr_givens;88// }89// }90// if (nr_givens < MIN_GIVENS) {91// throw "Too few givens. Minimum givens is " + MIN_GIVENS;92// }93// reverse = reverse || false;94// var candidates = sudoku._get_candidates_map(board);95// var result = sudoku._search(candidates, reverse);96// if (result) {97// var solution = "";98// for (var square in result) {99// solution += result[square];100// }101// return solution;102// }103// return false;104// };105// sudoku.get_candidates = function (board) {106// var report = sudoku.validate_board(board);107// if (report !== true) {108// throw report;109// }110// var candidates_map = sudoku._get_candidates_map(board);111// if (!candidates_map) {112// return false;113// }114// var rows = [];115// var cur_row = [];116// var i = 0;117// for (var square in candidates_map) {118// var candidates = candidates_map[square];119// cur_row.push(candidates);120// if (i % 9 == 8) {121// rows.push(cur_row);122// cur_row = [];123// }124// ++i;125// }126// return rows;127// };128// sudoku._get_candidates_map = function (board) {129// var report = sudoku.validate_board(board);130// if (report !== true) {131// throw report;132// }133// var candidate_map = {};134// var squares_values_map = sudoku._get_square_vals_map(board);135// for (var si in SQUARES) {136// candidate_map[SQUARES[si]] = sudoku.DIGITS;137// }138// for (var square in squares_values_map) {139// var val = squares_values_map[square];140// if (sudoku._in(val, sudoku.DIGITS)) {141// var new_candidates = sudoku._assign(candidate_map, square, val);142// if (!new_candidates) {143// return false;144// }145// }146// }147// return candidate_map;148// };149// sudoku._search = function (candidates, reverse) {150// if (!candidates) {151// return false;152// }153// reverse = reverse || false;154// var max_nr_candidates = 0;155// var max_candidates_square = null;156// for (var si in SQUARES) {157// var square = SQUARES[si];158// var nr_candidates = candidates[square].length;159// if (nr_candidates > max_nr_candidates) {160// max_nr_candidates = nr_candidates;161// max_candidates_square = square;162// }163// }164// if (max_nr_candidates === 1) {165// return candidates;166// }167// var min_nr_candidates = 10;168// var min_candidates_square = null;169// for (si in SQUARES) {170// var square = SQUARES[si];171// var nr_candidates = candidates[square].length;172// if (nr_candidates < min_nr_candidates && nr_candidates > 1) {173// min_nr_candidates = nr_candidates;174// min_candidates_square = square;175// }176// }177// var min_candidates = candidates[min_candidates_square];178// if (!reverse) {179// for (var vi in min_candidates) {180// var val = min_candidates[vi];181// var candidates_copy = JSON.parse(JSON.stringify(candidates));182// var candidates_next = sudoku._search(183// sudoku._assign(candidates_copy, min_candidates_square, val)184// );185// if (candidates_next) {186// return candidates_next;187// }188// }189// } else {190// for (var vi = min_candidates.length - 1; vi >= 0; --vi) {191// var val = min_candidates[vi];192// var candidates_copy = JSON.parse(JSON.stringify(candidates));193// var candidates_next = sudoku._search(194// sudoku._assign(candidates_copy, min_candidates_square, val),195// reverse196// );197// if (candidates_next) {198// return candidates_next;199// }200// }201// }202// return false;203// };204// sudoku._assign = function (candidates, square, val) {205// var other_vals = candidates[square].replace(val, "");206// for (var ovi in other_vals) {207// var other_val = other_vals[ovi];208// var candidates_next = sudoku._eliminate(candidates, square, other_val);209// if (!candidates_next) {210// return false;211// }212// }213// return candidates;214// };215// sudoku._eliminate = function (candidates, square, val) {216// if (!sudoku._in(val, candidates[square])) {217// return candidates;218// }219// candidates[square] = candidates[square].replace(val, "");220// var nr_candidates = candidates[square].length;221// if (nr_candidates === 1) {222// var target_val = candidates[square];223// for (var pi in SQUARE_PEERS_MAP[square]) {224// var peer = SQUARE_PEERS_MAP[square][pi];225// var candidates_new = sudoku._eliminate(candidates, peer, target_val);226// if (!candidates_new) {227// return false;228// }229// }230// }231// if (nr_candidates === 0) {232// return false;233// }234// for (var ui in SQUARE_UNITS_MAP[square]) {235// var unit = SQUARE_UNITS_MAP[square][ui];236// var val_places = [];237// for (var si in unit) {238// var unit_square = unit[si];239// if (sudoku._in(val, candidates[unit_square])) {240// val_places.push(unit_square);241// }242// }243// if (val_places.length === 0) {244// return false;245// } else if (val_places.length === 1) {246// var candidates_new = sudoku._assign(candidates, val_places[0], val);247// if (!candidates_new) {248// return false;249// }250// }251// }252// return candidates;253// };254// sudoku._get_square_vals_map = function (board) {255// /* Return a map of squares -> values256// */257// var squares_vals_map = {};258// // Make sure `board` is a string of length 81259// if (board.length != SQUARES.length) {260// throw "Board/squares length mismatch.";...

Full Screen

Full Screen

sudoku.js

Source:sudoku.js Github

copy

Full Screen

...53 54 var rand_option_idx = 55 sudoku._rand_range(options[square].length);56 var rand_option = options[square][rand_option_idx];57 if(!sudoku._assign(options, square, rand_option)){58 break;59 }60 61 var single_options = [];62 for(var si in SQUARES){63 var square = SQUARES[si];64 65 if(options[square].length == 1){66 single_options.push(options[square]);67 }68 }69 70 if(single_options.length >= difficulty && 71 sudoku._strip_dups(single_options).length >= 8){72 var board = "";73 var givens_idxs = [];74 for(var i in SQUARES){75 var square = SQUARES[i];76 if(options[square].length == 1){77 board += options[square];78 givens_idxs.push(i);79 } else {80 board += sudoku.BLANK_CHAR;81 }82 }83 84 var nr_givens = givens_idxs.length;85 if(nr_givens > difficulty){86 givens_idxs = sudoku._shuffle(givens_idxs);87 for(var i = 0; i < nr_givens - difficulty; ++i){88 var target = parseInt(givens_idxs[i]);89 board = board.substr(0, target) + sudoku.BLANK_CHAR + 90 board.substr(target + 1);91 }92 }93 94 if(sudoku.solve(board)){95 return board;96 }97 }98 }99 100 return sudoku.generate(difficulty);101 };102 // Solve103 sudoku.solve = function(board, reverse){104 var report = sudoku.validate_board(board);105 if(report !== true){106 throw report;107 }108 109 var nr_givens = 0;110 for(var i in board){111 if(board[i] !== sudoku.BLANK_CHAR && sudoku._in(board[i], sudoku.DIGITS)){112 ++nr_givens;113 }114 }115 if(nr_givens < MIN_GIVENS){116 throw "Too few givens. Minimum givens is " + MIN_GIVENS;117 }118 reverse = reverse || false;119 var options = sudoku._get_options_map(board);120 var result = sudoku._search(options, reverse);121 122 if(result){123 var solution = "";124 for(var square in result){125 solution += result[square];126 }127 return solution;128 }129 return false;130 };131 sudoku.get_options = function(board){132 var report = sudoku.validate_board(board);133 if(report !== true){134 throw report;135 }136 137 var options_map = sudoku._get_options_map(board);138 139 if(!options_map){140 return false;141 }142 143 var rows = [];144 var cur_row = [];145 var i = 0;146 for(var square in options_map){147 var options = options_map[square];148 cur_row.push(options);149 if(i % 9 == 8){150 rows.push(cur_row);151 cur_row = [];152 }153 ++i;154 }155 return rows;156 }157 sudoku._get_options_map = function(board){158 var report = sudoku.validate_board(board);159 if(report !== true){160 throw report;161 }162 163 var option_map = {};164 var squares_values_map = sudoku._get_square_vals_map(board);165 166 for(var si in SQUARES){167 option_map[SQUARES[si]] = sudoku.DIGITS;168 }169 170 for(var square in squares_values_map){171 var val = squares_values_map[square];172 173 if(sudoku._in(val, sudoku.DIGITS)){174 var new_options = sudoku._assign(option_map, square, val);175 176 if(!new_options){177 return false;178 }179 }180 }181 182 return option_map;183 };184 sudoku._search = function(options, reverse){185 if(!options){186 return false;187 }188 189 reverse = reverse || false;190 191 var max_nr_options = 0;192 var max_options_square = null;193 for(var si in SQUARES){194 var square = SQUARES[si];195 196 var nr_options = options[square].length;197 198 if(nr_options > max_nr_options){199 max_nr_options = nr_options;200 max_options_square = square;201 }202 }203 if(max_nr_options === 1){204 return options;205 }206 207 var min_nr_options = 10;208 var min_options_square = null;209 for(si in SQUARES){210 var square = SQUARES[si];211 212 var nr_options = options[square].length;213 214 if(nr_options < min_nr_options && nr_options > 1){215 min_nr_options = nr_options;216 min_options_square = square;217 }218 }219 220 var min_options = options[min_options_square];221 if(!reverse){222 for(var vi in min_options){223 var val = min_options[vi];224 225 var options_copy = JSON.parse(JSON.stringify(options));226 var options_next = sudoku._search(227 sudoku._assign(options_copy, min_options_square, val)228 );229 230 if(options_next){231 return options_next;232 }233 }234 235 } else {236 for(var vi = min_options.length - 1; vi >= 0; --vi){237 var val = min_options[vi];238 239 // TODO: Implement a non-rediculous deep copy function240 var options_copy = JSON.parse(JSON.stringify(options));241 var options_next = sudoku._search(242 sudoku._assign(options_copy, min_options_square, val), 243 reverse244 );245 246 if(options_next){247 return options_next;248 }249 }250 }251 252 return false;253 };254 sudoku._assign = function(options, square, val){255 var other_vals = options[square].replace(val, "");256 for(var ovi in other_vals){257 var other_val = other_vals[ovi];258 var options_next =259 sudoku._eliminate(options, square, other_val);260 if(!options_next){261 //console.log("Contradiction found by _eliminate.");262 return false;263 }264 }265 return options;266 };267 sudoku._eliminate = function(options, square, val){268 if(!sudoku._in(val, options[square])){269 return options;270 }271 options[square] = options[square].replace(val, '');272 273 var nr_options = options[square].length;274 if(nr_options === 1){275 var target_val = options[square];276 277 for(var pi in SQUARE_PEERS_MAP[square]){278 var peer = SQUARE_PEERS_MAP[square][pi];279 280 var options_new = 281 sudoku._eliminate(options, peer, target_val);282 283 if(!options_new){284 return false;285 }286 }287 288 } if(nr_options === 0){289 return false;290 }291 292 for(var ui in SQUARE_UNITS_MAP[square]){293 var unit = SQUARE_UNITS_MAP[square][ui];294 295 var val_places = [];296 for(var si in unit){297 var unit_square = unit[si];298 if(sudoku._in(val, options[unit_square])){299 val_places.push(unit_square);300 }301 }302 303 if(val_places.length === 0){304 return false;305 306 } else if(val_places.length === 1){307 var options_new = 308 sudoku._assign(options, val_places[0], val);309 310 if(!options_new){311 return false;312 }313 }314 }315 316 return options;317 };318 319 // Square relationships320 // Squares, and their relationships with values, units, and peers.321 322 sudoku._get_square_vals_map = function(board){...

Full Screen

Full Screen

sudoku_generator_01.js

Source:sudoku_generator_01.js Github

copy

Full Screen

...78 // If an assignment of a random chioce causes a contradictoin, give79 // up and try again80 var rand_candidate_idx = sudoku._rand_range(candidates[square].length);81 var rand_candidate = candidates[square][rand_candidate_idx];82 if(!sudoku._assign(candidates, square, rand_candidate)) break;83 var single_candidates = [];84 for(var si in sudoku.SQUARES) {85 var square = sudoku.SQUARES[si];86 if(candidates[square].length == 1) single_candidates.push(candidates[square]);87 }88 // If we have at least difficulty, and the unique candidate count is89 // at least 8, return the puzzle!90 if(single_candidates.length >= difficulty && sudoku._strip_dups(single_candidates).length >= 8){91 var board = '';92 var givens_idxs = [];93 for(var i in sudoku.SQUARES){94 var square = sudoku.SQUARES[i];95 if(candidates[square].length == 1){96 board += candidates[square];97 givens_idxs.push(i);98 }99 else {100 board += sudoku.BLANK_CHAR;101 }102 }103 // If we have more than `difficulty` givens, remove some random104 // givens until we're down to exactly `difficulty`105 var nr_givens = givens_idxs.length;106 if(nr_givens > difficulty){107 givens_idxs = sudoku._shuffle(givens_idxs);108 for(var i = 0; i < nr_givens - difficulty; ++i){109 var target = parseInt(givens_idxs[i]);110 board = board.substr(0, target) + sudoku.BLANK_CHAR + board.substr(target + 1);111 }112 }113 // Double check board is solvable114 // TODO: Make a standalone board checker. Solve is expensive.115 if(sudoku.solve(board)) return board;116 }117 }118 return sudoku.generate(difficulty);119 };120 sudoku.solve = function(board, reverse) {121 sudoku.init();122 var report = sudoku.validate_board(board);123 if(report !== true) throw report;124 var nr_givens = 0;125 for(var i in board) {126 if(board[i] !== sudoku.BLANK_CHAR && sudoku.DIGITS.includes(board[i])) ++nr_givens;127 }128 if(nr_givens < sudoku.MIN_GIVENS) throw 'Too few givens. Minimum givens is' + sudoku.MIN_GIVENS;129 reverse = reverse || false;130 var candidates = sudoku._get_candidates_map(board);131 var result = sudoku._search(candidates, reverse);132 if(result) {133 var solution = '';134 for(var square in result) solution += result[square];135 return solution;136 }137 return false;138 };139 sudoku._get_candidates_map = function(board){140 var report = sudoku.validate_board(board);141 if(report !== true) throw report;142 var candidate_map = {};143 var squares_values_map = sudoku._get_square_vals_map(board);144 for(var si in sudoku.SQUARES) candidate_map[sudoku.SQUARES[si]] = sudoku.DIGITS;145 for(var square in squares_values_map){146 var val = squares_values_map[square];147 if(sudoku.DIGITS.includes(val)) {148 var new_candidates = sudoku._assign(candidate_map, square, val);149 if(!new_candidates) return false;150 }151 }152 return candidate_map;153 };154 sudoku._search = function(candidates, reverse){155 if(!candidates) return false;156 reverse = reverse || false;157 var max_nr_candidates = 0;158 var max_candidates_square = null;159 for(var si in sudoku.SQUARES) {160 var square = sudoku.SQUARES[si];161 var nr_candidates = candidates[square].length;162 if(nr_candidates > max_nr_candidates) {163 max_nr_candidates = nr_candidates;164 max_candidates_square = square;165 }166 }167 if(max_nr_candidates === 1) return candidates;168 var min_nr_candidates = 10;169 var min_candidates_square = null;170 for(si in sudoku.SQUARES) {171 var square = sudoku.SQUARES[si];172 var nr_candidates = candidates[square].length;173 if(nr_candidates < min_nr_candidates && nr_candidates > 1){174 min_nr_candidates = nr_candidates;175 min_candidates_square = square;176 }177 }178 var min_candidates = candidates[min_candidates_square];179 if(!reverse) {180 //for(var vi in min_candidates) {181 for(var vi = 0; vi < min_candidates.length; vi++) {182 var val = min_candidates[vi];183 var candidates_copy = Object.assign({}, candidates);184 var candidates_next = sudoku._search(sudoku._assign(candidates_copy, min_candidates_square, val));185 if(candidates_next) return candidates_next;186 }187 }188 else {189 for(var vi = min_candidates.length - 1; vi >= 0; --vi) {190 var val = min_candidates[vi];191 var candidates_copy = Object.assign({}, candidates);192 var candidates_next = sudoku._search(sudoku._assign(candidates_copy, min_candidates_square, val), reverse);193 if(candidates_next) return candidates_next;194 }195 }196 return false;197 };198 sudoku._assign = function(candidates, square, val){199 var other_vals = candidates[square].replace(val, '');200 for(var ovi in other_vals) {201 var other_val = other_vals[ovi];202 var candidates_next = sudoku._eliminate(candidates, square, other_val);203 if(!candidates_next) return false;204 }205 return candidates;206 };207 sudoku._eliminate = function(candidates, square, val){208 if(!candidates[square].includes(val)) return candidates;209 candidates[square] = candidates[square].replace(val, '');210 var nr_candidates = candidates[square].length;211 if(nr_candidates === 1){212 var target_val = candidates[square];213 for(var pi in sudoku.SQUARE_PEERS_MAP[square]){214 var peer = sudoku.SQUARE_PEERS_MAP[square][pi];215 var candidates_new = sudoku._eliminate(candidates, peer, target_val);216 if(!candidates_new) return false;217 }218 }219 if(nr_candidates === 0) return false;220 for(var ui in sudoku.SQUARE_UNITS_MAP[square]) {221 var unit = sudoku.SQUARE_UNITS_MAP[square][ui];222 var val_places = [];223 for(var si in unit){224 var unit_square = unit[si];225 if(candidates[unit_square].includes(val)) val_places.push(unit_square);226 }227 if(val_places.length === 0) return false;228 if(val_places.length === 1) {229 var candidates_new = sudoku._assign(candidates, val_places[0], val);230 if(!candidates_new) return false;231 }232 }233 return candidates;234 };235 sudoku._get_square_vals_map = function(board) {236 var squares_vals_map = {};237 if(board.length != sudoku.SQUARES.length) throw new Error("Board/squares length mismatch.");238 for(var i in sudoku.SQUARES) squares_vals_map[sudoku.SQUARES[i]] = board[i];239 return squares_vals_map;240 };241 sudoku._get_square_units_map = function(squares, units){242 var square_unit_map = {};243 for(var si in squares){...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1Cypress.Commands.add('assign', (sudoku, cell, value) => {2 sudoku._assign(cell, value);3});4Cypress.Commands.add('unassign', (sudoku, cell, value) => {5 sudoku._unassign(cell, value);6});7Cypress.Commands.add('forwardCheck', (sudoku, cell, value) => {8 sudoku._forwardCheck(cell, value);9});10Cypress.Commands.add('unforwardCheck', (sudoku, cell, value) => {11 sudoku._unforwardCheck(cell, value);12});13Cypress.Commands.add('selectUnassignedVariable', (sudoku) => {14 sudoku._selectUnassignedVariable();15});16Cypress.Commands.add('orderDomainValues', (sudoku, cell, value) => {17 sudoku._orderDomainValues(cell, value);18});19Cypress.Commands.add('inference', (sudoku, cell, value) => {20 sudoku._inference(cell, value);21});22Cypress.Commands.add('backtrack', (sudoku) => {23 sudoku._backtrack();24});25Cypress.Commands.add('isConsistent', (sudoku, cell, value) => {26 sudoku._isConsistent(cell, value);27});28Cypress.Commands.add('isComplete', (sudoku) => {29 sudoku._isComplete();30});31Cypress.Commands.add('isSolved', (sudoku) => {32 sudoku._isSolved();33});34Cypress.Commands.add('

Full Screen

Using AI Code Generation

copy

Full Screen

1describe('Sudoku', () => {2 it('can be solved', () => {3 cy.get('input').then($inputs => {4 $inputs.each((i, input) => {5 if (value) {6 cy.get(input).invoke('attr', 'id').then(id => {7 const region = Math.floor((row - 1) / 3) * 3 + Math.floor((column - 1) / 3) + 18 const values = value.split('')9 cy.window().then(win => {10 values.forEach(v => {11 win.sudoku._assign(win.sudoku.cells[row + column + v], row, column, region, v)12 })13 })14 })15 }16 })17 })18 cy.get('.fkey').click()19 cy.get('input').should('have.value', '123456789')20 })21})

Full Screen

Using AI Code Generation

copy

Full Screen

1export default () => {2 cy.get('#new-game').click()3 cy.get('#solve').click()4 cy.get('#solve').should('be.disabled')5 cy.get('#new-game').click()6 cy.get('#solve').should('be.disabled')7 cy.get('#clear').click()8 cy.get('#solve').should('be.disabled')9 cy.get('#new-game').click()10 cy.get('#solve').click()11 cy.get('#solve').should('be.disabled')12 cy.get('#new-game').click()13 cy.get('#solve').should('be.disabled')14 cy.get('#clear').click()15 cy.get('#solve').should('be.disabled')16 cy.get('#new-game').click()17 cy.get('#solve').click()18 cy.get('#solve').should('be.disabled')19 cy.get('#new-game').click()20 cy.get('#solve').should('be.disabled')21 cy.get('#clear').click()22 cy.get('#solve').should('be.disabled')23 cy.get('#new-game').click()24 cy.get('#solve').click()25 cy.get('#solve').should('be.disabled')26 cy.get('#new-game').click()27 cy.get('#solve').should('be.disabled')28 cy.get('#clear').click()29 cy.get('#solve').should('be.disabled')30 cy.get('#new-game').click()31 cy.get('#solve').click()32 cy.get('#solve').should('be.disabled')33 cy.get('#new-game').click()34 cy.get('#solve').should('be.disabled')35 cy.get('#clear').click()36 cy.get('#solve').should('be.disabled')37 cy.get('#new-game').click()38 cy.get('#solve').click()39 cy.get('#solve').should('be.disabled')40 cy.get('#new-game').click()41 cy.get('#solve').should('be.disabled')42 cy.get('#clear').click()43 cy.get('#solve').should('be.disabled')44 cy.get('#new-game').click()

Full Screen

Cypress Tutorial

Cypress is a renowned Javascript-based open-source, easy-to-use end-to-end testing framework primarily used for testing web applications. Cypress is a relatively new player in the automation testing space and has been gaining much traction lately, as evidenced by the number of Forks (2.7K) and Stars (42.1K) for the project. LambdaTest’s Cypress Tutorial covers step-by-step guides that will help you learn from the basics till you run automation tests on LambdaTest.

Chapters:

  1. What is Cypress? -
  2. Why Cypress? - Learn why Cypress might be a good choice for testing your web applications.
  3. Features of Cypress Testing - Learn about features that make Cypress a powerful and flexible tool for testing web applications.
  4. Cypress Drawbacks - Although Cypress has many strengths, it has a few limitations that you should be aware of.
  5. Cypress Architecture - Learn more about Cypress architecture and how it is designed to be run directly in the browser, i.e., it does not have any additional servers.
  6. Browsers Supported by Cypress - Cypress is built on top of the Electron browser, supporting all modern web browsers. Learn browsers that support Cypress.
  7. Selenium vs Cypress: A Detailed Comparison - Compare and explore some key differences in terms of their design and features.
  8. Cypress Learning: Best Practices - Take a deep dive into some of the best practices you should use to avoid anti-patterns in your automation tests.
  9. How To Run Cypress Tests on LambdaTest? - Set up a LambdaTest account, and now you are all set to learn how to run Cypress tests.

Certification

You can elevate your expertise with end-to-end testing using the Cypress automation framework and stay one step ahead in your career by earning a Cypress certification. Check out our Cypress 101 Certification.

YouTube

Watch this 3 hours of complete tutorial to learn the basics of Cypress and various Cypress commands with the Cypress testing at LambdaTest.

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful