Module: FieldBetweenHelpers
- Included in:
- Bchess::Piece
- Defined in:
- lib/bchess/helpers/field_between_helpers.rb
Instance Method Summary collapse
- #column_fields(_dcolumn, drow) ⇒ Object
- #diagonal_fields(dcolumn, drow) ⇒ Object
- #fields_between(dcolumn, drow) ⇒ Object
- #row_fields(dcolumn, _drow) ⇒ Object
Instance Method Details
#column_fields(_dcolumn, drow) ⇒ Object
7 8 9 10 |
# File 'lib/bchess/helpers/field_between_helpers.rb', line 7 def column_fields(_dcolumn, drow) smaller, bigger = [row, drow].sort (smaller + 1..bigger - 1).map { |r| [column, r] } end |
#diagonal_fields(dcolumn, drow) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bchess/helpers/field_between_helpers.rb', line 12 def diagonal_fields(dcolumn, drow) fields = [] if dcolumn > column if drow > row (dcolumn - column - 1).times do |i| fields << [dcolumn - (i + 1), drow - (i + 1)] end else (dcolumn - column - 1).times do |i| fields << [dcolumn - (i + 1), drow + (i + 1)] end end else if drow > row (column - dcolumn - 1).times do |i| fields << [dcolumn + (i + 1), drow - (i + 1)] end else (column - dcolumn - 1).times do |i| fields << [dcolumn + (i + 1), drow + (i + 1)] end end end fields end |
#fields_between(dcolumn, drow) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bchess/helpers/field_between_helpers.rb', line 38 def fields_between(dcolumn, drow) return [] unless can_move_to_field?(dcolumn, drow) if same_row?(drow) row_fields(dcolumn, drow) elsif same_column?(dcolumn) column_fields(dcolumn, drow) elsif same_diagonal?(dcolumn, drow) diagonal_fields(dcolumn, drow) else [] end end |
#row_fields(dcolumn, _drow) ⇒ Object
2 3 4 5 |
# File 'lib/bchess/helpers/field_between_helpers.rb', line 2 def row_fields(dcolumn, _drow) smaller, bigger = [column, dcolumn].sort (smaller + 1..bigger - 1).map { |c| [c, row] } end |