↻'; $_SESSION["message"] = ""; unset($_SESSION["winningchip"]); fillboard(); } if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['selection'])) { $_SESSION["gamemode"] = (int) $_POST['selection']; $_SESSION["currentplayer"] = "red"; $_SESSION["playcounter"] = 0; $_SESSION["restart"] = ''; $_SESSION["message"] = ""; fillboard(); } if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['menu'])) { unset($_SESSION["gamemode"]); unset($_SESSION["currentplayer"]); unset($_SESSION["playcounter"]); unset($_SESSION["restart"]); unset($_SESSION["message"]); unset($_SESSION["board"]); unset($_SESSION["winningchip"]); } function fillboard(){ for ($row = 0; $row < 6; $row++) { for ($col = 0; $col < 7; $col++) { $_SESSION["board"][$row][$col] = "white"; } } } function gamelogic(){ if ($_SESSION["restart"] === '') { if ($_SESSION["gamemode"] === 1) { for ($i = 0; $i <= 1; $i++) { if ($i === 0) { $playerchoice = (int) $_POST['choice']; } else { $playerchoice = botturn(); } $placemem = placechip($playerchoice); $currentplayermem = currentplayerger(); $_SESSION["message"] = $placemem[0] ? "" : "Spalte ist schon voll!"; if (!($_SESSION["message"] === "")) { break; } $_SESSION["playcounter"]++; if ($currentplayermem === "Gelb") { $_SESSION["message"] = checkwin($placemem[1], $playerchoice) ? "Roboter " . $currentplayermem . " hat gewonnen!" : ""; } else { $_SESSION["message"] = checkwin($placemem[1], $playerchoice) ? "Spieler " . $currentplayermem . " hat gewonnen!" : ""; } swapplayer(); if ( $_SESSION["message"] === "Spieler " . $currentplayermem . " hat gewonnen!" || $_SESSION["message"] === "Roboter " . $currentplayermem . " hat gewonnen!" ) { $_SESSION["restart"] = ''; $_SESSION["winningchip"] = [$placemem[1], $playerchoice]; break; } $_SESSION["message"] = $_SESSION["playcounter"] === 42 ? "Unentschieden!" : ""; if ($_SESSION["message"] === "Unentschieden!") { $_SESSION["restart"] = ''; break; } } } else { $playerchoice = (int) $_POST['choice']; $placemem = placechip($playerchoice); $currentplayermem = currentplayerger(); $_SESSION["message"] = $placemem[0] ? "" : "Spalte ist schon voll!"; if (!($_SESSION["message"] === "")) { return; } $_SESSION["playcounter"]++; $_SESSION["message"] = checkwin($placemem[1], $playerchoice) ? "Spieler " . $currentplayermem . " hat gewonnen!" : ""; swapplayer(); if ( $_SESSION["message"] === "Spieler " . $currentplayermem . " hat gewonnen!" ) { $_SESSION["restart"] = ''; $_SESSION["winningchip"] = [$placemem[1], $playerchoice]; return; } $_SESSION["message"] = $_SESSION["playcounter"] === 42 ? "Unentschieden!" : ""; if ($_SESSION["message"] === "Unentschieden!") { $_SESSION["restart"] = ''; } } } } function placechip($col){ for ($row = 6; $row >= 0; $row--) { if ($_SESSION["board"][$row][$col] === "white") { $_SESSION["board"][$row][$col] = $_SESSION["currentplayer"]; return [true, $row]; } } return false; } function checkwin($row, $col){ return $_SESSION["board"][$row][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row + 1][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row + 2][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row + 3][$col] === $_SESSION["currentplayer"] || $_SESSION["board"][$row][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row - 1][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row - 2][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row - 3][$col] === $_SESSION["currentplayer"] || $_SESSION["board"][$row][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row][$col + 1] === $_SESSION["currentplayer"] && $_SESSION["board"][$row][$col + 2] === $_SESSION["currentplayer"] && $_SESSION["board"][$row][$col + 3] === $_SESSION["currentplayer"] || $_SESSION["board"][$row][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row][$col - 1] === $_SESSION["currentplayer"] && $_SESSION["board"][$row][$col - 2] === $_SESSION["currentplayer"] && $_SESSION["board"][$row][$col - 3] === $_SESSION["currentplayer"] || $_SESSION["board"][$row][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row + 1][$col + 1] === $_SESSION["currentplayer"] && $_SESSION["board"][$row + 2][$col + 2] === $_SESSION["currentplayer"] && $_SESSION["board"][$row + 3][$col + 3] === $_SESSION["currentplayer"] || $_SESSION["board"][$row][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row - 1][$col - 1] === $_SESSION["currentplayer"] && $_SESSION["board"][$row - 2][$col - 2] === $_SESSION["currentplayer"] && $_SESSION["board"][$row - 3][$col - 3] === $_SESSION["currentplayer"] || $_SESSION["board"][$row][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row + 1][$col - 1] === $_SESSION["currentplayer"] && $_SESSION["board"][$row + 2][$col - 2] === $_SESSION["currentplayer"] && $_SESSION["board"][$row + 3][$col - 3] === $_SESSION["currentplayer"] || $_SESSION["board"][$row][$col] === $_SESSION["currentplayer"] && $_SESSION["board"][$row - 1][$col + 1] === $_SESSION["currentplayer"] && $_SESSION["board"][$row - 2][$col + 2] === $_SESSION["currentplayer"] && $_SESSION["board"][$row - 3][$col + 3] === $_SESSION["currentplayer"] ? true : false; } function swapplayer(){ $_SESSION["currentplayer"] = $_SESSION["currentplayer"] === "red" ? "yellow" : "red"; } function currentplayerger(){ return $_SESSION["currentplayer"] === "red" ? "Rot" : "Gelb"; } function botturn(){ while (true) { $mem = random_int(0, 6); if ($_SESSION["board"][0][$mem] === "white") { return $mem; } } } ?> 4-GEWINNT

4-GEWINNT

1 Spieler' . ''; } if (isset($_SESSION["gamemode"])) { for ($row = 0; $row < 6; $row++) { for ($col = 0; $col < 7; $col++) { if ($row === 0 && !(isset($_SESSION["winningchip"]))) { echo ''; } else if (isset($_SESSION["winningchip"]) && $_SESSION["winningchip"][0] === $row && $_SESSION["winningchip"][1] === $col) { echo ''; } else { echo ''; } } echo "
"; } echo "
" . $_SESSION["restart"]; echo ''; } ?>