Games_Chess
[ class tree: Games_Chess ] [ index: Games_Chess ] [ all elements ]

Source for file HTMLChess.php

Documentation is available at HTMLChess.php

  1. <?php
  2. require_once 'Games/Chess/Standard.php';
  3. require_once 'Games/Chess/Losers.php';
  4.  
  5. if (!version_compare(phpversion()'4.2.0''>=')) {
  6.     die('Requires PHP version 4.2.0 or greater');
  7. }
  8.  
  9. // hack control
  10. if (!isset($_GET['start']|| !is_string($_GET['start'])) {
  11.     $_GET['start''';
  12. }
  13. if (strlen($_GET['start']!= 2{
  14.     $_GET['start''';
  15. elseif ($_GET['start'!= '' && !preg_match('/[a-h][1-8]/'$_GET['start'])) {
  16.     $_GET['start''';
  17. }
  18.  
  19. if (!isset($_GET['goto']|| !is_string($_GET['goto'])) {
  20.     $_GET['goto''';
  21. elseif (strlen($_GET['goto']!= 2{
  22.     $_GET['goto''';
  23. elseif (!preg_match('/[a-h][1-8]/'$_GET['goto'])) {
  24.     $_GET['goto''';
  25. }
  26.  
  27. if (!empty($_GET['promote']&& !in_array($_GET['promote']array('Q''R',
  28.       'N''B'))) {
  29.     $_GET['promote''Q';
  30. }
  31. /**
  32. * Creates the game and maintains persistence through sessions
  33. * Call this at the top of the webpage
  34. @param string name of the session containing the current game
  35. @param string name of the game variable (a {@link visualboard} class)
  36. */
  37. function setup_game($session_id$gamename)
  38. {
  39.     session_name($session_id);
  40.     session_start();
  41.     if (isset($_GET['newgame'])) {
  42.         session_destroy();
  43.     }
  44.     session_name($session_id);
  45.     session_register($gamename);
  46. }
  47.  
  48. /**
  49.  * The primary class - declare one of these at the top of the file
  50.  *
  51.  * Do it like this:
  52.  *
  53.  * <code>
  54.  * setup_game('mygame','x');
  55.  * $x = $_SESSION['x'];
  56.  * if (!isset($x)) $x = new visualboard;
  57.  * </code>
  58.  * @author Greg Beaver <cellog@users.sourceforge.net>
  59.  * @copyright Copyright 2002, Greg Beaver
  60.  * @version 1.0
  61.  */
  62. class visualboard
  63. {
  64.     /**
  65.      * The logical board
  66.      * @var Games_Chess 
  67.      */
  68.     var $_board;
  69.     /**
  70.      * Promotion contents
  71.      * @var false|array
  72.      */
  73.     var $promote = false;
  74.     
  75.     /**
  76.      * Initializes {@link $moves, $board}
  77.      */
  78.     function visualboard($fen = false$type 'Standard')
  79.     {
  80.         $board 'Games_Chess_' $type;
  81.         $this->_board = new $board;
  82.         $err $this->_board->resetGame($fen);
  83.         if ($this->_board->isError($err)) {
  84.             echo '<b>' .$err->getMessage('</b><br />';
  85.             $this->_board->resetGame();
  86.         }
  87.     }
  88.     
  89.     /**
  90.      * Prints javascript for the function addmove:
  91.      * {@source } 
  92.      */
  93.     function javascript()
  94.     {?>
  95. <script language="JavaScript" type="text/javascript">
  96. <!--
  97. function addMove(move)
  98. {
  99.     if (document.forms[0].start.value == '')
  100.     document.forms[0].start.value = move;
  101.     else
  102.     {
  103.         if (document.forms[0]["goto"].value == '')
  104.         {
  105.             document.forms[0]["goto"].value = move;
  106.             if (confirm("Do this move?"))
  107.             {
  108.                 document.forms[0].submit();
  109.             }
  110.         }
  111.     }
  112. }
  113. //-->
  114. </script>
  115. <?php
  116.     }
  117.     
  118.     /**
  119.      * Grabs the next move from form variables start, goto, kingcastle and queencastle
  120.      *
  121.      * If $_GET['start'] is not used, it checks for $_GET['kingcastle'] and tries
  122.      * to castle kingside if found.  Otherwise, it looks for $_GET['queencastle']
  123.      * and tries to castle queenside
  124.      */
  125.     function domove()
  126.     {
  127.         if (!empty($_GET['from']&& !empty($_GET['to']&& !empty($_GET['promote'])) {
  128.             $err $this->_board->moveSquare($_GET['from']$_GET['to'],
  129.                                              $_GET['promote']);
  130.             if ($this->_board->isError($err)) {
  131.                 echo '<b>' .$err->getMessage('</b><br />';
  132.             }
  133.             $this->promote = false;
  134.             return;
  135.         }
  136.         if (!empty($_GET['SAN'])) {
  137.             $err $this->_board->moveSAN($_GET['SAN']);
  138.             if ($this->_board->isError($err)) {
  139.                 echo '<b>' .$err->getMessage('</b><br />';
  140.             }
  141.             return;
  142.         }
  143.         if (!empty($_GET['start']&& !empty($_GET['goto'])) {
  144.             if ($this->_board->isPromoteMove($_GET['start']$_GET['goto'])) {
  145.                 $this->promote = array($_GET['start']$_GET['goto']);
  146.                 return;
  147.             else {
  148.                 $this->promote = false;
  149.             }
  150.             $err $this->_board->moveSquare($_GET['start']$_GET['goto']);
  151.             if ($this->_board->isError($err)) {
  152.                 echo '<b>' .$err->getMessage('</b><br />';
  153.             }
  154.         elseif (isset($_GET['kingcastle'])) {
  155.             $err $this->_board->moveSAN('O-O');
  156.             if ($this->_board->isError($err)) {
  157.                 echo '<b>' .$err->getMessage('</b><br />';
  158.             }
  159.         elseif (isset($_GET['queencastle'])) {
  160.             $err $this->_board->moveSAN('O-O-O');
  161.             if ($this->_board->isError($err)) {
  162.                 echo '<b>' .$err->getMessage('</b><br />';
  163.             }
  164.         }
  165.     }
  166.     
  167.     /**
  168.      * This function prints the javascript that will ask the user what to promote the pawn to
  169.      *
  170.      * Using the alert() function, this method asks the user if they want a queen.
  171.      * If they click Cancel, it asks if they want a rook, then knight, then
  172.      * bishop.  If they cancel on bishop, it promotes to queen
  173.      * to avoid any illogical possibilities
  174.      */
  175.     function dopromote()
  176.     {
  177.     ?>
  178. <form action="<?php echo $_SERVER['PHP_SELF'].'?'.session_name().'='.session_id(?>" name="chess" id="chess">
  179. <input type="hidden" name="promote" value=""><input type="hidden" name="from" value="">
  180. <input type="hidden" name="to" value="">
  181. <input type="submit" name="newmove" value="New move"></form>
  182. <script language="JavaScript" type="text/javascript">
  183. <!--
  184.  
  185. function promote(from, to)
  186. {
  187.     document.forms[0].from.value = from;
  188.     document.forms[0].to.value = to;
  189.     if (confirm("promote to Queen?"))
  190.     {
  191.         document.forms[0].promote.value = 'Q';
  192.         document.forms[0].submit();
  193.     } else 
  194.     {
  195.         if (confirm("promote to Rook?"))
  196.         {
  197.             document.forms[0].promote.value = 'R';
  198.             document.forms[0].submit();
  199.         } else
  200.         {
  201.             if (confirm("promote to Knight?"))
  202.             {
  203.                 document.forms[0].promote.value = 'N';
  204.                 document.forms[0].submit();
  205.             } else
  206.             {
  207.                 if (confirm("promote to Bishop?"))
  208.                 {
  209.                     document.forms[0].promote.value = 'B';
  210.                     document.forms[0].submit();
  211.                 } else
  212.                 {
  213.                     document.forms[0].promote.value = 'Q';
  214.                     document.forms[0].submit();
  215.                 }
  216.             }
  217.         }
  218.     }
  219. }
  220. promote('<?php print $this->promote[0"', '" $this->promote[1]?>');
  221. //-->
  222. </script>
  223. <?php
  224.     }
  225.     
  226.     /**
  227.     * Print out the chess game in its entirety: the board, move list and control buttons
  228.     *
  229.     * first, it checks for a pawn promotion and adds the promote move to {@link $moves} using
  230.     * {@link game::addpromote()}.  Then it moves the next piece by calling {@link domove()}.
  231.     * Then it checks for stalemate and checkmate, and stops the gameplay if either condition is met.
  232.     * It creates a visual representation of the board using {@link abstractboard::createrows()}, and
  233.     * displays the chessboard by linking together the display of each row on the chessboard using
  234.     * {@link row::draw()}, and finally the movelist using {@link game::draw()}
  235.     */
  236.     function draw()
  237.     {
  238.         $this->domove();
  239.         if ($this->promote)
  240.         {
  241.             return $this->dopromote();
  242.         }
  243.         $board $this->_board->toArray();
  244.         $checkmate $this->_board->inCheckmate();
  245.         $fen $this->_board->renderFen();
  246.         $colors = array('#999933''#FFFFFF');
  247.         $textcolors = array('#FFFFFF''#000000');
  248.         $cycle = 0;
  249.         if (!$checkmate$this->javascript();
  250.         echo '<table border="1">';
  251.         $imagepath dirname(__FILE__. DIRECTORY_SEPARATOR . 'images';
  252.         foreach($board as $square => $piece{
  253.             if ($square{0== 'a'{
  254.                 echo '<tr>';
  255.                 $cycle ($cycle + 1% 2;
  256.             }
  257.             echo '<td width="30" bgcolor="' .$colors[$cycle]'"><font color="' .$textcolors[$cycle]'">';
  258.             echo "<a href=\"#\" onClick=\"addMove('$square');return false;" .
  259.                 "\" id=\"$square\">";
  260.  
  261.  
  262.             if ($piece{
  263.                 if (file_exists($imagepath '/' strtoupper($piece'.gif')) {
  264.                     if ($piece != strtoupper($piece)) {
  265.                         $image 'dark/' $piece '.gif';
  266.                     else {
  267.                         $image $piece '.gif';
  268.                     }
  269.                     $imgsize = GetImageSize($imagepath . DIRECTORY_SEPARATOR . $image);
  270.                     echo '<img src="images/' $image '" border="0" width="' $imgsize[0.
  271.                          '" height="' $imgsize[1'" alt="'$piece '">';
  272.                 else {
  273.                     echo $piece;
  274.                 }
  275.             else {
  276.                 if (file_exists($imagepath '/blank.gif')) {
  277.                     if ($this->_board->getDiagonalColor($square== 'W'{
  278.                         $image 'blank.gif';
  279.                     else {
  280.                         $image 'dark/blank.gif';
  281.                     }
  282.                     $imgsize = GetImageSize($imagepath . DIRECTORY_SEPARATOR . $image);
  283.                     echo '<img src="images/' $image '" border="0" width="' $imgsize[0.
  284.                          '" height="' $imgsize[1'" alt="'$piece '">';
  285.                 else {
  286.                     echo '&nbsp;';
  287.                 }
  288.             }
  289.             echo '</a>';
  290.             echo '</font></td>';
  291.             if ($square{0== 'h'{
  292.                 echo '</tr>';
  293.             }
  294.             $cycle ($cycle + 1% 2;
  295.         }
  296.         echo '</table>';
  297.  
  298.         $side $this->_board->toMove(== 'W' 'White' 'Black';
  299.         $gameOver $this->_board->gameOver();
  300.         if ($gameOver{
  301.             $winner $gameOver == 'W' 'White' 'Black';
  302.             if ($stalemate $this->_board->inStalemate())
  303.             {
  304.                 print "<h1>STALEMATE</h1>";
  305.             elseif ($draw $this->_board->inDraw())
  306.             {
  307.                 print "<h1>DRAW</h1>";
  308.             elseif ($this->_board->inCheckmate()) {
  309.                 if (!is_a($this->_board'games_chess_standard')) {
  310.                     $winner $side;
  311.                 else {
  312.                     $winner ($side == 'White''Black' 'White';
  313.                 }
  314.                 print "<h1>CHECKMATE! $winner WINS!</h1>";
  315.             else {
  316.                 print "<h1>$winner WINS!</h1>";
  317.             }
  318.         }
  319.  
  320. ?><form action="<?php echo $_SERVER['PHP_SELF'].'?'.session_name().'='.session_id(?>" name="chess" id="chess">
  321. <?php         if (!$gameOverecho "<b>{$side} to move</b><br>";
  322.  ?>
  323. from <input type="text" name="start" size="2" maxlength="2"> to <input type="text" name="goto" size="2" maxlength="2">
  324. <input type="submit" name="newmove" value="New move"><br>
  325. (alternate) SAN move: <input type="text" name="SAN" size="5"><br>
  326. Reset with new FEN: <input type="text" name="FEN" size="70"><br>
  327. <br><input type="reset"><br>
  328. <?php $this->castlebutton()?><br><br>
  329.  
  330. <input type="submit" name="newgame" value="New Game"><input type="submit" name="newlosergame" value="New Losers Game"></form><?php if (!$gameOver?>
  331. for a friend to join, click here <a href="mailto:example@example.com?Subject=Join my chess game!&Body=<?php
  332. echo htmlentities("go to <a href=\"http://" $_SERVER['SERVER_NAME'$_SERVER['PHP_SELF'
  333. "?mygame=".session_id()."\">Here</a> to play me in chess! http://$_SERVER[SERVER_NAME]$_SERVER[PHP_SELF]?mygame=" .
  334. session_id())?>">Email my friend</a>
  335.  
  336. <form action="<?php echo $_SERVER['PHP_SELF'].'?'.session_name().'='.session_id(?>">
  337. <input type="submit" value="Check to see if opponent has moved"></form>
  338. <?php }
  339.         
  340.         echo "<blockquote>Current position FEN: <strong>$fen</strong></blockquote>";
  341.         $moves $this->_board->getMoveList();
  342.         echo '<table border="1"><th colspan="3" align="center">Moves</th><tr>' .
  343.              '<td>#</td><td>White</td><td>Black</td></tr>';
  344.         foreach($moves as $num => $moveset{
  345.             echo '<tr>';
  346.             echo "<td>$num</td>";
  347.             if (isset($moveset[0])) {
  348.                 echo "<td>$moveset[0]</td>";
  349.             else {
  350.                 echo "<td>&nbsp;</td>";
  351.             }
  352.             
  353.             if (isset($moveset[1])) {
  354.                 echo "<td>$moveset[1]</td>";
  355.             }
  356.             echo '</tr>';
  357.         }
  358.         echo '</table>';
  359.     }
  360.     
  361.     /**
  362.     * Prints the castling buttons after checking castling rights
  363.     *
  364.     * If castling rights have been taken away by a king or rook move, the button is not displayed.  This function
  365.     * uses {@link Games_Chess::canCastleKingside(), Games_Chess::canCastleQueenside()} to find out.
  366.     */
  367.     function castlebutton()
  368.     {
  369.         if ($this->_board->canCastleKingside())
  370.         echo '<input type="submit" name="kingcastle" value="Castle Your King Kingside">';
  371.         if ($this->_board->canCastleQueenside())
  372.         echo '<input type="submit" name="queencastle" value="Castle Your King Queenside">';
  373.     }
  374. }
  375.  
  376. setup_game('mygame','x');
  377. $x $_SESSION['x'];
  378. $fen = isset($_GET['FEN']$_GET['FEN': false;
  379. if (!isset($x|| isset($_GET['newgame']|| $fen
  380.       || isset($_GET['newlosergame'])) {
  381.     if (isset($_GET['newlosergame'])) {
  382.         $x = new visualboard($fen'Losers');
  383.     else {
  384.         $x = new visualboard($fen'Standard');
  385.     }
  386. }
  387. ?>
  388. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  389.  
  390. <html>
  391. <head>
  392.     <title>PHP Chess!</title>
  393. </head>
  394.  
  395. <body>
  396. <h1>Welcome to <a href="http://www.chiaraquartet.net">The Chiara Quartet's</a> PHP Chess</h1>
  397. <?php
  398. $x->draw();
  399. $_SESSION['x'$x;
  400. ?>
  401.  
  402.  
  403. </body>
  404. </html>

Documentation generated on Mon, 11 Mar 2019 10:14:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.