.
/**
* Creates a sudoku.
*
* @package mod_game
* @copyright 2007 Vasilis Daloukas
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require( "../../../config.php");
require_once("class.Sudoku.php");
require( '../header.php');
$action = optional_param('action', PARAM_ALPHA); // The action.
require_login();
if ($action == 'create') {
AppendSudokuB();
} else {
showform();
}
/**
* Show form
*/
function showform() {
$id = required_param('id', PARAM_NUMBER); // The action.
?>
data = packsudoku( $si, $sp);
if (strlen( $newrec->data) != 81) {
return 0;
}
$newrec->level = $level;
$newrec->opened = GetOpened( $si);
$DB->insert_record( 'game_sudoku_database', $newrec, true);
$level++;
if ($level > $level2) {
$level = $level1;
}
echo get_string( 'sudoku_creating', 'game', $i)."
\r\n";
}
}
/**
* Pack sudoku
*
* @param $si
* @param $sp
*
* @return the packed sudoku
*/
function packsudoku( $si, $sp) {
$data = '';
for ($i = 1; $i <= 9; $i++) {
for ($j = 1; $j <= 9; $j++) {
$c = &$sp->thesquares[$i];
$c = &$c->getcell($j);
$solution = $c->asstring( false);
$c = &$si->thesquares[$i];
$c = &$c->getCell($j);
$thesolvedstate = $c->solvedstate();
if ($thesolvedstate == 1) {
// Hint.
$solution = substr( 'ABCDEFGHI', $c->asString( false) - 1, 1);
}
$data .= $solution;
}
}
return $data;
}
/**
* Creates a sudoku
*
* @param $id
* @param $sp
* @param $level
*
* @return true if created correctly
*/
function create( &$si, &$sp, $level=1) {
for ($i = 1; $i <= 40; $i++) {
$sp = new sudoku();
$theinitialposition = $sp->generatepuzzle( 10, 50, $level);
if (count( $theinitialposition)) {
break;
}
}
if ($i > 40) {
return false;
}
$si = new sudoku();
$si->initializepuzzlefromarray( $theinitialposition);
return true;
}
/**
* get opened
*
* @param $si
*
* @return count of opened
*/
function getopened( $si) {
$count = 0;
for ($i = 1; $i <= 9; $i++) {
for ($j = 1; $j <= 9; $j++) {
$c = &$si->thesquares[$i];
$c = &$c->getcell($j);
$thesolvedstate = $c->solvedstate();
if ($thesolvedstate == 1) {
// Hint.
$count++;
}
}
}
return $count;
}