Jump to content

LONG CODE- Soduku solver - RESETING THE SERVER


lj11

Recommended Posts

I coded this long long script that solves the soduku puzzle from puzz.txt. but every time I run it, it resets the server. If you have any time, could you please loook over my code and help me figure out what went wrong? ??? ??? ??? :-X

 

solver.php:

<?php
$filename = "puzz.txt";						// open puzzle
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename)-1);
fclose($handle);

$cell = array();
$cell = explode(',', $contents);				// make puzzle into $cell array

$cellgroups = array (						// group cells by:
	"r1"=>array(0,1,2,3,4,5,6,7,,			//			rows
	"r2"=>array(9,10,11,12,13,14,15,16,17),
	"r3"=>array(18,19,20,21,22,23,24,25,26),
	"r4"=>array(27,28,29,30,31,32,33,34,35),
	"r5"=>array(36,37,38,39,40,41,42,43,44),
	"r6"=>array(45,46,47,48,49,50,51,52,53),
	"r7"=>array(54,55,56,57,58,59,60,61,62),
	"r8"=>array(63,64,65,66,67,68,69,70,71),
	"r9"=>array(72,73,74,75,76,77,78,79,80),
	"c1"=>array(0,9,18,27,36,45,54,63,72),		//			columns
	"c2"=>array(1,10,19,28,37,46,55,64,73),
	"c3"=>array(2,11,20,29,38,47,56,65,74),
	"c4"=>array(3,12,21,30,39,48,57,66,75),
	"c5"=>array(4,13,22,31,40,49,58,67,76),
	"c6"=>array(5,14,23,32,41,50,59,68,77),
	"c7"=>array(6,15,24,33,42,51,60,69,78),
	"c8"=>array(7,16,25,34,43,52,61,70,79),
	"c9"=>array(8,17,26,35,44,53,62,71,80),
	"b1"=>array(0,1,2,9,10,11,18,19,20),		//			blocks
	"b2"=>array(27,28,29,36,37,38,45,46,47),
	"b3"=>array(54,55,56,63,64,65,72,73,74),
	"b4"=>array(3,4,5,12,13,14,21,22,23),
	"b5"=>array(30,31,32,39,40,41,48,49,50),
	"b6"=>array(57,58,59,66,67,68,75,76,77),
	"b7"=>array(6,7,8,15,16,17,24,25,26),
	"b8"=>array(33,34,35,42,43,44,51,52,53),
	"b9"=>array(60,61,62,69,70,71,78,79,80),
	);


foreach ($cell as $key => $value) {
if ($cell[$key] == null) {
	$cell[$key] = array(1,2,3,4,5,6,7,8,9);		// make empty cells an array from 1-9
}
}

checkNine('r', 1);						// begins the logic process

function checkNine($type, $spot)
{
global $cell, $cellgroups;
$taken=array();
$takenpairs=array();
$pairs=array();
$setnum=$type.$spot;
$thisNine;

foreach ($cellgroups[$setnum] as $o => $n) 
{
	if (($type == 'c') || ($type == 'b'))
	{
		global $thisNine;
		$thisNine = $cellgroups[$setnum];
	}
	if ((count($cell[$n])) == 1)
	{
		$taken[]=$n;
	} else if ((count($cell[$n])) == 2)
	{
		if ((count($pairs) != 0))
		{
			foreach($pairs as $c => $p)
			{
				if ((count(array_diff($cell[$n],$pairs[$c])) == 0))
				{
					$taken[]=$cell[$n][0];
					$taken[]=$cell[$n][1];
					$taken=array_unique($taken);
					$takenpairs[$n]=array(($cell[$n][0]),($cell[$n][1]));
				} else
				{
					$pairs[$n]=array(($cell[$n][0]),($cell[$n][1]));
				}
				unset($c, $p);
			}
		} else
		{
			$pairs[$n]=array(($cell[$n][0]),($cell[$n][1]));
		}
	} else
	{
		continue;
	}
	unset($o, $n);
}
foreach ($cellgroups[$setnum] as $o => $n) 
{
	global $cell, $cellgroups, $thisNine;
	if ((count($cell[$n])) == 1)
	{
		continue;
	} else if ((count($cell[$n])) == 2)
	{
		if ((count($takenpairs)) != 0)
		{
			foreach ($takenpairs as $c => $p)
			{
				if ((count(array_diff($cell[$n],$takenpairs[$c])) == 0))
				{
					continue;
				} else
				{
					$cell[$n] = array_diff($cell[$n], $taken);
					$cell[$n] = array_values($cell[$n]);
				}
				unset($c, $p);
			}
		} else
		{
			$cell[$n] = array_diff($cell[$n], $taken);
			$cell[$n] = array_values($cell[$n]);
		}
	} else
	{
		$cell[$n] = array_diff($cell[$n], $taken);
		$cell[$n] = array_values($cell[$n]);
	}
	if (($type == 'c') || ($type == 'b'))
	{
		if (count(array_diff($cellgroups[$setnum],$thisNine)) != 0)
		{
			unsetVars();
			$type = 'r';
			$spot = 1;
			unset($thisNine);
			checkNine($type, $spot);
		} else
		{
			unset($thisNine);
		}
	}
	unset($o, $n);		
}
unsetVars();

if ($spot < 
{
	$spot++;
	checkNine($type, $spot);
} else if ($type = 'r')
{
	$type = 'c';
	$spot = 1;
	checkNine($type, $spot);
} else if ($type = 'c')
{
	$type = 'b';
	$spot = 1;
	checkNine($type, $spot);
} else if ($type = 'b')
{
	outputFin();
}
}

function unsetVars()
{
unset($taken);
unset($takenpairs);
unset($pairs);
unset($setnum);
return;
}

function outputFin()
{
print_r ($cell);
stop();
}

 

here's puzz.txt, the puzzle solver.php tries to solve:

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

Well, a good way to find what section is causing your server to restart is put the line die('test'); or something similar in your script.

 

Start at the top and move it down until the server crashes, then you know the line or lines right above it caused the crash... that is, assuming it doesn't crash before it even starts to run the script.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.