/    Sign up×
Community /Pin to ProfileBookmark

online php-based sudoku solver

[code=php]
<?php

$puzzle = array();

//get the known
foreach($_POST as $coordinates => $value){
//make sure that its posted coordinates
if(substr($coordinates, 0, 5)== “coord”){
//strip the string “coord” from the beginning of the coordinates
$coordinates = str_replace(“coord”, “”, $coordinates);
//make sure that its only two integers
$coordinates = int(substr($coordinates, 0, 2));
//stick it’s value in the array
$puzzle[$coordinates][$value] = TRUE;
}
}

//fill in the rest of the puzzle with all possibilites
for ($x = 0; $x <= 8; $x++) {
for ($y = 0; $y <= 8; $y++) {
if(!array_key_exists($x . $y, $puzzle)){
for ($i = 1; $i <= 9; $i++) {
$puzzle[$x . $y][$i] = TRUE;
}
}
}
}

function get_box(){

}

function is_in_row($row, $number){

}

function is_in_column($blankRow, $blankColumn){

}

function is_in_box(){

}

function is_solved(){

}

while(!is_solved()){

}
?>

<fieldset>
<form method=”post” action=”<?php $_SERVER[‘PHP_SELF’] ?>”>
<table>
<?php
for ($x = 0; $x <= 8; $x++) {
echo “nt” . ‘<tr>’;
for ($y = 0; $y <= 8; $y++) {
echo “ntt” . ‘<td>’;
echo “nttt” . ‘<input type=”text” name=”coord’ . $x . $y . ‘” />’;
echo “ntt” . ‘</td>’;
}
echo “nt” . ‘</tr>’;
}
?>
</table>
</form>
</fieldset>
[/code]

this is my source so far. ive seen a couple nice sudoku sites around before, and i wanted to see if i could make a nicer one. i know im probably getting in over my head, but i wanted to see how far i could take this. quickly ran into my first problem, the server times out before the array of all possibilities is finished. the solvers ive seen around have solved the puzzle instantly. i’d rather not have to change the server configs to be able to work on one page for 10 minutes, because the average visitor isnt going to sit around that long anyway. right now it times out at a minute. is there something im doing wrong? will this just not be fast enough with php? would it be faster if i used a bunch of variables instead of a big array? HELP!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@cridleyJul 12.2006 — Haven't read your code, but I feel solving sudoku by brute force(ie trying all the diff possibilities) is going to take awhile. I think you need an algorithm that solves it in a similar way to the way a human would, ie go through each 9x9 box in turn, filling what you can, then each row then column, then loop until it's done. This is obviously a lot more complicated to code, but should reach a solution a lot faster than just trying every combo then checking to see if it's correct (I haven't done the maths but I'm guessing there are a LOT of combinations, which would take some time in any language due to the n# of operations) Having played sudoku myself, I have often considered how one would go about writing a prog to solve it, just never had the time.

Don't know if this helps.
Copy linkTweet thisAlerts:
@gameguy43authorJul 12.2006 — of course i wasn't trying to brute force. the approach is to put a list of all possibilities for each square into an array, then use logic to cross off the ones that dont work until the whole thing is solved. whats timing out the server is simply making this array of all possibilities for each square! (this is different from writing out all combinations)
Copy linkTweet thisAlerts:
@cridleyJul 12.2006 — Ok, sorry, my mistake, I misunderstood what you were saying.
Copy linkTweet thisAlerts:
@gameguy43authorJul 12.2006 — no, my misteak. i figured out the problem. the while statement after all those blank functions was an infinite loop cus it ran while a blank function returned nothing. i'm still looking for any suggestions, but the timing out problem is solved.
×

Success!

Help @gameguy43 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...