Skip to content

Commit

Permalink
Add new method board.generate_all_moves
Browse files Browse the repository at this point in the history
  • Loading branch information
pioz committed Aug 30, 2024
1 parent d5f2f3e commit d319243
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
chess (0.3.6)
chess (0.4.0)

GEM
remote: http://rubygems.org/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ end
puts g.status
```

**Documentation** is available **[here](http://pioz.github.io/chess)**.
📚 **Documentation** is available [here](https://pioz.github.io/chess).

## Questions or problems?

Expand All @@ -44,5 +44,5 @@ pull request.

## Copyright

Copyright (c) 2021 [Enrico Pilotto (@pioz)](https://github.com/pioz). See
Copyright (c) 2024 [Enrico Pilotto (@pioz)](https://github.com/pioz). See
[LICENSE](https://github.com/pioz/chess/blob/master/LICENSE) for details.
33 changes: 33 additions & 0 deletions ext/chess/chess.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,38 @@ board_generate_moves (VALUE self, VALUE square)
return moves;
}

/*
* @overload generate_all_moves
* Generate all legal moves for the current board position.
* @return [Array<String>] Returns all legal moves that can be performed
* in the current board position. The moves are in short algebraic chess
* notation format.
* @example
* :001 > g = Chess::Game.new
* => #<Chess::Game:0x007f88a529fa88>
* :002 > g.board.generate_all_moves
* => ["Na3", "Nc3", "Nf3", "Nh3", "a3", "a4", "b3", "b4", "c3", "c4", "d3", "d4", "e3", "e4", "f3", "f4", "g3", "g4", "h3", "h4"]
*/
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;
}

/*
* @overload to_fen
* Returns the FEN string of the board.
Expand Down Expand Up @@ -729,6 +761,7 @@ Init_chess ()
rb_define_method (board_klass, "halfmove_clock", board_halfmove_clock, 0);
rb_define_method (board_klass, "fullmove_number", board_fullmove_number, 0);
rb_define_method (board_klass, "generate_moves", board_generate_moves, 1);
rb_define_method (board_klass, "generate_all_moves", board_generate_all_moves, 0);
rb_define_method (board_klass, "to_fen", board_to_fen, 0);
rb_define_method (board_klass, "to_s", board_to_s, 0);

Expand Down
1 change: 1 addition & 0 deletions ext/chess/chess.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ VALUE board_active_color (VALUE self);
VALUE board_halfmove_clock (VALUE self);
VALUE board_fullmove_number (VALUE self);
VALUE board_generate_moves (VALUE self, VALUE square);
VALUE board_generate_all_moves (VALUE self);
VALUE board_to_fen (VALUE self);
VALUE board_to_s (VALUE self);

Expand Down
2 changes: 1 addition & 1 deletion lib/chess/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The Chess library module.
module Chess
# The library version.
VERSION = '0.3.6'.freeze
VERSION = '0.4.0'.freeze
end
7 changes: 7 additions & 0 deletions test/test_move_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class ChessTest < Minitest::Test
end
end

def test_generate_all_moves
game = Chess::Game.new
result = game.board.generate_all_moves

assert_equal %w[Na3 Nc3 Nf3 Nh3 a3 a4 b3 b4 c3 c4 d3 d4 e3 e4 f3 f4 g3 g4 h3 h4], result
end

def test_github_issue32
game = Chess::Game.new
game.moves = %w[f2f4 d7d6 d2d3 h7h5 b1d2 e7e5 f4f5 a7a5 c2c3 d8f6 d2c4 b7b6 c4d2 a8a6 d2f3 a6a7 f3e5 b6b5 h2h4]
Expand Down

0 comments on commit d319243

Please sign in to comment.