0% found this document useful (0 votes)
4 views9 pages

Sudoku Assignment

The document outlines a series of tasks for creating a Sudoku puzzle algorithm, including functions for generating vectors, permuting rows, checking columns, and validating grids. It also discusses limitations such as handling invalid user input and the algorithm's restriction to 4x4 grids. Additionally, suggestions for improving the algorithm to create varied puzzles and accommodate different grid sizes are provided.

Uploaded by

Poseidon CSBD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Sudoku Assignment

The document outlines a series of tasks for creating a Sudoku puzzle algorithm, including functions for generating vectors, permuting rows, checking columns, and validating grids. It also discusses limitations such as handling invalid user input and the algorithm's restriction to 4x4 grids. Additionally, suggestions for improving the algorithm to create varied puzzles and accommodate different grid sizes are provided.

Uploaded by

Poseidon CSBD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Task 1:

function MakeVector(row)

new Vector puzzle(4)

puzzle[1] ← row

puzzle[2] ← row

puzzle[3] ← row

puzzle[4] ← row

return puzzle

end function
Task 2:

function PERMUTEVECTOR(row, p)

new Queue q

ENQUEUE[row[4], q]

ENQUEUE[row[3], q]

ENQUEUE[row[2], q]

ENQUEUE[row[1], q]

for 0 ≤ i < p do

store ← HEAD[q]

DEQUEUE[q]

ENQUEUE[store,q]

end for

row[4] ← HEAD[q]

DEQUEUE[q]

row[3] ← HEAD[q]

DEQUEUE[q]

row[2] ← HEAD[q]

DEQUEUE[q]

row[1] ← HEAD[q]

DEQUEUE[q]

return row

end function
Task 3:

function PermuteRow(puzzle, x, y, z)

PERMUTEVECTOR(puzzle[1],x)

PERMUTEVECTOR(puzzle[2],y)

PERMUTEVECTOR(puzzle[3],z)

return puzzle

end function
Task 4:

function CheckColumn(puzzle, j)

new Vector temp(4)

for 1 ≤ i ≤ 4 do

temp[i] ← puzzle[i][j]

end for

for 1 ≤ k ≤ 4 do

if LinearSearch(temp, k) = false then

return false

end if

end for

return true

end function

function LinearSearch(vector, item)

for 1 ≤ i ≤ LENGTH[vector] do

if vector[i]=item then

return TRUE

end if

end for

return FALSE

end function
Task 5:

function ColCheck(puzzle)

for 1 ≤ j ≤ 4 do

if CheckColumn(puzzle, j) = false then

return false

end if

end for

return true

end function
Task 6:

function CheckGrid(puzzle, row1, col1, row2, col2)

new Vector temp2(4)

temp2[1] ← puzzle[row1][col1]

temp2[2] ← puzzle[row1][col2]

temp2[3] ← puzzle[row2][col1]

temp2[4] ← puzzle[row2][col2]

for 1 ≤ k ≤ 4 do

if LinearSearch(temp2, k) = false then

return false

end if

end for

return true

end function

function LinearSearch(vector, item)

for 1 ≤ i ≤ LENGTH[vector] do

if vector[i]=item then

return TRUE

end if

end for

return FALSE

end function
Task 7:

function MakeSolution(row)

new Vector puzzle(4)

puzzle ← MakeVector(row)
/* We assigned the returned[generated] value from MakeVector(row) to ‘puzzle’ which is a vector of 4
elements */

for 1 ≤ x ≤ 3 do

for 1 ≤ y ≤ 3 do

for 1 ≤ z ≤ 3 do

puzzle ← PermuteRow(puzzle, x, y, z)

if ColCheck(puzzle) = true then

if CheckGrids(puzzle) = true then

return puzzle

end if

end if

end for

end for

end for

end function
Task 8:

We need to set the 'n' number of values to be blank characters in the elements of the output of
MAKESOLUTION function.

MAKESOLUTION returns a vector puzzle with 4 elements and each element has 4 elements of its own.

We can use the random() and floor() functions to solve this case with some algorithms. I am showing a
method with pseudocode.

totalElements ← 4*4

counter ← 0

if n>= totalElements then

return "Error Message: All the elements are already blank or invalid input"

end if

while counter < n do

a ← floor(random(1,totalElements+1)

x ← a/4

y ← a-x*4

if puzzle[x+1][y] = ' ' then [Here ' ' means blank]

continue

end if

puzzle[x+1][y] ← ' '

counter ← counter + 1

end while

Through this function, we can set blank characters in the element of the output of MAKESOLUTION
function.
Task 9:

There are some limitations of the algorithm of this assignment. This algorithm can't make all possible
Sudoku puzzles. I will try to briefly explain some of the major limitations of the algorithm below:

One of the main weaknesses of this algorithm is that it doesn't filter out the user input in case of invalid
input. The algorithm won't work if the user inputs any value other than integers and it will not crash the
algorithm. To overcome this problem, we can implement exception handling and ask the user to re-
enter integer only, in case he/she enters any non-integer input.

Or we need to change the algorithm. Suppose, a user inputted 1/2 character as an element for the row.
The way, we implemented LinearSearch function inside CheckColumn or other function, won't work
because here with the for loop we are passing an integer as the 'item' value. So LinearSearch function
will always return false and our algorithm will fail.

In this case, we need to call LinearSearch function without using a for loop and have to manually pass
the value of item. We have to do the same thing in task 6 also. Now suppose the user inputted a
character as 'n'. There's no way the algorithm can run successfully without having an integer value for
'n'. So he/she must re-enter or an error will show.

Another problem is, this algorithm will work on a 4X4 sudoku. It won't work on any other grid of sudoku.
The way we implemented the CHECKGRIDS function won't work on different grid types. The equation
will be different for other grid-type puzzles. And in this assignment, we checked 2-by-2 sub-grids. This
won't be the case if the grid type is different. In that case we need to find out an equation according to
that particular grid type and check if the CHECKGRID function satisfies the conditions. We here worked
on a 4X4 sudoku and got 2by2 sub-grids. This will not be the case if we want to create another sudoku
with a different grid. For example, a 10X10 Sudoku usually has 5by2 sub grids. We will get squared
subgrids if and only if we use 2X2, 9X9, 16X16 etc grid patterns. Whatever grid we chose rather than 4X4
will break our algorithm. We need to change the parameter of CHECKGRID function accordingly to make
it work. But things will get more complex when we will use larger grids.

And even for a 4X4 grid, this algorithm will always produce the same puzzle for a particular row element
sequence. This is because the puzzle is permuted in a structured way and it will always satisfy the
conditions of soduku at a fixed point every time for the same sequence of row elements. To get a variety
of puzzles, we can change the order of the for loops in task 7 or change the parameter order of x,y and z
in PermuteRow function. This will create different varieties of sudoku puzzles or what we are calling it in
this assignment 'Pseudocode' puzzle.

You might also like