<?php
//Show some source baby!
if(isset($_GET['source'])) {highlight_file(__FILE__);die();}

/*
Copyright (c) 2007, Josh Erickson

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the Josh Erickson nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

require_once 'class.piece.php';

class 
pawn extends piece {
    public function 
__construct() {
        
$this->addPossibleMove('$nr=$cr+1','$nf=$cf');
        
$this->addPossibleMove('$nr=$cr+2','$nf=$cf');
        
        
//attack!!
        
$this->addPossibleMove('$nr=$cr+1','$nf=$cf+1');
        
$this->addPossibleMove('$nr=$cr+1','$nf=$cf-1');
    }
}

class 
rook extends piece {
    
    public function 
__construct() {
        
$this->addPossibleMove('$nr=$cr+$rn','$nf=$cf');
        
$this->addPossibleMove('$nr=$cr-$rn','$nf=$cf');
        
$this->addPossibleMove('$nr=$cr','$nf=$cf+$fn');
        
$this->addPossibleMove('$nr=$cr','$nf=$cf-$fn');
    }
}

class 
knight extends piece {
    public function 
__construct() {
        
$this->addPossibleMove('$nr=$cr+2','$nf=$cf+1');
        
$this->addPossibleMove('$nr=$cr-2','$nf=$cf-1');
        
$this->addPossibleMove('$nr=$cr+2','$nf=$cf-1');
        
$this->addPossibleMove('$nr=$cr-2','$nf=$cf+1');
        
        
$this->addPossibleMove('$nr=$cr+1','$nf=$cf+2');
        
$this->addPossibleMove('$nr=$cr-1','$nf=$cf-2');
        
$this->addPossibleMove('$nr=$cr+1','$nf=$cf-2');
        
$this->addPossibleMove('$nr=$cr-1','$nf=$cf+2');
    }
    
    public function 
movedSpaces($newr$newf) {
        
/*
            We need  a special method for the Knight since he jumps other pieces.
            
            Methods returns just the current position and the new position specified since that is all that is required to check for opposing piece captures and movement restrictions (which except for running off the board, aren't applied to the knight).
        */
        
if(!$this->checkMove($newr$newf)) {
            return 
false;
        }
        
$rtn = array();
        
$rtn[] = array("rank" => $this->current_rank"file" => $this->current_file);
        
$rtn[] = array("rank" => $newr"file" => $newf);
        return 
$rtn;
    }
}

class 
bishop extends piece {
    
    public function 
__construct() {
        
$this->addPossibleMove('$nr=$cr+$rn','$nf=$cf+$rn');
        
$this->addPossibleMove('$nr=$cr-$rn','$nf=$cf-$rn');
        
$this->addPossibleMove('$nr=$cr+$rn','$nf=$cf-$rn');
        
$this->addPossibleMove('$nr=$cr-$rn','$nf=$cf+$rn');
    }
}

class 
queen extends piece {
    public function 
__construct() {        
        if(
class_exists('rook')) { $rook = new rook(); }
        if(
class_exists('bishop')) { $bishop = new bishop(); }
        
$moves array_merge($rook->exportMoves(),$bishop->exportMoves());
        foreach(
$moves as $val) {
            
$this->addPossibleMove($val[0],$val[1]);
        }
    }
}

header('content-type: text/plain');
echo 
"To the source of pieces.php copy http://{$_SERVER["HTTP_HOST"]}{$_SERVER['PHP_SELF']}?source into the address bar.\r\n";
echo 
"You can use ?source on class.piece.php too!\r\n\r\n\r\n\r\n";
echo 
"Rook\r\n";
$r = new rook();
$r->setCurrent(6,2);
var_dump($r->checkMove(3,2));
var_dump($r->movedSpaces(3,2));
echo 
"\r\n\r\n";

echo 
"Knight\r\n";
$k = new knight();
$k->setCurrent(1,2);
var_dump($k->checkMove(4,1)); //Fail move!
var_dump($k->movedSpaces(4,1)); //Fail mode!
var_dump($k->checkMove(3,1)); //Win move!
var_dump($k->movedSpaces(3,1)); //Win mode!
echo "\r\n\r\n";

echo 
"Queen\r\n";
$k = new queen();
$k->setCurrent(4,2);
var_dump($k->checkMove(2,4));
var_dump($k->movedSpaces(2,4));

?>