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(default_format = :csa, position = nil) ⇒ Board

Returns a new instance of Board.



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

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

Instance Attribute Details

#default_formatObject

Returns the value of attribute default_format.



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

def default_format
  @default_format
end

#validate_movementObject

Returns the value of attribute validate_movement.



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

def validate_movement
  @validate_movement
end

Instance Method Details

#at(place) ⇒ Object



129
130
131
132
133
# File 'lib/shogi/board.rb', line 129

def at(place)
  array_x = to_array_x_from_shogi_x(place[0].to_i)
  array_y = to_array_y_from_shogi_y(place[1].to_i)
  @position[array_y][array_x]
end

#move(movement_lines, format = @default_format) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/shogi/board.rb', line 121

def move(movement_lines, format=@default_format)
  movement_lines.each_line do |movement|
    movement.chomp!
    __send__("move_by_#{format.to_s}", movement)
  end
  self
end

#move_from_csa(movement) ⇒ Object



116
117
118
119
# File 'lib/shogi/board.rb', line 116

def move_from_csa(movement)
  $stderr.puts "warning: Shogi::Board#move_from_csa(movement) is deprecated. Use Shogi::Board#move(movement, :csa)"
  move(movement, :csa)
end

#set_from_csa(csa) ⇒ Object



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
81
82
83
84
85
86
# File 'lib/shogi/board.rb', line 53

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 P#{i + 1}"
    end
    row[2..28].scan(/#{cell_pattern}/) do |cell|
      if cell == " * "
        position_row << ""
      else
        position_row << cell
      end
    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 piece 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

#show(format = @default_format) ⇒ Object



135
136
137
# File 'lib/shogi/board.rb', line 135

def show(format=@default_format)
  $stdout.puts __send__("to_#{format}")
end

#to_csaObject



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
50
51
# File 'lib/shogi/board.rb', line 23

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/shogi/board.rb', line 88

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 = Piece.const_get(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