Class: ConsoleShogi::Board
- Inherits:
-
Object
- Object
- ConsoleShogi::Board
- Defined in:
- lib/console_shogi/board.rb
Instance Method Summary collapse
- #deep_copy ⇒ Object
- #fetch_piece(x:, y:) ⇒ Object
-
#initialize(pieces:) ⇒ Board
constructor
A new instance of Board.
- #matrix ⇒ Object
- #move_piece!(from:, to:) ⇒ Object
- #promote_piece!(x:, y:) ⇒ Object
- #put_piece!(piece:, to:) ⇒ Object
- #within_range?(x:, y:) ⇒ Boolean
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_copy ⇒ Object
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 |
#matrix ⇒ Object
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
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 |