shogi-ruby 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 469ae5eee31baa8c42df5bf289836bbc8656ef35
4
- data.tar.gz: 987b2fd2abe8c56684eb1039bbdf5498a8909ffe
3
+ metadata.gz: 1d5a3884fbd500566d19348e3068232b1e0b2daa
4
+ data.tar.gz: 19c5fd3246ff70a0dffd7ff6ec6719bdb5605150
5
5
  SHA512:
6
- metadata.gz: 3c4248c0be25827324230f8cde9d3f04a08c5e1d82344cb2ad6a4508eb9da1bae16222980129e9e86b22eed971c3face6e3de28a8613209f253fd0adce003fd3
7
- data.tar.gz: 1ddb6295dabc9ad55c97d9471060e6674cffce6d6bd2503f8954d935a840dc5c870be07fd29776846a4ccdd7bb66aa5f397a09a9acdbb016dcdf6fe998a9e975
6
+ metadata.gz: 3690d7afaa7f2305d4c892f98f636831f98c9c01fd96c78f1f6802b4a1318c20f5f7c19e7372401dfd869dcad1dfaf3cf694dbde9a9602b7d1c9975d96861226
7
+ data.tar.gz: 419b0e66515dd7c6ed60e982a469b880e9ee2a115d02f3c63c03ee370767467bcf5dc82f06307ff7c9e8179fadf926f6891a041c48abcf76547a8230295cb3bd
data/README.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # Shogi Library for Ruby [![Build Status](https://secure.travis-ci.org/myokoym/shogi-ruby.png?branch=master)](http://travis-ci.org/myokoym/shogi-ruby)
2
2
 
3
+ Ruby用の将棋ライブラリです。
4
+
5
+ ## 用途
6
+ ### 向いていること
7
+ * 将棋盤の簡単なコンソール表示
8
+ * GUIアプリケーションの内部状態の管理
9
+
10
+ ### 向いていないこと
11
+ * 速度的に、思考エンジンのデータ構造には向きません。
12
+
13
+ ## 機能
14
+ ### できること
15
+ * CSA形式で、将棋盤オブジェクトを作成、操作、表示できます。
16
+ * 駒の動きが正しいかどうかチェックできます。
17
+
18
+ ### まだできないこと
19
+ * 二歩チェック
20
+ * 行きどころのない駒のチェック
21
+ * 詰みチェック
22
+ * 手番の管理
23
+ * 棋譜の管理
24
+ * USI形式との相互変換
25
+
3
26
  ## Installation
4
27
 
5
28
  Add this line to your application's Gemfile:
@@ -36,7 +59,7 @@ Ruby 2.0.0 or later. (and 1.9.3)
36
59
  P+
37
60
  P-
38
61
 
39
- board.move_from_csa("+7776FU")
62
+ board.move("+7776FU", :csa)
40
63
  puts board.to_csa
41
64
  #=> P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
42
65
  P2 * -HI * * * * * -KA *
@@ -50,9 +73,9 @@ Ruby 2.0.0 or later. (and 1.9.3)
50
73
  P+
51
74
  P-
52
75
 
53
- board.move_from_csa("-3334FU")
54
- board.move_from_csa("+8822UM")
55
- board.to_csa
76
+ board.move("-3334FU", :csa)
77
+ board.move("+8822UM", :csa)
78
+ puts board.to_csa
56
79
  #=> P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
57
80
  P2 * -HI * * * * * +UM *
58
81
  P3-FU-FU-FU-FU-FU-FU * -FU-FU
@@ -65,9 +88,10 @@ Ruby 2.0.0 or later. (and 1.9.3)
65
88
  P+00KA
66
89
  P-
67
90
 
68
- board.move_from_csa("-3122GI")
69
- board.move_from_csa("+0055KA")
70
- board.to_csa
91
+ board.default_format = :csa
92
+ board.move("-3122GI")
93
+ board.move("+0055KA")
94
+ board.show
71
95
  #=> P1-KY-KE-GI-KI-OU-KI * -KE-KY
72
96
  P2 * -HI * * * * * -GI *
73
97
  P3-FU-FU-FU-FU-FU-FU * -FU-FU
data/lib/shogi/board.rb CHANGED
@@ -7,10 +7,12 @@ module Shogi
7
7
  class MoveError < Error; end
8
8
  class MovementError < Error; end
9
9
 
10
+ attr_accessor :default_format
10
11
  attr_accessor :validate_movement
11
- def initialize(csa=nil)
12
- if csa
13
- set_from_csa(csa)
12
+ def initialize(default_format=:csa, position=nil)
13
+ @default_format = default_format
14
+ if position
15
+ set_from_csa(position)
14
16
  else
15
17
  @position = default_position
16
18
  @captured = []
@@ -56,10 +58,14 @@ module Shogi
56
58
  position_row = []
57
59
  row.chomp!
58
60
  unless /\AP#{i + 1}(#{cell_pattern}){9}\z/ =~ row
59
- raise FormatError, "Format Error: line #{i + 1}"
61
+ raise FormatError, "Format Error: line P#{i + 1}"
60
62
  end
61
63
  row[2..28].scan(/#{cell_pattern}/) do |cell|
62
- position_row << cell
64
+ if cell == " * "
65
+ position_row << ""
66
+ else
67
+ position_row << cell
68
+ end
63
69
  end
64
70
  position << position_row
65
71
  end
@@ -69,7 +75,7 @@ module Shogi
69
75
  csa_lines.slice(9, 2).each do |captured_line|
70
76
  captured_line.chomp!
71
77
  unless /\AP[+-](00[A-Z]{2})*\z/ =~ captured_line
72
- raise FormatError, "Format Error: captured line"
78
+ raise FormatError, "Format Error: captured piece line"
73
79
  end
74
80
  turn = captured_line[1]
75
81
  captured_line[2..-1].scan(/00([A-Z]{2})/) do |cell|
@@ -91,7 +97,7 @@ module Shogi
91
97
  usi_row << space_count.to_s
92
98
  space_count = 0
93
99
  end
94
- usi = eval("Piece::#{cell[1..2]}").new.usi
100
+ usi = Piece.const_get(cell[1..2]).new.usi
95
101
  if cell[0] == "-"
96
102
  usi_row << usi.downcase
97
103
  else
@@ -107,7 +113,37 @@ module Shogi
107
113
  }.join("/") << "\n"
108
114
  end
109
115
 
110
- def move_from_csa(csa)
116
+ def move_from_csa(movement)
117
+ $stderr.puts "warning: Shogi::Board#move_from_csa(movement) is deplicated. Use Shogi::Board#move(movement, :csa)"
118
+ move(movement, :csa)
119
+ end
120
+
121
+ def move(movement_lines, format=@default_format)
122
+ movement_lines.each_line do |movement|
123
+ movement.chomp!
124
+ __send__("move_by_#{format.to_s}", movement)
125
+ end
126
+ self
127
+ end
128
+
129
+ def show(format=@default_format)
130
+ $stdout.puts __send__("to_#{format}")
131
+ end
132
+
133
+ private
134
+ def default_position
135
+ [["-KY", "-KE", "-GI", "-KI", "-OU", "-KI", "-GI", "-KE", "-KY"],
136
+ [ "", "-HI", "", "", "", "", "", "-KA", ""],
137
+ ["-FU", "-FU", "-FU", "-FU", "-FU", "-FU", "-FU", "-FU", "-FU"],
138
+ [ "", "", "", "", "", "", "", "", ""],
139
+ [ "", "", "", "", "", "", "", "", ""],
140
+ [ "", "", "", "", "", "", "", "", ""],
141
+ ["+FU", "+FU", "+FU", "+FU", "+FU", "+FU", "+FU", "+FU", "+FU"],
142
+ [ "", "+KA", "", "", "", "", "", "+HI", ""],
143
+ ["+KY", "+KE", "+GI", "+KI", "+OU", "+KI", "+GI", "+KE", "+KY"]]
144
+ end
145
+
146
+ def move_by_csa(csa)
111
147
  unless /\A[+-](00|[1-9]{2})[1-9]{2}[A-Z]{2}\z/ =~ csa
112
148
  raise FormatError, "Wrong CSA format: #{csa}"
113
149
  end
@@ -119,10 +155,10 @@ module Shogi
119
155
  if csa[1..2] == "00"
120
156
  before_piece = csa[0] + csa[5..6]
121
157
  unless @captured.include?(before_piece)
122
- raise MoveError, "Not captured piece: #{csa}"
158
+ raise MoveError, "Not captured piece: #{before_piece}"
123
159
  end
124
160
  before_cell = before_piece
125
- before_piece = eval("Piece::#{before_cell[1..2]}").new
161
+ before_piece = Piece.const_get(before_cell[1..2]).new
126
162
  else
127
163
  before_x = 9 - csa[1].to_i
128
164
  before_y = csa[2].to_i - 1
@@ -130,13 +166,13 @@ module Shogi
130
166
  if before_cell == ""
131
167
  raise MoveError, "Before cell is blank"
132
168
  end
133
- before_piece = eval("Piece::#{before_cell[1..2]}").new
169
+ before_piece = Piece.const_get(before_cell[1..2]).new
134
170
 
135
171
  unless csa[0] == before_cell[0]
136
- raise MoveError, "Don't your piece: #{before_cell}"
172
+ raise MoveError, "Not your piece: #{before_cell}"
137
173
  end
138
174
  unless csa[5..6] == before_cell[1..2]
139
- after_piece = eval("Piece::#{csa[5..6]}").new
175
+ after_piece = Piece.const_get(csa[5..6]).new
140
176
  unless before_piece.promoter == after_piece.class
141
177
  raise MoveError, "Don't promote: #{before_cell[1..2]} -> #{csa[5..6]}"
142
178
  end
@@ -144,11 +180,11 @@ module Shogi
144
180
  after_y = csa[4].to_i - 1
145
181
  if csa[0] == "+"
146
182
  unless after_y < 3 || before_y < 3
147
- raise_movement_error("Don't promote line: #{csa}")
183
+ raise_movement_error("Don't promote this move: #{csa}")
148
184
  end
149
185
  else
150
186
  unless after_y > 6 || before_y > 6
151
- raise_movement_error("Don't promote line: #{csa}")
187
+ raise_movement_error("Don't promote this move: #{csa}")
152
188
  end
153
189
  end
154
190
  end
@@ -175,12 +211,12 @@ module Shogi
175
211
  end
176
212
 
177
213
  unless before_piece.move?(movement_x, movement_y)
178
- raise_movement_error("Invalid movement")
214
+ raise_movement_error("Invalid movement: #{csa}")
179
215
  end
180
216
  end
181
217
 
182
218
  unless after_cell == ""
183
- after_piece = eval("Piece::#{after_cell[1..2]}").new
219
+ after_piece = Piece.const_get(after_cell[1..2]).new
184
220
  if after_piece.class.const_defined?(:CHILD)
185
221
  @captured << "#{csa[0]}#{after_piece.class::CHILD}"
186
222
  else
@@ -206,27 +242,7 @@ module Shogi
206
242
  @position[before_y][before_x] = ""
207
243
  end
208
244
 
209
- true
210
- end
211
-
212
- def move_from_csa_lines(csa_lines)
213
- csa_lines.each_line do |csa|
214
- csa.chomp!
215
- move_from_csa(csa)
216
- end
217
- end
218
-
219
- private
220
- def default_position
221
- [["-KY", "-KE", "-GI", "-KI", "-OU", "-KI", "-GI", "-KE", "-KY"],
222
- [ "", "-HI", "", "", "", "", "", "-KA", ""],
223
- ["-FU", "-FU", "-FU", "-FU", "-FU", "-FU", "-FU", "-FU", "-FU"],
224
- [ "", "", "", "", "", "", "", "", ""],
225
- [ "", "", "", "", "", "", "", "", ""],
226
- [ "", "", "", "", "", "", "", "", ""],
227
- ["+FU", "+FU", "+FU", "+FU", "+FU", "+FU", "+FU", "+FU", "+FU"],
228
- [ "", "+KA", "", "", "", "", "", "+HI", ""],
229
- ["+KY", "+KE", "+GI", "+KI", "+OU", "+KI", "+GI", "+KE", "+KY"]]
245
+ self
230
246
  end
231
247
 
232
248
  def raise_movement_error(message)
data/lib/shogi/game.rb ADDED
@@ -0,0 +1,37 @@
1
+ module Shogi
2
+ class Game
3
+ attr_accessor :default_format
4
+ attr_reader :turn
5
+ def initialize(format=:csa, turn="+", kifu=[])
6
+ raise ArgumentError, "Undefined format: #{format}" unless /\Acsa\z/ =~ format
7
+ raise ArgumentError, "Invalid turn: #{turn}" unless /\A[+-]\z/ =~ turn
8
+
9
+ @default_format = format
10
+ @board = Shogi::Board.new(@default_format)
11
+ @turn = turn
12
+ @kifu = kifu
13
+ end
14
+
15
+ def to_csa
16
+ @board.to_csa << turn << "\n"
17
+ end
18
+
19
+ def move(movement_lines, format=@default_format)
20
+ movement_lines.each_line do |movement|
21
+ movement.chomp!
22
+ @kifu << movement
23
+ @board.move(movement, format)
24
+ @turn = (@turn == "+") ? "-" : "+"
25
+ end
26
+ self
27
+ end
28
+
29
+ def kifu
30
+ @kifu.join("\n") << "\n"
31
+ end
32
+
33
+ def show(format=@default_format)
34
+ $stdout.puts __send__("to_#{format}")
35
+ end
36
+ end
37
+ end
data/lib/shogi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Shogi
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/shogi.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "shogi/version"
2
2
  require "shogi/board"
3
3
  require "shogi/piece"
4
+ require "shogi/game"
4
5
 
5
6
  module Shogi
6
7
  end
data/test/test-board.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "shogi/board"
2
+ require "stringio"
2
3
 
3
4
  class BoardTest < Test::Unit::TestCase
4
5
  def setup
@@ -12,7 +13,7 @@ class BoardTest < Test::Unit::TestCase
12
13
  end
13
14
 
14
15
  def test_initialize_csa
15
- csa = <<-EOT
16
+ position = <<-EOT
16
17
  P1 * * * * +HI * * -KE *
17
18
  P2 * * * * * +KA-OU * -KY
18
19
  P3 * * * * * * -FU-FU-FU
@@ -25,8 +26,24 @@ P9 * * * * * * * * *
25
26
  P+00HI00GI00KE
26
27
  P-
27
28
  EOT
28
- @board = Shogi::Board.new(csa)
29
- assert_equal(csa, @board.to_csa)
29
+ @board = Shogi::Board.new(:csa, position)
30
+ assert_equal(position, @board.to_csa)
31
+ assert_nothing_raised do
32
+ @board.move("+0031HI")
33
+ end
34
+ assert_equal(<<-EOT, @board.to_csa)
35
+ P1 * * * * +HI * +HI-KE *
36
+ P2 * * * * * +KA-OU * -KY
37
+ P3 * * * * * * -FU-FU-FU
38
+ P4 * * * * +KY * * -GI *
39
+ P5 * * * * * * * * *
40
+ P6 * * * * * * * * *
41
+ P7 * * * * * * * * *
42
+ P8 * * * * * * * * *
43
+ P9 * * * * * * * * *
44
+ P+00GI00KE
45
+ P-
46
+ EOT
30
47
  end
31
48
 
32
49
  def test_to_csa
@@ -47,6 +64,32 @@ P-
47
64
  assert_equal(before_state, @board.instance_variable_get(:@position))
48
65
  end
49
66
 
67
+ def test_show
68
+ before_state = @board.instance_variable_get(:@position).dup
69
+ s = ""
70
+ io = StringIO.new(s)
71
+ $stdout = io
72
+ @board.default_format = :csa
73
+
74
+ @board.show
75
+
76
+ $stdout = STDOUT
77
+ assert_equal(<<-EOT, s)
78
+ P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
79
+ P2 * -HI * * * * * -KA *
80
+ P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
81
+ P4 * * * * * * * * *
82
+ P5 * * * * * * * * *
83
+ P6 * * * * * * * * *
84
+ P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
85
+ P8 * +KA * * * * * +HI *
86
+ P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
87
+ P+
88
+ P-
89
+ EOT
90
+ assert_equal(before_state, @board.instance_variable_get(:@position))
91
+ end
92
+
50
93
  def test_set_from_csa
51
94
  csa = <<-EOT
52
95
  P1 * * * * +HI * * -KE *
@@ -63,6 +106,9 @@ P-
63
106
  EOT
64
107
  @board.set_from_csa(csa)
65
108
  assert_equal(csa, @board.to_csa)
109
+ assert_nothing_raised do
110
+ @board.move("+0031HI", :csa)
111
+ end
66
112
  end
67
113
 
68
114
  def test_to_usi
@@ -73,31 +119,37 @@ lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL
73
119
  assert_equal(before_state, @board.instance_variable_get(:@position))
74
120
  end
75
121
 
76
- def test_move_from_csa
122
+ def test_move_csa
123
+ @board.default_format = :csa
124
+
77
125
  assert_raise Shogi::Board::FormatError do
78
- @board.move_from_csa("+27FU")
126
+ @board.move("+27FU")
79
127
  end
80
128
  assert_raise Shogi::Board::UndefinedPieceError do
81
- @board.move_from_csa("+2726AA")
129
+ @board.move("+2726AA")
82
130
  end
83
131
  assert_raise Shogi::Board::MoveError do
84
- assert_false(@board.move_from_csa("+2726HI"))
132
+ assert_false(@board.move("+2726HI"))
85
133
  end
86
134
  assert_raise Shogi::Board::MoveError do
87
- assert_false(@board.move_from_csa("+2827HI"))
135
+ assert_false(@board.move("+2827HI"))
88
136
  end
89
137
  assert_raise Shogi::Board::MoveError do
90
- assert_false(@board.move_from_csa("+2625FU"))
138
+ assert_false(@board.move("+2625FU"))
91
139
  end
92
140
  assert_raise Shogi::Board::MovementError do
93
- assert_false(@board.move_from_csa("+2725FU"))
141
+ assert_false(@board.move("+2725FU"))
94
142
  end
95
143
  assert_raise Shogi::Board::MoveError do
96
- assert_false(@board.move_from_csa("-4131KI"))
144
+ assert_false(@board.move("-4131KI"))
97
145
  end
98
- assert_true(@board.move_from_csa("+7776FU"))
99
- assert_true(@board.move_from_csa("-4132KI"))
100
- assert_true(@board.move_from_csa("+2868HI"))
146
+
147
+ assert_nothing_raised do
148
+ @board.move("+7776FU")
149
+ @board.move("-4132KI")
150
+ @board.move("+2868HI")
151
+ end
152
+
101
153
  assert_equal(<<-EOT, @board.to_csa)
102
154
  P1-KY-KE-GI-KI-OU * -GI-KE-KY
103
155
  P2 * -HI * * * * -KI-KA *
@@ -113,11 +165,32 @@ P-
113
165
  EOT
114
166
  end
115
167
 
116
- def test_move_from_csa_at_captured
117
- @board.move_from_csa("+7776FU")
118
- @board.move_from_csa("-3334FU")
119
- assert_true(@board.move_from_csa("+8822KA"))
120
- assert_true(@board.move_from_csa("-3122GI"))
168
+ def test_move_csa_chain
169
+ assert_nothing_raised do
170
+ @board.move("+7776FU", :csa).move("-4132KI", :csa)
171
+ end
172
+ assert_equal(<<-EOT, @board.to_csa)
173
+ P1-KY-KE-GI-KI-OU * -GI-KE-KY
174
+ P2 * -HI * * * * -KI-KA *
175
+ P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
176
+ P4 * * * * * * * * *
177
+ P5 * * * * * * * * *
178
+ P6 * * +FU * * * * * *
179
+ P7+FU+FU * +FU+FU+FU+FU+FU+FU
180
+ P8 * +KA * * * * * +HI *
181
+ P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
182
+ P+
183
+ P-
184
+ EOT
185
+ end
186
+
187
+ def test_move_csa_at_captured
188
+ assert_nothing_raised do
189
+ @board.move("+7776FU", :csa)
190
+ @board.move("-3334FU", :csa)
191
+ @board.move("+8822KA", :csa)
192
+ @board.move("-3122GI", :csa)
193
+ end
121
194
  assert_equal(<<-EOT, @board.to_csa)
122
195
  P1-KY-KE-GI-KI-OU-KI * -KE-KY
123
196
  P2 * -HI * * * * * -GI *
@@ -131,7 +204,9 @@ P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
131
204
  P+00KA
132
205
  P-00KA
133
206
  EOT
134
- assert_true(@board.move_from_csa("+0055KA"))
207
+ assert_nothing_raised do
208
+ @board.move("+0055KA", :csa)
209
+ end
135
210
  assert_equal(<<-EOT, @board.to_csa)
136
211
  P1-KY-KE-GI-KI-OU-KI * -KE-KY
137
212
  P2 * -HI * * * * * -GI *
@@ -147,13 +222,15 @@ P-00KA
147
222
  EOT
148
223
  end
149
224
 
150
- def test_move_from_csa_promote
151
- @board.move_from_csa("+7776FU")
152
- @board.move_from_csa("-3334FU")
225
+ def test_move_csa_promote
226
+ @board.move("+7776FU", :csa)
227
+ @board.move("-3334FU", :csa)
153
228
  assert_raise Shogi::Board::MovementError do
154
- assert_false(@board.move_from_csa("+2726TO"))
229
+ assert_false(@board.move("+2726TO", :csa))
230
+ end
231
+ assert_nothing_raised do
232
+ @board.move("+8822UM", :csa)
155
233
  end
156
- assert_true(@board.move_from_csa("+8822UM"))
157
234
  assert_equal(<<-EOT, @board.to_csa)
158
235
  P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
159
236
  P2 * -HI * * * * * +UM *
@@ -167,7 +244,9 @@ P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
167
244
  P+00KA
168
245
  P-
169
246
  EOT
170
- assert_true(@board.move_from_csa("-3122GI"))
247
+ assert_nothing_raised do
248
+ @board.move("-3122GI", :csa)
249
+ end
171
250
  assert_equal(<<-EOT, @board.to_csa)
172
251
  P1-KY-KE-GI-KI-OU-KI * -KE-KY
173
252
  P2 * -HI * * * * * -GI *
@@ -181,10 +260,12 @@ P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
181
260
  P+00KA
182
261
  P-00KA
183
262
  EOT
184
- assert_true(@board.move_from_csa("+0033KA"))
185
- assert_true(@board.move_from_csa("-0078KA"))
186
- assert_true(@board.move_from_csa("+3366UM"))
187
- assert_true(@board.move_from_csa("-7867UM"))
263
+ assert_nothing_raised do
264
+ @board.move("+0033KA", :csa)
265
+ @board.move("-0078KA", :csa)
266
+ @board.move("+3366UM", :csa)
267
+ @board.move("-7867UM", :csa)
268
+ end
188
269
  assert_equal(<<-EOT, @board.to_csa)
189
270
  P1-KY-KE-GI-KI-OU-KI * -KE-KY
190
271
  P2 * -HI * * * * * -GI *
@@ -200,7 +281,7 @@ P-00FU
200
281
  EOT
201
282
  end
202
283
 
203
- def test_move_from_csa_lines
284
+ def test_move_csa_lines
204
285
  csa_lines = <<-EOT
205
286
  +7776FU
206
287
  -3334FU
@@ -209,7 +290,7 @@ P-00FU
209
290
  +0055KA
210
291
  EOT
211
292
 
212
- @board.move_from_csa_lines(csa_lines)
293
+ @board.move(csa_lines, :csa)
213
294
 
214
295
  assert_equal(<<-EOT, @board.to_csa)
215
296
  P1-KY-KE-GI-KI-OU-KI * -KE-KY
@@ -230,6 +311,8 @@ P-00KA
230
311
  assert_true(@board.validate_movement)
231
312
  @board.validate_movement = false
232
313
  assert_false(@board.validate_movement)
233
- assert_true(@board.move_from_csa("+2755FU"))
314
+ assert_nothing_raised do
315
+ @board.move("+2755FU", :csa)
316
+ end
234
317
  end
235
318
  end
data/test/test-game.rb ADDED
@@ -0,0 +1,61 @@
1
+ require "shogi/game"
2
+
3
+ class GameTest < Test::Unit::TestCase
4
+ def setup
5
+ @game = Shogi::Game.new
6
+ end
7
+
8
+ def test_initialize
9
+ assert_equal(:csa, @game.default_format)
10
+ assert_equal("+", @game.turn)
11
+ assert_equal([], @game.instance_variable_get(:@kifu))
12
+ end
13
+
14
+ def test_to_csa
15
+ assert_equal(<<-EOT, @game.to_csa)
16
+ P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
17
+ P2 * -HI * * * * * -KA *
18
+ P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
19
+ P4 * * * * * * * * *
20
+ P5 * * * * * * * * *
21
+ P6 * * * * * * * * *
22
+ P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
23
+ P8 * +KA * * * * * +HI *
24
+ P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
25
+ P+
26
+ P-
27
+ +
28
+ EOT
29
+ end
30
+
31
+ def test_move
32
+ @game.default_format = :csa
33
+
34
+ assert_nothing_raised do
35
+ @game.move("+7776FU")
36
+ @game.move("-4132KI")
37
+ @game.move("+2868HI")
38
+ end
39
+
40
+ assert_equal(<<-EOT, @game.to_csa)
41
+ P1-KY-KE-GI-KI-OU * -GI-KE-KY
42
+ P2 * -HI * * * * -KI-KA *
43
+ P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
44
+ P4 * * * * * * * * *
45
+ P5 * * * * * * * * *
46
+ P6 * * +FU * * * * * *
47
+ P7+FU+FU * +FU+FU+FU+FU+FU+FU
48
+ P8 * +KA * +HI * * * * *
49
+ P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
50
+ P+
51
+ P-
52
+ -
53
+ EOT
54
+
55
+ assert_equal(<<-EOT, @game.kifu)
56
+ +7776FU
57
+ -4132KI
58
+ +2868HI
59
+ EOT
60
+ end
61
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shogi-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masafumi Yokoyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-14 00:00:00.000000000 Z
11
+ date: 2013-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
@@ -96,6 +96,7 @@ files:
96
96
  - lib/shogi.rb
97
97
  - lib/shogi/board.rb
98
98
  - lib/shogi/cell.rb
99
+ - lib/shogi/game.rb
99
100
  - lib/shogi/piece.rb
100
101
  - lib/shogi/piece/base.rb
101
102
  - lib/shogi/piece/promoter.rb
@@ -104,6 +105,7 @@ files:
104
105
  - test/run-test.rb
105
106
  - test/test-board.rb
106
107
  - test/test-cell.rb
108
+ - test/test-game.rb
107
109
  - test/test-piece.rb
108
110
  homepage: https://github.com/myokoym/shogi-ruby
109
111
  licenses:
@@ -133,5 +135,6 @@ test_files:
133
135
  - test/run-test.rb
134
136
  - test/test-board.rb
135
137
  - test/test-cell.rb
138
+ - test/test-game.rb
136
139
  - test/test-piece.rb
137
140
  has_rdoc: