[code]<?php

session_start();

?>

<html>
<head>
<title>PHP-based mastermind - By KoenB</title>
</head>
<body>

<?php

if($_SESSION['poging'] == "" || $_SESSION['poging'] == 0){

	$_SESSION['poging'] = 1;

	$_SESSION['g1'] = rand(1,6);

	$_SESSION['g2'] = rand(1,6);
	while($_SESSION['g2'] == $_SESSION['g1']){
		$_SESSION['g2'] = rand(1,6);
	}

	$_SESSION['g3'] = rand(1,6);
	while($_SESSION['g3'] == $_SESSION['g1'] || $_SESSION['g3'] == $_SESSION['g2']){
		$_SESSION['g3'] = rand(1,6);
	}

	$_SESSION['g4'] = rand(1,6);
	while($_SESSION['g4'] == $_SESSION['g1'] || $_SESSION['g4'] == $_SESSION['g2'] || $_SESSION['g4'] == $_SESSION['g3']){
		$_SESSION['g4'] = rand(1,6);
	}

}

if($_POST['speel']){

	$n1 = $_POST['n1'];
	$n2 = $_POST['n2'];
	$n3 = $_POST['n3'];
	$n4 = $_POST['n4'];

	if(!(is_numeric($n1) && is_numeric($n2) && is_numeric($n3) && is_numeric($n4))){

		echo "Vul getallen in.<p>";

		echo "<a href='index.php'>Terug.</a>";

	} else if(($n1 > 6 || $n1 < 1) || ($n2 > 6 || $n2 < 1) || ($n3 > 6 || $n3 < 1) || ($n4 > 6 || $n4 < 1)){

		echo "Vul getallen van 1 t/m 6 in.<p>";

		echo "<a href='index.php'>Terug.</a>";

	} else {

		$this_poging = $_SESSION['poging'];
	
		$_SESSION['een'.$this_poging] = $n1;
		$_SESSION['twee'.$this_poging] = $n2;
		$_SESSION['drie'.$this_poging] = $n3;
		$_SESSION['vier'.$this_poging] = $n4;
	
		$_SESSION['goed'.$this_poging] = 0;
		$_SESSION['half'.$this_poging] = 0;
	
		if($n1 == $_SESSION['g1']){
			$_SESSION['goed'.$this_poging] ++;
		}
		if($n2 == $_SESSION['g2']){
			$_SESSION['goed'.$this_poging] ++;
		}
		if($n3 == $_SESSION['g3']){
			$_SESSION['goed'.$this_poging] ++;
		}
		if($n4 == $_SESSION['g4']){
			$_SESSION['goed'.$this_poging] ++;
		}
	
		if($n1 == $_SESSION['g2'] || $n1 == $_SESSION['g3'] || $n1 == $_SESSION['g4']){
			$_SESSION['half'.$this_poging]++;
		}
		if($n2 == $_SESSION['g3'] || $n2 == $_SESSION['g4'] || $n2 == $_SESSION['g1']){
			$_SESSION['half'.$this_poging]++;
		}
		if($n3 == $_SESSION['g4'] || $n3 == $_SESSION['g1'] || $n3 == $_SESSION['g2']){
			$_SESSION['half'.$this_poging]++;
		}
		if($n4 == $_SESSION['g1'] || $n4 == $_SESSION['g2'] || $n4 == $_SESSION['g3']){
			$_SESSION['half'.$this_poging]++;
		}		
	
		$_SESSION['poging'] ++;
	
		echo "<script language='JavaScript'> document.location = 'index.php'; </script>";

	}

} else {

	$check_poging = $_SESSION['poging'] - 1;

	if($_SESSION['goed'.$check_poging] == 4){

		echo "Gewonnen!";

	} else if($_SESSION['poging'] < 7){

		?>

		<form method='post' target='_self'>

		<input type='text' name='n1' size='1' maxlength='1'> 
		<input type='text' name='n2' size='1' maxlength='1'> 
		<input type='text' name='n3' size='1' maxlength='1'> 
		<input type='text' name='n4' size='1' maxlength='1'><p>

		<input type='submit' name='speel' value='Speel'>

		</form>

		<?

	} else {

		echo "Verloren!";

	}

	echo "<hr>";

	if($_SESSION['poging'] == 1){

		echo "<li>Raad een code van 4 verschillende cijfers van 1 t/m 6.<br>";
		echo "<li>Vul 4 getallen in in bovenstaande vakjes en klik op 'Speel'.<br>";
		echo "<li>Voor elk getal wat op de goede plaats staat, komt er een zwart hokje te staan.<br>";
		echo "<li>Voor elk getal wat niet op de goede plaats staat, maar wel in de code zit, komt er een wit hokje te staan.<br>";
		echo "<li>Voor elk getal wat niet in de code zit, komt er geen hokje te staan.";

	} else {

		echo "<table bgcolor='gray' border='1' cellspacing='2' cellpadding='0'>";
	
		$teller = 1;
		while($teller < $_SESSION['poging']){
	
			echo "<tr>";
	
			echo "<td align='center' width='70' rowspan='2'>Poging ".$teller."</td>";
	
			echo "<td align='center' width='24' rowspan='2'>".$_SESSION['een'.$teller]."</td>";
			echo "<td align='center' width='24' rowspan='2'>".$_SESSION['twee'.$teller]."</td>";
			echo "<td align='center' width='24' rowspan='2'>".$_SESSION['drie'.$teller]."</td>";
			echo "<td align='center' width='24' rowspan='2'>".$_SESSION['vier'.$teller]."</td>";
	
			$teller_g = 1;
			while($teller_g <= 4){
	
				if($teller_g <= $_SESSION['goed'.$teller]){
					echo "<td align='center' width='12' height='12' bgcolor='black'>";
					echo "</td>";
				} else {
					echo "<td align='center' width='12' height='12'>";
					echo "</td>";
				}
	
				$teller_g++;
	
			}
	
			echo "</tr>";
			echo "<tr>";
	
			$teller_h = 1;
			while($teller_h <= 4){
	
				if($teller_h <= $_SESSION['half'.$teller]){
					echo "<td align='center' width='12' height='12' bgcolor='white'>";
					echo "</td>";
				} else {
					echo "<td align='center' width='12' height='12'>";
					echo "</td>";
				}
	
				$teller_h++;
	
			}
	
			echo "</tr>";
	
			$teller++;
	
		}
	
		echo "</table>";

	}

	echo "<hr>";

	if($_POST['opnieuw']){

		session_destroy();

		echo "<script language='JavaScript'> document.location = 'index.php'; </script>";

	} else {

		echo "<form method='post' target='_self'>";
		echo "<input type='submit' name='opnieuw' value='Opnieuw spelen'>";
		echo "</form>";

	}

}

?>

</body>
</html>