Location: PHPKode > scripts > Anoï Towers > ano-towers/anoi.php
<?php
// ------------------------------------------------------------------------- //
// Description du script.                                                    //
// ------------------------------------------------------------------------- //
// Auteur: Fabien Dehopré                                                    //
// Email:  hide@address.com                                      //
// Web:    http://kattack.multimania.com/                                    //
// ------------------------------------------------------------------------- //
	if ($restart=="Recommencer") {
		unset($start);
		unset($difficulty);
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> phpInfo.net - Défi PHP n° 6 - Tours de Anoï </TITLE>
	<STYLE>
		BODY { font-family: Helvetica, Arial; font-size: 13px; color: #323769 }
		TD   { font-family: Helvetica, Arial; font-size: 13px; color: #323769 }
	</STYLE>
</HEAD>

<BODY>
<?php
function brouille($str,$key) {
	$tmp = "";
	$kPos = 0;
	for ($i=0; $i<strlen($str); $i++) {
		if ($kPos == strlen($key)) $kPos = 0;
		$tmp .= substr($str,$i,1) ^ substr($key,$kPos,1);
		$kPos++;
	}
	return $tmp;
}

function init($difficulty=7) {
	global $A,$B,$C,$start;
	$A = array();
	$B = array();
	$C = array();
	if ($difficulty >= 7) array_push($A,140);
	if ($difficulty >= 6) array_push($A,120);
	if ($difficulty >= 5) array_push($A,100);
	if ($difficulty >= 4) array_push($A,80);
	array_push($A,60);
	array_push($A,40);
	array_push($A,20);
	$start = time();
}

function deplace($from,$to) {
	global $A,$B,$C;
	$elem = array_pop($$from);
	if ($elem == NULL) return false;
	if ($$to[0] == "") $$to = array();
	array_push($$to,$elem);
	return true;
}

function array_top(&$array) { 
	return $array[count($array) - 1];
}

function check($from,$to) {
	global $A,$B,$C;
	if ($from == $to) return false;
	if ((count($$to) == 0) || $$to[0] == "") {
		return true;
	}
	$from_elt = array_top($$from);
	$to_elt = array_top($$to);
	if ($to_elt < $from_elt) {
		return false;
	}
	else {
		return true;
	}
}

function check_finish() {
	global $C,$difficulty;
	if (count($C) == $difficulty) return true;
	else return false;
}

function elemAt(&$array,$level) {
	return $array[$level-1];
}

function display_histo($histo) {
	$h = explode("\n",$histo);
	$nMvt = count($h)-1;
	echo "Nombre de déplacement: $nMvt<br>";
	echo "<table width=\"250\" border=\"1\">\n";
	echo "<tr><th>&nbsp;</th><th>Mouvement</th></tr>\n";
	for ($i=0; $i<$nMvt; $i++) {
		echo "<tr><td>";
		echo ($i+1).".";
		echo "</td><td>";
		echo $h[$i];
		echo "</td></tr>\n";
	}
	echo "</table>\n";
}

$key = "4410ec34d9e6c1a68100ca0ce033fb17";
if (!isset($start)) {
	if (isset($difficulty)) {
		init($difficulty);
	}
	else {
?>
<H1 align=center>Choisissez le niveau de difficulté</H1>
<table width="150" border="1" align="center">
<tr>
<td>
<FORM action="<?php echo $PHP_SELF;?>" method="post">
&nbsp;&nbsp;&nbsp;<input type="radio" name="difficulty" value="7" checked>Sept disques<br>
&nbsp;&nbsp;&nbsp;<input type="radio" name="difficulty" value="6">Six disques<br>
&nbsp;&nbsp;&nbsp;<input type="radio" name="difficulty" value="5">Cinq disques<br>
&nbsp;&nbsp;&nbsp;<input type="radio" name="difficulty" value="4">Quatre disques<br>
&nbsp;&nbsp;&nbsp;<input type="radio" name="difficulty" value="3">Trois disques<br><br>
<div align=center><input type="submit" value="Commencer"></div>
</form>
</td>
</tr>
</table>
</body>
</html>
<?php
		exit;
	}
}
else {
	$A = explode(";",brouille(base64_decode($hA),$key));
	$B = explode(";",brouille(base64_decode($hB),$key));
	$C = explode(";",brouille(base64_decode($hC),$key));
	$history = brouille(base64_decode($history),$key);
	$start = brouille(base64_decode($start),$key);
	$difficulty = brouille(base64_decode($difficulty),$key);
	if ($A[0] == "") $A = array();
	if ($B[0] == "") $B = array();
	if ($C[0] == "") $C = array();
	if (!isset($depart)) {
		echo "<H1 align=center>Vous avez oubliez de donner le point de départ</H1>";
	}
	elseif (!isset($arrivee)) {
		echo "<H1 align=center>Vous avez oubliez de donner le point d'arrivée</H1>";
	}
	elseif (check($depart,$arrivee)) {
		deplace($depart,$arrivee);
		$history .= "$depart ---> $arrivee\n";
	}
	else {
		echo "<H1 align=center>Déplacement non permis!</H1>";
	}
}
if (check_finish()) {
	$stop = time();
	$diff = $stop - $start;
	$hours = intval($diff / 3600);
	$diff = $diff % 3600;
	$minutes = intval($diff / 60);
	$secs = $diff % 60;
	unset($start);
	echo "<H1 align=center>Bien joué!</H1>";
	echo "<P align=center>Tu as mis $hours heure(s), $minutes minute(s) et $secs seconde(s) pour trouver la solution.</p>";
	echo "<P align=center><u>Récapitulatif des déplacements:</u><br>";
	display_histo($history);
	echo "<br><a href=\"$PHP_SELF\">Clique ici pour recommencer</a></P>";
	unset($difficulty);
	exit;
}
?>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>

<TR>
<TD WIDTH=150 ALIGN=center><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0></TD>
<TD WIDTH=150 ALIGN=center><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0></TD>
<TD WIDTH=150 ALIGN=center><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0></TD>
<TD></TD>
</TR>

<TR>
<TD WIDTH=150 ALIGN=center><?php
if (count($A) == 7) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($A,7);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($B) == 7) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($B,7);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($C) >= 7) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($C,7);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD></TD>
</TR>

<TR>
<TD WIDTH=150 ALIGN=center><?php
if (count($A) >= 6) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($A,6);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($B) >= 6) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($B,6);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($C) >= 6) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($C,6);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD></TD>
</TR>

<TR>
<TD WIDTH=150 ALIGN=center><?php
if (count($A) >= 5) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($A,5);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($B) >= 5) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($B,5);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($C) >= 5) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($C,5);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD></TD>
</TR>

<TR>
<TD WIDTH=150 ALIGN=center><?php
if (count($A) >= 4) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($A,4);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($B) >= 4) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($B,4);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($C) >= 4) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($C,4);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD></TD>
</TR>

<TR>
<TD WIDTH=150 ALIGN=center><?php
if (count($A) >= 3) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($A,3);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($B) >= 3) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($B,3);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($C) >= 3) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($C,3);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD></TD>
</TR>

<TR>
<TD WIDTH=150 ALIGN=center><?php
if (count($A) >= 2) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($A,2);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($B) >= 2) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($B,2);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($C) >= 2) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($C,2);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD></TD>
</TR>

<TR>
<TD WIDTH=150 ALIGN=center><?php
if (count($A) >= 1) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($A,1);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($B) >= 1) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($B,1);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD WIDTH=150 ALIGN=center><?php
if (count($C) >= 1) {
?><IMG SRC="piece.gif" WIDTH="<?php echo elemAt($C,1);?>" HEIGHT="16" BORDER=0><?php
} else {
?><IMG SRC="tige.gif" WIDTH="10" HEIGHT="16" BORDER=0><?php
}
?></TD>
<TD></TD>
</TR>

<TR>
<TD COLSPAN=3 BGCOLOR=#D9AB8A WIDTH=450>&nbsp;</TD>
</TR>

<TR>
<TD WIDTH=150 ALIGN=center><B>A</B></TD>
<TD WIDTH=150 ALIGN=center><B>B</B></TD>
<TD WIDTH=150 ALIGN=center><B>C</B></TD>
<TD></TD>
</TR>

<TR BGCOLOR="#DBDBDB">
<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF;?>">
<TD WIDTH=150 ALIGN=center>
<?php if (count($A) > 0) {?>
<INPUT TYPE="radio" NAME="depart" value="A">
<?php } else {?>
&nbsp;
<?php }?>
</TD>
<TD WIDTH=150 ALIGN=center>
<?php if (count($B) > 0) {?>
<INPUT TYPE="radio" NAME="depart" value="B">
<?php } else {?>
&nbsp;
<?php }?>
</TD>
<TD WIDTH=150 ALIGN=center>
<?php if (count($C) > 0) {?>
<INPUT TYPE="radio" NAME="depart" value="C">
<?php } else {?>
&nbsp;
<?php }?>
</TD>
<TD>&nbsp;<B>Départ</B>&nbsp;</TD>
</TR>

<TR BGCOLOR="#C1C1C1">
<TD WIDTH=150 ALIGN=center>
<?php if (count($A) < 7) {?>
<INPUT TYPE="radio" NAME="arrivee" value="A">
<?php } else {?>
&nbsp;
<?php }?>
</TD>
<TD WIDTH=150 ALIGN=center>
<?php if (count($B) < 7) {?>
<INPUT TYPE="radio" NAME="arrivee" value="B">
<?php } else {?>
&nbsp;
<?php }?>
</TD>
<TD WIDTH=150 ALIGN=center>
<?php if (count($C) < 7) {?>
<INPUT TYPE="radio" NAME="arrivee" value="C">
<?php } else {?>
&nbsp;
<?php }?>
</TD>
<TD>&nbsp;<B>Arrivée</B>&nbsp;</TD>
</TR>
<?php
$start = base64_encode(brouille($start,$key));
$hA = base64_encode(brouille(implode(";",$A),$key));
$hB = base64_encode(brouille(implode(";",$B),$key));
$hC = base64_encode(brouille(implode(";",$C),$key));
$history = base64_encode(brouille($history,$key));
$difficulty = base64_encode(brouille($difficulty,$key));
?>
<input type="hidden" name="hA" value="<?php echo $hA;?>">
<input type="hidden" name="hB" value="<?php echo $hB;?>">
<input type="hidden" name="hC" value="<?php echo $hC;?>">
<input type="hidden" name="history" value="<?php echo $history;?>">
<input type="hidden" name="start" value="<?php echo $start;?>">
<input type="hidden" name="difficulty" value="<?php echo $difficulty;?>">
<TR>
<TD COLSPAN=3 ALIGN='center'><BR><INPUT TYPE="submit" VALUE="Déplacer pièce" name="submit"></TD>
<TD><br><input type="submit" value="Recommencer" name="restart"></TD>
</TR>
</FORM>
</TABLE>
</BODY>
</HTML>
Return current item: Anoï Towers