pgn2fen 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'pgn2fen'
5
+ s.version = '0.9.0'
6
+ s.summary = %q{Converts a single game chess PGN to an array of FEN strings}
7
+ s.description = <<-EOS
8
+ Converts a single game chess PGN to an array of FEN strings.
9
+ The FEN follows the specification as listed on [Forsyth–Edwards Notation](http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation).
10
+ EOS
11
+ s.authors = ["Vinay Doma"]
12
+ s.email = %q{vinay.doma@gmail.com}
13
+ s.license = "Ruby"
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.md",
17
+ "TODO",
18
+ "VERSION"
19
+ ]
20
+ s.files = Dir["**/*"] - Dir["*.gem"] - ["Gemfile.lock"]
21
+ s.homepage = %q{http://github.com/nivyatech/pgn2fen}
22
+ s.rdoc_options = ["--charset=UTF-8"]
23
+ s.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,143 @@
1
+ $: << File.join( File.dirname( __FILE__ ), '../lib/')
2
+ require 'pgn2fen'
3
+ require 'test/unit'
4
+
5
+ class Pgn2FenTest < Test::Unit::TestCase
6
+
7
+ def test_simple
8
+ pgn = %{
9
+ [Event "Rapperswil, Switzerland"]
10
+ [Site "Rapperswil, Switzerland"]
11
+ [Date "1955.??.??"]
12
+ [EventDate "?"]
13
+ [Round "?"]
14
+ [Result "1-0"]
15
+ [White "Martin"]
16
+ [Black "Pompei"]
17
+ [ECO "C45"]
18
+ [WhiteElo "?"]
19
+ [BlackElo "?"]
20
+ [PlyCount "41"]
21
+
22
+ 1. e4 e5 2. Nf3 Nc6 3. d4 exd4 4. Bc4 Bc5 5. O-O d6 6. c3 dxc3
23
+ 7. Nxc3 Be6 8. Nd5 Qd7 9. a3 Ne5 10. Nxe5 dxe5 11. Qb3 c6
24
+ 12. Rd1 Bd4 13. Be3 O-O-O 14. Rac1 Kb8 15. Rxd4 exd4 16. Bf4+
25
+ Kc8 17. Qa4 Bxd5 18. exd5 Qg4 19. g3 Ne7 20. dxc6 Nxc6 21. Ba6
26
+ 1-0
27
+ }
28
+ validate_fen_array(Pgn2Fen::Game.new(pgn).parse_pgn().fen_array)
29
+ end
30
+
31
+ def test_pawn_capture_two_ways
32
+ pgn = %{
33
+ [Event "Enpassent Test"]
34
+ [Site ""]
35
+ [Date ""]
36
+ [Round ""]
37
+ [White ""]
38
+ [Black ""]
39
+ [Result "0-1"]
40
+ [WhiteElo ""]
41
+ [BlackElo ""]
42
+
43
+ 1.e4 c5 2.Nf3 Nc6 3.Bb5 e6 4.Bxc6 bxc6 5.b3 Ne7 6.Bb2 Ng6 7.h4 h5 8.e5 Be7
44
+ 9.Nc3 d5 0-1
45
+ }
46
+ validate_fen_array(Pgn2Fen::Game.new(pgn).parse_pgn().fen_array)
47
+ end
48
+
49
+ def test_promotion_via_capture
50
+ pgn = %{
51
+ [Event "Enpassent Test"]
52
+ [Site ""]
53
+ [Date ""]
54
+ [Round ""]
55
+ [White ""]
56
+ [Black ""]
57
+ [Result "0-1"]
58
+ [WhiteElo ""]
59
+ [BlackElo ""]
60
+
61
+ 1.e4 f5 2.exf5 e6 3.fxe6 Ke7 4.exd7 Qe8 5.dxc8=Q 0-1
62
+ }
63
+ validate_fen_array(Pgn2Fen::Game.new(pgn).parse_pgn().fen_array)
64
+ end
65
+
66
+ def test_promotion
67
+ pgn = %{
68
+ [Event "Enpassent Test"]
69
+ [Site ""]
70
+ [Date ""]
71
+ [Round ""]
72
+ [White ""]
73
+ [Black ""]
74
+ [Result "0-1"]
75
+ [WhiteElo ""]
76
+ [BlackElo ""]
77
+
78
+ 1.e4 f5 2.exf5 g6 3.fxg6 Nf6 4.g7 h6 5.g8=Q 0-1
79
+ }
80
+ validate_fen_array(Pgn2Fen::Game.new(pgn).parse_pgn().fen_array)
81
+ end
82
+
83
+ def validate_fen_array(fen_array)
84
+ fen_array.each {|fen|
85
+ assert_equal true, validate_fen(fen)
86
+ }
87
+ end
88
+
89
+ # borrowed some ideas/code from the excellent https://github.com/jhlywa/chess.js
90
+ def validate_fen fen
91
+ tokens = fen.split
92
+ # check has required fields
93
+ unless tokens.length == 6
94
+ raise 'FEN string must contain six space-delimited fields.'
95
+ end
96
+ # check board layout
97
+ board_rows = tokens[0].split('/')
98
+ unless board_rows.length == 8
99
+ raise 'numbers of rows in piece placement field must be 8.'
100
+ end
101
+ board_rows.each {|row|
102
+ sum = 0
103
+ atoms = row.split("");
104
+ atoms.each {|atom|
105
+ if atom =~ /\b[0-8]\b/
106
+ sum += atom.to_i
107
+ elsif atom =~ /\b[a-z]|[A-Z]\b/
108
+ sum += 1
109
+ else
110
+ raise "invalid character #{atom} in piece placement row."
111
+ end
112
+ }
113
+ unless sum == 8
114
+ raise "row #{row} too large in piece placement field."
115
+ end
116
+ }
117
+ # check active color field is "w" or "b"
118
+ unless tokens[1] =~ /^(w|b)$/
119
+ raise "active color field needs to be w or b."
120
+ end
121
+ # check castling availability field
122
+ unless tokens[2] =~ /^(KQ?k?q?|Qk?q?|kq?|q|-)$/
123
+ raise "castling availability field is incorrect."
124
+ end
125
+ # check enpassent field
126
+ unless tokens[3] =~ /^(-|[abcdefgh][36])$/
127
+ raise "enpassent field is incorrect."
128
+ end
129
+ # check halfmove clock field is >= 0
130
+ unless tokens[4] =~ /\b[0-9]+\b/
131
+ raise "halfmove clock field is incorrect."
132
+ end
133
+ # check fullmove number field is > 0
134
+ unless tokens[5] =~ /\b[0-9]+\b/
135
+ raise "fullmove number field is incorrect."
136
+ end
137
+ if tokens[5].to_i == 0
138
+ raise "fullmove number field cannot be zero."
139
+ end
140
+ true
141
+ end
142
+
143
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pgn2fen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Vinay Doma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: "Converts a single game chess PGN to an array of FEN strings. \nThe FEN
14
+ follows the specification as listed on [Forsyth–Edwards Notation](http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation).\n"
15
+ email: vinay.doma@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - LICENSE
20
+ - README.md
21
+ - TODO
22
+ - VERSION
23
+ files:
24
+ - Gemfile
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - TODO
29
+ - VERSION
30
+ - lib/pgn2fen.rb
31
+ - pgn2fen.gemspec
32
+ - test/pgn2fen_test.rb
33
+ homepage: http://github.com/nivyatech/pgn2fen
34
+ licenses:
35
+ - Ruby
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options:
39
+ - "--charset=UTF-8"
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.6
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Converts a single game chess PGN to an array of FEN strings
58
+ test_files: []