| 1 | # | ||
| 2 | # Rules.pm | ||
| 3 | # | ||
| 4 | # Games::Chess::Rules Perl module. | ||
| 5 | # | ||
| 6 | # This module is an attempt to codify the rules of chess in a fairly general | ||
| 7 | # way so that a general mechanism can be used to generate possible plies and | ||
| 8 | # to check plies for validity. | ||
| 9 | # | ||
| 10 | # Copyright (C) 1999-2006 Gregor N. Purdy. All rights reserved. | ||
| 11 | # This program is free software; you can redistribute it and/or | ||
| 12 | # modify it under the same terms as Perl itself. | ||
| 13 | # | ||
| 14 | |||
| 15 | package Games::Chess::Rules; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use Games::Chess qw(:constants); | ||
| 19 | use Games::Chess::Piece::General; | ||
| 20 | |||
| 21 | |||
| 22 | # | ||
| 23 | # ::new() | ||
| 24 | # | ||
| 25 | |||
| 26 | sub new | ||
| 27 | { | ||
| 28 | my $class = shift; | ||
| 29 | |||
| 30 | my $this = { }; | ||
| 31 | |||
| 32 | return bless($this, $class); | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | # | ||
| 37 | # TODO: Rework how this stuff functions. | ||
| 38 | # | ||
| 39 | # Validate: Board + Ply -> Board | ||
| 40 | # Generate: Board + Space -> Plies | ||
| 41 | # Board -> Plies | ||
| 42 | # | ||
| 43 | |||
| 44 | # | ||
| 45 | # ::color() | ||
| 46 | # | ||
| 47 | |||
| 48 | sub color | ||
| 49 | { | ||
| 50 | my $this = shift; | ||
| 51 | |||
| 52 | if (@_) { $this->{COLOR} = shift; } | ||
| 53 | else { return $this->{COLOR}; } | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | # | ||
| 58 | # ::piece() | ||
| 59 | # | ||
| 60 | |||
| 61 | sub piece | ||
| 62 | { | ||
| 63 | my $this = shift; | ||
| 64 | |||
| 65 | if (@_) { | ||
| 66 | $this->{PIECE} = shift; | ||
| 67 | $this->from(undef); | ||
| 68 | $this->to(undef); | ||
| 69 | $this->color(undef); | ||
| 70 | } else { | ||
| 71 | my $piece = $this->{PIECE}; | ||
| 72 | if !defined($piece) { | ||
| 73 | my $space = $this->from(); | ||
| 74 | return undef unless defined($space); | ||
| 75 | $this->board()->piece($space); | ||
| 76 | } | ||
| 77 | |||
| 78 | return $this->{PIECE}; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | # | ||
| 85 | # ::from() | ||
| 86 | # | ||
| 87 | |||
| 88 | sub from | ||
| 89 | { | ||
| 90 | my $this = shift; | ||
| 91 | |||
| 92 | if (@_) { $this->{FROM} = shift; } | ||
| 93 | else { return $this->{FROM}; } | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | # | ||
| 98 | # ::to() | ||
| 99 | # | ||
| 100 | |||
| 101 | sub to | ||
| 102 | { | ||
| 103 | my $this = shift; | ||
| 104 | |||
| 105 | if (@_) { $this->{TO} = shift; } | ||
| 106 | else { return $this->{TO}; } | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | #########################################################################################3 | ||
| 112 | # | ||
| 113 | # Predicates: | ||
| 114 | # | ||
| 115 | #########################################################################################3 | ||
| 116 | |||
| 117 | |||
| 118 | # | ||
| 119 | # white(<piece>) | ||
| 120 | # white(<space>) | ||
| 121 | # | ||
| 122 | |||
| 123 | sub white ($) | ||
| 124 | { | ||
| 125 | my $thing = shift; | ||
| 126 | |||
| 127 | return 0 unless defined($thing); | ||
| 128 | |||
| 129 | return white($thing->piece) if $thing->isa('Games::Chess::Space'); | ||
| 130 | return ($thing->color == &WHITE) if $thing->isa('Games::Chess::Piece'); | ||
| 131 | |||
| 132 | croak 'white() requires a piece or a space'; | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | # | ||
| 137 | # black(<piece>) | ||
| 138 | # black(<space>) | ||
| 139 | # | ||
| 140 | |||
| 141 | sub black ($) | ||
| 142 | { | ||
| 143 | my $thing = shift; | ||
| 144 | |||
| 145 | return 0 unless defined($thing); | ||
| 146 | |||
| 147 | return black($thing->piece) if $thing->isa('Games::Chess::Space'); | ||
| 148 | return ($thing->color == &BLACK) if $thing->isa('Games::Chess::Piece'); | ||
| 149 | |||
| 150 | croak 'black() requires a piece or a space'; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | # | ||
| 155 | # home(<piece>) | ||
| 156 | # home(<space>) | ||
| 157 | # | ||
| 158 | |||
| 159 | sub home ($) | ||
| 160 | { | ||
| 161 | my $thing = shift; | ||
| 162 | |||
| 163 | return 0 unless defined($thing); | ||
| 164 | |||
| 165 | return home($thing->piece) if $thing->isa('Games::Chess::Space'); | ||
| 166 | return (scalar($thing->trajectory) == 0) if $thing->isa('Games::Chess::Piece'); | ||
| 167 | |||
| 168 | croak 'home() requires a piece or a space'; | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | # | ||
| 173 | # empty(<space>) | ||
| 174 | # | ||
| 175 | |||
| 176 | sub empty ($) | ||
| 177 | { | ||
| 178 | my $thing = shift; | ||
| 179 | |||
| 180 | return 0 unless defined($thing); | ||
| 181 | |||
| 182 | return !defined($thing->piece) if $thing->isa('Games::Chess::Space'); | ||
| 183 | |||
| 184 | croak 'empty() requires a space'; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | # | ||
| 189 | # open(<space>, <space>) | ||
| 190 | # | ||
| 191 | # The spaces between the starting and resulting square are empty. | ||
| 192 | # | ||
| 193 | |||
| 194 | sub open ($$) | ||
| 195 | { | ||
| 196 | my $start; | ||
| 197 | my $end; | ||
| 198 | |||
| 199 | # TODO | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | # | ||
| 204 | # clear(<space>, <space>) | ||
| 205 | # | ||
| 206 | # The intervening spaces are open and not guarded, and the starting and ending spaces | ||
| 207 | # are not guarded. This is used in the description of castling. | ||
| 208 | # | ||
| 209 | |||
| 210 | sub clear ($$) | ||
| 211 | { | ||
| 212 | my ($from, $to) = @_; | ||
| 213 | |||
| 214 | # TODO | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | # | ||
| 219 | # enemy(<color>, <piece>) | ||
| 220 | # enemy(<color>, <space>) | ||
| 221 | # | ||
| 222 | |||
| 223 | sub enemy ($$) | ||
| 224 | { | ||
| 225 | my ($color, $thing) = @_; | ||
| 226 | |||
| 227 | # TODO | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | # | ||
| 232 | # friend(<color>, <piece>) | ||
| 233 | # friend(<color>, <space>) | ||
| 234 | # | ||
| 235 | |||
| 236 | sub friend ($$) | ||
| 237 | { | ||
| 238 | my ($color, $thing) = @_; | ||
| 239 | |||
| 240 | # TODO | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | # | ||
| 245 | # pawn(<piece>) | ||
| 246 | # pawn(<space>) | ||
| 247 | # | ||
| 248 | |||
| 249 | sub pawn ($) | ||
| 250 | { | ||
| 251 | # TODO | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | # | ||
| 256 | # guarded(<piece>) | ||
| 257 | # guarded(<space>) | ||
| 258 | # | ||
| 259 | |||
| 260 | sub guarded ($) | ||
| 261 | { | ||
| 262 | # TODO | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | # | ||
| 267 | # last_from(<space>) | ||
| 268 | # | ||
| 269 | # True if the square given is the `from' from the last ply. | ||
| 270 | # | ||
| 271 | |||
| 272 | sub last_from ($) | ||
| 273 | { | ||
| 274 | # TODO | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | # | ||
| 279 | # last_to(<space>) | ||
| 280 | # | ||
| 281 | # True if the square given is the `to' from the last ply. | ||
| 282 | # | ||
| 283 | |||
| 284 | sub last_to ($) | ||
| 285 | { | ||
| 286 | # TODO | ||
| 287 | } | ||
| 288 | |||
| 289 | |||
| 290 | # | ||
| 291 | # last(<space>, <space>) | ||
| 292 | # | ||
| 293 | |||
| 294 | sub last ($$) | ||
| 295 | { | ||
| 296 | # TODO | ||
| 297 | } | ||
| 298 | |||
| 299 | |||
| 300 | # | ||
| 301 | # delta(<space>, <files>, <ranks>) | ||
| 302 | # | ||
| 303 | |||
| 304 | sub delta ($$$) | ||
| 305 | { | ||
| 306 | my ($space, $files, $ranks) = @_; | ||
| 307 | |||
| 308 | my $file = $space->file_num; | ||
| 309 | my $rank = $space->rank_num; | ||
| 310 | |||
| 311 | return $space->board->space($file_num + $files, $rank_num + $ranks); | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | # | ||
| 316 | # queenward(<space>) | ||
| 317 | # | ||
| 318 | |||
| 319 | sub queenward ($) | ||
| 320 | { | ||
| 321 | my $space = shift; | ||
| 322 | |||
| 323 | return delta($space, -1, 0); | ||
| 324 | } | ||
| 325 | |||
| 326 | |||
| 327 | # | ||
| 328 | # kingward(<space>) | ||
| 329 | # | ||
| 330 | |||
| 331 | sub kingward ($) | ||
| 332 | { | ||
| 333 | my $space = shift; | ||
| 334 | |||
| 335 | return delta($space, +1, 0); | ||
| 336 | } | ||
| 337 | |||
| 338 | |||
| 339 | # | ||
| 340 | # whiteward(<space>) | ||
| 341 | # | ||
| 342 | |||
| 343 | sub whiteward ($) | ||
| 344 | { | ||
| 345 | my $space = shift; | ||
| 346 | |||
| 347 | return delta($space, 0, -1); | ||
| 348 | } | ||
| 349 | |||
| 350 | |||
| 351 | # | ||
| 352 | # blackward(<space>) | ||
| 353 | # | ||
| 354 | |||
| 355 | sub blackward ($) | ||
| 356 | { | ||
| 357 | my $space = shift; | ||
| 358 | |||
| 359 | return delta($space, 0, +1); | ||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 363 | # | ||
| 364 | # forward(<color>, <space>) | ||
| 365 | # | ||
| 366 | |||
| 367 | sub forward ($$) | ||
| 368 | { | ||
| 369 | my ($color, $space) = @_; | ||
| 370 | |||
| 371 | if ($color == &WHITE) { | ||
| 372 | return blackward($space); | ||
| 373 | } elsif ($color == &BLACK) { | ||
| 374 | return whiteward($space); | ||
| 375 | } else { | ||
| 376 | croak 'Invalid color'; | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | # | ||
| 382 | # backward(<color>, <space>) | ||
| 383 | # | ||
| 384 | |||
| 385 | sub backward ($$) | ||
| 386 | { | ||
| 387 | my ($color, $space) = @_; | ||
| 388 | |||
| 389 | if ($color == &BLACK) { | ||
| 390 | return blackward($space); | ||
| 391 | } elsif ($color == &WHITE) { | ||
| 392 | return whiteward($space); | ||
| 393 | } else { | ||
| 394 | croak 'Invalid color'; | ||
| 395 | } | ||
| 396 | } | ||
| 397 | |||
| 398 | |||
| 399 | # | ||
| 400 | # TODO: Stuff to incorporate. | ||
| 401 | # | ||
| 402 | |||
| 403 | =pod | ||
| 404 | |||
| 405 | =item C<kings_rook> | ||
| 406 | |||
| 407 | The rook home position on the king's side. | ||
| 408 | |||
| 409 | =item C<queens_rook> | ||
| 410 | |||
| 411 | The rook home position on the queen's side. | ||
| 412 | |||
| 413 | =back | ||
| 414 | |||
| 415 | |||
| 416 | =head2 Vectors | ||
| 417 | |||
| 418 | vectors may be added, subtracted, and multiplied by a constant. | ||
| 419 | |||
| 420 | =over 4 | ||
| 421 | |||
| 422 | =item C<ahead> | ||
| 423 | |||
| 424 | The space immediately `ahead', meaning away from the home row of the color. | ||
| 425 | |||
| 426 | |||
| 427 | =head2 Compound predicates | ||
| 428 | |||
| 429 | =over 4 | ||
| 430 | |||
| 431 | =item C<white_passant> | ||
| 432 | |||
| 433 | C< | ||
| 434 | (enemy(left) and pawn(left) and last(left + [0, 2], left)) | ||
| 435 | or | ||
| 436 | (enemy(right) and pawn(right) and last(right + [0, 2], right)) | ||
| 437 | > | ||
| 438 | |||
| 439 | =item C<black_passant> | ||
| 440 | |||
| 441 | C< | ||
| 442 | (enemy(left) and pawn(left) and last(left - [0, 2], left)) | ||
| 443 | or | ||
| 444 | (enemy(right) and pawn(right) and last(right - [0, 2], right)) | ||
| 445 | > | ||
| 446 | |||
| 447 | =item C<passant> | ||
| 448 | |||
| 449 | C< | ||
| 450 | (white and white_passant) or (black and black_passant) | ||
| 451 | > | ||
| 452 | |||
| 453 | =back | ||
| 454 | |||
| 455 | The default action is for the piece to move to the destination and the enemy piece at that | ||
| 456 | location to be removed. For specialty moves, these can be overridden. | ||
| 457 | |||
| 458 | It is assumed that any moves generated by these rules that produce off-board result | ||
| 459 | positions are rejected. | ||
| 460 | |||
| 461 | |||
| 462 | =head2 Actions | ||
| 463 | |||
| 464 | =over 4 | ||
| 465 | |||
| 466 | =item C<mutate> | ||
| 467 | |||
| 468 | After the move, mutat to the designated piece (used for pawn promotion in standard chess). | ||
| 469 | |||
| 470 | =back | ||
| 471 | |||
| 472 | |||
| 473 | =head2 Formalized Plies | ||
| 474 | |||
| 475 | Pawns: | ||
| 476 | |||
| 477 | [ahead] if empty; | ||
| 478 | [ahead * 2] if empty and clear and home; | ||
| 479 | [ahead + left] if enemy or passant; | ||
| 480 | [ahead + right] if enemy or passant. | ||
| 481 | |||
| 482 | Rooks: | ||
| 483 | |||
| 484 | [0, ?] if clear and not friend; | ||
| 485 | [?, 0] if clear and not friend. | ||
| 486 | |||
| 487 | Knights: | ||
| 488 | |||
| 489 | [+1, +2] if not friend; | ||
| 490 | [+1, -2] if not friend; | ||
| 491 | [-1, +2] if not friend; | ||
| 492 | [-1, -2] if not friend; | ||
| 493 | [+2, +1] if not friend; | ||
| 494 | [+2, -1] if not friend; | ||
| 495 | [-2, +1] if not friend; | ||
| 496 | [-2, -1] if not friend. | ||
| 497 | |||
| 498 | Bishops: | ||
| 499 | |||
| 500 | [?, ?] if clear and not friend; | ||
| 501 | [?, -?] if clear and not friend. | ||
| 502 | |||
| 503 | Queens: | ||
| 504 | |||
| 505 | [0, ?] if clear and not friend; | ||
| 506 | [?, 0] if clear and not friend. | ||
| 507 | [?, ?] if clear and not friend; | ||
| 508 | [?, -?] if clear and not friend. | ||
| 509 | |||
| 510 | Kings: | ||
| 511 | |||
| 512 | [-1, -1] if not friend and not threat; | ||
| 513 | [-1, 0] if not friend and not threat; | ||
| 514 | [-1, +1] if not friend and not threat; | ||
| 515 | [ 0, -1] if not friend and not threat; | ||
| 516 | [ 0, +1] if not friend and not threat; | ||
| 517 | [+1, -1] if not friend and not threat; | ||
| 518 | [+1, 0] if not friend and not threat; | ||
| 519 | [+1, +1] if not friend and not threat; | ||
| 520 | [king * 2] if home and home[kings_rook] and clear[king, kings_rook]; | ||
| 521 | [queen * 3] if home and home[queens_rook] and clear[king, queens_rook]; | ||
| 522 | |||
| 523 | |||
| 524 | We still need notations for the rook movements during castling. | ||
| 525 | |||
| 526 | Globally: C<not threat[king]>; | ||
| 527 | |||
| 528 | |||
| 529 | =cut | ||
| 530 | |||
| 531 | |||
| 532 | # | ||
| 533 | # End of file. | ||
| 534 | # | ||
| 535 |