Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.49 KB | None | 0 0
  1. <?php
  2. #php3T version 1.2
  3. ############################################
  4. #Copyright (C) 2001 Evan Schulz
  5. #
  6. #This program is free software; you can redistribute it and/or
  7. #modify it under the terms of the GNU General Public License
  8. #as published by the Free Software Foundation; either version 2
  9. #of the License, or (at your option) any later version.
  10. #
  11. #This program is distributed in the hope that it will be useful,
  12. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #GNU General Public License for more details.
  15. #
  16. #You should have received a copy of the GNU General Public License
  17. #along with this program; if not, write to the Free Software
  18. #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. ###########################################
  20. # SET THESE:
  21. $pagetitle='php3T 1.2';
  22. $pagebackground='lightgreen';
  23. $pageimage='title.gif';
  24. $pageimagealt=$pagetitle;
  25.  
  26. $tblbackground='darkgreen';
  27.  
  28. $cellbackground='lightgreen';
  29.  
  30. $ximage='x.gif';
  31. $ximagealt='x';
  32. $oimage='o.gif';
  33. $oimagealt='o';
  34.  
  35. $defaultdifficulty='Normal';
  36. #####################
  37. if (isset($new))
  38. {
  39.     session_start();
  40.     unset($b);
  41.     unset($turn);
  42.     unset($cdiff);
  43.     session_destroy();
  44. }
  45.  
  46. session_start();
  47. if (! isset($turn))
  48. {
  49.     session_register(turn);
  50.     $turn=1;
  51.     session_register(b);
  52.     session_register(cdiff);
  53.     if ($diff == 'e')
  54.         $cdiff = 'Easy';
  55.     elseif ($diff == 'n')
  56.         $cdiff = 'Normal';
  57.     elseif ($diff == 'i')
  58.         $cdiff = 'Impossible';
  59.     else
  60.         $cdiff = $defaultdifficulty;
  61. }
  62. global $b;
  63. global $gwin;
  64. global $gover;
  65. ?>
  66. <HTML><HEAD><TITLE><?php echo $pagetitle ?></TITLE></HEAD>
  67. <BODY BGCOLOR="<?php echo $pagebackground ?>" TOPMARGIN="0">
  68. <IMG SRC="<?php echo $pageimage ?>" ALT="<?php echo $pageimagealt ?>">
  69. <BR>
  70. <FORM ACTION="<?php echo $PHP_SELF ?>" METHOD="POST">
  71. <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" BGCOLOR="<?php echo $tblbackground ?>">
  72. <TR><TD>
  73. <TABLE BORDER="1" CELLPADDING="0" CELLSPACING="10">
  74. <?php
  75. //take input if got input
  76. if (isset($mv))
  77.     $b[$mv]='x';
  78.  
  79. checkwin();
  80. checkfull();
  81.  
  82.  
  83. // calculate computers move
  84. if ($gover <> 1 && $gwin == '' && $mv <> '')
  85. {
  86.     if ($cdiff == 'Easy')
  87.     {
  88.         comprand();
  89.     }
  90.     elseif ($cdiff == 'Normal')
  91.     {
  92.         compmove();
  93.                
  94.         if ($cmv == '')
  95.         {
  96.             comprand();
  97.         }
  98.     }
  99.     elseif ($cdiff == 'Impossible')
  100.     {
  101.         compmove();
  102.         if ($cmv == '')
  103.         {
  104.             if ($b[4] == '')
  105.                 $cmv=4;
  106.             elseif ($b[0] == '')
  107.                 $cmv=0;
  108.             elseif ($b[2] == '')
  109.                 $cmv=2;
  110.             elseif ($b[6] == '')
  111.                 $cmv=6;
  112.             elseif ($b[8] == '')
  113.                 $cmv=8;
  114.             if ($cmv == '')
  115.                 comprand();
  116.         }
  117.        
  118.     }
  119.     $b[$cmv] = 'o';
  120. }
  121. // **********
  122. checkwin();
  123. checkfull();
  124.  
  125.  
  126. // Print board
  127. for ($i = 0; $i <= 8; $i++)
  128. {
  129.     if ($i == 0 || $i == 3 || $i == 6)
  130.         print '<TR>';
  131.     print '<TD WIDTH="75" HEIGHT="75" ALIGN="MIDDLE" VALIGN="MIDDLE" BGCOLOR="'.$cellbackground.'">';
  132.    
  133.     if ($b[$i] == 'x')
  134.         print '<IMG SRC="'.$ximage.'" ALT="'.$ximagealt.'">';
  135.     elseif ($b[$i] == 'o')
  136.         print '<IMG SRC="'.$oimage.'" ALT="'.$oimagealt.'">';
  137.     elseif ($gwin == '')
  138.         print '<INPUT TYPE="SUBMIT" NAME="mv" VALUE="'.$i.'">';
  139.    
  140.     print '</TD>';
  141.     if ($i == 2 || $i == 5 || $i == 8)
  142.         print '</TR>';
  143. }
  144. // *************
  145. ?>
  146. </TABLE></TD></TR></TABLE>
  147. <?php
  148. print '<FONT SIZE="-1">Current Difficulty: '.$cdiff.'</FONT>';
  149. if ($gwin == 'O' || $gwin == 'X')
  150.     print "<P><B>$gwin is the winner!</B></P>";
  151. elseif ($gover == 1)
  152.     print "<P><B>The Game is a Draw!</B></P>";
  153. print '<P>New Difficulty: <SELECT NAME="diff"><OPTION VALUE="e">Easy</OPTION><OPTION VALUE="n" SELECTED>Normal</OPTION><OPTION VALUE="i">Impossible</OPTION></SELECT><BR><INPUT TYPE="SUBMIT" NAME="new" VALUE="Start a new game">';
  154. ?>
  155.  
  156. </FORM><p><a href="/">Back to ivan37.com</a></BODY></HTML>
  157. <?php
  158. ## functions:
  159. function checkfull()
  160. {
  161.     global $b;
  162.     global $gover;
  163.    
  164.     $gover = 1;
  165.     for ($ii = 0; $ii <= 8; $ii++)
  166.     {
  167.         if ($b[$ii] == '')
  168.         {
  169.             $gover = 0;
  170.             return;
  171.         }
  172.     }
  173. }
  174. ####
  175. function checkwin()
  176. {
  177.     global $b;
  178.     global $gwin;
  179.     $c=1;
  180.     while ($c <= 2)
  181.     {
  182.         if ($c == 1)
  183.             $t='o';
  184.         else
  185.             $t='x';
  186.         if (
  187.         # horizontal
  188.         ($b[0] == $t && $b[1] == $t && $b[2] == $t) ||
  189.         ($b[3] == $t && $b[4] == $t && $b[5] == $t) ||
  190.         ($b[6] == $t && $b[7] == $t && $b[8] == $t) ||
  191.         # vertical
  192.         ($b[0] == $t && $b[3] == $t && $b[6] == $t) ||
  193.         ($b[1] == $t && $b[4] == $t && $b[7] == $t) ||
  194.         ($b[2] == $t && $b[5] == $t && $b[8] == $t) ||
  195.         # diagonal
  196.         ($b[0] == $t && $b[4] == $t && $b[8] == $t) ||
  197.         ($b[2] == $t && $b[4] == $t && $b[6] == $t))
  198.         {
  199.             $gwin = strtoupper($t);
  200.             return;
  201.         }
  202.         $c++;
  203.     }
  204. }
  205.  
  206. function compmove()
  207. {
  208.     global $cmv;
  209.     global $b;
  210.     for ($c = 0; $c <=1; $c++)
  211.     {
  212.         if ($c == 0)
  213.             $t='o';
  214.         else
  215.             $t='x';
  216.    
  217.         if ($b[0] == $t && $b[1] == $t && $b[2] == '')
  218.             $cmv = 2;
  219.     if ($b[0] == $t && $b[1] == '' && $b[2] == $t)
  220.             $cmv = 1;      
  221.         if ($b[0] == '' && $b[1] == $t && $b[2] == $t)
  222.             $cmv = 0;
  223.         if ($b[3] == $t && $b[4] == $t && $b[5] == '')
  224.             $cmv = 5;
  225.         if ($b[3] == $t && $b[4] == '' && $b[5] == $t)
  226.             $cmv = 4;      
  227.         if ($b[3] == '' && $b[4] == $t && $b[5] == $t)
  228.             $cmv = 3;
  229.            
  230.         if ($b[6] == $t && $b[7] == $t && $b[8] == '')
  231.             $cmv = 8;
  232.         if ($b[6] == $t && $b[7] == '' && $b[8] == $t)
  233.             $cmv = 7;      
  234.         if ($b[6] == '' && $b[7] == $t && $b[8] == $t)
  235.             $cmv = 6;
  236.    
  237.         if ($b[0] == $t && $b[3] == $t && $b[6] == '')
  238.             $cmv = 6;
  239.         if ($b[0] == $t && $b[3] == '' && $b[6] == $t)
  240.             $cmv = 3;      
  241.         if ($b[0] == '' && $b[3] == $t && $b[6] == $t)
  242.             $cmv = 0;
  243.            
  244.         if ($b[1] == $t && $b[4] == $t && $b[7] == '')
  245.             $cmv = 7;
  246.         if ($b[1] == $t && $b[4] == '' && $b[7] == $t)
  247.             $cmv = 4;      
  248.         if ($b[1] == '' && $b[4] == $t && $b[7] == $t)
  249.             $cmv = 1;
  250.         if ($b[2] == $t && $b[5] == $t && $b[8] == '')
  251.             $cmv = 8;
  252.         if ($b[2] == $t && $b[5] == '' && $b[8] == $t)
  253.             $cmv = 5;      
  254.         if ($b[2] == '' && $b[5] == $t && $b[8] == $t)
  255.             $cmv = 2;
  256.    
  257.    
  258.         if ($b[0] == $t && $b[4] == $t && $b[8] == '')
  259.             $cmv = 8;
  260.         if ($b[0] == $t && $b[4] == '' && $b[8] == $t)
  261.             $cmv = 4;      
  262.         if ($b[0] == '' && $b[4] == $t && $b[8] == $t)
  263.             $cmv = 0;
  264.            
  265.         if ($b[2] == $t && $b[4] == $t && $b[6] == '')
  266.             $cmv = 6;
  267.         if ($b[2] == $t && $b[4] == '' && $b[6] == $t)
  268.             $cmv = 4;      
  269.         if ($b[2] == '' && $b[4] == $t && $b[6] == $t)
  270.             $cmv = 2;
  271.         if ($cmv <> '')
  272.             break;
  273.     }
  274. }
  275. function comprand()
  276. {
  277.     global $b;
  278.     global $cmv;
  279.     srand ((double) microtime() * 1000000);
  280.     while (! isset($cmv))
  281.     {
  282.         $test=rand(0, 8);
  283.         if ($b[$test] == '')
  284.             $cmv=$test;
  285.     }  
  286. }
  287. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement