Jogo da Velha com PHP 
Autor: WebEder | Publicado: 2014-05-28T12:38:00-07:00




Exemplo Simples do Jogo da Velha usando GET e SESSION.














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

?&gt;

&lt;?php 

if(isset($_SESSION['views'])){
$pega = $_SESSION['views']= $_SESSION['views']+1;
   
      if($pega % 2 == 0){
      $img =  '&lt;img src="xis.jpg" width="57" height="66"&gt;';
      }  else{
      $img = '&lt;img src="zero.jpg" width="57" height="66"&gt;';
      }
    }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;

}

?&gt;
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;Documento sem título&lt;/title&gt;
&lt;style type="text/css"&gt;
.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;
}
&lt;/style&gt;
&lt;/head&gt;

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