SHOW:
|
|
- or go back to the newest paste.
| 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)) {
|
| 29 | + | if($this->y===6) return ($diff_y<=-1 && $diff_y>=-2); |
| 30 | - | if($y===1) return ($diff_y>=1 && $diff_y<=2); |
| 30 | + | else return ($diff_y==-1); |
| 31 | - | else return ($diff_y==1); |
| 31 | + | |
| 32 | public function can_eat($diff_x, $diff_y) {
| |
| 33 | - | elseif(is_white($c)) {//*/
|
| 33 | + | return (abs($diff_x)==1 && $diff_y==-1); |
| 34 | - | if($this->y===6) return ($diff_y<=-1 && $diff_y>=-2); |
| 34 | + | |
| 35 | - | else return ($diff_y==-1); |
| 35 | + | |
| 36 | - | /*} |
| 36 | + | |
| 37 | - | return false;//*/ |
| 37 | + | |
| 38 | } | |
| 39 | class Rook extends Piece {
| |
| 40 | - | /*if(is_black($c)) {
|
| 40 | + | |
| 41 | - | return (abs($diff_x)==1 && $diff_y==1); |
| 41 | + | |
| 42 | ( ($diff_x==0 && abs($diff_y)>0) || ($diff_y==0 && abs($diff_x)>0) ) && | |
| 43 | - | elseif(is_white($c)) {//*/
|
| 43 | + | |
| 44 | - | return (abs($diff_x)==1 && $diff_y==-1); |
| 44 | + | |
| 45 | - | /*} |
| 45 | + | |
| 46 | - | return false;//*/ |
| 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 | - | return ( ($diff_x==0 && abs($diff_y)>0) || ($diff_y==0 && abs($diff_x)>0) ); |
| 54 | + | |
| 55 | public function __toString() {
| |
| 56 | return 'Knight'; | |
| 57 | } | |
| 58 | } | |
| 59 | - | $this->can_move($diff_x, $diff_y) && |
| 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 | - | if($this->get_piece($src_x+$px*$i, $src_y+$py*$i) instanceof Piece) return false; |
| 93 | + | |
| 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]=' '; | |
| 103 | } | |
| 104 | } | |
| 105 | } | |
| 106 | - | $board->grid[7][0]='Rook'; |
| 106 | + | |
| 107 | - | $board->grid[7][1]='Knight'; |
| 107 | + | |
| 108 | - | $board->grid[7][2]='Bishop'; |
| 108 | + | |
| 109 | - | $board->grid[7][3]='Queen'; |
| 109 | + | |
| 110 | - | $board->grid[7][4]='King'; |
| 110 | + | |
| 111 | - | $board->grid[7][5]='Bishop'; |
| 111 | + | |
| 112 | - | $board->grid[7][6]='Knight'; |
| 112 | + | |
| 113 | - | for($i=0;$i<8;++$i) {
|
| 113 | + | |
| 114 | - | $board->grid[6][$i]=new Pawn($board); |
| 114 | + | |
| 115 | for($i=1; $i<max(abs($diff_x),abs($diff_y)); ++$i) {
| |
| 116 | - | $board->grid[7][7]='Rook'; |
| 116 | + | if($this->is_empty($src_x+$px*$i, $src_y+$py*$i)) return false; |
| 117 | } | |
| 118 | - | var_dump($board->grid[6][0]->parent->grid[7][7]); |
| 118 | + | |
| 119 | - | $test=new Rook($board); |
| 119 | + | |
| 120 | - | $test->x=0; |
| 120 | + | |
| 121 | - | $test->y=7; |
| 121 | + | |
| 122 | - | $board->grid[7][0]=clone $test; |
| 122 | + | |
| 123 | - | $test->x=7; |
| 123 | + | |
| 124 | - | $board->grid[7][7]=clone $test; |
| 124 | + | |
| 125 | - | var_dump($board->grid[7][0]->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> |