Here is the code: (I have post mysql.php and the database as replies.)
It takes WAY to long to run. If you do not try and update each column in the database it works just fine. Any suggestions. (my arrays skills are not up to par)
<?php
ini_set(max_execution_time, 400);
include ('mysql.php');
global $row, $col, $reg,$mysql_sud;
function randomsq($min,$max) {
$randomsq = rand($min,$max);
}
function checkrow($rowwanted) {
global $row;
include ('mysql.php');
$result = mysql_query("SELECT num FROM $mysql_sud WHERE row='$rowwanted' LIMIT 9");
while($basic = mysql_fetch_array($result)){
$basic2[] = $basic['num'];
}
$row = $basic2;
}
function findreg($row,$col) {
global $foundreg;
if($row <=3 AND $col <=3) {
$foundreg = 1;
}elseif($row <=3 AND $col <=6) {
$foundreg = 2;
}elseif($row <=3 AND $col <=9) {
$foundreg = 3;
}elseif($row <=6 AND $col <=3) {
$foundreg = 4;
}elseif($row <=6 AND $col <=6) {
$foundreg = 5;
}elseif($row <=6 AND $col <=9) {
$foundreg = 6;
}elseif($row <=9 AND $col <=3) {
$foundreg = 7;
}elseif($row <=9 AND $col <=8) {
$foundreg = 8;
}elseif($row <=9 AND $col <=9) {
$foundreg = 9;
}
return $foundreg;
}
function updatecol ($row1a, $col1a, $num1a){
global $mysql_sud;
$query = "UPDATE $mysql_sud SET `num` = $num1a WHERE `col` = $col1a AND `row` = $row1a LIMIT 1 ";
$result = @mysql_query($query);
if($result) {
echo 'Box updated.';
} else {
echo 'Error: ' . mysql_error() . '';
}
mysql_free_result($result);
}
function checkcol($col1){
global $col;
include ('mysql.php');
$colwanted = $col1;
$result = mysql_query("SELECT num FROM $mysql_sud WHERE col='$colwanted' LIMIT 9");
while($basic = mysql_fetch_array($result)){
$basic2[] = $basic['num'];
}
$col = $basic2;
}
function checkreg($reg1) {
global $reg;
include ('mysql.php');
$regwanted = $reg1;
$result = mysql_query("SELECT num FROM $mysql_sud WHERE reg='$regwanted' LIMIT 9");
while($basic = mysql_fetch_array($result)){
$basic2[] = $basic['num'];
}
$reg = $basic2;
}
function checkeverything($regnum, $rownum, $colnum) {
checkreg($regnum);
checkcol($colnum);
checkrow($rownum);
}
function findusable($ro,$re,$c) {
$start = microtime();
global $used, $reg, $col, $row, $num3,$ttime;
checkeverything($re,$ro,$c);
$all = array(1,2,3,4,5,6,7,8,9);
$used = array_merge($reg, $col, $row);
$usable = array_diff($all, $used);
//echo @implode('',$usable);echo '<br>';
while(count($num3)<1){
// Generate rand number from 1 to 9
$randVar = rand(1,9);
// If the rand number does not
// exist in the array add it
if (in_array($randVar,$usable)) {
$num3 = $randVar;
}
}
unset($randVar); unset($usable);unset($row); unset($col); unset($used); unset($reg);
$end = microtime();
$ms_start = explode(" ",$start);
$ms_end = explode(" ",$end);
$ttime = round(($ms_end[1] - $ms_start[1] + $ms_end[0] - $ms_start[0]),2);
}
//Create each row
$onrownum = 1;
for ($n=1; $n<=9; $n++) {
//run for each column in row
$oncolnum = 1;
for ($n2=1; $n2<=9; $n2++) {
$onregnum = findreg($onrownum,$oncolnum);
findusable($onrownum,$oncolnum,$onregnum);
echo $num3;
updatecol($onrownum, $oncolnum, $num3);
echo '<br>';
$blah[] = $num3;
$oncolnum++;
unset($num3);
}
$onrownum++;
}
echo '<br>';
//echo @implode('',$blah);
?>
Need help building Sudoku generater
Moderator: General Moderators
-
mikesmith007
- Forum Newbie
- Posts: 3
- Joined: Wed Jan 13, 2010 3:17 pm
-
mikesmith007
- Forum Newbie
- Posts: 3
- Joined: Wed Jan 13, 2010 3:17 pm
Re: Need help building Sudoku generater
<?php
$mysql_host = 'localhost';
$mysql_user = '------_sud';
$mysql_password = 'sud';
$mysql_db = '-----_sud';
$mysql_prefix = '';
$mysql_sud = $mysql_prefix . 'sudoku';
global $mysql_sud;
$dbc = @mysql_connect($mysql_host, $mysql_user, $mysql_password);
if(!$dbc) {
exit('Error: failed to connect to MySQL server.');
} else {
if(!@mysql_select_db($mysql_db)) {
exit('Error: failed to connect to MySQL database.');
}
}
?>
$mysql_host = 'localhost';
$mysql_user = '------_sud';
$mysql_password = 'sud';
$mysql_db = '-----_sud';
$mysql_prefix = '';
$mysql_sud = $mysql_prefix . 'sudoku';
global $mysql_sud;
$dbc = @mysql_connect($mysql_host, $mysql_user, $mysql_password);
if(!$dbc) {
exit('Error: failed to connect to MySQL server.');
} else {
if(!@mysql_select_db($mysql_db)) {
exit('Error: failed to connect to MySQL database.');
}
}
?>
-
mikesmith007
- Forum Newbie
- Posts: 3
- Joined: Wed Jan 13, 2010 3:17 pm
Re: Need help building Sudoku generater THE DB
-- phpMyAdmin SQL Dump
-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 13, 2010 at 04:02 PM
-- Server version: 5.1.37
-- PHP Version: 5.3.0
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `sudoku`
--
-- --------------------------------------------------------
--
-- Table structure for table `sudoku`
--
CREATE TABLE IF NOT EXISTS `sudoku` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`col` varchar(3) NOT NULL,
`row` varchar(3) NOT NULL,
`reg` varchar(10) NOT NULL,
`num` int(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=82 ;
--
-- Dumping data for table `sudoku`
--
INSERT INTO `sudoku` (`id`, `col`, `row`, `reg`, `num`) VALUES
(1, '1', '1', '1', 3),
(2, '2', '1', '1', 2),
(3, '3', '1', '1', 7),
(4, '4', '1', '2', 4),
(5, '5', '1', '2', 5),
(6, '6', '1', '2', 1),
(7, '7', '1', '3', 9),
(8, '8', '1', '3',
,
(9, '9', '1', '3', 6),
(10, '1', '2', '1', 9),
(11, '2', '2', '1', 7),
(12, '3', '2', '1', 4),
(13, '4', '2', '2', 5),
(14, '5', '2', '2', 6),
(15, '6', '2', '2', 3),
(16, '7', '2', '3', 2),
(17, '8', '2', '3', 1),
(18, '9', '2', '3',
,
(19, '1', '3', '1',
,
(20, '2', '3', '1', 2),
(21, '3', '3', '1', 4),
(22, '4', '3', '2', 6),
(23, '5', '3', '2', 1),
(24, '6', '3', '2', 9),
(25, '7', '3', '3', 5),
(26, '8', '3', '3', 3),
(27, '9', '3', '3', 0),
(28, '1', '4', '4', 0),
(29, '2', '4', '4', 0),
(30, '3', '4', '4', 0),
(31, '4', '4', '5', 0),
(32, '5', '4', '5', 0),
(33, '6', '4', '5', 0),
(34, '7', '4', '6', 0),
(35, '8', '4', '6', 0),
(36, '9', '4', '6', 0),
(37, '1', '5', '4', 0),
(38, '2', '5', '4', 0),
(39, '3', '5', '4', 0),
(40, '4', '5', '5', 0),
(41, '5', '5', '5', 0),
(42, '6', '5', '5', 0),
(43, '7', '5', '6', 0),
(44, '8', '5', '6', 0),
(45, '9', '5', '6', 0),
(46, '1', '6', '4', 0),
(47, '2', '6', '4', 0),
(48, '3', '6', '4', 0),
(49, '4', '6', '5', 0),
(50, '5', '6', '5', 0),
(51, '6', '6', '5', 0),
(52, '7', '6', '6', 0),
(53, '8', '6', '6', 0),
(54, '9', '6', '6', 0),
(55, '1', '7', '7', 0),
(56, '2', '7', '7', 0),
(57, '3', '7', '7', 0),
(58, '4', '7', '8', 0),
(59, '5', '7', '8', 0),
(60, '6', '7', '8', 0),
(61, '7', '7', '9', 0),
(62, '8', '7', '9', 0),
(63, '9', '7', '9', 0),
(64, '1', '8', '7', 0),
(65, '2', '8', '7', 0),
(66, '3', '8', '7', 0),
(67, '4', '8', '8', 0),
(68, '5', '8', '8', 0),
(69, '6', '8', '8', 0),
(70, '7', '8', '9', 0),
(71, '8', '8', '9', 0),
(72, '9', '8', '9', 0),
(73, '1', '9', '7', 0),
(74, '2', '9', '7', 0),
(75, '3', '9', '7', 0),
(76, '4', '9', '8', 0),
(77, '5', '9', '8', 0),
(78, '6', '9', '8', 0),
(79, '7', '9', '9', 0),
(80, '8', '9', '9', 0),
(81, '9', '9', '9', 0);
-- version 3.2.0.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jan 13, 2010 at 04:02 PM
-- Server version: 5.1.37
-- PHP Version: 5.3.0
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `sudoku`
--
-- --------------------------------------------------------
--
-- Table structure for table `sudoku`
--
CREATE TABLE IF NOT EXISTS `sudoku` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`col` varchar(3) NOT NULL,
`row` varchar(3) NOT NULL,
`reg` varchar(10) NOT NULL,
`num` int(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=82 ;
--
-- Dumping data for table `sudoku`
--
INSERT INTO `sudoku` (`id`, `col`, `row`, `reg`, `num`) VALUES
(1, '1', '1', '1', 3),
(2, '2', '1', '1', 2),
(3, '3', '1', '1', 7),
(4, '4', '1', '2', 4),
(5, '5', '1', '2', 5),
(6, '6', '1', '2', 1),
(7, '7', '1', '3', 9),
(8, '8', '1', '3',
(9, '9', '1', '3', 6),
(10, '1', '2', '1', 9),
(11, '2', '2', '1', 7),
(12, '3', '2', '1', 4),
(13, '4', '2', '2', 5),
(14, '5', '2', '2', 6),
(15, '6', '2', '2', 3),
(16, '7', '2', '3', 2),
(17, '8', '2', '3', 1),
(18, '9', '2', '3',
(19, '1', '3', '1',
(20, '2', '3', '1', 2),
(21, '3', '3', '1', 4),
(22, '4', '3', '2', 6),
(23, '5', '3', '2', 1),
(24, '6', '3', '2', 9),
(25, '7', '3', '3', 5),
(26, '8', '3', '3', 3),
(27, '9', '3', '3', 0),
(28, '1', '4', '4', 0),
(29, '2', '4', '4', 0),
(30, '3', '4', '4', 0),
(31, '4', '4', '5', 0),
(32, '5', '4', '5', 0),
(33, '6', '4', '5', 0),
(34, '7', '4', '6', 0),
(35, '8', '4', '6', 0),
(36, '9', '4', '6', 0),
(37, '1', '5', '4', 0),
(38, '2', '5', '4', 0),
(39, '3', '5', '4', 0),
(40, '4', '5', '5', 0),
(41, '5', '5', '5', 0),
(42, '6', '5', '5', 0),
(43, '7', '5', '6', 0),
(44, '8', '5', '6', 0),
(45, '9', '5', '6', 0),
(46, '1', '6', '4', 0),
(47, '2', '6', '4', 0),
(48, '3', '6', '4', 0),
(49, '4', '6', '5', 0),
(50, '5', '6', '5', 0),
(51, '6', '6', '5', 0),
(52, '7', '6', '6', 0),
(53, '8', '6', '6', 0),
(54, '9', '6', '6', 0),
(55, '1', '7', '7', 0),
(56, '2', '7', '7', 0),
(57, '3', '7', '7', 0),
(58, '4', '7', '8', 0),
(59, '5', '7', '8', 0),
(60, '6', '7', '8', 0),
(61, '7', '7', '9', 0),
(62, '8', '7', '9', 0),
(63, '9', '7', '9', 0),
(64, '1', '8', '7', 0),
(65, '2', '8', '7', 0),
(66, '3', '8', '7', 0),
(67, '4', '8', '8', 0),
(68, '5', '8', '8', 0),
(69, '6', '8', '8', 0),
(70, '7', '8', '9', 0),
(71, '8', '8', '9', 0),
(72, '9', '8', '9', 0),
(73, '1', '9', '7', 0),
(74, '2', '9', '7', 0),
(75, '3', '9', '7', 0),
(76, '4', '9', '8', 0),
(77, '5', '9', '8', 0),
(78, '6', '9', '8', 0),
(79, '7', '9', '9', 0),
(80, '8', '9', '9', 0),
(81, '9', '9', '9', 0);