Skip to content

Instantly share code, notes, and snippets.

@guibranco
Created January 4, 2018 01:52
Show Gist options
  • Save guibranco/2ef2cdb0d6d25a040e24bc823a9bf92b to your computer and use it in GitHub Desktop.
Save guibranco/2ef2cdb0d6d25a040e24bc823a9bf92b to your computer and use it in GitHub Desktop.
Famoso jogo da velha em PHP
<?php
session_start();
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$checked = false;
$encerrado = false;
$preenchido = false;
$ganhador = null;
function ganhador(){
global $ganhador;
$diaA = $_SESSION["game"][0][0] == $_SESSION["game"][1][1] && $_SESSION["game"][0][0] == $_SESSION["game"][2][2] && $_SESSION["game"][0][0] != null;
$diaB = $_SESSION["game"][0][2] == $_SESSION["game"][1][1] && $_SESSION["game"][0][2] == $_SESSION["game"][2][0] && $_SESSION["game"][0][2] != null;
if($diaA)
$ganhador = "Ganhador diagonal esquerda: " . $_SESSION["game"][0][0];
else if($diaB)
$ganhador = "Ganhador diagonal direita: " . $_SESSION["game"][0][2];
if($diaA || $diaB)
return true;
for($x = 0; $x <= 2; $x++)
{
$row = $_SESSION["game"][$x][0] == $_SESSION["game"][$x][1] && $_SESSION["game"][$x][0] == $_SESSION["game"][$x][2] && $_SESSION["game"][$x][0] != null;
$col = $_SESSION["game"][0][$x] == $_SESSION["game"][1][$x] && $_SESSION["game"][0][$x] == $_SESSION["game"][2][$x] && $_SESSION["game"][0][$x] != null;
if($row)
$ganhador = "Ganhador linha " . $x . ": " . $_SESSION["game"][$x][0];
else if($col)
$ganhador = "Ganhador coluna " . $x . ": " . $_SESSION["game"][0][$x];
if($row || $col)
return true;
}
return false;
}
function verifica(&$argumento,$valor){
global $checked, $encerrado, $preenchido;
if(ganhador()){
$checked = true;
$encerrado = true;
return;
}
if($argumento == null)
$argumento=$valor;
else
$preenchido = true;;
}
if($_SERVER['REQUEST_METHOD'] == "POST" && !isset($_POST["mostrar"])){
$_SESSION["jogada"] = isset($_SESSION["jogada"]) ? ++$_SESSION["jogada"] : 0;
$valor = $_SESSION["jogada"] % 2 == 0 ? "X" : "O";
if(isset($_POST['botao0']))
verifica($_SESSION["game"][0][0],$valor);
if(isset($_POST['botao1']))
verifica($_SESSION["game"][0][1],$valor);
if(isset($_POST['botao2']))
verifica($_SESSION["game"][0][2],$valor);
if(isset($_POST['botao3']))
verifica($_SESSION["game"][1][0],$valor);
if(isset($_POST['botao4']))
verifica($_SESSION["game"][1][1],$valor);
if(isset($_POST['botao5']))
verifica($_SESSION["game"][1][2],$valor);
if(isset($_POST['botao6']))
verifica($_SESSION["game"][2][0],$valor);
if(isset($_POST['botao7']))
verifica($_SESSION["game"][2][1],$valor);
if(isset($_POST['botao8']))
verifica($_SESSION["game"][2][2],$valor);
if(isset($_POST['destruir'])){
session_unset();
session_destroy();
}
}
if(!$checked)
ganhador();
?>
<!DOCTYPE html>
<html>
<head>
<title>Jogo da Velha</title>
<style>
input.classeTeste{
width:100px;
height:50px;
margin-bottom:10px;
background-color: blue;
font-size:45px;
color:black;
text-shadow:1px 1px 4px white;
box-shadow: 2px 2px 2px gray;
border:none;
padding-bottom:-5px;
}
input.classeTeste:hover{
background-color:green;
}
input.classeTeste[disabled^='disabled']{
background-color:red;
}
</style>
</head>
<body>
<div style="float:left; width:400px;">
<form action="" name="form" method="POST">
<?php
$b = 0;
for($x = 0; $x<3; $x++){
for($y = 0; $y<3; $y++){
$v = !isset($_SESSION["game"][$x][$y]) ? " " : $_SESSION["game"][$x][$y];
$c = $v == " " ? "" : " disabled='disabled'";
echo "<input type='submit' class='classeTeste' name='botao" . ($b++). "' value='" . $v . "'" . $c . " />\r\n\t\t";
}
echo "<br />\r\n\t\t";
}
?>
<br />
<br />
<input type="submit" name="mostrar" value="mostrar">
<input type="submit" name="destruir" value="destruir">
<br />
<br />
<?php
if($encerrado)
echo "Jogo encerrado! Já existe um ganhador!!!<br />";
if($preenchido)
echo "Esta posição já foi preenchida anteriormente!<br />";
if($ganhador != null)
echo $ganhador;
?>
</form>
</div>
<div style="float:left;width:calc(100% - 550px)">
<?php
var_dump($_SESSION);
?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment