Sudoku solving program problems - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Sudoku solving program problems

SwithSwith Posts: 4Member
I was trying to write a program that would solve sudokus, and as far as I can tell what I have done should work. It presents no error messages, but if you enter a valid sudoku puzzle, it does nothing at all and accepts no further input. The only thing that it does strangely is if you enter a solved sudoku, the final line that it prints after it thinks it has solved it is a row of zeros. Any help would be greatly appreciated!

[code]program sudoku;

var
input : array[1..9, 1..9] of integer;
cell : array[1..9, 1..9, 1..9] of integer;
output : array[1..9, 1..9] of integer;
cposs : array[1..9, 1..9, 1..9] of integer;
cloop, cloop2, rloop, rloop2, bloop, bloop2, loop, loop2, loop3, c, d : integer;
cloop3, rloop3, bloop3 : integer;
cloop4, rloop4, bloop4 : integer;
cloop5, rloop5, bloop5 : integer;
solved : boolean;

begin



{sets all items of input array to 0}
for loop := 1 to 9 do
begin

for loop2 := 1 to 9 do

input[loop2,loop] := 0;
end;

{sets all items of cell array to 10}
for loop := 1 to 9 do
begin

for loop2 := 1 to 9 do
begin

for loop3 := 1 to 9 do
begin

cell[loop3,loop2,loop] := 10;
end;
end;
end;

{instructions}
writeln('== Sudoku Solver ==':50);
writeln;
writeln;
writeln('Please enter the numbers of your sudoku from left to right, one row at a time from top to bottom, WITH A SPACE BETWEEN EACH CHARACTER, pressing enter after each row. If the cell is empty, please write a 0.');
writeln;

{reading inputs}
for rloop := 1 to 9 do

begin

for cloop := 1 to 9 do

read( input[cloop,rloop] );
end;

{copies inputs to "cell" array, and adds box position}
for rloop := 1 to 9 do
begin

for cloop := 1 to 9 do
begin

if cloop in [1..3] then
if rloop in [1..3] then
cell[cloop,rloop,1] := input[cloop,rloop] else

if rloop in [4..6] then
cell[cloop,rloop,4] := input[cloop,rloop] else

if rloop in [7..8] then
cell[cloop,rloop,7] := input[cloop,rloop];


if cloop in [4..6] then
if rloop in [1..3] then
cell[cloop,rloop,2] := input[cloop,rloop] else

if rloop in [4..6] then
cell[cloop,rloop,5] := input[cloop,rloop] else

if rloop in [7..8] then
cell[cloop,rloop,8] := input[cloop,rloop];

if cloop in [7..9] then
if rloop in [1..3] then
cell[cloop,rloop,3] := input[cloop,rloop] else

if rloop in [4..6] then
cell[cloop,rloop,6] := input[cloop,rloop] else

if rloop in [7..8] then
cell[cloop,rloop,9] := input[cloop,rloop];
end;
end;


repeat


c := 0;
d := 0;

{the big block}
for bloop := 1 to 9 do
begin

for rloop := 1 to 9 do
begin

for cloop := 1 to 9 do
begin

{checks if it is possible to have cell in that box
by checking if a value from input has been copied
over (the value wont be 10}
if cell[cloop,rloop,bloop] < 10 then
begin
if cell[cloop,rloop,bloop] > 0 then
begin

{no possibility for that number in the cells in this box}
for bloop2 := 1 to 9 do
begin

for rloop2 := 1 to 9 do
begin
for cloop2 := 1 to 9 do
begin
if cell[cloop2,rloop2,bloop2] < 10 then
begin
if bloop2 = bloop then
cposs[cloop2, rloop2, cell[cloop,rloop,bloop] ] := 1;
end;
end;
end;
end;

{no possibility for that number in the cells in that row}
for bloop3 := 1 to 9 do
begin
for rloop3 := 1 to 9 do
begin
for cloop3 := 1 to 9 do
begin
if cell[cloop3,rloop3,bloop3] < 10 then
begin
if rloop3 = rloop then
cposs[cloop3, rloop3, cell[cloop,rloop,bloop] ] := 1;
end;
end;
end;
end;

{no possibility for that number in the cells in that column}
for bloop4 := 1 to 9 do
begin
for rloop4 := 1 to 9 do
begin
for cloop4 := 1 to 9 do
begin
if cell[cloop4,rloop4,bloop4] < 10 then
begin
if cloop4 = cloop then
cposs[cloop4, rloop4, cell[cloop,rloop,bloop] ] := 1;
end;
end;
end;
end;

end;

end;

{checks if cell has only one possible answer}
for loop := 1 to 9 do
begin
if cposs[cloop,rloop,loop] = 0 then
c := c + 1;
end;
if c = 1 then
begin
for loop := 1 to 9 do
begin
if cposs[cloop, rloop, loop] = 0 then
cell[cloop,rloop,bloop] := loop;
end;
end;

end;
end;
end;
{checks if sudoku is finished}
for bloop5 := 1 to 9 do
begin
for rloop5 := 1 to 9 do
begin
for cloop5 := 1 to 9 do
begin
if cell[cloop5, rloop5, bloop5] = 0 then
d := d + 1;
end;
end;
end;
if d = 0 then solved := true;

until solved = true;

{copying}
for bloop := 1 to 9 do
begin

for rloop := 1 to 9 do
begin

for cloop := 1 to 9 do
begin

if cell[cloop,rloop,bloop] < 10 then
begin

output[cloop,rloop] := cell[cloop,rloop,bloop];
end;
end;
end;
end;

writeln;

{printing}
for rloop := 1 to 9 do
begin

for cloop := 1 to 9 do
begin

write(output[cloop,rloop],' ');
end;
writeln;

end;
writeln;
writeln('Solved!');
readln;
readln;

end.[/code]

Comments

  • quikcarlxquikcarlx Hollywood, FlPosts: 26Member
    Looks fine except for some testing that you have. And why all the Begin .. end statements in loops that only have one statement. Just a lot of extra writing on your part.
    I think you need to change these:
    [code] if rloop in [[color=Green]7..8[/color]] then
    cell[cloop,rloop,7] := input[cloop,rloop];

    [/code]
    [code] if rloop in [[color=Red]7..9[/color]] then
    cell[cloop,rloop,7] := input[cloop,rloop];

    [/code]
    [code] if rloop in [[color=Green]7..8[/color]] then
    cell[cloop,rloop,8] := input[cloop,rloop];
    [/code]
    [code] if rloop in [[color=Red]7..9[/color]] then
    cell[cloop,rloop,8] := input[cloop,rloop];
    [/code]
    [code] if rloop in [[color=Green]7..8[/color]] then
    cell[cloop,rloop,9] := input[cloop,rloop];
    [/code]
    [code] if rloop in [[color=Red]7..9[/color]] then
    cell[cloop,rloop,9] := input[cloop,rloop];
    [/code]
    I have another program that I found on a european site that I altered for doing sudoku puzzles on screen.
  • quikcarlxquikcarlx Hollywood, FlPosts: 26Member
    Looks fine except for some testing that you have. And why all the Begin .. end statements in loops that only have one statement. Just a lot of extra writing on your part.
    I think you need to change these:
    [code] if rloop in [[color=Green]7..8[/color]] then
    cell[cloop,rloop,7] := input[cloop,rloop];

    [/code]
    [code] if rloop in [[color=Red]7..9[/color]] then
    cell[cloop,rloop,7] := input[cloop,rloop];

    [/code]
    [code] if rloop in [[color=Green]7..8[/color]] then
    cell[cloop,rloop,8] := input[cloop,rloop];
    [/code]
    [code] if rloop in [[color=Red]7..9[/color]] then
    cell[cloop,rloop,8] := input[cloop,rloop];
    [/code]
    [code] if rloop in [[color=Green]7..8[/color]] then
    cell[cloop,rloop,9] := input[cloop,rloop];
    [/code]
    [code] if rloop in [[color=Red]7..9[/color]] then
    cell[cloop,rloop,9] := input[cloop,rloop];
    [/code]
    I have another program that I found on a european site that I altered for doing sudoku puzzles on screen.
  • SwithSwith Posts: 4Member
    You're absolutely right about needing to change those, and doing so does stop the row of zeroes appearing when I enter a solved sudoku, but the program still doesn't solve an unsolved sudoku; it refuses input after you enter the final line, suggesting that it is stuck in a loop. Any ideas why?
  • SwithSwith Posts: 4Member
    You're absolutely right about needing to change those, and doing so does stop the row of zeroes appearing when I enter a solved sudoku, but the program still doesn't solve an unsolved sudoku; it refuses input after you enter the final line, suggesting that it is stuck in a loop. Any ideas why?

    [code]program sudoku;

    var
    input : array[1..9, 1..9] of integer;
    cell : array[1..9, 1..9, 1..9] of integer;
    output : array[1..9, 1..9] of integer;
    cposs : array[1..9, 1..9, 1..9] of integer;
    cloop, cloop2, rloop, rloop2, bloop, bloop2, loop, loop2, loop3, c, d : integer;
    cloop3, rloop3, bloop3 : integer;
    cloop4, rloop4, bloop4 : integer;
    cloop5, rloop5, bloop5 : integer;
    solved : boolean;

    begin



    {sets all items of input array to 0}
    for loop := 1 to 9 do
    begin

    for loop2 := 1 to 9 do

    input[loop2,loop] := 0;
    end;

    {sets all items of cell array to 10}
    for loop := 1 to 9 do
    begin

    for loop2 := 1 to 9 do
    begin

    for loop3 := 1 to 9 do
    begin

    cell[loop3,loop2,loop] := 10;
    end;
    end;
    end;

    {instructions}
    writeln('== Sudoku Solver ==':50);
    writeln;
    writeln;
    writeln('Please enter the numbers of your sudoku from left to right, one row at a time from top to bottom, WITH A SPACE BETWEEN EACH CHARACTER, pressing enter after each row. If the cell is empty, please write a 0.');
    writeln;

    {reading inputs}
    for rloop := 1 to 9 do

    begin

    for cloop := 1 to 9 do

    read( input[cloop,rloop] );
    end;

    {copies inputs to "cell" array, and adds box position}
    for rloop := 1 to 9 do
    begin

    for cloop := 1 to 9 do
    begin

    if cloop in [1..3] then
    if rloop in [1..3] then
    cell[cloop,rloop,1] := input[cloop,rloop] else

    if rloop in [4..6] then
    cell[cloop,rloop,4] := input[cloop,rloop] else

    if rloop in [7..9] then
    cell[cloop,rloop,7] := input[cloop,rloop];


    if cloop in [4..6] then
    if rloop in [1..3] then
    cell[cloop,rloop,2] := input[cloop,rloop] else

    if rloop in [4..6] then
    cell[cloop,rloop,5] := input[cloop,rloop] else

    if rloop in [7..9] then
    cell[cloop,rloop,8] := input[cloop,rloop];

    if cloop in [7..9] then
    if rloop in [1..3] then
    cell[cloop,rloop,3] := input[cloop,rloop] else

    if rloop in [4..6] then
    cell[cloop,rloop,6] := input[cloop,rloop] else

    if rloop in [7..9] then
    cell[cloop,rloop,9] := input[cloop,rloop];
    end;
    end;


    repeat


    c := 0;
    d := 0;

    {the big block}
    for bloop := 1 to 9 do
    begin

    for rloop := 1 to 9 do
    begin

    for cloop := 1 to 9 do
    begin

    {checks if it is possible to have cell in that box
    by checking if a value from input has been copied
    over (the value wont be 10}
    if cell[cloop,rloop,bloop] < 10 then
    begin
    if cell[cloop,rloop,bloop] > 0 then
    begin

    {no possibility for that number in the cells in this box}
    for bloop2 := 1 to 9 do
    begin

    for rloop2 := 1 to 9 do
    begin
    for cloop2 := 1 to 9 do
    begin
    if cell[cloop2,rloop2,bloop2] < 10 then
    begin
    if bloop2 = bloop then
    cposs[cloop2, rloop2, cell[cloop,rloop,bloop] ] := 1;
    end;
    end;
    end;
    end;

    {no possibility for that number in the cells in that row}
    for bloop3 := 1 to 9 do
    begin
    for rloop3 := 1 to 9 do
    begin
    for cloop3 := 1 to 9 do
    begin
    if cell[cloop3,rloop3,bloop3] < 10 then
    begin
    if rloop3 = rloop then
    cposs[cloop3, rloop3, cell[cloop,rloop,bloop] ] := 1;
    end;
    end;
    end;
    end;

    {no possibility for that number in the cells in that column}
    for bloop4 := 1 to 9 do
    begin
    for rloop4 := 1 to 9 do
    begin
    for cloop4 := 1 to 9 do
    begin
    if cell[cloop4,rloop4,bloop4] < 10 then
    begin
    if cloop4 = cloop then
    cposs[cloop4, rloop4, cell[cloop,rloop,bloop] ] := 1;
    end;
    end;
    end;
    end;

    end;

    end;

    {checks if cell has only one possible answer}
    for loop := 1 to 9 do
    begin
    if cposs[cloop,rloop,loop] = 0 then
    c := c + 1;
    end;
    if c = 1 then
    begin
    for loop := 1 to 9 do
    begin
    if cposs[cloop, rloop, loop] = 0 then
    cell[cloop,rloop,bloop] := loop;
    end;
    end;

    end;
    end;
    end;
    {checks if sudoku is finished}
    for bloop5 := 1 to 9 do
    begin
    for rloop5 := 1 to 9 do
    begin
    for cloop5 := 1 to 9 do
    begin
    if cell[cloop5, rloop5, bloop5] = 0 then
    d := d + 1;
    end;
    end;
    end;
    if d = 0 then solved := true;

    until solved = true;

    {copying}
    for bloop := 1 to 9 do
    begin

    for rloop := 1 to 9 do
    begin

    for cloop := 1 to 9 do
    begin

    if cell[cloop,rloop,bloop] < 10 then
    begin

    output[cloop,rloop] := cell[cloop,rloop,bloop];
    end;
    end;
    end;
    end;

    writeln;

    {printing}
    for rloop := 1 to 9 do
    begin

    for cloop := 1 to 9 do
    begin

    write(output[cloop,rloop],' ');
    end;
    writeln;

    end;
    writeln;
    writeln('Solved!');
    readln;
    readln;

    end.[/code]
  • SwithSwith Posts: 4Member
    You're absolutely right about needing to change those, and doing so does stop the row of zeroes appearing when I enter a solved sudoku, but the program still doesn't solve an unsolved sudoku; it refuses input after you enter the final line, suggesting that it is stuck in a loop. Any ideas why?

    [code]program sudoku;

    var
    input : array[1..9, 1..9] of integer;
    cell : array[1..9, 1..9, 1..9] of integer;
    output : array[1..9, 1..9] of integer;
    cposs : array[1..9, 1..9, 1..9] of integer;
    cloop, cloop2, rloop, rloop2, bloop, bloop2, loop, loop2, loop3, c, d : integer;
    cloop3, rloop3, bloop3 : integer;
    cloop4, rloop4, bloop4 : integer;
    cloop5, rloop5, bloop5 : integer;
    solved : boolean;

    begin



    {sets all items of input array to 0}
    for loop := 1 to 9 do
    begin

    for loop2 := 1 to 9 do

    input[loop2,loop] := 0;
    end;

    {sets all items of cell array to 10}
    for loop := 1 to 9 do
    begin

    for loop2 := 1 to 9 do
    begin

    for loop3 := 1 to 9 do
    begin

    cell[loop3,loop2,loop] := 10;
    end;
    end;
    end;

    {instructions}
    writeln('== Sudoku Solver ==':50);
    writeln;
    writeln;
    writeln('Please enter the numbers of your sudoku from left to right, one row at a time from top to bottom, WITH A SPACE BETWEEN EACH CHARACTER, pressing enter after each row. If the cell is empty, please write a 0.');
    writeln;

    {reading inputs}
    for rloop := 1 to 9 do

    begin

    for cloop := 1 to 9 do

    read( input[cloop,rloop] );
    end;

    {copies inputs to "cell" array, and adds box position}
    for rloop := 1 to 9 do
    begin

    for cloop := 1 to 9 do
    begin

    if cloop in [1..3] then
    if rloop in [1..3] then
    cell[cloop,rloop,1] := input[cloop,rloop] else

    if rloop in [4..6] then
    cell[cloop,rloop,4] := input[cloop,rloop] else

    if rloop in [7..9] then
    cell[cloop,rloop,7] := input[cloop,rloop];


    if cloop in [4..6] then
    if rloop in [1..3] then
    cell[cloop,rloop,2] := input[cloop,rloop] else

    if rloop in [4..6] then
    cell[cloop,rloop,5] := input[cloop,rloop] else

    if rloop in [7..9] then
    cell[cloop,rloop,8] := input[cloop,rloop];

    if cloop in [7..9] then
    if rloop in [1..3] then
    cell[cloop,rloop,3] := input[cloop,rloop] else

    if rloop in [4..6] then
    cell[cloop,rloop,6] := input[cloop,rloop] else

    if rloop in [7..9] then
    cell[cloop,rloop,9] := input[cloop,rloop];
    end;
    end;


    repeat


    c := 0;
    d := 0;

    {the big block}
    for bloop := 1 to 9 do
    begin

    for rloop := 1 to 9 do
    begin

    for cloop := 1 to 9 do
    begin

    {checks if it is possible to have cell in that box
    by checking if a value from input has been copied
    over (the value wont be 10}
    if cell[cloop,rloop,bloop] < 10 then
    begin
    if cell[cloop,rloop,bloop] > 0 then
    begin

    {no possibility for that number in the cells in this box}
    for bloop2 := 1 to 9 do
    begin

    for rloop2 := 1 to 9 do
    begin
    for cloop2 := 1 to 9 do
    begin
    if cell[cloop2,rloop2,bloop2] < 10 then
    begin
    if bloop2 = bloop then
    cposs[cloop2, rloop2, cell[cloop,rloop,bloop] ] := 1;
    end;
    end;
    end;
    end;

    {no possibility for that number in the cells in that row}
    for bloop3 := 1 to 9 do
    begin
    for rloop3 := 1 to 9 do
    begin
    for cloop3 := 1 to 9 do
    begin
    if cell[cloop3,rloop3,bloop3] < 10 then
    begin
    if rloop3 = rloop then
    cposs[cloop3, rloop3, cell[cloop,rloop,bloop] ] := 1;
    end;
    end;
    end;
    end;

    {no possibility for that number in the cells in that column}
    for bloop4 := 1 to 9 do
    begin
    for rloop4 := 1 to 9 do
    begin
    for cloop4 := 1 to 9 do
    begin
    if cell[cloop4,rloop4,bloop4] < 10 then
    begin
    if cloop4 = cloop then
    cposs[cloop4, rloop4, cell[cloop,rloop,bloop] ] := 1;
    end;
    end;
    end;
    end;

    end;

    end;

    {checks if cell has only one possible answer}
    for loop := 1 to 9 do
    begin
    if cposs[cloop,rloop,loop] = 0 then
    c := c + 1;
    end;
    if c = 1 then
    begin
    for loop := 1 to 9 do
    begin
    if cposs[cloop, rloop, loop] = 0 then
    cell[cloop,rloop,bloop] := loop;
    end;
    end;

    end;
    end;
    end;
    {checks if sudoku is finished}
    for bloop5 := 1 to 9 do
    begin
    for rloop5 := 1 to 9 do
    begin
    for cloop5 := 1 to 9 do
    begin
    if cell[cloop5, rloop5, bloop5] = 0 then
    d := d + 1;
    end;
    end;
    end;
    if d = 0 then solved := true;

    until solved = true;

    {copying}
    for bloop := 1 to 9 do
    begin

    for rloop := 1 to 9 do
    begin

    for cloop := 1 to 9 do
    begin

    if cell[cloop,rloop,bloop] < 10 then
    begin

    output[cloop,rloop] := cell[cloop,rloop,bloop];
    end;
    end;
    end;
    end;

    writeln;

    {printing}
    for rloop := 1 to 9 do
    begin

    for cloop := 1 to 9 do
    begin

    write(output[cloop,rloop],' ');
    end;
    writeln;

    end;
    writeln;
    writeln('Solved!');
    readln;
    readln;

    end.[/code]
  • quikcarlxquikcarlx Hollywood, FlPosts: 26Member
    Okay, the obvious problems has been taken care of. Now for the difficult. I use Free Pascal Compiler 2.6.0, [color=Purple]http://www.freepascal.org[/color], with the IDE. It has Debug and works quite well if you take the time to check out the features. So, set up your variables to watch, step through the program to see what's going on with each statement. If you're like me, you'll have an intuitive feel what it should be doing and what the variables should be. I've done this with many programs to discover my blunders and misthoughts. Debug can really find those problems that you thought weren't happenning.
Sign In or Register to comment.