Zend - The PHP Company




Games

Add Code


Tic Tac Toe?  

Type: application
Added by: bleach
Entered: 11/03/2000
Last modified: 08/12/1999
Rating: *** (9 votes)
Views: 10710
This is a rough copy of a tic tac toe type game we had to write once, its not pretty, but its playable how it is, and it depends heavily on sessions...


<?
// This is a fully functional tic-tak-toe game, assuming you have session
// variables installed. Its not clean or pretty, but its one way to do it.
// By the way, the AI for the computer to pick its spot is just a random
// placement, I wrote this game for little kids, if you want to replace the
// alograthim with something complex, that can't be beat, go for it.
// Copyright 2000, Derek Young, at GS Data Design

session_register("tikboard");
session_register("tikplayers");
session_register("tikturn");
session_register("winner");

// This is what happens when they click on start over.
if ($reset) { 
   unset(
$tikboard); unset($tikplayers); unset($tikturn); unset($winner);
}

tophtml("Tik-Tak-Toe!");

// replace these lines to insert the X and O image you want.
// example $X="<IMG SRC="http://blabla.com/myimage.gif">";
// it will work just the way it is, just not pretty ;>

$X="<FONT SIZE="10">X</FONT>";
$O="<FONT SIZE="10">O</FONT>";
$BLANK="<FONT SIZE="10">&nbsp</FONT>"

function 
tophtml($title) {
   global 
$PHP_SELF;
   echo 
"<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ";
   
echo ""http://www.w3c.org/TR/REC-html40/loose.dtd">n";
   
echo "<HTML>n<HEAD>n<TITLE>$title</TITLE>n</HEAD>n";
   echo 
"<BODY ALINK="0000FF" LINK="0000FF" VLINK="0000FF" ";
   echo 
"BGCOLOR="WHITE">n";
   echo 
"<FORM METHOD="POST" ACTION="" . $PHP_SELF . "">n";
}

function 
endhtml() {
   echo 
"</FORM></BODY></HTML>n"
}

//If no turn is decided yet, make X first
if(!$tikturn$tikturn="X";

if(!
$tikboard) {
   
// Initilize Game Board
   
$tikboard[1]="";
   
$tikboard[2]="";
   
$tikboard[3]="";
   
$tikboard[4]="";
   
$tikboard[5]="";
   
$tikboard[6]="";
   
$tikboard[7]="";
   
$tikboard[8]="";
   
$tikboard[9]="";
}

if(!
$tikplayers) {
   
//Initilize Players
   
echo "How many players?n";
   echo 
"<INPUT TYPE="SUBMIT" NAME="tikplayers" VALUE="One">n";
   echo 
"<INPUT TYPE="SUBMIT" NAME="tikplayers" VALUE="Two">n";
   
endhtml();
   exit();
}

function 
fill($space) {
   global 
$tikboard;
   global 
$PHP_SELF;
   global 
$winner;
   global 
$X,$O,$BLANK;
   
   if (
$tikboard[$space]=="X") return $X;
   if (
$tikboard[$space]=="O") return $O;
   if (!
$tikboard[$space]) 
     if (
$winner) return $BLANK;
     else
       return 
"<A HREF="$PHP_SELF?click=$space">".$BLANK."</A>";
}

// handles where they click
if($click && !$tikboard[$click] && !$winner) {
   
$tikboard[$click]=$tikturn;
   if (
$tikturn=="X"$tikturn="O"; else $tikturn="X";
   
//see if we won.
   
checkwinner();
   if (
$tikplayers=="One" && !$winner) {
      
// Play Computers Turn
      
srand((double)microtime()*1000000);
      
$attempt=rand(1,9);
      
$stopat=$attempt;
      while (
$attempt++) {
         if (
$attempt==10$attempt=1;
         if (!
$tikboard[$attempt]) { $tikboard[$attempt]=$tikturn;break; }
         if (
$attempt==$stopat) break;
      }
      if (
$tikturn=="X"$tikturn="O"; else $tikturn="X";
      
//see if he won.
      
checkwinner();
   }
}

function 
checkwinner() {
   global 
$winner;
   global 
$tikboard;

   if ((
$tikboard[1]==$tikboard[2]) && ($tikboard[2]==$tikboard[3]))
       if(
$tikboard[1]) { $winner=$tikboard[1]; return; }

   if ((
$tikboard[4]==$tikboard[5]) && ($tikboard[5]==$tikboard[6]))
       if(
$tikboard[4]) { $winner=$tikboard[1]; return; }

   if ((
$tikboard[7]==$tikboard[8]) && ($tikboard[8]==$tikboard[9]))
       if(
$tikboard[7]) { $winner=$tikboard[1]; return; }

   if ((
$tikboard[1]==$tikboard[4]) && ($tikboard[4]==$tikboard[7]))
       if(
$tikboard[1]) { $winner=$tikboard[1]; return; }

   if ((
$tikboard[2]==$tikboard[5]) && ($tikboard[5]==$tikboard[8]))
       if(
$tikboard[2]) { $winner=$tikboard[1]; return; }

   if ((
$tikboard[3]==$tikboard[6]) && ($tikboard[6]==$tikboard[9]))
       if(
$tikboard[3]) { $winner=$tikboard[1]; return; }

   if ((
$tikboard[1]==$tikboard[5]) && ($tikboard[5]==$tikboard[9]))
       if(
$tikboard[1]) { $winner=$tikboard[1]; return; }

   if ((
$tikboard[3]==$tikboard[5]) && ($tikboard[5]==$tikboard[7]))
       if(
$tikboard[3]) { $winner=$tikboard[1]; return; }
}

//Display the board if all goes well
$space=1;
echo 
"Current Player: " $tikturn "<BR><BR>n";
echo 
"<TABLE BORDER="1" WIDTH="200">n";
echo 
"<TR ALIGN="CENTER">n";
echo 
"<TD WIDTH="33%">".fill($space++)."</TD>n";
echo 
"<TD WIDTH="33%">".fill($space++)."</TD>n";
echo 
"<TD WIDTH="33%">".fill($space++)."</TD>n";
echo 
"</TR><TR ALIGN="CENTER">n";
echo 
"<TD WIDTH="33%">".fill($space++)."</TD>n";
echo 
"<TD WIDTH="33%">".fill($space++)."</TD>n";
echo 
"<TD WIDTH="33%">".fill($space++)."</TD>n";
echo 
"</TR><TR ALIGN="CENTER">n";
echo 
"<TD WIDTH="33%">".fill($space++)."</TD>n";
echo 
"<TD WIDTH="33%">".fill($space++)."</TD>n";
echo 
"<TD WIDTH="33%">".fill($space++)."</TD>n";
echo 
"</TR>n</TABLE><BR>n";
if (
$winner) echo $winner." is the winner!<BR>";
echo 
"<A HREF="$PHP_SELF?reset=1">Start Over</A>";

endhtml();
?>


Usage Example




Rate This Script





Search



This Category All Categories