Jump to content

PHP sudoku maker any ideas?


cooldude832

Recommended Posts

This idea just came to me(i don't know its like a gift (guess what movie that's from)) making a sudoku (single level) off php.  Anyone got a logical idea on doing it, I know i need to generate 81 random numbers and then compare it to values, but i have a feeling since its is a top down sort of system i will get to the last squares and it will never find a match and end up making dead puzzles any ideas?

Link to comment
Share on other sites

Doing the if then or switch (select case) statements is probably the easiest thing to do. Then the next would be the displaying.

 

I think perhaps you were looking for some magical formula or equation or algorithm or similar.

 

Why not keep it simplistic and make the puzzle howyou would by hand, check this out:

http://www.sudokuonline.us/make_your_own_sudoku_puzzle.php

 

I know that once you've done it I would be interested in the code.  Sounds like a fun thing to actually create too.

 

May I suggest perhaps AJAX or JS for the actual game play.

Can't wait to see your finished product :D

Link to comment
Share on other sites

I got it going except i want to define the 3x3 boxes as i go and i'm having trouble figuring out how to do it in logic, this is what i got

<?php
<?php
DEFINE("MAX", 9);
$board = array();
$colored = 0;
while($colored <=27){
$temp1 = rand(1,MAX);
$temp2 = rand(1,MAX);
$square = $temp1."_".$temp2;
if(empty($board[$square])){
	$board[$square] = "colored";
	$colored++;
}
}
$i = 1;
while($i <=MAX){
$j = 1;
while($j <=MAX){
	$temp = $j."_".$i;
	if(empty($board[$temp])){
		$board[$temp] = "empty";
	}
	$j++;
}
$i++;
}
?>
<html>
<head>
<style type="text/css">
td{
	width: 48px;
	height: 40px;
	border-style: solid;
}
td.filled{
	background-color: #0066CC;
}
tr.third{
	border-style: double;
}
</style>
</head>
<body>
<CENTER>
<table width="600">
<?php
$i = 1;
while($i <=MAX){
if($i%3 == 0){
	echo "<tr class=\"third\">\n";
}
else{
	echo "<tr>\n";
}
$j = 1;
while($j <=MAX){
	$temp = $j."_".$i;
	if($board[$temp] == "colored"){
		echo "<td class=\"filled\">".$temp."</td>";
		}
	else{
			echo "<td>".$temp."</td>";
	}
	$j++;
}
echo "\n</tr>\n";
$i++;
}
?>

Link to comment
Share on other sites

I reworked it with a lot of while loops to get the 28 blocked out squares so to speak of

<?php
DEFINE("MAX", 9);
$board = array();
$colored = 0;
while($colored <=27){
$temp1 = rand(1,MAX);
$temp2 = rand(1,MAX);
$square = $temp1."_".$temp2;
if(empty($board[$square])){
	$board[$square] = "colored";
	$colored++;
}
}
$i = 1;
while($i <=MAX){
$j = 1;
while($j <=MAX){
	$temp = $j."_".$i;
	if(empty($board[$temp])){
		$board[$temp] = "empty";
	}
	$j++;
}
$i++;
}
?>
<html>
<head>
<style type="text/css">
table.grid td{
	width: 48px;
	height: 40px;
	border-style: solid;
}
td.filled{
	background-color: #0066CC;
}
</style>
</head>
<body>
<CENTER>
<table width="600" cellspace="5">
<?php
$x = 1;
$y = 1;
$rows = 1;
while($rows <=3){
echo "<tr>";
$cols = 1;
	while($cols <=3){
		echo "<td>\n<table class=\"grid\">\n";
		$i = 1;
		while($i <=3){
			echo "<tr>\n";
			$j = 1;
				while($j <=3){
					$temp = $x."_".$y;
					if($board[$temp] == "colored"){
						echo "<td class=\"filled\">".$temp."</td>\n";
					}
					else{
						echo "<td>".$temp."</td>\n";
					}
					$j++;
					if($x <9){$x++;}
					else{$x = 1;}
				}
			echo "</tr>\n";
			$i++;
			if($y <9){$y++;}
			else{$y = 1;}
		}
		echo "</table>\n\n</td>";
		$cols++;
	}
echo "</tr>";
$rows++;
}

?>
</table>

it echos a nice table with the 3x3 all there

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.