Jump to content

[SOLVED] Sudoku Grid


gazever

Recommended Posts

Hi I'm trying to create a sudoku grid in css using nested tables, however I'm getting gaps between the cells and cannot seem to get rid of them, any suggestions, on gapless nested tables.

 

Hope that made sense.

 

Gaz

Link to comment
Share on other sites

Let me make a suggestion... this sounds to me like a good use for using divs in a creative way. The content is definitely tabular, but I would probably handle the layout with divs instead of tables anyway, just to give the extra edge of creativity to it. Here's an idea of what I've come up with:

<?php
$grid = array();
for ($i = 0; $i < 9; $i++) {
$grid[$i] = array();
for ($j = 0; $j < 9; $j++) {
	$grid[$i][$j] = rand(0, 9);
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link type="text/css" rel="stylesheet" href="styles.css" />
<style type="text/css">
#sudokuBoard {
border: 1px solid #454545;
height: 174px;
width: 174px;
}

#sudokuBoard .cell {
float: left;
border-top: 1px solid #454545;
border-left: 1px solid #454545;
height: 18px;
width: 18px;
}

#sudokuBoard .floor {
border-bottom: 1px solid #454545;
}

#sudokuBoard .wall {
border-right: 1px solid #454545;
}
</style>
</head>

<body>
<div id="sudokuBoard">
<?php
foreach ($grid as $y => $row) {
foreach ($row as $x => $cell) {
	$class = "cell";
	if (($y + 1) % 3 == 0) $class .= " floor";
	if (($x + 1) % 3 == 0) $class .= " wall";
	echo "<div class=\"$class\"></div>\n";
}
}
?>
</div>
</body>
</html>

 

This contains the CSS and PHP required to generate the markup. It works in both FF and IE for sure.

Link to comment
Share on other sites

Obsidian,

 

Thanks I love it, this is exactly what I need, I already have the php script to run through my array and make it into a table, but I would rather have divs any day and control it all by css, just been to lunch at work, and was considering using this option anyway, however not too clever with php yet, now just got to work out how to translate your script into my script.

 

Thanks

 

Gazever,

 

I'll post the link when its all running.

Link to comment
Share on other sites

???

 

Please help with this, don't really understand the 2d array, basically what i had before was this code to open my array which is in a txt file,

 

<?php
function makepuzzle($file)
{
//set initial variables -----
$majorcounter = 1;
$rowounter = 1;
$col = 1;
$row = 1;
// load data, make it an array -----------------
$myFile = $file;
$fh = fopen($myFile, 'r');
$data = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
$array = explode(',', $data);
array_unshift($array, "notneeded");
//print_r($array);
echo $file;

// use this array to draw the table ---------

echo '<div class="sudoku">
<table border="1">
<tr><TH COLSPAN="9">'; 
echo 'This is the puzzle';
echo '</TH></tr>
<tr>
';

// loop through all the numbers in the grid to draw them in the table
for ($i = 1; $i <=81; $i+=1) :

// check if the array is empty if so replace it with a space character
if ($array[$i] == "") {
$array[$i] = " ";
}
// check to see if its the end of a table row, if so close it then start a new one
if ($i == 9 || $i == 18 || $i == 27 || $i == 36 || $i == 45 || $i == 54 || $i == 63 || $i == 72) {
echo '<td>'.$array[$i].'</td></tr>
<tr>';
	}
// else draw the usual <td> cell if its within a table row			
	else {
echo '<td>'.$array[$i].'</td>';
		}
endfor;

// draw the end of the table and close the div
echo '</tr>
</table>
</div>';
}
?>

 

and the file looks like this

 

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

 

How do I get my array into the 2d type array you have, and what do I put in the <div>here bit</div>.

 

Thanks

Link to comment
Share on other sites

OK, here's how you can very easily load the numbers in your file into the array I'm using for my layout. Keep in mind that I'm going from left to right, top to bottom.

<?php
$string = "7,,4,8,,9,6,5,,1,,,,,6,3,4,7,,6,5,4,,3,,,,3,4,,2,9,8,,,,,,,7,6,5,,,,,,,1,3,4,,2,9,,,,6,,2,7,3,,8,2,6,3,,,,,4,,7,3,9,,1,2,,8";
$array = explode(',', $string);

$x = 0;
$grid = array();
for ($i = 0; $i < 9; $i++) {
$grid[$i] = array();
for ($j = 0; $j < 9; $j++) {
	$grid[$i][$j] = $array[$x++];
}
}

// Then, when you display it, you need to access the "$cell" variable:
foreach ($grid as $y => $row) {
foreach ($row as $x => $cell) {
	$class = "cell";
	if (($y + 1) % 3 == 0) $class .= " floor";
	if (($x + 1) % 3 == 0) $class .= " wall";
	echo "<div class=\"$class\">$cell</div>\n"; // Right here
}
}
?>

 

Make sense?

Link to comment
Share on other sites

Another problem,

 

Obsidian, I used the code and worked great, however resized it slightly larger, still looked fine, then opened up safari (main browser is firefox), gap round the outside container div, so just removed the border around this, still looked ok, however showed my g/f that night on her IE6 machine (promptly downloaded FF for her), and even more problems, when printed (these would need to be 100%reliable printed) lots of the cells reflowed. would it be more stable to develop this in a table, as I plan on HTML emailing these, so I guess this has even worse CSS capabilities than the major browsers, would using tables be more reliable?

 

Thanks

 

Gaz

Link to comment
Share on other sites

Gaz, with the additional information you've provided, you may be better off using tables (especially for the emailing). I prefer to set it up with divs and CSS for screen display, but for printing and the variety of email clients that have their own rendering engine, you really may want to switch back to the tables for this. I'd say try something like this for the tables (Tested and printed in FF and IE6):

<?php
$string = "7,,4,8,,9,6,5,,1,,,,,6,3,4,7,,6,5,4,,3,,,,3,4,,2,9,8,,,,,,,7,6,5,,,,,,,1,3,4,,2,9,,,,6,,2,7,3,,8,2,6,3,,,,,4,,7,3,9,,1,2,,8";
$array = explode(',', $string);

$x = 0;
$grid = array();
for ($i = 0; $i < 9; $i++) {
$grid[$i] = array();
for ($j = 0; $j < 9; $j++) {
	$grid[$i][$j] = $array[$x++];
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link type="text/css" rel="stylesheet" href="styles.css" />
<style type="text/css">
#sudokuBoard {
border: 1px solid #454545;
}

#sudokuBoard .cell {
float: left;
border-top: 1px solid #454545;
border-left: 1px solid #454545;
line-height: 1.3em;
text-align: center;
vertical-align: middle;
width: 16px;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 0.8em;
}

#sudokuBoard .floor td {
border-bottom: 1px solid #454545;
}

#sudokuBoard .wall {
border-right: 1px solid #454545;
}
</style>
</head>

<body>
<table id="sudokuBoard" border="0" cellpadding="0" cellspacing="0">
<?php
$a = -1;
echo ($a > 1);
foreach ($grid as $y => $row) {
echo "<tr";
if (($y + 1) % 3 == 0) echo " class=\"floor\"";
echo ">";
foreach ($row as $x => $cell) {
	$class = "cell";
	if (($x + 1) % 3 == 0) $class .= " wall";
	echo "<td class=\"$class\">";
	echo empty($cell) ? ' ' : $cell;
	echo "</td>\n";
}
echo "</tr>\n";
}
?>
</table>
</body>
</html>

 

I found the issue with display in IE6: IE likes to not display an element if there is nothing contained within it. So, that's why there is the little empty() check now that inserts a non breaking space into those cells with no numbers. This will require IE to display those cells as well.

 

Hope this helps.

Link to comment
Share on other sites

Obsidian,

 

Thanks, thats exactly what I need, I'll incorporate that into my scripts later and test, but cant see this not working,

 

Thank You so much for your help.

 

PM me your paypal or moneybookers email address, so i can show you a little appreciation!

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.