Location: PHPKode > projects > Sudoku Cheater PHP > v.0.9.2/index.php
<?php
	/*
	 * Copyright by Frieder Kesch
	 * http://sudokucheater.ikesch.de
	 * 
	 * License:   GNU General Public License 2 (GPL 2) or later
	 * 
	 * This program is free software; you can redistribute it
	 * and/or modify it under the terms of the GNU General Public
	 * License as published by the Free Software Foundation; either
	 * version 2 of the License, or (at your option) any later
	 * version.
	 * 
	 * This program is distributed in the hope that it will be
	 * useful, but WITHOUT ANY WARRANTY; without even the implied
	 * warrenty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
	 * PURPOSE.
	 * See the GNU General Public License for more details.
	 *
	 * SUPPORTED BROWSERS (@Vista x64)
	 * Firefox 2
	 * Opera 9.27
	 * IE 7
	 * Not testet in IE6 - y? because it's a crime not a browser!
	 */

	// Load Classes
	require_once 'classes/Sudoku.php';
	require_once 'classes/SudokuField.php';
	require_once 'classes/Helper.php';

	// init
	$defaultLanguage	= 'de'; // de - German; en - English
	$defaultRndFields	= 30;	// default 30
	$numRows			= 9;
	$numCols			= 9;
	$cellClass			= 'cellEven';
	$helper				= new Helper();
	$sudoku				= new Sudoku();
	
	// Get current actions
	$action		= preg_replace('/[^a-z]/', '', $_REQUEST['action']);
	
	// Get the language
	switch ($_REQUEST['lang']) {
		case 'de': $language = 'de'; break;
		case 'en': $language = 'en'; break;
		case 'fr': $language = 'fr'; break;
		default: $language = $defaultLanguage; break;
	}
	
	// Load Language
	$helper->loadLanguage($language);
	
	if ($action == 'calculate' || $action == 'check') {
		// Fill the board with values
		for ($row = 0; $row < $numRows; $row++) {
			for ($col = 0; $col < $numCols; $col++) {
				// Get value
				$value = preg_replace('/[^1-9]/', '', $_POST['field_'.$row.'_'.$col]);
				// Add Field
				$sudoku->setField($row, $col, $value);
			}
		}
		
		if ($action == 'calculate') {
			// Try to Solve the Sudoku
			$sudoku->solve();
		}
		else {
			// Just check the Board for rule violations
			$sudoku->validateBoard();
		}
	}
	else if ($action == 'generate') {
		// create random sudoku
		$sudoku->generate(preg_replace('/[^0-9]/', '', $_POST['numRandomFields']));
	}
	
	// Get the current Board
	$sudokuBoard = $sudoku->getBoard(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title><?php echo LANG_PAGE_TITLE ?></title>
	<link rel="stylesheet" type="text/css" media="screen" href="styles/screen.css" />
	<link rel="shortcut icon" type="image/x-icon" href="images/favicon.ico" />
	<script type="text/javascript" src="functions.js"></script>
</head>
<body>
	
	<div class="mainContainer">
		
		<form action="index.php" method="post">
			<div class="header">
				<a href="index.php?lang=de" onclick="return confirmLangChange();"><img src="images/flag_de.png" alt="de" /></a>
				<a href="index.php?lang=en" onclick="return confirmLangChange();"><img src="images/flag_en.png" alt="en" /></a>
				<a href="index.php?lang=fr" onclick="return confirmLangChange();"><img src="images/flag_fr.png" alt="fr" /></a>
			</div>
			<div class="leftsigns"></div>
			<div style="float: left; width: 360px;">
				<table cellspacing="0">
					<?php for ($row = 0; $row < $numRows; $row++): ?>
						<?php $cellClass = ($row > 2 && $row < 6) ? 'cellOdd' : 'cellEven'; ?>
						<tr>
						
							<?php for ($col = 0; $col < $numCols; $col++): ?>
								<?php $field = $sudokuBoard[$row][$col] ?>
								<?php $cellClass = ($col % 3 == 0) ? $helper->switchCellClass($cellClass) : $cellClass; ?>
								<td class="<?php echo $cellClass ?>">
									<input type="text" maxlength="1" class="inputText_<?php echo $cellClass ?>" name="field_<?php echo $row.'_'.$col ?>" id="field_<?php echo $row.'_'.$col ?>" value="<?php echo ($field->getIsGiven()) ? $field->getValue() : '' ?>" onkeyup="checkField('<?php echo $row ?>', '<?php echo $col ?>');" />
								</td>
							<?php endfor ?>
							
						</tr>
					<?php endfor ?>
				</table>
			</div>
			<div class="rightsigns"></div>
			<div style="clear: both;"></div>
			
			<p style="margin: 4px auto;">
				<input type="button" value="<?php echo BUTTON_GENERATE ?>" onclick="confirmGenerate();" />
				<input type="button" value="<?php echo BUTTON_CHECK ?>" onclick="checkSudoku();" />
				<input type="submit" value="<?php echo BUTTON_SOLVE ?>" />
				<input type="button" value="<?php echo BUTTON_RESET ?>" onclick="confirmReset();" />
				
				<input type="hidden" name="action" id="action" value="calculate" />
				<input type="hidden" name="numRandomFields" id="numRandomFields" value="0" />
				<input type="hidden" name="lang" id="lang" value="<?php echo $language ?>" />
			</p>
		</form>
		
		<?php if ($action == 'calculate' || $action == 'check'): ?>
			<div class="infoBox">
				<img src="images/information.png" style="float: left; margin-right: 3px;" alt="Info" />
				<?php if ($action == 'calculate' && $sudoku->getIsSolved()): ?>
					<?php echo MSG_SUDOKU_IS_SOLVABLE ?>
				<?php elseif ($action == 'calculate' && !$sudoku->getIsSolved()): ?>
					<?php echo MSG_SUDOKU_IS_NOT_SOLVABLE ?>
				<?php elseif ($action == 'check' && $sudoku->getIsValid()): ?>
					<?php echo MSG_SUDOKU_IS_VALID ?>
				<?php elseif ($action == 'check' && !$sudoku->getIsValid()): ?>
					<?php echo MSG_SUDOKU_IS_NOT_VALID ?>
				<?php endif ?>
			</div>
			<?php if (($action == 'calculate' && $sudoku->getIsSolved()) || ($action == 'check')): ?>
				<table cellspacing="0">
					<?php for ($row = 0; $row < $numRows; $row++): ?>
						<?php $cellClass = ($row > 2 && $row < 6) ? 'cellOdd' : 'cellEven'; ?>
						<tr>
						
							<?php for ($col = 0; $col < $numCols; $col++): ?>
									<?php $field = $sudokuBoard[$row][$col] ?>
								<?php $cellClass = ($col % 3 == 0) ? $helper->switchCellClass($cellClass) : $cellClass; ?>
								<td class="<?php echo $cellClass ?>" <?php if (!$field->getIsValid()) echo 'style="color: red;"' ?>>
									<?php echo (!is_null($field->getValue())) ? $field->getValue() : '&nbsp;' ?>
								</td>
							<?php endfor ?>
							
						</tr>
					<?php endfor ?>
				</table>
			<?php endif ?>
		<?php else: ?>
			<div class="infoBox">
				<img src="images/information.png" style="float: left; margin-right: 3px;" alt="Info" />
				<p style="font-weight: bold; margin-bottom: 6px;"><?php echo MSG_WELCOME_PART1 ?></p>
				<p class="welcomeMsg"><?php echo MSG_WELCOME_PART2 ?></p>
				<p class="welcomeMsg"><?php echo MSG_WELCOME_PART3 ?></p>
				<p class="welcomeMsg"><a href="http://sourceforge.net/projects/sudokucheater/" onclick="window.open(this.href); return false;"><?php echo MSG_WELCOME_PART4 ?></a></p>
			</div>
		<?php endif ?>
		
		<p class="footer">
			[<a href="mailto:<?php echo $helper->string2ascii('hide@address.com') ?>">contact - sudokucheater at ikesch de</a>]
		</p>
		<p>
			<a href="http://sourceforge.net/projects/sudokucheater/" onclick="window.open(this.href); return false;"><img src="images/sflogo.gif" alt="Sourceforge.net Logo" style="margin-right: 10px;" /></a>
			<a href="http://validator.w3.org/check?uri=referer" onclick="window.open(this.href); return false;"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>
			<a href="http://jigsaw.w3.org/css-validator/check/referer" onclick="window.open(this.href); return false;"><img style="border:0;width:88px;height:31px" src="http://jigsaw.w3.org/css-validator/images/vcss" alt="CSS ist valide!" /></a>
		</p>
	</div>
	
	<script type="text/javascript">
	//<![CDATA[
	
		function confirmLangChange() {
			return confirm("<?php echo MSG_CONFIRM_LANGUAGE_CHANGE ?>");
		}
		
		function confirmGenerate() {
			var myRandomFields = prompt("<?php echo MSG_PROMPT_NUM_RANDOM_FIELDS ?>", "<?php echo $defaultRndFields ?>");
			
			if (myRandomFields.length > 0 && myRandomFields.length < 3) {
				document.getElementById('numRandomFields').value = myRandomFields;
				document.getElementById('action').value = 'generate';
				document.forms[0].submit();
			}
		}
		
		function confirmReset() {
			if (confirm("<?php echo MSG_CONFIRM_RESET ?>")) {
				location.href = 'index.php?lang=<?php echo $language ?>';
			}
		}
		
		function checkSudoku() {
			document.getElementById('action').value = 'check';
			document.forms[0].submit();
		}

	//]]>
	</script>
	
</body>
</html>
Return current item: Sudoku Cheater PHP