chess 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/board.c +1 -1
- data/ext/chess.c +40 -1
- data/ext/chess.h +2 -1
- data/ext/game.c +8 -0
- data/lib/chess/version.rb +2 -2
- data/test/test_big_pgn_collection.rb +4 -6
- data/test/test_checkmate.rb +10 -12
- data/test/test_fifty_rule_move.rb +4 -6
- data/test/test_helper.rb +2 -2
- data/test/test_insufficient_material.rb +6 -8
- data/test/test_load_fen.rb +32 -0
- data/test/test_pgn_collection.rb +10 -12
- data/test/test_stalemate.rb +5 -7
- data/test/test_threefold_repetition.rb +4 -6
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63c659ca2766109f9a2df4d1f60ef3d162d3a4e6
|
4
|
+
data.tar.gz: 437b5bb1ccd23dce72218906271b188277a26c9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b61302810fd83b5e942e360c04a46a7f74ee16c21a384ecd7e4698964665acb1770fc8af29f83fe6ec9222280ce662893d827bb1a3b5e756435d5d8186418ad6
|
7
|
+
data.tar.gz: 517a93bf52e02eebb2a824292b4793bc71333195bfba476af843b6d1ada3ed1a8207521fa2ef9e308cd3786695c00648cfdc7fc0c75898b8d6e676fcfce3e4b9
|
data/ext/board.c
CHANGED
data/ext/chess.c
CHANGED
@@ -383,6 +383,44 @@ board_placement (VALUE self)
|
|
383
383
|
}
|
384
384
|
}
|
385
385
|
|
386
|
+
/*
|
387
|
+
* call-seq: [square]
|
388
|
+
*
|
389
|
+
* Returns the piece on the +square+ of the chessboard. If there is no piece or
|
390
|
+
* the square is not valid return the blank string.
|
391
|
+
*
|
392
|
+
* Each square on the chessboard is represented by an integer according to the following scheme:
|
393
|
+
* 8 | 56 57 58 59 60 61 62 63
|
394
|
+
* 7 | 48 49 50 51 52 53 54 55
|
395
|
+
* 6 | 40 41 42 43 44 45 46 47
|
396
|
+
* 5 | 32 33 34 35 36 37 38 39
|
397
|
+
* 4 | 24 25 26 27 28 29 30 31
|
398
|
+
* 3 | 16 17 18 19 20 21 22 23
|
399
|
+
* 2 | 8 9 10 11 12 13 14 15
|
400
|
+
* 1 | 0 1 2 3 4 5 6 7
|
401
|
+
* +-------------------------
|
402
|
+
* a b c d e f g h
|
403
|
+
*
|
404
|
+
* Parameters are:
|
405
|
+
* +square+:: the square of the board. Can be a fixnum between 0 and 63 or a
|
406
|
+
* string like 'a2', 'c5'... The string must be downcase.
|
407
|
+
*/
|
408
|
+
VALUE
|
409
|
+
board_get_piece (VALUE self, VALUE square)
|
410
|
+
{
|
411
|
+
Board *board;
|
412
|
+
Data_Get_Struct (self, Board, board);
|
413
|
+
int i;
|
414
|
+
char piece[2];
|
415
|
+
piece[1] = '\0';
|
416
|
+
if (TYPE (square) == T_STRING)
|
417
|
+
i = coord_to_square (StringValuePtr (square));
|
418
|
+
else
|
419
|
+
i = FIX2INT (square);
|
420
|
+
piece[0] = board->placement[i];
|
421
|
+
return rb_str_new2 (piece);
|
422
|
+
}
|
423
|
+
|
386
424
|
/*
|
387
425
|
* call-seq: check?
|
388
426
|
*
|
@@ -575,6 +613,7 @@ Init_chess ()
|
|
575
613
|
*/
|
576
614
|
board_klass = rb_define_class_under (chess, "Board", rb_cObject);
|
577
615
|
rb_define_method (board_klass, "placement", board_placement, 0);
|
616
|
+
rb_define_method (board_klass, "[]", board_get_piece, 1);
|
578
617
|
rb_define_method (board_klass, "check?", board_king_in_check, 0);
|
579
618
|
rb_define_method (board_klass, "checkmate?", board_king_in_checkmate, 0);
|
580
619
|
rb_define_method (board_klass, "stalemate?", board_stalemate, 0);
|
@@ -590,4 +629,4 @@ Init_chess ()
|
|
590
629
|
* This exception will be raised when making an illegal move.
|
591
630
|
*/
|
592
631
|
illegal_move_error = rb_define_class_under (chess, "IllegalMoveError", rb_eStandardError);
|
593
|
-
}
|
632
|
+
}
|
data/ext/chess.h
CHANGED
@@ -31,6 +31,7 @@ VALUE game_to_s (VALUE self);
|
|
31
31
|
// Board
|
32
32
|
|
33
33
|
VALUE board_placement (VALUE self);
|
34
|
+
VALUE board_get_piece (VALUE self, VALUE square);
|
34
35
|
VALUE board_king_in_check (VALUE self);
|
35
36
|
VALUE board_king_in_checkmate (VALUE self);
|
36
37
|
VALUE board_stalemate (VALUE self);
|
@@ -44,4 +45,4 @@ VALUE board_to_s (VALUE self);
|
|
44
45
|
|
45
46
|
// INIT
|
46
47
|
|
47
|
-
void Init_chess ();
|
48
|
+
void Init_chess ();
|
data/ext/game.c
CHANGED
@@ -324,6 +324,14 @@ set_fen (Game *g, const char *fen)
|
|
324
324
|
strcpy (g->moves[g->current], "SET BY FEN");
|
325
325
|
strcpy (g->full_moves[g->current], "SET BY FEN");
|
326
326
|
g->current++;
|
327
|
+
|
328
|
+
// check result
|
329
|
+
if (king_in_checkmate (board, board->active_color))
|
330
|
+
g->result = board->active_color;
|
331
|
+
else
|
332
|
+
if (stalemate (board, board->active_color) || insufficient_material (board))
|
333
|
+
g->result = DRAW;
|
334
|
+
|
327
335
|
free (s);
|
328
336
|
free (pch);
|
329
337
|
}
|
data/lib/chess/version.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class ChessTest < Test
|
3
|
+
class ChessTest < Minitest::Test
|
4
4
|
TestHelper.pgns('.', TestHelper::BIG_PGN_COLLECTION).each do |file|
|
5
5
|
define_method "test_big_pgn_#{file}" do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
assert(game.checkmate?) if pgn.moves.last =~ /\#$/
|
10
|
-
end
|
6
|
+
pgn = Chess::Pgn.new(file)
|
7
|
+
game = Chess::Game.new(pgn.moves)
|
8
|
+
assert(game.checkmate?) if pgn.moves.last =~ /\#$/
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
data/test/test_checkmate.rb
CHANGED
@@ -1,22 +1,20 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class ChessTest < Test
|
3
|
+
class ChessTest < Minitest::Test
|
4
4
|
|
5
5
|
TestHelper.pgns('checkmate').each do |file|
|
6
6
|
name = File.basename(file, '.pgn')
|
7
7
|
win = file.include?('white') ? 'white' : 'black'
|
8
8
|
define_method "test_#{win}_checkmate_#{name}" do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
assert_equal(game.active_player, :white)
|
19
|
-
end
|
9
|
+
pgn = Chess::Pgn.new(file)
|
10
|
+
game = Chess::Game.new(pgn.moves)
|
11
|
+
assert(game.board.checkmate?)
|
12
|
+
if file.include?('white_won')
|
13
|
+
assert_equal(game.result, '1-0')
|
14
|
+
assert_equal(game.active_player, :black)
|
15
|
+
elsif file.include?('black_won')
|
16
|
+
assert_equal(game.result, '0-1')
|
17
|
+
assert_equal(game.active_player, :white)
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
@@ -1,15 +1,13 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class ChessTest < Test
|
3
|
+
class ChessTest < Minitest::Test
|
4
4
|
|
5
5
|
TestHelper.pgns('fifty_move_rule').each do |file|
|
6
6
|
name = File.basename(file, '.pgn')
|
7
7
|
define_method "test_fifty_rule_move_#{name}" do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
assert(game.board.fifty_rule_move?)
|
12
|
-
end
|
8
|
+
pgn = Chess::Pgn.new(file)
|
9
|
+
game = Chess::Game.new(pgn.moves)
|
10
|
+
assert(game.board.fifty_rule_move?)
|
13
11
|
end
|
14
12
|
end
|
15
13
|
|
data/test/test_helper.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class ChessTest < Test
|
3
|
+
class ChessTest < Minitest::Test
|
4
4
|
FENS = [
|
5
5
|
'8/k7/8/7B/6b1/7B/6b1/4K3 w - - 0 1',
|
6
6
|
'8/2k5/8/1B6/4b3/8/2K5/5B2 w - - 0 1',
|
@@ -19,13 +19,11 @@ class ChessTest < Test::Unit::TestCase
|
|
19
19
|
TestHelper.pgns('insufficient_material').each do |file|
|
20
20
|
name = File.basename(file, '.pgn')
|
21
21
|
define_method "test_insufficient_material_#{name}" do
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
assert_equal(game.result, '1/2-1/2')
|
27
|
-
end
|
22
|
+
pgn = Chess::Pgn.new(file)
|
23
|
+
game = Chess::Game.new(pgn.moves)
|
24
|
+
assert(game.board.insufficient_material?)
|
25
|
+
assert_equal(game.result, '1/2-1/2')
|
28
26
|
end
|
29
27
|
end
|
30
28
|
|
31
|
-
end
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ChessTest < Minitest::Test
|
4
|
+
|
5
|
+
def test_fen_in_progress
|
6
|
+
g = Chess::Game.load_fen('rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2')
|
7
|
+
assert_equal 'P', g.board['e4']
|
8
|
+
assert_equal 'p', g.board['c5']
|
9
|
+
assert_equal 'K', g.board[4]
|
10
|
+
assert_equal '*', g.result
|
11
|
+
assert_equal :in_progress, g.status
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_fen_white_won
|
15
|
+
g = Chess::Game.load_fen('rnb1kbnr/pppp1ppp/4p3/8/5PPq/8/PPPPP2P/RNBQKBNR w KQkq - 1 3')
|
16
|
+
assert_equal 'q', g.board['h4']
|
17
|
+
assert_equal 'P', g.board['f4']
|
18
|
+
assert_equal 'p', g.board[44]
|
19
|
+
assert_equal '1-0', g.result
|
20
|
+
assert_equal :white_won, g.status
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_fen_stalemate
|
24
|
+
g = Chess::Game.load_fen('8/6b1/8/8/8/n7/PP6/K7 w KQkq - 1 3')
|
25
|
+
assert_equal 'b', g.board['g7']
|
26
|
+
assert_equal 'n', g.board['a3']
|
27
|
+
assert_equal 'K', g.board[0]
|
28
|
+
assert_equal '1/2-1/2', g.result
|
29
|
+
assert_equal :stalemate, g.status
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/test/test_pgn_collection.rb
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class ChessTest < Test
|
3
|
+
class ChessTest < Minitest::Test
|
4
4
|
TestHelper.pgns('valid').each do |file|
|
5
5
|
name = File.basename(file, '.pgn')
|
6
6
|
define_method "test_pgn_#{name}" do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
assert(game.board.checkmate?) if m =~ /\#$/
|
17
|
-
end
|
7
|
+
pgn = Chess::Pgn.new(file)
|
8
|
+
game = Chess::Game.new
|
9
|
+
pgn.moves.each do |m|
|
10
|
+
mm = game.move(m)
|
11
|
+
mm.gsub!(/\+$/, '') if m !~ /\+$/
|
12
|
+
mm.gsub!('ep', '') if m !~ /ep/
|
13
|
+
assert_equal(m, mm)
|
14
|
+
assert(game.board.check?) if m =~ /\+$/
|
15
|
+
assert(game.board.checkmate?) if m =~ /\#$/
|
18
16
|
end
|
19
17
|
end
|
20
18
|
define_method "test_pgn_result_#{name}" do
|
data/test/test_stalemate.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class ChessTest < Test
|
3
|
+
class ChessTest < Minitest::Test
|
4
4
|
|
5
5
|
TestHelper.pgns('stalemate').each do |file|
|
6
6
|
name = File.basename(file, '.pgn')
|
7
7
|
define_method "test_stalemate_#{name}" do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
assert_equal(game.result, '1/2-1/2')
|
13
|
-
end
|
8
|
+
pgn = Chess::Pgn.new(file)
|
9
|
+
game = Chess::Game.new(pgn.moves)
|
10
|
+
assert(game.board.stalemate?)
|
11
|
+
assert_equal(game.result, '1/2-1/2')
|
14
12
|
end
|
15
13
|
end
|
16
14
|
|
@@ -1,15 +1,13 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class ChessTest < Test
|
3
|
+
class ChessTest < Minitest::Test
|
4
4
|
|
5
5
|
TestHelper.pgns('threefold_repetition').each do |file|
|
6
6
|
name = File.basename(file, '.pgn')
|
7
7
|
define_method "test_threefold_repetition_#{name}" do
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
assert(game.threefold_repetition?)
|
12
|
-
end
|
8
|
+
pgn = Chess::Pgn.new(file)
|
9
|
+
game = Chess::Game.new(pgn.moves)
|
10
|
+
assert(game.threefold_repetition?)
|
13
11
|
end
|
14
12
|
end
|
15
13
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chess
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Enrico Pilotto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-12-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A fast chess library that use bitboards to play chess with Ruby.
|
14
14
|
email:
|
@@ -18,7 +18,7 @@ extensions:
|
|
18
18
|
- ext/extconf.rb
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
-
- .gitignore
|
21
|
+
- ".gitignore"
|
22
22
|
- Gemfile
|
23
23
|
- LICENSE
|
24
24
|
- README.rdoc
|
@@ -1247,6 +1247,7 @@ files:
|
|
1247
1247
|
- test/test_fifty_rule_move.rb
|
1248
1248
|
- test/test_helper.rb
|
1249
1249
|
- test/test_insufficient_material.rb
|
1250
|
+
- test/test_load_fen.rb
|
1250
1251
|
- test/test_pgn_collection.rb
|
1251
1252
|
- test/test_stalemate.rb
|
1252
1253
|
- test/test_threefold_repetition.rb
|
@@ -1260,17 +1261,17 @@ require_paths:
|
|
1260
1261
|
- lib
|
1261
1262
|
required_ruby_version: !ruby/object:Gem::Requirement
|
1262
1263
|
requirements:
|
1263
|
-
- -
|
1264
|
+
- - ">="
|
1264
1265
|
- !ruby/object:Gem::Version
|
1265
1266
|
version: '0'
|
1266
1267
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
1267
1268
|
requirements:
|
1268
|
-
- -
|
1269
|
+
- - ">="
|
1269
1270
|
- !ruby/object:Gem::Version
|
1270
1271
|
version: '0'
|
1271
1272
|
requirements: []
|
1272
1273
|
rubyforge_project: chess
|
1273
|
-
rubygems_version: 2.
|
1274
|
+
rubygems_version: 2.2.2
|
1274
1275
|
signing_key:
|
1275
1276
|
specification_version: 4
|
1276
1277
|
summary: A fast chess library to play chess with Ruby.
|
@@ -2478,6 +2479,7 @@ test_files:
|
|
2478
2479
|
- test/test_fifty_rule_move.rb
|
2479
2480
|
- test/test_helper.rb
|
2480
2481
|
- test/test_insufficient_material.rb
|
2482
|
+
- test/test_load_fen.rb
|
2481
2483
|
- test/test_pgn_collection.rb
|
2482
2484
|
- test/test_stalemate.rb
|
2483
2485
|
- test/test_threefold_repetition.rb
|