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

Source for file Losers.php

Documentation is available at Losers.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2003 The PHP Group                                     |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Gregory Beaver <cellog@php.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Losers.php,v 1.3 2004/04/09 06:22:30 cellog Exp $
  20. /**
  21.  * A Losers chess game representation (wild 16 on ICC)
  22.  * @package Games_Chess
  23.  * @author Gregory Beaver <cellog@php.net>
  24.  */
  25. /**
  26.  * The parent class
  27.  */
  28. require_once 'Games/Chess/Standard.php';
  29.  
  30. /**
  31.  * Losers chess game
  32.  *
  33.  * The goal of the game is to lose all of your pieces or force your opponent
  34.  * to checkmate you.  The only differences from standard chess are that if
  35.  * a capture is possible it must be executed, similar to checkers, and
  36.  * checkmate actually loses the game!
  37.  * @package Games_Chess
  38.  * @author Gregory Beaver <cellog@php.net>
  39.  */
  40.     /**
  41.      * Determine whether it is possible to capture an opponent's piece
  42.      * @access protected
  43.      */
  44.     function _capturePossible()
  45.     {
  46.         $allmoves = array();
  47.         $color $this->_move == 'W' 'B' 'W';
  48.         foreach ($this->_pieces as $name => $loc{
  49.             if (!$loc{
  50.                 continue;
  51.             }
  52.             if ($name{0== $this->_move{
  53.                 // don't return castle move shortcuts
  54.                 if ($name{1== 'P'{
  55.                     $allmoves array_merge($allmoves,
  56.                         $this->getPossibleMoves($loc[1]$loc[0]$this->_movefalse));
  57.                 else {
  58.                     $allmoves array_merge($allmoves,
  59.                         $this->getPossibleMoves($name{1}$loc$this->_movefalse));
  60.                 }
  61.             }
  62.         }
  63.         foreach ($this->_pieces as $name => $loc{
  64.             if ($name{0== $this->_move || !$loc{
  65.                 continue;
  66.             }
  67.             if (is_array($loc)) {
  68.                 if (in_array($loc[0]$allmoves)) {
  69.                     return true;
  70.                 }
  71.             else {
  72.                 if (in_array($loc$allmoves)) {
  73.                     return true;
  74.                 }
  75.             }
  76.         }
  77.         return false;
  78.     }
  79.     
  80.     /**
  81.      * Validate a move
  82.      * @param array parsed move array from {@link _parseMove()}
  83.      * @return true|PEAR_Error
  84.      * @throws GAMES_CHESS_ERROR_MOVE_MUST_CAPTURE
  85.      * @access protected
  86.      */
  87.     function _validMove($move)
  88.     {
  89.         list($type$infoeach($move);
  90.         if ($this->_capturePossible(&&
  91.               ($type == GAMES_CHESS_CASTLE ||
  92.               $this->_board[$info['square']] == $info['square'])) {
  93.             if ($type == GAMES_CHESS_CASTLE{
  94.                 $san $info == 'K' 'O-O' 'O-O-O';
  95.             else {
  96.                 $san $info['piece'$info['disambiguate'$info['takes'$info['square'];
  97.             }
  98.             return $this->raiseError(GAMES_CHESS_ERROR_MOVE_MUST_CAPTURE,
  99.                       array('san' => $san));
  100.         }
  101.         return parent::_validMove($move);
  102.     }
  103.     
  104.     /**
  105.      * @return W|B|Dwinner of game, or draw
  106.      */
  107.     function gameOver()
  108.     {
  109.         $opposite $this->_move == 'W' 'B' 'W';
  110.         if ($this->inCheckmate()) {
  111.             return $this->_move;
  112.         }
  113.         if ($this->inDraw()) {
  114.             return 'D';
  115.         }
  116.         $W = array();
  117.         $B = array();
  118.         foreach ($this->_pieces as $name => $loc{
  119.             if (!$loc || $name{1== 'K'{
  120.                 continue;
  121.             }
  122.             if ($name{0== 'W'{
  123.                 $W[= 1;
  124.             else {
  125.                 $B[= 1;
  126.             }
  127.         }
  128.         if (!count($W)) {
  129.             return 'W';
  130.         }
  131.         if (!count($B)) {
  132.             return 'B';
  133.         }
  134.         return false;
  135.     }
  136. }
  137. ?>

Documentation generated on Sun, 17 Jun 2007 02:00:51 -0400 by phpDocumentor 1.3.2. PEAR Logo Copyright © PHP Group 2004.