Jogo da Velha com PHP

Exemplo Simples do Jogo da Velha usando GET e SESSION.







<?php 
    session_start();
    function destroy(){
    session_unset();
    unset($_SESSION['views']);
    unset($_SESSION['lance1']);
     
}
if ((isset($_GET['limpa'])) &&($_GET['limpa']=="true")){
     session_unset();
    session_destroy();
    destroy();
}

?>

<?php 

if(isset($_SESSION['views'])){
$pega = $_SESSION['views']= $_SESSION['views']+1;
   
      if($pega % 2 == 0){
      $img =  '<img src="xis.jpg" width="57" height="66">';
      }  else{
      $img = '<img src="zero.jpg" width="57" height="66">';
      }
    }else{
        //se não existe ele começa em 1
        $_SESSION['views']=1;
        }



if (isset($_GET['jogada'])){
switch($_GET['jogada']){    
    case 1:
    $lance1 = $_SESSION['lance1']=$img;
    break;
    case 2:
    $lance2 = $_SESSION['lance2']=$img;
    break;  
    case 3:
    $lance1 = $_SESSION['lance3']=$img;
    break;
    case 4:
    $lance2 = $_SESSION['lance4']=$img;
    break;
    case 5:
    $lance1 = $_SESSION['lance5']=$img;
    break;
    case 6:
    $lance2 = $_SESSION['lance6']=$img;
    break;
    case 7:
    $lance1 = $_SESSION['lance7']=$img;
    break;
    case 8:
    $lance2 = $_SESSION['lance8']=$img;
    break;
    case 9:
    $lance9 = $_SESSION['lance9']=$img;
    break;
}

$play = $_SESSION['views'];
$msg = "Total de jogadas = ".--$play;

}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sem título</title>
<style type="text/css">
.botao {
    background-color: #F93;
    padding: 10px;
    color: #000;
    text-decoration: none;
}
.botao a {
    color: #FFF;
    text-decoration: none;
}
.botao a:hover {
    background-color: #C90;
    padding: 15px;
}
.botao a:visited  {
    color: #FFF;
    text-decoration: none;
}

.botao a:active  {
    color: #FFF;
    text-decoration: none;
}
.msg {
    font-family: Verdana, Geneva, sans-serif;
    font-size: 24px;
    color: #F93;
}
#fundo {
    background-image: url(fundo.jpg);
    background-repeat: no-repeat;
    height: 443px;
    width: 515px;
    margin-right: auto;
    margin-left: auto;
    padding-bottom: 60px;
    margin-bottom: 60px;
}
a:link {
    font-family: Verdana, Geneva, sans-serif;
    color: #000;
    text-decoration: none;
    padding: 5px;
}
a:visited {
    color: #000;
    text-decoration: none;
}
a:active {
    color: #000;
}
</style>
</head>

<body>
<table width="517" border="0" align="center">
  <tr>
    <td height="45" colspan="2" align="center" valign="top"><span class="msg"><?php if(!empty($msg)){ echo $msg; }?></span></td>
  </tr>
  <tr>
    <td width="254" align="center" bgcolor="#FF9933"><span class="botao"><a href="array.php?limpa=true">Limpar </a></span></td>
    <td width="253" align="center" bgcolor="#FF9933"><span class="botao"><a href="array.php?jagada=play" >Play</a></span></td>
  </tr>
</table>
<br>
<br>
<div id="fundo">
<table width="515" height="487" border="0" align="center" cellspacing="0">
  <tr>
    <td width="121" height="145" align="center"><table width="161" height="126" border="0">
        <tr>
          <td align="center" valign="middle"><?php if(isset($_SESSION['lance1'])){
    echo $_SESSION['lance1'];
}else{
echo '<p><a href="array.php?jogada=1">Jogar</a></p>';   
}
    
    ?></td>
        </tr>
    </table></td>
    <td width="112" align="center"><table width="161" height="126" border="0">
        <tr>
          <td align="center" valign="middle"><?php if(isset($_SESSION['lance2'])){
    echo $_SESSION['lance2'];
}else{
echo '<p><a href="array.php?jogada=2">Jogar</a></p>';   
}
    
    ?></td>
        </tr>
    </table></td>
    <td width="80" align="center"><table width="161" height="126" border="0">
        <tr>
          <td align="center" valign="middle"><?php if(isset($_SESSION['lance3'])){
    echo $_SESSION['lance3'];
}else{
echo '<p><a href="array.php?jogada=3">Jogar</a></p>';   
}
    
    ?></td>
        </tr>
    </table></td>
  </tr>
  <tr>
    <td height="165" align="center"><table width="161" height="126" border="0">
        <tr>
          <td align="center" valign="middle"><?php if(isset($_SESSION['lance4'])){
    echo $_SESSION['lance4'];
}else{
echo '<p><a href="array.php?jogada=4">Jogar</a></p>';   
}
    
    ?></td>
        </tr>
    </table></td>
    <td align="center"><table width="161" height="126" border="0">
        <tr>
          <td align="center" valign="middle"><?php if(isset($_SESSION['lance5'])){
    echo $_SESSION['lance5'];
}else{
echo '<p><a href="array.php?jogada=5">Jogar</a></p>';   
}
    
    ?></td>
        </tr>
    </table></td>
    <td align="center"><table width="161" height="126" border="0">
        <tr>
          <td align="center" valign="middle"><?php if(isset($_SESSION['lance6'])){
    echo $_SESSION['lance6'];
}else{
echo '<p><a href="array.php?jogada=6">Jogar</a></p>';   
}
    
    ?></td>
        </tr>
    </table></td>
  </tr>
  <tr>
    <td height="177" align="center" valign="top"><table width="161" height="126" border="0">
        <tr>
          <td align="center" valign="middle"><?php if(isset($_SESSION['lance7'])){
    echo $_SESSION['lance7'];
}else{
echo '<p><a href="array.php?jogada=7">Jogar</a></p>';   
}
    
    ?></td>
        </tr>
    </table></td>
    <td align="center" valign="top"><table width="162" height="126" border="0">
      <tr>
        <td width="156" align="center" valign="middle"><?php if(isset($_SESSION['lance8'])){
    echo $_SESSION['lance8'];
}else{
echo '<p><a href="array.php?jogada=8">Jogar</a></p>';   
}
    
    ?></td>
      </tr>
    </table></td>
    <td align="center" valign="top"><table width="161" height="126" border="0">
      <tr>
        <td align="center" valign="middle"><?php if(isset($_SESSION['lance9'])){
    echo $_SESSION['lance9'];
}else{
echo '<p><a href="array.php?jogada=9">Jogar</a></p>';   
}
    
    ?></td>
      </tr>
    </table></td>
  </tr>
</table></div>
</body>
</html>

Comentários