Advertisement
sebbu

PHP Chess Game

Aug 19th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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($this->y===6) return ($diff_y<=-1 && $diff_y>=-2);
  30.         else return ($diff_y==-1);
  31.     }
  32.     public function can_eat($diff_x, $diff_y) {
  33.         return (abs($diff_x)==1 && $diff_y==-1);
  34.     }
  35.     public function __toString() {
  36.         return 'Pawn';
  37.     }
  38. }
  39. class Rook extends Piece {
  40.     public function can_move($diff_x, $diff_y) {
  41.         return (
  42.             ( ($diff_x==0 && abs($diff_y)>0) || ($diff_y==0 && abs($diff_x)>0) ) &&
  43.             $this->parent->is_free($this->x, $this->y, $this->x+$diff_x, $this->y+$diff_y)
  44.         );
  45.         //return ($diff_x^$diff_y>0);
  46.     }
  47.     public function __toString() {
  48.         return 'Rook';
  49.     }
  50. }
  51. class Knight extends Piece {
  52.     public function can_move($diff_x, $diff_y) {
  53.         return ( ( abs($diff_x)>=1 && abs($diff_x)<=2 ) && ( abs($diff_y)>=1 && abs($diff_y)<=2) && ( abs($diff_x)+abs($diff_y)==3 ) );
  54.     }
  55.     public function __toString() {
  56.         return 'Knight';
  57.     }
  58. }
  59. class Bishop extends Piece {
  60.     public function can_move($diff_x, $diff_y) {
  61.         return (
  62.             ( abs($diff_x)==abs($diff_y) ) && abs($diff_x)>0 &&
  63.             $this->parent->is_free($this->x, $this->y, $this->x+$diff_x, $this->y+$diff_y)
  64.         );
  65.     }
  66.     public function __toString() {
  67.         return 'Bishop';
  68.     }
  69. }
  70. class King extends Piece {
  71.     public function can_move($diff_x, $diff_y) {
  72.         return ( abs($diff_x)<=1 && abs($diff_y)<=1 && (abs($diff_x)+abs($diff_y)>=1) );
  73.     }
  74.     public function __toString() {
  75.         return 'King';
  76.     }
  77. }
  78. class Queen extends Piece {
  79.     public function can_move($diff_x, $diff_y) {
  80.         return (
  81.             (abs($diff_x)==abs(diff_y)) || ($diff_x==0 && abs($diff_y)>0) || ($diff_y==0 && abs($diff_x)>0) &&
  82.             $this->parent->is_free($this->x, $this->y, $this->x+$diff_x, $this->y+$diff_y)
  83.         );
  84.     }
  85.     public function __toString() {
  86.         return 'Queen';
  87.     }
  88. }
  89. //black up (y=0), white down (y=7)
  90. class board {
  91.     public $grid;
  92.     public $width;
  93.     public $height;
  94.     public $cur_color;
  95.     public function __construct() {
  96.         $this->grid=array();
  97.         $this->width=8;
  98.         $this->height=8;
  99.         for($i=0;$i<$this->height;++$i) {
  100.             $this->grid[$i]=array();
  101.             for($j=0;$j<$this->width;++$j) {
  102.                 $this->grid[$i][$j]='&nbsp;';
  103.             }
  104.         }
  105.     }
  106.     public function is_empty($x, $y) {
  107.         return ( ! ($grid[$y][$x] instanceof Piece) );
  108.     }
  109.     public function is_free($src_x, $src_y, $dst_x, $dst_y) {
  110.         assert( $src_x==$dst_x || $src_y==$dst_y || (($src_x-$dst_x)==($src_y-$dst_y)) );
  111.         $diff_x=$dst_x-$src_x;
  112.         $diff_y=$dst_y-$src_y;
  113.         $px=$diff_x/abs($diff_x);
  114.         $py=$diff_y/abs($diff_y);
  115.         for($i=1; $i<max(abs($diff_x),abs($diff_y)); ++$i) {
  116.             if($this->is_empty($src_x+$px*$i, $src_y+$py*$i)) return false;
  117.         }
  118.         return true;
  119.     }
  120.     public function pos_exists($x, $y) {
  121.         return ($x>=0 && $x<$this->width && $y>=0 && $y<=$this->height);
  122.     }
  123.     public function get_piece($x, $y) {
  124.         return $this->grid[$y][$x];
  125.     }
  126.     public function add_piece($x, $y, $piece) {
  127.         $p=new $piece($this);
  128.         $p->x=$x;
  129.         $p->y=$y;
  130.         $p->color=$this->cur_color;
  131.         $this->grid[$y][$x]=$p;
  132.     }
  133. };
  134. $board=new board();
  135. //var_dump($board->grid);
  136. $board->cur_color='W';
  137. $h=7;
  138.     $board->add_piece(0, $h, 'Rook');
  139.     $board->add_piece(1, $h, 'Knight');
  140.     $board->add_piece(2, $h, 'Bishop');
  141.     $board->add_piece(3, $h, 'Queen');
  142.     $board->add_piece(4, $h, 'King');
  143.     $board->add_piece(5, $h, 'Bishop');
  144.     $board->add_piece(6, $h, 'Knight');
  145.     $board->add_piece(7, $h, 'Rook');
  146. --$h;
  147.     for($i=0;$i<8;++$i) {
  148.         $board->add_piece($i, $h, 'Pawn');
  149.     }
  150. $board->cur_color='B';
  151. $h=0;
  152.     $board->add_piece(0, $h, 'Rook');
  153.     $board->add_piece(1, $h, 'Knight');
  154.     $board->add_piece(2, $h, 'Bishop');
  155.     $board->add_piece(3, $h, 'Queen');
  156.     $board->add_piece(4, $h, 'King');
  157.     $board->add_piece(5, $h, 'Bishop');
  158.     $board->add_piece(6, $h, 'Knight');
  159.     $board->add_piece(7, $h, 'Rook');
  160. ++$h;
  161.     for($i=0;$i<8;++$i) {
  162.         $board->add_piece($i, $h, 'Pawn');
  163.     }
  164. ?><table border="1"><?php
  165. for($y=0;$y<8;++$y) {
  166. ?>
  167.     <tr>
  168. <?php
  169.     for($x=0;$x<8;++$x) {
  170. ?>      <td><?php echo $board->get_piece($x, $y); ?></td>
  171. <?php
  172.     }
  173. ?>  </tr><?php
  174. }
  175. ?>
  176. </table>
  177.  
  178. </body>
  179. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement