1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
<?php
class MasterMind {
private $code;
private $tableauPropositions = array();
private $bienPlaces = array();
private $malPlaces = array();
private $nbTentatives;
public function __construct() { // constructeur qui genere mon code aleatoire
$chiffres = array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
shuffle($chiffres); // je melange mon tableau de 9 chiffres...
$this->code = array ($chiffres[0], $chiffres[1], $chiffres[2], $chiffres[3]); //...et je prends les 4 premieres cases
$this->nbTentatives = 0;
}
public function getCode($i) {
return $this->code[$i];
}
public function getTableauPropositions($i) {
return $this->tableauProposiitons[$i];
}
public function getBienPlaces($i) {
return $this->bienPlaces[$i];
}
public function getMalPlaces($i) {
return $this->malPlaces[$i];
}
public function getNbTentatives() {
return $this->nbTentatives;
}
public function setCode($i, $valeur) {
$this->code[$i] = $valeur;
}
public function setTableauPropositions($i, $valeur) {
$this->tableauPropositions[$i] = $valeur;
}
public function setBienPlaces($i, $valeur) {
$this->bienPlaces[$i] = $valeur;
}
public function setMalPlaces($i, $valeur) {
$this->malPlaces[$i] = $valeur;
}
public function setNbTentatives($valeur) {
$this->nbTentatives = $valeur;
}
public function test($prop) {
// je mets l'entier passé en paramètre dans un tableau
$propTableau[3] = $prop % 10;
$propTableau[2] = ($prop / 10) % 10;
$propTableau[1] = ($prop / 100) % 10;
$propTableau[0] = (int)($prop / 1000);
// je compare les deux tableaux $propTableau et $code
$this->bienPlaces[$this->nbTentatives] = 0;
$this->malPlaces[$this->nbTentatives] = 0;
for ($i=0 ; $i<4 ; $i++) {
if ($propTableau[$i] == $this->code[$i]) {
$this->bienPlaces[$this->nbTentatives]++;
}
else if (in_array($propTableau[$i], $this->code)) {
$this->malPlaces[$this->nbTentatives]++;
}
}
}
public function afficheTableauJeu() {
?>
<table border="1">
<tr><td>Numero</td> <td>Propositions</td> <td>Bien placé(s)</td> <td>Mal placé(s)</td></tr>
<?php
for ($i=1 ; $i <= $this->nbTentatives ; $i++) {
?>
<tr><td><?php echo $i;?></td>
<td> <?php echo $this->tableauPropositions[$i];?></td>
<td> <?php echo $this->bienPlaces[$i];?></td>
<td> <?php echo $this->malPlaces[$i];?></td></tr>
<?php
}
?>
<tr><td></td>
<td><form action="mastermindSession2.php" method="post">
<input type="number" name="proposition" /></td>
<td></td><td></td></tr>
</table>
<input type="submit" value="Valider" />
</form>
<?php
}
}
$lifetime=600;
session_start();
setcookie(session_name(),session_id(),time()+$lifetime);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="" />
<title>Mastermind avec Session</title>
</head>
<body>
<?php
if (!(isset($_POST['proposition']))) { // si j'arrive la premiere fois sur la page
$_SESSION['mastermind'] = new Mastermind(); // je genere une instance de Mastermind et son code dans la session
$_SESSION['mastermind']->afficheTableauJeu();
} else if ((is_numeric($_POST['proposition'])) && (strlen(strval($_POST['proposition']))) == 4) { // cas normal
$_SESSION['mastermind']->setNbTentatives($_SESSION['mastermind']->getNbTentatives() + 1);
$_SESSION['mastermind']->setTableauPropositions($_SESSION['mastermind']->getNbTentatives(), $_POST['proposition']);
$_SESSION['mastermind']->test($_POST['proposition']);
$_SESSION['mastermind']->afficheTableauJeu();
if (($_SESSION['mastermind']->getBienPlaces($_SESSION['mastermind']->getNbTentatives())) == 4) { // cas de victoire
echo "VICTOIRE! FELICITATIONS!";
}
}
else { // cas "saisie incorrecte"
$_SESSION['mastermind']->afficheTableauJeu();
echo "Veuillez seulement saisir 4 chiffres distincts, svp!";
}
?>
</body>
</html> |
Partager