jkf 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d818940b6bb6a5173cdff2ccadedeffa608204a7
4
+ data.tar.gz: 68032a91c6c67e182783093b6a30e7a5f2b8cf22
5
+ SHA512:
6
+ metadata.gz: 16d4a5fc1a4d4ce5e200451716c239d18c07e8225e5ba7ddfe719d31863d30f7d57f34f1d9ba8fc21556a20aa974fb51c0cd277357bc3fd10774d21a0f2eae72
7
+ data.tar.gz: b49d9d3c088278ba553d4e06b53f079368877b6ddc5a3da22b407849f77d91fae04a8632d27e586d8d1d99f3064f79f73b92c5b9c95fb77f0772b5124fac8697
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
5
+ bundler_args: --jobs 3 --retry 3
6
+ cache: bundler
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jkf.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,70 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(rails.controllers) do |m|
49
+ [
50
+ rspec.spec.("routing/#{m[1]}_routing"),
51
+ rspec.spec.("controllers/#{m[1]}_controller"),
52
+ rspec.spec.("acceptance/#{m[1]}")
53
+ ]
54
+ end
55
+
56
+ # Rails config changes
57
+ watch(rails.spec_helper) { rspec.spec_dir }
58
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
59
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
60
+
61
+ # Capybara features specs
62
+ watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
63
+ watch(rails.layouts) { |m| rspec.spec.("features/#{m[1]}") }
64
+
65
+ # Turnip features and steps
66
+ watch(%r{^spec/acceptance/(.+)\.feature$})
67
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
68
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
69
+ end
70
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 iyuuya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Jkf
2
+ [![Build Status](https://travis-ci.org/iyuuya/jkf.svg?branch=master)](https://travis-ci.org/iyuuya/jkf)
3
+
4
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jkf`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+
6
+ TODO: Delete this and the text above, and describe your gem
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'jkf'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install jkf
23
+
24
+ ## Usage
25
+
26
+ TODO: Write usage instructions here
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Run `bundle exec jkf` to use the gem in this directory, ignoring other installed copies of this gem.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jkf.
37
+
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
42
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jkf"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ if require "pry"
11
+ Pry.start
12
+ else
13
+ require "irb"
14
+ IRB.start
15
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/jkf.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jkf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jkf"
8
+ spec.version = Jkf::VERSION
9
+ spec.authors = ["iyuuya"]
10
+ spec.email = ["i.yuuya@gmail.com"]
11
+
12
+ spec.summary = %q{jkf/csa/kif/ki2 parser and converter}
13
+ spec.description = %q{converter/parser of records of shogi}
14
+ spec.homepage = "https://github.com/iyuuya/jkf"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "guard-rspec"
26
+ end
@@ -0,0 +1,249 @@
1
+ module Jkf::Converter
2
+ class Ki2
3
+ def convert(jkf)
4
+ hash = if jkf.is_a?(Hash)
5
+ jkf
6
+ else
7
+ JSON.parse(jkf)
8
+ end
9
+ @forks = []
10
+
11
+ result = ''
12
+ result += convert_header(hash['header']) if hash['header']
13
+ result += convert_initial(hash['initial']) if hash['initial']
14
+ result += convert_moves(hash['moves']) if hash['moves']
15
+ if @forks.size > 0
16
+ result += "\n"
17
+ result += @forks.join("\n")
18
+ end
19
+
20
+ result
21
+ end
22
+
23
+ def convert_header(header)
24
+ header.map { |(key, value)| "#{key}:#{value}\n" }.join
25
+ end
26
+
27
+ def convert_initial(initial)
28
+ result = ''
29
+ result += "手合割:#{preset2str(initial["preset"])}\n" if initial["preset"] != "OTHER"
30
+
31
+ data = initial["data"]
32
+
33
+ if data
34
+ if data['color'] == 0
35
+ result += "先手番\n"
36
+ elsif data['color'] == 1
37
+ result += "後手番\n"
38
+ end
39
+
40
+ if data['hands']
41
+ if data['hands'][0]
42
+ result += '先手の持駒:'
43
+ result += convert_motigoma(data['hands'][0])
44
+ end
45
+ if data['hands'][1]
46
+ result += '後手の持駒:'
47
+ result += convert_motigoma(data['hands'][1])
48
+ end
49
+ end
50
+
51
+ if data['board']
52
+ result += " 9 8 7 6 5 4 3 2 1\n"
53
+ result += "+---------------------------+\n"
54
+ 9.times { |y|
55
+ line = "|"
56
+ 9.times { |x|
57
+ line += convert_board_piece(data['board'][8-x][y])
58
+ }
59
+ line += "|#{n2kan(y+1)}\n"
60
+ result += line
61
+ }
62
+ result += "+---------------------------+\n"
63
+ end
64
+ end
65
+
66
+ result
67
+ end
68
+
69
+ def convert_moves(moves, idx=0)
70
+ result = "\n"
71
+ i = 0
72
+ before_split = ''
73
+ moves.each_with_index { |move, j|
74
+ if move['special']
75
+ result += "\n"
76
+ # first_board+speical分を引く(-2)
77
+ result += convert_special(move['special'], j-2+idx) if move['special']
78
+ else
79
+ result += before_split
80
+ if move['move']
81
+ result_move = convert_move(move['move'])
82
+ i += 1
83
+ before_split = if i % 6 == 0
84
+ "\n"
85
+ else
86
+ result_move.size == 4 ? " "*4 : " "*2
87
+ end
88
+ result += result_move
89
+ end
90
+
91
+ if move['comments']
92
+ result += "\n" if result[-1] != "\n"
93
+ result += convert_comments(move['comments'])
94
+ i = 0
95
+ end
96
+
97
+ @forks.unshift convert_forks(move['forks'], j+idx) if move['forks']
98
+ end
99
+ }
100
+ result
101
+ end
102
+
103
+ def convert_move(move)
104
+ result = move['color'] == 0 ? '▲' : '△'
105
+ result += if move['to']
106
+ n2zen(move['to']['x']) + n2kan(move['to']['y'])
107
+ elsif move['same']
108
+ '同 '
109
+ else
110
+ raise "error??"
111
+ end
112
+ result += csa2kind(move['piece'])
113
+ result += '成' if move['promote']
114
+ result += csa2relative(move['relative']) if move['relative']
115
+ result
116
+ end
117
+
118
+ def convert_special(special, index)
119
+ result = "まで#{index+1}手"
120
+
121
+ if special == 'TORYO' || special =~ /ILLEGAL/
122
+ turn = index % 2 == 0 ? '後' : '先'
123
+ result += "で#{turn}手の"
124
+ result += case special
125
+ when "TORYO" then "勝ち"
126
+ when "ILLEGAL_ACTION" then "反則勝ち"
127
+ when "ILLEGAL_MOVE" then "反則負け"
128
+ end
129
+ else
130
+ turn = index % 2 == 0 ? '先' : '後'
131
+ result += case special
132
+ when "TIME_UP" then "で時間切れにより#{turn}手の勝ち"
133
+ when "CHUDAN" then "で中断"
134
+ when "JISHOGI" then "で持将棋"
135
+ when "SENNICHITE" then "で千日手"
136
+ when "TSUMI" then "で詰み"
137
+ when "FUZUMI" then "で不詰"
138
+ end
139
+ end
140
+ result += "\n"
141
+ result
142
+ end
143
+
144
+ def convert_comments(comments)
145
+ comments.map { |comment| "*#{comment}\n" }.join
146
+ end
147
+
148
+ def convert_forks(forks, index)
149
+ result = "\n"
150
+ result = "変化:%4d手"%[index]
151
+ forks.each do |moves|
152
+ result += convert_moves(moves, index)
153
+ end
154
+ result
155
+ end
156
+
157
+ def convert_board_piece(piece)
158
+ result = ''
159
+
160
+ if piece == {}
161
+ result = ' ・'
162
+ else
163
+ result += piece['color'] == 0 ? ' ' : 'v'
164
+ result += csa2kind(piece['kind'])
165
+ end
166
+
167
+ result
168
+ end
169
+
170
+ def convert_motigoma(pieces)
171
+ pieces.map do |piece, num|
172
+ if num > 0
173
+ str = csa2kind(piece)
174
+ if num > 1
175
+ str += n2kan(num/10) if num / 10 > 0
176
+ num %= 10
177
+ str += n2kan(num)
178
+ end
179
+ str
180
+ else
181
+ nil
182
+ end
183
+ end.compact.join(' ') + " \n"
184
+ end
185
+
186
+ protected
187
+
188
+ def n2zen(n)
189
+ "0123456789"[n]
190
+ end
191
+
192
+ def n2kan(n)
193
+ "〇一二三四五六七八九"[n]
194
+ end
195
+
196
+ def csa2kind(csa)
197
+ {
198
+ "FU" => "歩",
199
+ "KY" => "香",
200
+ "KE" => "桂",
201
+ "GI" => "銀",
202
+ "KI" => "金",
203
+ "KA" => "角",
204
+ "HI" => "飛",
205
+ "OU" => "玉",
206
+ "TO" => "と",
207
+ "NY" => "成香",
208
+ "NK" => "成桂",
209
+ "NG" => "成銀",
210
+ "UM" => "馬",
211
+ "RY" => "龍",
212
+ }[csa]
213
+ end
214
+
215
+ def preset2str(preset)
216
+ {
217
+ "HIRATE" => "平手",
218
+ "KY" => "香落ち",
219
+ "KY_R" => "右香落ち",
220
+ "KA" => "角落ち",
221
+ "HI" => "飛車落ち",
222
+ "HIKY" => "飛香落ち",
223
+ "2" => "二枚落ち",
224
+ "3" => "三枚落ち",
225
+ "4" => "四枚落ち",
226
+ "5" => "五枚落ち",
227
+ "5_L" => "左五枚落ち",
228
+ "6" => "六枚落ち",
229
+ "8" => "八枚落ち",
230
+ "10" => "十枚落ち",
231
+ "OTHER" => "その他"
232
+ }[preset]
233
+ end
234
+
235
+ def csa2relative(relative)
236
+ case relative
237
+ when 'L' then '左'
238
+ when 'C' then '直'
239
+ when 'R' then '右'
240
+ when 'U' then '上'
241
+ when 'M' then '寄'
242
+ when 'D' then '引'
243
+ when 'H' then '打'
244
+ else
245
+ ''
246
+ end
247
+ end
248
+ end
249
+ end