Class: Chess::Board

Inherits:
Object
  • Object
show all
Defined in:
ext/chess.c,
ext/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

Instance Method Details

#[](square) ⇒ String

Returns the piece on the ‘square` of the chessboard. If there is no piece or the square is not valid return `nil`. Each square on the chessboard is represented by an integer according to the following scheme:

8 | 56 57 58 59 60 61 62 63
7 | 48 49 50 51 52 53 54 55
6 | 40 41 42 43 44 45 46 47
5 | 32 33 34 35 36 37 38 39
4 | 24 25 26 27 28 29 30 31
3 | 16 17 18 19 20 21 22 23
2 |  8  9 10 11 12 13 14 15
1 |  0  1  2  3  4  5  6  7
  +-------------------------
     a  b  c  d  e  f  g  h

Parameters:

  • square (Integer, String)

    The square of the Chess::Board. Can be an integer between 0 and 63 or a string like ‘a2’, ‘c5’…

Returns:

  • (String)

    The symbol that identify the piece _(‘P’, ‘R’, ‘N’, ‘B’, ‘Q’, ‘K’)_. Upcase for a white piece, downcase for a black piece.



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'ext/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_colorBoolean

Returns the active color: ‘false` means white turn, `true` means black turn.

Returns:

  • (Boolean)


565
566
567
568
569
570
571
572
573
574
# File 'ext/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

Returns ‘true` if the king of the color that has the turn is in check, `false` otherwise.

Returns:

  • (Boolean)


468
469
470
471
472
473
474
475
476
477
# File 'ext/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

Returns ‘true` if the king of the color that has the turn is in checkmate, `false` otherwise.

Returns:

  • (Boolean)


484
485
486
487
488
489
490
491
492
493
# File 'ext/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

Returns ‘true` if a player can claim draw by the fifty-move rule, `false` otherwise.

Returns:

  • (Boolean)


548
549
550
551
552
553
554
555
556
557
# File 'ext/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_numberInteger

Returns the fullmove number: the number of the full move. It starts at 1, and * is incremented after black’s move.

Returns:

  • (Integer)


597
598
599
600
601
602
603
# File 'ext/chess.c', line 597

VALUE
board_fullmove_number (VALUE self)
{
  Board *board;
  Data_Get_Struct (self, Board, board);
  return INT2FIX (board->fullmove_number);
}

#generate_moves(square) ⇒ Array<String>

Generate all legal moves for the piece in ‘square` position.

Examples:

:001 > g = Chess::Game.new
 => #<Chess::Game:0x007f88a529fa88>
:002 > g.board.generate_moves('b1')
 => ["Na3", "Nc3"]

Parameters:

  • square (Integer, String)

    The square of the Chess::Board. Can be an integer between 0 and 63 or a string like ‘a2’, ‘c5’…

Returns:

  • (Array<String>)

    Returns all legal moves that the piece in ‘square` position can perform. The moves are in short algebraic chess notation format.



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.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_clockInteger

Returns the halfmove clock that is the number of halfmoves since the last pawn advance or capture. This is used to determine if a draw can be claimed under the fifty-move rule.

Returns:

  • (Integer)


583
584
585
586
587
588
589
# File 'ext/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

Returns ‘true` if the board has insufficient material to checkmate, `false` otherwise.

Returns:

  • (Boolean)


516
517
518
519
520
521
522
523
524
525
# File 'ext/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

Returns ‘true` if on the board there are only the two kings, `false` otherwise.

Returns:

  • (Boolean)


532
533
534
535
536
537
538
539
540
541
# File 'ext/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>

Cycle the Chess::Board squares.

Yields:

  • (piece, index)

    Calls ‘block` once for each square in the Chess::Board, passing the `piece` in the square and the `index` as parameters.

Returns:

  • (Board)

    Returns ‘self` if a block is given.

  • (Array<String>)

    Returns the array of pieces if no block is given.



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.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

Returns ‘true` if the pieces of the color that has the turn are in stalemate, `false` otherwise.

Returns:

  • (Boolean)


500
501
502
503
504
505
506
507
508
509
# File 'ext/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_fenString

Returns the FEN string of the board.

Returns:

  • (String)


648
649
650
651
652
653
654
655
656
657
# File 'ext/chess.c', line 648

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_sString

Chess::Board to string.

Returns:

  • (String)


664
665
666
667
668
669
670
671
672
673
# File 'ext/chess.c', line 664

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;
}