![]() You generate a random valid first row, e.g.: row_1 =. ![]() You could probably add another condition that takes the subgrids into account, but there are much easier ways to generate a valid sudoku. You might notice that although rows and columns only contain unique numbers, the 2×2 subgrids contain duplicates. The resulting row_1, row_2, row_3 and row_4 are: (printed as a sudoku) ╔═════╤═════╗ Row_4 = permutations.sample #=> (or permutations.last) You then remove all elements that have 3 in the 1st spot, or 2 in the 2nd, 1 in 3rd, or 4 in 4th: lete_if #, ,, ,, ]įrom this array, you pick a random element as the first row: row_1 = permutations.sample #=> You start by generating all permutations: (I'm using a 4×4 sudoku for demonstration purposes) permutations =. You could use a loop to remove those values. I doubt that you can generate a sudoku using uniq because the method operates on a single element at a time without taking the other elements into account. Like, if any permutation has any number at same index, then drop that one. I'm trying to use those three parameters in uniqueness, somehow.
0 Comments
Leave a Reply. |