Class: Chess::Board
- Inherits:
-
Object
- Object
- Chess::Board
- Defined in:
- ext/chess/chess.c,
ext/chess/chess.c
Overview
This class rappresents a chess board. The rappresentation of the board use bitboards where each bit represents a game position or state, designed for optimization of speed and/or memory or disk use in mass calculations. This ensures a fast library.
Instance Method Summary collapse
-
#[](square) ⇒ String
Returns the piece on the ‘square` of the chessboard.
-
#active_color ⇒ Boolean
Returns the active color: ‘false` means white turn, `true` means black turn.
-
#check? ⇒ Boolean
Returns ‘true` if the king of the color that has the turn is in check, `false` otherwise.
-
#checkmate? ⇒ Boolean
Returns ‘true` if the king of the color that has the turn is in checkmate, `false` otherwise.
-
#fifty_move_rule? ⇒ Boolean
Returns ‘true` if a player can claim draw by the fifty-move rule, `false` otherwise.
-
#fullmove_number ⇒ Integer
Returns the fullmove number: the number of the full move.
-
#generate_all_moves ⇒ Array<String>
Generate all legal moves for the current board position.
-
#generate_moves(square) ⇒ Array<String>
Generate all legal moves for the piece in ‘square` position.
-
#halfmove_clock ⇒ Integer
Returns the halfmove clock that is the number of halfmoves since the last pawn advance or capture.
-
#insufficient_material? ⇒ Boolean
Returns ‘true` if the board has insufficient material to checkmate, `false` otherwise.
-
#only_kings? ⇒ Boolean
Returns ‘true` if on the board there are only the two kings, `false` otherwise.
-
#placement {|piece, index| ... } ⇒ Board, Array<String>
Cycle the Board squares.
-
#stalemate? ⇒ Boolean
Returns ‘true` if the pieces of the color that has the turn are in stalemate, `false` otherwise.
-
#to_fen ⇒ String
Returns the FEN string of the board.
-
#to_s ⇒ String
Board to string.
Instance Method Details
#[](square) ⇒ String
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
# File 'ext/chess/chess.c', line 446
VALUE
board_get_piece (VALUE self, VALUE square)
{
Board *board;
Data_Get_Struct (self, Board, board);
int i;
if (TYPE (square) == T_STRING)
i = coord_to_square (StringValuePtr (square));
else
i = FIX2INT (square);
char piece = board->placement[i];
if (i < 0 || i > 63 || !piece)
return Qnil;
else
return rb_str_new (&piece, 1);
}
|
#active_color ⇒ Boolean
565 566 567 568 569 570 571 572 573 574 |
# File 'ext/chess/chess.c', line 565
VALUE
board_active_color (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
if (board->active_color)
return Qtrue;
else
return Qfalse;
}
|
#check? ⇒ Boolean
468 469 470 471 472 473 474 475 476 477 |
# File 'ext/chess/chess.c', line 468
VALUE
board_king_in_check (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
if (king_in_check (board, board->active_color))
return Qtrue;
else
return Qfalse;
}
|
#checkmate? ⇒ Boolean
484 485 486 487 488 489 490 491 492 493 |
# File 'ext/chess/chess.c', line 484
VALUE
board_king_in_checkmate (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
if (king_in_checkmate (board, board->active_color))
return Qtrue;
else
return Qfalse;
}
|
#fifty_move_rule? ⇒ Boolean
548 549 550 551 552 553 554 555 556 557 |
# File 'ext/chess/chess.c', line 548
VALUE
board_fifty_move_rule (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
if (fifty_move_rule (board))
return Qtrue;
else
return Qfalse;
}
|
#fullmove_number ⇒ Integer
597 598 599 600 601 602 603 |
# File 'ext/chess/chess.c', line 597
VALUE
board_fullmove_number (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
return INT2FIX (board->fullmove_number);
}
|
#generate_all_moves ⇒ Array<String>
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File 'ext/chess/chess.c', line 655
VALUE
board_generate_all_moves (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
VALUE moves = rb_ary_new ();
Board new_board;
char *move_done;
char capture;
for (int i = 0; i < 64; i++)
for (int j = 0; j < 64; j++)
if (pseudo_legal_move (board, i, j))
{
move_done = castling (board, castling_type (board, i, j), &new_board);
if (move_done || try_move (board, i, j, 'Q', &new_board, &move_done, &capture))
rb_ary_push (moves, rb_str_new2 (move_done));
}
return moves;
}
|
#generate_moves(square) ⇒ Array<String>
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
# File 'ext/chess/chess.c', line 619
VALUE
board_generate_moves (VALUE self, VALUE square)
{
Board *board;
Data_Get_Struct (self, Board, board);
int from;
if (TYPE (square) == T_STRING)
from = coord_to_square (StringValuePtr (square));
else
from = FIX2INT (square);
VALUE moves = rb_ary_new ();
Board new_board;
char *move_done;
char capture;
for (int i = 0; i < 64; i++)
if (pseudo_legal_move (board, from, i))
{
move_done = castling (board, castling_type (board, from, i), &new_board);
if (move_done || try_move (board, from, i, 'Q', &new_board, &move_done, &capture))
rb_ary_push (moves, rb_str_new2 (move_done));
}
return moves;
}
|
#halfmove_clock ⇒ Integer
583 584 585 586 587 588 589 |
# File 'ext/chess/chess.c', line 583
VALUE
board_halfmove_clock (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
return INT2FIX (board->halfmove_clock);
}
|
#insufficient_material? ⇒ Boolean
516 517 518 519 520 521 522 523 524 525 |
# File 'ext/chess/chess.c', line 516
VALUE
board_insufficient_material (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
if (insufficient_material (board))
return Qtrue;
else
return Qfalse;
}
|
#only_kings? ⇒ Boolean
532 533 534 535 536 537 538 539 540 541 |
# File 'ext/chess/chess.c', line 532
VALUE
board_only_kings (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
if (only_kings (board))
return Qtrue;
else
return Qfalse;
}
|
#placement {|piece, index| ... } ⇒ Board, Array<String>
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'ext/chess/chess.c', line 397
VALUE
board_placement (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
if (!rb_block_given_p ())
{
VALUE placement = rb_ary_new ();
for (int i = 0; i < 64; i++)
{
char piece = board->placement[i];
rb_ary_push (placement, rb_str_new (&piece, 1));
}
return placement;
}
else
{
for (int i = 0; i < 64; i++)
{
char piece = board->placement[i];
rb_yield_values (2, rb_str_new (&piece, 1), INT2FIX (i));
}
return self;
}
}
|
#stalemate? ⇒ Boolean
500 501 502 503 504 505 506 507 508 509 |
# File 'ext/chess/chess.c', line 500
VALUE
board_stalemate (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
if (stalemate (board, board->active_color))
return Qtrue;
else
return Qfalse;
}
|
#to_fen ⇒ String
680 681 682 683 684 685 686 687 688 689 |
# File 'ext/chess/chess.c', line 680
VALUE
board_to_fen (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
char *fen = to_fen (board);
VALUE rb_fen = rb_str_new2 (fen);
free (fen);
return rb_fen;
}
|
#to_s ⇒ String
696 697 698 699 700 701 702 703 704 705 |
# File 'ext/chess/chess.c', line 696
VALUE
board_to_s (VALUE self)
{
Board *board;
Data_Get_Struct (self, Board, board);
char *s = print_board (board);
VALUE rb_s = rb_str_new2 (s);
free (s);
return rb_s;
}
|