Class: Shogi::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/shogi/board.rb

Defined Under Namespace

Classes: CodingError, Error, FormatError, MoveError, MovementError, UndefinedPieceError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csa = nil) ⇒ Board

Returns a new instance of Board.



11
12
13
14
15
16
17
18
19
# File 'lib/shogi/board.rb', line 11

def initialize(csa=nil)
  if csa
    set_from_csa(csa)
  else
    @position = default_position
    @captured = []
  end
  @validate_movement = true
end

Instance Attribute Details

#validate_movementObject

Returns the value of attribute validate_movement.



10
11
12
# File 'lib/shogi/board.rb', line 10

def validate_movement
  @validate_movement
end

Instance Method Details

#move_from_csa(csa) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/shogi/board.rb', line 110

def move_from_csa(csa)
  unless /\A[+-](00|[1-9]{2})[1-9]{2}[A-Z]{2}\z/ =~ csa
    raise FormatError, "Wrong CSA format: #{csa}"
  end

  unless Piece.const_defined?(csa[5..6])
    raise UndefinedPieceError, "Undefined piece: #{csa[5..6]}"
  end

  if csa[1..2] == "00"
    before_piece = csa[0] + csa[5..6]
    unless @captured.include?(before_piece)
      raise MoveError, "Not captured piece: #{csa}"
    end
    before_cell = before_piece
    before_piece = eval("Piece::#{before_cell[1..2]}").new
  else
    before_x = 9 - csa[1].to_i
    before_y = csa[2].to_i - 1
    before_cell = @position[before_y][before_x]
    if before_cell == ""
      raise MoveError, "Before cell is blank"
    end
    before_piece = eval("Piece::#{before_cell[1..2]}").new

    unless csa[0] == before_cell[0]
      raise MoveError, "Don't your piece: #{before_cell}"
    end
    unless csa[5..6] == before_cell[1..2]
      after_piece = eval("Piece::#{csa[5..6]}").new
      unless before_piece.promoter == after_piece.class
      raise MoveError, "Don't promote: #{before_cell[1..2]} -> #{csa[5..6]}"
      end

      after_y = csa[4].to_i - 1
      if csa[0] == "+"
        unless after_y < 3 || before_y < 3
          raise_movement_error("Don't promote line: #{csa}")
        end
      else
        unless after_y > 6 || before_y > 6
          raise_movement_error("Don't promote line: #{csa}")
        end
      end
    end
  end

  after_x = 9 - csa[3].to_i
  after_y = csa[4].to_i - 1
  after_cell = @position[after_y][after_x]
  if csa[0] == after_cell[0]
    raise MoveError, "Your piece on after cell: #{csa}"
  end

  if csa[1..2] == "00"
    unless after_cell == ""
      raise MoveError, "Exist piece on after cell"
    end
  else
    if csa[0] == "+"
      movement_x = after_x - before_x
      movement_y = before_y - after_y
    else
      movement_x = before_x - after_x
      movement_y = after_y - before_y
    end

    unless before_piece.move?(movement_x, movement_y)
      raise_movement_error("Invalid movement")
    end
  end

  unless after_cell == ""
    after_piece = eval("Piece::#{after_cell[1..2]}").new
    if after_piece.class.const_defined?(:CHILD)
      @captured << "#{csa[0]}#{after_piece.class::CHILD}"
    else
      @captured << "#{csa[0]}#{after_cell[1..2]}"
    end
  end
  @position[after_y][after_x] = "#{csa[0]}#{csa[5..6]}"

  if csa[1..2] == "00"
    used = nil

    @captured.each_with_index do |captured_piece, i|
      if captured_piece == before_cell
        used = @captured.delete_at(i)
        break
      end
    end

    unless used == before_cell
      raise CodingError, "[Bug] missing piece in captured"
    end
  else
    @position[before_y][before_x] = ""
  end

  true
end

#move_from_csa_lines(csa_lines) ⇒ Object



212
213
214
215
216
217
# File 'lib/shogi/board.rb', line 212

def move_from_csa_lines(csa_lines)
  csa_lines.each_line do |csa|
    csa.chomp!
    move_from_csa(csa)
  end
end

#set_from_csa(csa) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shogi/board.rb', line 51

def set_from_csa(csa)
  position = []
  cell_pattern = '[+-][A-Z]{2}| \* '
  csa_lines = csa.each_line.to_a
  csa_lines.slice(0, 9).to_enum.with_index do |row, i|
    position_row = []
    row.chomp!
    unless /\AP#{i + 1}(#{cell_pattern}){9}\z/ =~ row
      raise FormatError, "Format Error: line #{i + 1}"
    end
    row[2..28].scan(/#{cell_pattern}/) do |cell|
      position_row << cell
    end
    position << position_row
  end
  @position = position

  captured = []
  csa_lines.slice(9, 2).each do |captured_line|
    captured_line.chomp!
    unless /\AP[+-](00[A-Z]{2})*\z/ =~ captured_line
      raise FormatError, "Format Error: captured line"
    end
    turn = captured_line[1]
    captured_line[2..-1].scan(/00([A-Z]{2})/) do |cell|
      captured << turn + cell[0]
    end
  end
  @captured = captured
end

#to_csaObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shogi/board.rb', line 21

def to_csa
  csa_rows = ""

  @position.each_with_index do |row, i|
    csa_row = ""
    row.each do |cell|
      if cell == ""
        csa_row << " * "
      else
        csa_row << cell
      end
    end
    csa_rows << "P#{i + 1}#{csa_row}\n"
  end

  sente = "P+"
  gote = "P-"
  @captured.each do |piece|
    if piece[0] == "+"
      sente << "00#{piece[1..2]}"
    else
      gote << "00#{piece[1..2]}"
    end
  end
  csa_rows << "#{sente}\n"
  csa_rows << "#{gote}\n"

  csa_rows
end

#to_usiObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/shogi/board.rb', line 82

def to_usi
  @position.map {|row|
    usi_row = ""
    space_count = 0
    row.each do |cell|
      if cell == ""
        space_count += 1
      else
        if space_count > 0
          usi_row << space_count.to_s
          space_count = 0
        end
        usi = eval("Piece::#{cell[1..2]}").new.usi
        if cell[0] == "-"
          usi_row << usi.downcase
        else
          usi_row << usi
        end
      end
    end
    if space_count > 0
      usi_row << space_count.to_s
      space_count = 0
    end
    usi_row
  }.join("/") << "\n"
end