Created
June 6, 2017 12:05
-
-
Save everaldo/49d624f22311b8d4003955d175c6a5ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define("PERDEU", -1); | |
define("JOGANDO", 0); | |
define("GANHOU", 1); | |
define("MAX_CHUTES", 5); | |
define("MIN_SEGREDO", 1); | |
define("MAX_SEGREDO", 100); | |
function eh_novo_jogo(){ | |
return $_SERVER["REQUEST_METHOD"] == "GET"; | |
} | |
function get_segredo(){ | |
if(eh_novo_jogo() || empty($_SESSION["segredo"])){ | |
$_SESSION["segredo"] = rand(MIN_SEGREDO, MAX_SEGREDO); | |
} | |
return $_SESSION["segredo"]; | |
} | |
function limpar_chutes(){ | |
$_SESSION["chutes"] = []; | |
} | |
function validar_chute(){ | |
return isset($_POST["chute"]) && //existe | |
! empty($_POST["chute"]) && //não é vazia | |
is_numeric($_POST["chute"]) && // é numérica | |
! in_array($_POST["chute"], $_SESSION["chutes"]) && | |
! (count($_SESSION["chutes"]) >= MAX_CHUTES) && | |
//não está no array | |
$_POST["chute"] >= MIN_SEGREDO && | |
$_POST["chute"] <= MAX_SEGREDO; | |
} | |
function registrar_chute(){ | |
if(validar_chute()){ | |
$chute = $_POST["chute"]; | |
$_SESSION["chutes"][] = $chute; | |
return true; | |
} | |
return false; | |
} | |
function imprimir_chutes(){ | |
foreach($_SESSION["chutes"] as $chute){ | |
echo $chute . ", " ; | |
} | |
echo "<br>"; | |
} | |
function acertou_segredo($segredo, $chutes){ | |
return in_array($segredo, $chutes); | |
} | |
function verifica_jogo($segredo, $chutes){ | |
$num_chutes = count($chutes); | |
if(acertou_segredo($segredo, $chutes)){ | |
return GANHOU; | |
} | |
else if($num_chutes == MAX_CHUTES){ | |
return PERDEU; | |
} | |
return JOGANDO; | |
} | |
function jogo_adivinha(){ | |
session_start(); | |
$segredo = get_segredo(); | |
if(eh_novo_jogo()){ | |
limpar_chutes(); | |
imprimir_formulario(); | |
} | |
else{ | |
$chute_valido = registrar_chute(); | |
if(! $chute_valido){ | |
imprimir_formulario(); | |
return false; | |
} | |
$estado_jogo = verifica_jogo($segredo, $_SESSION["chutes"]); | |
imprimir_resultado($estado_jogo, $segredo, $_SESSION["chutes"], | |
$_POST["chute"]); | |
} | |
} | |
function imprime_jogando($segredo, $chutes, $chute){ | |
if($segredo > $chute){ | |
echo "<p>O segredo é maior</p>"; | |
} | |
else{ | |
echo "<p>O segredo é menor</p>"; | |
} | |
imprimir_formulario(); | |
imprimir_chutes(); | |
} | |
function imprime_perdeu($segredo){ | |
echo "Você não é X-Men. O segredo era: " . $segredo; | |
} | |
function imprime_ganhou(){ | |
echo "Bem-vindo à mansão Xavier"; | |
} | |
function imprimir_resultado($estado_jogo, | |
$segredo, $chutes, $chute){ | |
switch($estado_jogo){ | |
case PERDEU: | |
imprime_perdeu($segredo); | |
break; | |
case JOGANDO: | |
imprime_jogando($segredo, $chutes, $chute); | |
break; | |
case GANHOU: | |
imprime_ganhou(); | |
break; | |
} | |
} | |
function imprimir_formulario(){ | |
echo <<<EOT | |
<form action="" method="post"> | |
<input type="text" name="chute" value=""> | |
<input type="submit" name="" value="ENVIAR"> | |
</form> | |
EOT; | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Jogo do Adivinha</title> | |
</head> | |
<body> | |
<?php jogo_adivinha(); ?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment