Class: ConsoleShogi::Board

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

Instance Method Summary collapse

Constructor Details

#initialize(pieces:) ⇒ Board

Returns a new instance of Board.



7
8
9
# File 'lib/console_shogi/board.rb', line 7

def initialize(pieces:)
  @pieces = Matrix.rows(pieces)
end

Instance Method Details

#deep_copyObject



15
16
17
# File 'lib/console_shogi/board.rb', line 15

def deep_copy
  Board.new(pieces: pieces.map(&:dup).to_a)
end

#fetch_piece(x:, y:) ⇒ Object



28
29
30
# File 'lib/console_shogi/board.rb', line 28

def fetch_piece(x:, y:)
  pieces[y, x]
end

#matrixObject



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

def matrix
  @pieces
end

#move_piece!(from:, to:) ⇒ Object



40
41
42
43
# File 'lib/console_shogi/board.rb', line 40

def move_piece!(from:, to:)
  @pieces[to[:y], to[:x]] = @pieces[from[:y], from[:x]]
  @pieces[from[:y], from[:x]] = NonePiece.new
end

#promote_piece!(x:, y:) ⇒ Object



32
33
34
# File 'lib/console_shogi/board.rb', line 32

def promote_piece!(x:, y:)
  @pieces[y, x] = pieces[y, x].promote
end

#put_piece!(piece:, to:) ⇒ Object



36
37
38
# File 'lib/console_shogi/board.rb', line 36

def put_piece!(piece:, to:)
  @pieces[to[:y], to[:x]] = piece
end

#within_range?(x:, y:) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
# File 'lib/console_shogi/board.rb', line 19

def within_range?(x:, y:)
  case {x: x, y: y}
  in x: 0..8, y: 0..8
    true
  else
    false
  end
end