WIP: correction-tp4 #3
@@ -1,54 +1,83 @@
|
|||||||
<?php
|
<?php
|
||||||
|
function initializeGame()
|
||||||
|
{
|
||||||
|
// Une grille vide contient 9 cases valant 0.
|
||||||
|
$_SESSION['grid'] = array_fill(0, 9, 0);
|
||||||
|
$_SESSION['playerTurn'] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function nextPlayer($player)
|
||||||
|
{
|
||||||
|
return $player === 1 ? 2 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
function isWinner($grid, $player)
|
function isWinner($grid, $player)
|
||||||
{
|
{
|
||||||
$winStates = array
|
$winStates = array(
|
||||||
(
|
array(0, 1, 2), array(3, 4, 5), array(6, 7, 8),
|
||||||
array(0, 1, 2), array(3, 4, 5), array(6, 7, 8), // Horizontal
|
array(0, 3, 6), array(1, 4, 7), array(2, 5, 8),
|
||||||
array(0, 3, 6), array(1, 4, 7), array(2, 5, 8), // Vertical
|
array(0, 4, 8), array(2, 4, 6)
|
||||||
array(0, 4, 8), array(2, 4, 6) // Diagonal
|
);
|
||||||
);
|
|
||||||
|
|
||||||
foreach ($winStates as $winState)
|
foreach ($winStates as $winState) {
|
||||||
{
|
if (
|
||||||
if ($grid[$winState[0]] == $player &&
|
$grid[$winState[0]] == $player &&
|
||||||
$grid[$winState[1]] == $player &&
|
$grid[$winState[1]] == $player &&
|
||||||
$grid[$winState[2]] == $player)
|
$grid[$winState[2]] == $player
|
||||||
{
|
) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function noWinner($grid)
|
function noWinner($grid)
|
||||||
{
|
{
|
||||||
for($i = 0; $i < 9; $i++)
|
for ($i = 0; $i < 9; $i++) {
|
||||||
if ($grid[$i] == 0)
|
if ($grid[$i] == 0) {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function displayGrid($grid)
|
function getWinner($grid)
|
||||||
|
{
|
||||||
|
if (isWinner($grid, 1)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isWinner($grid, 2)) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function displayGrid($grid, $isInteractive = true)
|
||||||
{
|
{
|
||||||
echo '<table class="morpion">';
|
echo '<table class="morpion">';
|
||||||
for ($i = 0; $i < 3; $i ++){
|
for ($i = 0; $i < 3; $i++) {
|
||||||
echo "<tr>";
|
echo '<tr>';
|
||||||
for ($j = 0; $j < 3; $j ++){
|
for ($j = 0; $j < 3; $j++) {
|
||||||
|
echo '<td>';
|
||||||
|
|
||||||
echo "<td>";
|
$pos = 3 * $i + $j;
|
||||||
|
if ($grid[$pos] == 0 && $isInteractive) {
|
||||||
$pos = 3*$i + $j;
|
|
||||||
if ($grid[$pos] == 0)
|
|
||||||
echo "<a href='?pos=$pos'></a>";
|
echo "<a href='?pos=$pos'></a>";
|
||||||
if ($grid[$pos] == 1)
|
}
|
||||||
|
if ($grid[$pos] == 1) {
|
||||||
echo '<i class="fa fa-times" aria-hidden="true"></i>';
|
echo '<i class="fa fa-times" aria-hidden="true"></i>';
|
||||||
if ($grid[$pos] == 2)
|
}
|
||||||
|
if ($grid[$pos] == 2) {
|
||||||
echo '<i class="fa fa-circle-o" aria-hidden="true"></i>';
|
echo '<i class="fa fa-circle-o" aria-hidden="true"></i>';
|
||||||
|
}
|
||||||
|
|
||||||
echo "</td>";
|
echo '</td>';
|
||||||
}
|
}
|
||||||
echo "</tr>";
|
echo '</tr>';
|
||||||
}
|
}
|
||||||
echo "</table>";
|
echo '</table>';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,70 @@
|
|||||||
<?php
|
<?php
|
||||||
include 'helpers.php';
|
session_start();
|
||||||
|
|
||||||
// view variables
|
include_once 'helpers.php';
|
||||||
$message = "";
|
|
||||||
$grid = [0,1,0,1,0,2,2,0,0];
|
|
||||||
$playerTurn = 1; // 1 or 2
|
|
||||||
|
|
||||||
|
// Si on demande une nouvelle partie, on réinitialise l'état stocké
|
||||||
|
// en session puis on recharge la page sans paramètre.
|
||||||
|
if (isset($_GET['reset'])) {
|
||||||
|
initializeGame();
|
||||||
|
header('Location: .');
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
include './views/tictactoe.php';
|
if (!isset($_SESSION['grid']) || !isset($_SESSION['playerTurn'])) {
|
||||||
|
initializeGame();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Variables transmises à la vue.
|
||||||
|
$message = '';
|
||||||
|
$grid = $_SESSION['grid'];
|
||||||
|
$playerTurn = (int) $_SESSION['playerTurn'];
|
||||||
|
|
||||||
|
$winner = getWinner($grid);
|
||||||
|
$isGameOver = $winner !== 0 || noWinner($grid);
|
||||||
|
|
||||||
|
// On joue uniquement si la partie est encore en cours.
|
||||||
|
if (!$isGameOver && isset($_GET['pos'])) {
|
||||||
|
$pos = filter_input(
|
||||||
|
INPUT_GET,
|
||||||
|
'pos',
|
||||||
|
FILTER_VALIDATE_INT,
|
||||||
|
array('options' => array('min_range' => 0, 'max_range' => 8))
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($pos === false || $pos === null) {
|
||||||
|
$message = 'Coup invalide.';
|
||||||
|
} elseif ($grid[$pos] !== 0) {
|
||||||
|
$message = 'Cette case est déjà occupée.';
|
||||||
|
} else {
|
||||||
|
// On enregistre le coup du joueur courant dans la grille.
|
||||||
|
$grid[$pos] = $playerTurn;
|
||||||
|
|
||||||
|
if (isWinner($grid, $playerTurn)) {
|
||||||
|
$message = "Le joueur $playerTurn gagne la partie.";
|
||||||
|
} elseif (noWinner($grid)) {
|
||||||
|
$message = 'Match nul.';
|
||||||
|
} else {
|
||||||
|
$playerTurn = nextPlayer($playerTurn);
|
||||||
|
}
|
||||||
|
|
||||||
|
$_SESSION['grid'] = $grid;
|
||||||
|
$_SESSION['playerTurn'] = $playerTurn;
|
||||||
|
}
|
||||||
|
|
||||||
|
$winner = getWinner($grid);
|
||||||
|
$isGameOver = $winner !== 0 || noWinner($grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si la page est simplement rafraîchie, on recalcule un message cohérent
|
||||||
|
// à partir de l'état stocké en session.
|
||||||
|
if ($message === '') {
|
||||||
|
if ($winner !== 0) {
|
||||||
|
$message = "Le joueur $winner gagne la partie.";
|
||||||
|
} elseif (noWinner($grid)) {
|
||||||
|
$message = 'Match nul.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
include_once './views/tictactoe.php';
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,35 @@
|
|||||||
<html>
|
<!DOCTYPE html>
|
||||||
|
<html lang="fr">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
|
<title>Tic Tac Toe</title>
|
||||||
<link
|
<link
|
||||||
rel="stylesheet"
|
rel="stylesheet"
|
||||||
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
|
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
|
||||||
/>
|
/>
|
||||||
<link rel="stylesheet" href="./css/style.css">
|
<link rel="stylesheet" href="./css/style.css">
|
||||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<main class="container center">
|
<main class="container center">
|
||||||
<h4>Tic Tac Toe : <?php echo "player $playerTurn turn";?></h4>
|
<h4>
|
||||||
|
<?php if ($isGameOver) { ?>
|
||||||
|
Partie terminée
|
||||||
|
<?php } else { ?>
|
||||||
|
Tic Tac Toe : joueur <?php echo $playerTurn; ?> à vous de jouer
|
||||||
|
<?php } ?>
|
||||||
|
</h4>
|
||||||
<?php
|
<?php
|
||||||
displayGrid($grid);
|
// Quand la partie est finie, les liens dans les cases vides
|
||||||
|
// disparaissent pour empêcher tout coup supplémentaire.
|
||||||
|
displayGrid($grid, !$isGameOver);
|
||||||
|
|
||||||
if ($message != "")
|
if ($message != '') {
|
||||||
echo "<h5>$message <a href='.'>new game</a></h5>";
|
echo '<h5>' . htmlspecialchars($message, ENT_QUOTES) . " <a href='?reset=1'>nouvelle partie</a></h5>";
|
||||||
?>
|
} else {
|
||||||
|
echo "<p><a href='?reset=1'>Recommencer la partie</a></p>";
|
||||||
|
}
|
||||||
|
?>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user