Advertisement
sebbu

PHP Chess Game

Aug 19th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.38 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Chess</title>
  5. <meta charset="UTF-8"/>
  6. </head>
  7. <body>
  8.  
  9. <?php
  10. function is_black($c) {
  11.     return ($c==='B'||$c==='Black');
  12. }
  13. function is_white($c) {
  14.     return ($c==='W'||$c==='White');
  15. }
  16. abstract class Piece {
  17.     public $x;
  18.     public $y;
  19.     public $color;
  20.     public $parent;
  21.     public function __construct(&$parent=NULL) {
  22.         $this->parent=$parent;
  23.     }
  24.     public function can_move($diff_x, $diff_y) { return false; }
  25.     public function can_eat($diff_x, $diff_y) { return $this->can_move($diff_x, $diff_y); }
  26. }
  27. class Pawn extends Piece {
  28.     public function can_move($diff_x, $diff_y) {
  29.         /*if(is_black($c)) {
  30.             if($y===1) return ($diff_y>=1 && $diff_y<=2);
  31.             else return ($diff_y==1);
  32.         }
  33.         elseif(is_white($c)) {//*/
  34.             if($this->y===6) return ($diff_y<=-1 && $diff_y>=-2);
  35.             else return ($diff_y==-1);
  36.         /*}
  37.         return false;//*/
  38.     }
  39.     public function can_eat($diff_x, $diff_y) {
  40.         /*if(is_black($c)) {
  41.             return (abs($diff_x)==1 && $diff_y==1);
  42.         }
  43.         elseif(is_white($c)) {//*/
  44.             return (abs($diff_x)==1 && $diff_y==-1);
  45.         /*}
  46.         return false;//*/
  47.     }
  48.     public function __toString() {
  49.         return 'Pawn';
  50.     }
  51. }
  52. class Rook extends Piece {
  53.     public function can_move($diff_x, $diff_y) {
  54.         return ( ($diff_x==0 && abs($diff_y)>0) || ($diff_y==0 && abs($diff_x)>0) );
  55.         //return ($diff_x^$diff_y>0);
  56.     }
  57.     public function can_eat($diff_x, $diff_y) {
  58.         return (
  59.             $this->can_move($diff_x, $diff_y) &&
  60.             $this->parent->is_free($this->x, $this->y, $this->x+$diff_x, $this->y+$diff_y)
  61.         );
  62.     }
  63.     public function __toString() {
  64.         return 'Rook';
  65.     }
  66. }
  67. //black up (y=0), white down (y=7)
  68. class board {
  69.     public $grid;
  70.     public $width;
  71.     public $height;
  72.     public function __construct() {
  73.         $this->grid=array();
  74.         $this->width=8;
  75.         $this->height=8;
  76.         for($i=0;$i<$this->height;++$i) {
  77.             $this->grid[$i]=array();
  78.             for($j=0;$j<$this->width;++$j) {
  79.                 $this->grid[$i][$j]='&nbsp;';
  80.             }
  81.         }
  82.     }
  83.     public function is_empty($x, $y) {
  84.         return ( ! ($grid[$y][$x] instanceof Piece) );
  85.     }
  86.     public function is_free($src_x, $src_y, $dst_x, $dst_y) {
  87.         assert( $src_x==$dst_x || $src_y==$dst_y || (($src_x-$dst_x)==($src_y-$dst_y)) );
  88.         $diff_x=$dst_x-$src_x;
  89.         $diff_y=$dst_y-$src_y;
  90.         $px=$diff_x/abs($diff_x);
  91.         $py=$diff_y/abs($diff_y);
  92.         for($i=1; $i<max(abs($diff_x),abs($diff_y)); ++$i) {
  93.             if($this->get_piece($src_x+$px*$i, $src_y+$py*$i) instanceof Piece) return false;
  94.         }
  95.         return true;
  96.     }
  97.     public function pos_exists($x, $y) {
  98.         return ($x>=0 && $x<$this->width && $y>=0 && $y<=$this->height);
  99.     }
  100.     public function get_piece($x, $y) {
  101.         return $this->grid[$y][$x];
  102.     }
  103. };
  104. $board=new board();
  105. //var_dump($board->grid);
  106. $board->grid[7][0]='Rook';
  107. $board->grid[7][1]='Knight';
  108. $board->grid[7][2]='Bishop';
  109. $board->grid[7][3]='Queen';
  110. $board->grid[7][4]='King';
  111. $board->grid[7][5]='Bishop';
  112. $board->grid[7][6]='Knight';
  113. for($i=0;$i<8;++$i) {
  114.     $board->grid[6][$i]=new Pawn($board);
  115. }
  116. $board->grid[7][7]='Rook';
  117. //var_dump($board->grid);
  118. var_dump($board->grid[6][0]->parent->grid[7][7]);
  119. $test=new Rook($board);
  120. $test->x=0;
  121. $test->y=7;
  122. $board->grid[7][0]=clone $test;
  123. $test->x=7;
  124. $board->grid[7][7]=clone $test;
  125. var_dump($board->grid[7][0]->x);
  126. ?><table border="1"><?php
  127. for($y=0;$y<8;++$y) {
  128. ?>
  129.     <tr>
  130. <?php
  131.     for($x=0;$x<8;++$x) {
  132. ?>      <td><?php echo $board->get_piece($x, $y); ?></td>
  133. <?php
  134.     }
  135. ?>  </tr><?php
  136. }
  137. ?>
  138. </table>
  139.  
  140. </body>
  141. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement