Sudoku Puzzle Creater / Solver

Submitted by Kevin on Sun, 07/22/2007 - 20:19.

Will create or solve Sudoku grid. Redirect the output into an html file and open with a browser. Interesting technical note: use of goto at line 278...so sue me!

 

#!/usr/bin/perl -w

# sudoku puzzle creator

my @grid = (
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 4, 5, 6, 7, 8, 9, 1, 2, 3 ],
[ 7, 8, 9, 1, 2, 3, 4, 5, 6 ],

[ 5, 6, 7, 8, 9, 1, 2, 3, 4 ],
[ 8, 9, 1, 2, 3, 4, 5, 6, 7 ],
[ 2, 3, 4, 5, 6, 7, 8, 9, 1 ],

[ 9, 1, 2, 6, 7, 8, 3, 4, 5 ],
[ 3, 4, 5, 9, 1, 2, 6, 7, 8 ],
[ 6, 7, 8, 3, 4, 5, 9, 1, 2 ] );


# alternatively fill this array with a existing puzzle to need solved ( 0=values to find)
# and set $numberToRemove to 0 in the main program

#@grid = (
# [ 0, 0, 0, 0, 0, 0, 3, 0, 0 ],
# [ 2, 1, 3, 0, 0, 0, 5, 0, 7 ],
# [ 4, 0, 0, 7, 1, 0, 0, 0, 0 ],
# [ 8, 6, 0, 0, 9, 0, 0, 7, 0 ],
# [ 9, 0, 0, 5, 0, 0, 1, 2, 6 ],
# [ 0, 0, 5, 6, 0, 0, 0, 0, 0 ],
# [ 0, 0, 7, 0, 5, 4, 0, 3, 1 ],
# [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
# [ 3, 0, 1, 0, 0, 6, 0, 0, 9 ] );

#@grid = (
# [ 7, 0, 0, 0, 0, 5, 3, 1, 0 ],
# [ 2, 1, 3, 0, 0, 0, 5, 0, 7 ],
# [ 4, 5, 0, 7, 1, 3, 0, 0, 0 ],
# [ 8, 6, 2, 3, 9, 1, 4, 7, 5 ],
# [ 9, 0, 4, 5, 0, 0, 1, 2, 6 ],
# [ 1, 0, 5, 6, 4, 2, 0, 0, 0 ],
# [ 6, 0, 7, 0, 5, 4, 0, 3, 1 ],
# [ 5, 0, 0, 1, 3, 0, 0, 0, 0 ],
# [ 3, 0, 1, 0, 0, 6, 0, 5, 9 ] );

JumbleGrid(@grid);

print "<table><tr><td>Starting grid...</td></tr></table>\n";
PrintGrid(@grid);


my $i;
my $numberToRemove = 35;
for ( $i=0; $i < $numberToRemove; $i++ ) {
$lastgoodgrid = $grid;
$rnd1 = int(rand()*9);
$rnd2 = int(rand()*9);
$grid[$rnd1][$rnd2] = 0;
}
$grid = $lastgoodgrid;

print "<table><tr><td>Grid with removed numbers...</td></tr></table>\n";
PrintGrid(@grid);

SolveGrid(map [@$_], @grid);

sub JumbleGrid {
@grid = @_;

for ( $j=0; $j<9; $j++ ) {
for ( $i=0; $i < 9; $i += 3 ) {
$rnd1 = int(rand()*3);
$rnd2 = int(rand()*3);

while ( $rnd1 == $rnd2 ) {
$rnd2 = int(rand()*3);
}
my $r1 = $i+$rnd1;
my $r2 = $i+$rnd2;
SwapRows($r1, $r2, @grid);
}

for ( $i=0; $i < 9; $i += 3 ) {
$rnd1 = int(rand()*3);
$rnd2 = int(rand()*3);

while ( $rnd1 == $rnd2 ) {
$rnd2 = int(rand()*3);
}
my $c1 = $i+$rnd1;
my $c2 = $i+$rnd2;
SwapCols($c1, $c2, @grid);
}
$rnd1 = int(rand()*8)+1;
$rnd2 = int(rand()*8)+1;

while ( $rnd1 == $rnd2 ) {
$rnd2 = int(rand()*8)+1;
}

SwapNumbers($rnd1, $rnd2, @grid);
}
return @grid;
}

sub PrintGrid {
print "<table border=\"1\">\n";
my @grid = @_;
for ( $y = 0; $y < 9; $y++ ) {
print "<tr>";
for ( $x = 0; $x < 9; $x++ ) {
if ( ( $grid[$x][$y] ) > 0 ) {
print "<td>".$grid[$x][$y] . "</td>";
} else {
print "<td> </td>";
}


if (( ($x+1) % 3 ) == 0 ) {
print "<td></td>";
}

}
if (( ($y+1) % 3 ) == 0 ) {
print "<tr><td colspan=\"11\"> </td></tr>\n";
} else {
print "</tr>\n";
}
}
}

sub PrintHelper {
print "<table border=\"1\">\n";
my @grid = @_;
for ( $y = 0; $y < 9; $y++ ) {
print "<tr>";
for ( $x = 0; $x < 9; $x++ ) {
if ( ( $grid[$x][$y] ) == 0 ) {
my @c = CheckSquare($x, $y, 0, @grid);
print "<td> @c </td>";
} else {
print "<td>".$grid[$x][$y]."</td>";
}


if (( ($x+1) % 3 ) == 0 ) {
print "<td></td>";
}

}
if (( ($y+1) % 3 ) == 0 ) {
print "<tr><td colspan=\"11\"> </td></tr>\n";
} else {
print "</tr>\n";
}
}
}



sub SwapRows {
my ( $row1, $row2, @grid ) = @_;
my $t;

for ( $x = 0; $x < 9; $x++ ) {
$t = $grid[$x][$row1];
$grid[$x][$row1] = $grid[$x][$row2];
$grid[$x][$row2] = $t;
}
}

sub SwapCols {
my ( $col1, $col2, @grid ) = @_;
my $t;

for ( $y = 0; $y < 9; $y++ ) {
$t = $grid[$col1][$y];
$grid[$col1][$y] = $grid[$col2][$y];
$grid[$col2][$y] = $t;
}
}



sub SwapNumbers {
my ( $num1, $num2, @grid ) = @_;

for ( $y = 0; $y < 9; $y++ ) {
for ( $x = 0; $x < 9; $x++ ) {
if ( $grid[$x][$y] == $num1 ) {
$grid[$x][$y] = $num2;
} else {
if ( $grid[$x][$y] == $num2 ) {
$grid[$x][$y] = $num1;
}
}
}
}
}


# check x,y co-ordinate and return numbers possible in this square
sub CheckSquare {
my ( $x, $y, $complexrules, @grid ) = @_;

my $i;

my @possibles = ( 0,1,1,1,1,1,1,1,1,1 );



# rule 1 - check the row
for ( my $i=0; $i < 9; $i++ ) {
$possibles[ $grid[$i][$y] ] = 0;
}

# rule 2 - check the column
for ( my $i=0; $i < 9; $i++ ) {
$possibles[ $grid[$x][$i] ] = 0;
}

# rule 3 - check the enclosing 3x3 grid
my $sx = (int( $x / 3 )) * 3;
my $sy = (int( $y / 3 )) * 3;

for ( my $i=0; $i < 3; $i++ ) {
for ( my $j=0; $j < 3; $j++ ) {
$possibles[ $grid[$sx+$i][$sy+$j] ] = 0;
}
}

# check what we've done...if we have a exact match then return
my @choice = ();
my $c = 0;

# go through possible values and pick out ones
for ( $i=1; $i <=9; $i++ ) {
if ( $possibles[$i] > 0 ) {
$choice[$c++] = $i;
}
}

# if we have an exact match dont bother with the complex rules
if ( scalar @choice == 1 ) {
return @choice;
}

if ( $complexrules ) {
if ( $x == 8 && $y == 1 ) {
print "\n";
}
# @choice contains matches so far, check if any of these appear just the once

# rule 4 - a unique number in 3x3 grid
$sx = (int( $x / 3 )) * 3;
$sy = (int( $y / 3 )) * 3;

%tNum = ();
@tCh = ();
for ( my $i=0; $i < 3; $i++ ) {
for ( my $j=0; $j < 3; $j++ ) {
my @tCh;
if ( $grid[$i][$j] == 0 ) {
@tCh = CheckSquare($i, $j, 0, @grid);
foreach my $myc ( @tCh ) {
$tNum{$myc}++;
}
}
}
}

# now check if any choices appear just once...

foreach my $tco ( @choice ) {
if ( exists ( $tNum{$tco} ) ) {
if ( $tNum{$tco} == 1 ) {
#print "Matched $tco at $x, $y using rule 4!\n";
@possibles = ( 0,0,0,0,0,0,0,0,0,0 );
$possibles[$tco] = 1;
goto FOUND;
}
}
}
# rule 5 - a unique number in row
%tNum = ();
@tCh = ();
for ( my $i=0; $i < 9; $i++ ) {
if ( $grid[$i][$y] == 0 ) {
@tCh = CheckSquare($i, $y, 0, @grid);
foreach my $myc ( @tCh ) {
$tNum{$myc}++;
}
}
}

foreach my $tco ( @choice ) {
if ( exists ( $tNum{$tco} ) ) {
if ( $tNum{$tco} == 1 ) {
#print "Matched to $tco at $x, $y using rule 6!\n";
@possibles = ( 0,0,0,0,0,0,0,0,0,0 );
$possibles[$tco] = 1;
goto FOUND;
}
}
}


# rule 6 - a unique number in col
%tNum = ();
@tCh = ();
for ( my $i=0; $i < 9; $i++ ) {
if ( $grid[$x][$i] == 0 ) {
@tCh = CheckSquare($x, $i, 0, @grid);
foreach my $myc ( @tCh ) {
$tNum{$myc}++;
}
}
}

foreach my $tco ( @choice ) {
if ( exists ( $tNum{$tco} ) ) {
if ( $tNum{$tco} == 1 ) {
#print "Matched to $tco at $x, $y using rule 6!\n";
@possibles = ( 0,0,0,0,0,0,0,0,0,0 );
$possibles[$tco] = 1;
goto FOUND;
}
}
}

}
FOUND:

@choice = ();
$c = 0;

#if ( $grid[$x][$y] != 0 ) {
# return @choice;
#}
# go through possible values and pick out ones
for ( $i=1; $i <=9; $i++ ) {
if ( $possibles[$i] > 0 ) {
$choice[$c++] = $i;
}
}

return @choice;
}

sub SolveGrid {
my @grid = @_;

# find square with smallest number of possibles

my $foundR;
my $foundC;
my @myChoices;

my $minVal = 10;

for ( my $c=0; $c < 9; $c++ ) {
for ( my $r=0; $r < 9; $r++ ) {
if ( $grid[$r][$c] == 0 ) {
@choices = CheckSquare($r, $c, 1, @grid );

if ( @choices < $minVal ) {
$minVal = @choices;
$foundR = $r;
$foundC = $c;
@myChoices = @choices;
}
}
}
}

if ( $minVal == 0 ) {
# jeeez! this grid gone screwy...try another approach
return 0;
}

if ( $minVal < 10 ) {
if ( scalar @myChoices > 1 ) {
print "<table><tr><td>Minimum number of choices at $foundR, $foundC [ @myChoices ]</td></tr></table>\n";
print "<table><tr><td>Current grid...</td></tr></table>\n";
PrintGrid(@grid);
print "<table><tr><td>Helper...</td></tr></table>\n";
PrintHelper(@grid);
}
foreach my $tryNum ( @myChoices ) {
$grid[$foundR][$foundC] = $tryNum;
if ( SolveGrid(map [@$_], @grid) ) {
last;
}
}
} else {
print "<table><tr><td>Solved!!</td></tr></table>\n\n";
PrintGrid(@grid);

return 1;
}
}

generic lasix, buy generic

buy clomid cheap, amoxil

The posts you have on your

The posts you have on your site are always so enjoyable to read. I also like how you have your website designed. Good work and I will be returning often. HDTV antenna | Home Theater Installation Los Angeles | DTV antenna | Home Theater Installation Chicago | Digital Signage | electronic cigarette | Digital Signage | TV Antenna Installation

buy lasix, lexapro order

The posts you have on your

The posts you have on your site are always very interesting to read. Will keep you bookmarked.Thanks.
Drug Testing | E Cigarette Preston | E Cigarette Norwich | E Cigarette Ipswich | E Cigarette Edinburgh | E Cigarette Derby | E Cigarette Chelmsford

order zithromax online,

soma online

generic cialis

order tramadol

Reply topic

Add Comment Submitted by celebrex buy now http://virtualatdp.berkeley.edu:8081/apbiology/~celebrexonline buy generic celebrex celebrex price celebrex online prescription ; order abilify http://www.ibm.com/developerworks/wikis/display/~leonccarvajal abilify overnight shipping abilify price cheap abilify ; cialis low prices http://virtualatdp.berkeley.edu:8081/apbiology/~cheapcialis cialis oral cialis cost take cialis ; cialis at the price http://virtualatdp.berkeley.edu:8081/apbiology/~genericcialis cialis online price order cialis cialis oral ; sell hydrocodone online http://virtualatdp.berkeley.edu:8081/apbiology/~hydrocodone order hydrocodone hydrocodone generic online cheap hydrocodone ; fioricet medication http://virtualatdp.berkeley.edu:8081/apbiology/~cheapfioricet best prices on fioricet generic fioricet price cheap fioricet generic ; kamagra on line http://virtualatdp.berkeley.edu:8081/apbiology/~cheapkamagra kamagra lowest price price kamagra search kamagra for sale ; kamagra net price http://virtualatdp.berkeley.edu:8081/apbiology/~kamagraonline buy kamagra kamagra online prescription kamagra minimum price ; levitra net price http://virtualatdp.berkeley.edu:8081/apbiology/~cheaplevitra levitra buy now order levitra discount levitra ; levitra no prescription http://virtualatdp.berkeley.edu:8081/apbiology/~genericlevitra cheap levitra levitra rx buy levitra ; buy lexapro cheap http://virtualatdp.berkeley.edu:8081/apbiology/~cheaplexapro buying lexapro lexapro online price buy lexapro online ; lexapro best prices http://virtualatdp.berkeley.edu:8081/apbiology/~lexaproonline lexapro drug discount lexapro buy lexapro cod ; pill lorazepam http://virtualatdp.berkeley.edu:8081/apbiology/~cheaplorazepam lorazepam buy price buy generic lorazepam lorazepam purchase price ; meridia cost http://virtualatdp.berkeley.edu:8081/apbiology/~cheapmeridia meridia sale meridia price best prices for meridia ; phentermine sale http://virtualatdp.berkeley.edu:8081/apbiology/~cheapphentermine phentermine on line phentermine cost phentermine best price online ; buying sildenafil l http://virtualatdp.berkeley.edu:8081/apbiology/~cheapsildenafil sildenafil rx order sildenafil price sildenafil search ; supplements sleep aid http://virtualatdp.berkeley.edu:8081/apbiology/~cheapsleepwell sleeping aid sleep aid sleeping disorders ; buy generic tadalafil http://virtualatdp.berkeley.edu:8081/apbiology/~cheaptadalafil tadalafil no prescription tadalafil generic online order tadalafil ; best prices on tamiflu http://virtualatdp.berkeley.edu:8081/apbiology/~cheaptamiflu order tamiflu tamiflu medication tamiflu drug ; tramadol cheapest prices http://virtualatdp.berkeley.edu:8081/apbiology/~cheaptramadol tramadol cost buy tramadol cheap tramadol rx ; price ultram search http://virtualatdp.berkeley.edu:8081/apbiology/~cheapultram ultram best price online best prices on ultram ultram minimum price ; valium sale http://virtualatdp.berkeley.edu:8081/apbiology/~cheapvalium valium overnight shipping order valium price best prices on valium ; vardenafil purchase price http://virtualatdp.berkeley.edu:8081/apbiology/~cheapvardenafil vardenafil online vardenafil sale vardenafil at the price ; viagra low prices http://virtualatdp.berkeley.edu:8081/apbiology/~cheapviagra take viagra viagra low prices link price viagra ; viagra generic online http://virtualatdp.berkeley.edu:8081/apbiology/~viagraonline viagra minimum price viagra overnight shipping best prices on viagra ; best prices on vicodin http://virtualatdp.berkeley.edu:8081/apbiology/~cheapvicodin best prices for vicodin vicodin best price online order vicodin online ; cheap xanax http://virtualatdp.berkeley.edu:8081/apbiology/~cheapxanax xanax for sale purchase xanax xanax on line ; zyprexa on line http://virtualatdp.berkeley.edu:8081/apbiology/~cheapzyprexa zyprexa oral best prices for zyprexa zyprexa buy price ; zyprexa cost http://virtualatdp.berkeley.edu:8081/apbiology/~zyprexaonline purchase zyprexa cheap zyprexa order zyprexa online ; aciphex minimum price http://virtualatdp.berkeley.edu:8081/apbiology/~cheapaciphex cheapest aciphex online aciphex buy now aciphex best prices ; aciphex online prescription http://virtualatdp.berkeley.edu:8081/apbiology/~aciphexonline generic aciphex aciphex purchase price aciphex black price ; price for actonel http://virtualatdp.berkeley.edu:8081/apbiology/~cheapactonel cheap actonel generic actonel oral actonel oral ; cheap actonel generic http://virtualatdp.berkeley.edu:8081/apbiology/~actonelonline actonel on line cheapest actonel online generic actonel price ; take actos http://virtualatdp.berkeley.edu:8081/apbiology/~cheapactos sell actos online actos online prescription actos purchase price ; discount actos http://virtualatdp.berkeley.edu:8081/apbiology/~actosonline actos purchase price generic actos price actos best prices ; arimidex online http://virtualatdp.berkeley.edu:8081/apbiology/~cheaparimidex buy arimidex online arimidex rx cheap arimidex generic ; cheap arimidex http://virtualatdp.berkeley.edu:8081/apbiology/~arimidexonline take arimidex arimidex no prescription arimidex buy now ; price avodart search http://virtualatdp.berkeley.edu:8081/apbiology/~cheapavodart order avodart online discount avodart avodart no prescription ; avodart cost http://virtualatdp.berkeley.edu:8081/apbiology/~avodartonline buy avodart cheap price avodart search avodart lowest price ; januvia overnight shipping http://virtualatdp.berkeley.edu:8081/apbiology/~cheapjanuvia buy januvia januvia price januvia lower price ; januvia at the price http://virtualatdp.berkeley.edu:8081/apbiology/~januviaonline januvia buy now order januvia online januvia generic online ; cheap namenda generic http://virtualatdp.berkeley.edu:8081/apbiology/~cheapnamenda generic namenda price order namenda namenda minimum price ; buy generic nasonex http://virtualatdp.berkeley.edu:8081/apbiology/~cheapnasonex buying nasonex nasonex for sale buy nasonex online ; nexium best prices http://virtualatdp.berkeley.edu:8081/apbiology/~cheapnexium nexium on line generic nexium price buy nexium ; buy nexium online http://virtualatdp.berkeley.edu:8081/apbiology/~nexiumonline nexium for sale buy generic nexium generic nexium ; plavix buy now http://virtualatdp.berkeley.edu:8081/apbiology/~cheapplavix plavix sale buy plavix cheap buy plavix cod ; plavix sale http://virtualatdp.berkeley.edu:8081/apbiology/~plavixonline purchase plavix plavix online plavix overnight shipping ; order prevacid http://virtualatdp.berkeley.edu:8081/apbiology/~cheapprevacid buying prevacid order prevacid price order prevacid price ; take prevacid http://virtualatdp.berkeley.edu:8081/apbiology/~prevacidonline prevacid oral price for prevacid prevacid medication ; prograf net price http://virtualatdp.berkeley.edu:8081/apbiology/~cheapprograf prograf no prescription prograf low prices order prograf ; buy prograf cod http://virtualatdp.berkeley.edu:8081/apbiology/~prografonline order prograf buy prograf buy generic prograf ; protonix no prescription http://virtualatdp.berkeley.edu:8081/apbiology/~cheapprotonix protonix online protonix best price online protonix rx ; protonix lower price http://virtualatdp.berkeley.edu:8081/apbiology/~protonixonline protonix best price online best prices on protonix price protonix search ; pill provigil http://virtualatdp.berkeley.edu:8081/apbiology/~cheapprovigil provigil price best prices for provigil prices on provigil ; risperdal purchase price http://virtualatdp.berkeley.edu:8081/apbiology/~cheaprisperdal cheap risperdal risperdal best prices risperdal online prescription ; risperdal low prices http://virtualatdp.berkeley.edu:8081/apbiology/~risperdalonline take risperdal risperdal buy now link price risperdal ; seroquel generic online http://virtualatdp.berkeley.edu:8081/apbiology/~cheapseroquel price seroquel search seroquel overnight shipping buy seroquel cod ; seroquel rx http://virtualatdp.berkeley.edu:8081/apbiology/~seroquelonline seroquel medication seroquel lowest price order seroquel online ; sell singulair online http://virtualatdp.berkeley.edu:8081/apbiology/~cheapsingulair singulair sale singulair at the price singulair online ; buy generic singulair http://virtualatdp.berkeley.edu:8081/apbiology/~singulaironline cheap singulair buy singulair cod singulair for sale ; abilify on line http://virtualatdp.berkeley.edu:8081/apbiology/~genericabilify abilify net price cheap abilify order abilify price ; adipex cheapest prices http://virtualatdp.berkeley.edu:8081/apbiology/~genericadipex cheapest adipex online adipex online adipex buy price ;ambien online prescription http://virtualatdp.berkeley.edu:8081/apbiology/~cheapambien discount ambien generic ambien ambien black price ; adipex lowest price http://virtualatdp.berkeley.edu:8081/apbiology/~cheapadipex adipex lowest price" >pill adipex ; ativan best prices http://virtualatdp.berkeley.edu:8081/apbiology/~cheapativan ativan overnight shipping order ativan price buying ativan ; take carisoprodol http://virtualatdp.berkeley.edu:8081/apbiology/~carisoprodol generic carisoprodol price carisoprodol rx buying carisoprodol ; pill caverta http://virtualatdp.berkeley.edu:8081/apbiology/~caverta caverta online price caverta overnight shipping take caverta ; celebrex online http://virtualatdp.berkeley.edu:8081/apbiology/~cheapcelebrex best prices on celebrex celebrex cheapest prices sell celebrex online ; valium lowest price http://talk.rachaelraymag.com/members/Mark_5F00_G/default.aspx valium best prices sell valium online buy valium cheap ; valium sale http://mipagina.univision.com/leonccarvajal valium drug valium cost buy valium online

buy viagra online

cheers!

bonjorno!

hello!

hello

Sweet! comprare Cialis in Italia ngerew!

An interesting puzzle games

An interesting puzzle games involving simple maths,nice puzzle game for all generations.

The concept of devices that

The concept of devices that operate following a pre-defined set of instructions traces back to Greek Mythology, notably Hephaestus and his mechanical servants. The Antikythera mechanism was a calculator dedicated servers utilizing gears of various sizes and configuration to determine its operation. The earliest known programmable machines (machines whose behavior can be controlled and predicted with a set of instructions) were Al-Jazari's programmable Automata in 1206.One of Al-Jazari's robots was originally a boat with four automatic web designer musicians that floated on a lake to entertain guests at royal drinking parties. Programming this mechanism's behavior meant placing pegs and cams into a wooden drum at specific locations. These would then bump into little levers that operate a percussion instrument. The output of this device was a small drummer playing various rhythms and drum patterns.Another sophisticated programmable machine by Al-Jazari was the castle search engine optimization clock, notable for its concept of variables which the operator could manipulate as necessary (i.e. the length of day and night). The Jacquard Loom, which Joseph Marie Jacquard developed in 1801, uses a series of pasteboard cards with holes punched in them. The hole pattern represented the pattern that the loom had to follow in weaving cloth. The loom could produce entirely different weaves using different sets of cards. Charles Babbage adopted the use of punched cards around 1830 to control his Analytical Engine. The synthesis of numerical calculation, predetermined operation and output internet phone service, along with a way to organize and input instructions in a manner relatively easy for humans to conceive and produce, led to the modern development of computer programming. Development of computer programming accelerated through the Industrial Revolution.

hello