Module: Boards::Grid

Included in:
Chess::Board, Connect4::Board
Defined in:
lib/boardgame_engine/board_modules.rb

Overview

Boards laid out in grid format

Instance Method Summary collapse

Instance Method Details

#clear_diag_path?(start_location, end_location) ⇒ Boolean

Check whether there exists a clear diagonal path from start to a destination

Parameters:

  • start_location (Array<Integer, Integer>)

    the start location

  • end_location (Array<Integer, Integer>)

    the intended destination

Returns:

  • (Boolean)

    true if there exists an unblocked path to destination



74
75
76
77
78
79
80
# File 'lib/boardgame_engine/board_modules.rb', line 74

def clear_diag_path?(start_location, end_location)
  row, col = start_location
  end_row, end_col = end_location

  ((end_row - row).abs == (end_col - col).abs) \
  && clear_path?(row, col, end_row, end_col, board)
end

#clear_horz_path?(start_location, end_location) ⇒ Boolean

Check whether there exists an unblocked horizontal path from start to a destination

Parameters:

  • start_location (Array<Integer, Integer>)

    the start location

  • end_location (Array<Integer, Integer>)

    the intended destination

Returns:

  • (Boolean)

    true if there exists an unblocked path to destination



89
90
91
92
93
94
# File 'lib/boardgame_engine/board_modules.rb', line 89

def clear_horz_path?(start_location, end_location)
  row, col = start_location
  end_row, end_col = end_location

  (end_row == row) && clear_path?(row, col, end_row, end_col, board)
end

#clear_vert_path?(start_location, end_location) ⇒ Boolean

Check whether there exists an unblocked vertical path from start to a destination

Parameters:

  • start_location (Array<Integer, Integer>)

    the start location

  • end_location (Array<Integer, Integer>)

    the intended destination

Returns:

  • (Boolean)

    true if there exists an unblocked path to destination



103
104
105
106
107
108
# File 'lib/boardgame_engine/board_modules.rb', line 103

def clear_vert_path?(start_location, end_location)
  row, col = start_location
  end_row, end_col = end_location

  (end_col == col) && clear_path?(row, col, end_row, end_col, board)
end

#consecutive?(num, row: true, col: true, diagonal: true) ⇒ Boolean

Check whether there are consecutive pieces on the board

consecutive pieces

Parameters:

  • num (Integer)

    the number of consecutive pieces to check for.

  • row (Boolean) (defaults to: true)

    check for row-wise consecutive pieces

  • col (Boolean) (defaults to: true)

    check for column-wise consecutive pieces

  • diagonal (Boolean) (defaults to: true)

    check for diagonally consecutive pieces

Returns:

  • (Boolean)

    true if any specified directins have num number of



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/boardgame_engine/board_modules.rb', line 140

def consecutive?(num, row: true, col: true, diagonal: true)
  configs = []
  configs << @board if row
  configs << @board.transpose if col
  configs << align_diagonal(@board) << align_diagonal(@board.transpose) if diagonal

  configs.each do |board|
    board.each { |array| return true if row_consecutive?(num, array) }
  end
  false
end

#display(show_row: false, show_col: false) ⇒ void

This method returns an undefined value.

Print a visual representation of the board

Parameters:

  • show_row (Boolean) (defaults to: false)

    whether to show row labels

  • show_col (Boolean) (defaults to: false)

    whether to show column labels



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/boardgame_engine/board_modules.rb', line 13

def display(show_row: false, show_col: false)
  if show_row
    @board.each_with_index { |row, idx| puts("#{idx} " + format_row(row)) }
  else
    @board.each { |row| puts format_row(row) }
  end

  return unless show_col

  column_spacer = show_row ? '  ' : ''
  puts format_col_numbering(column_spacer)
end

#generate_board(row, col) ⇒ Array<Array>

Make a 2D Array representing the board

Parameters:

  • row (Integer)

    the number of rows

  • col (Integer)

    the number of columns

Returns:

  • (Array<Array>)

    A 2D Array



127
128
129
# File 'lib/boardgame_engine/board_modules.rb', line 127

def generate_board(row, col)
  Array.new(row) { Array.new(col) { nil } }
end

#get_piece_at(location) ⇒ Piece

Accessor for getting piece at a location on the board

get the piece from

Parameters:

  • location (Array<Integer, Integer>)

    the coordinates on the grid to

Returns:

  • (Piece)

    the piece at the location



32
33
34
35
# File 'lib/boardgame_engine/board_modules.rb', line 32

def get_piece_at(location)
  row, col = location
  @board.dig(row, col)
end

#parse_input(input) ⇒ Array<Integer, Integer>

Parse an input from the user that has a corresponding board location. This must be used in after valid_board_input? has confirmed the input is in the correct format

Parameters:

  • input (String)

    a valid

Returns:

  • (Array<Integer, Integer>)


117
118
119
# File 'lib/boardgame_engine/board_modules.rb', line 117

def parse_input(input)
  input.split(',').map(&:to_i)
end

#set_piece_at(location, piece) ⇒ void

This method returns an undefined value.

Setter for setting a piece at a location on the board

Parameters:

  • location (Array<Integer, Integer>)

    <description>

  • piece (Piece)

    the piece to place down



43
44
45
46
# File 'lib/boardgame_engine/board_modules.rb', line 43

def set_piece_at(location, piece)
  row, col = location
  @board[row][col] = piece
end

#valid_board_input?(input, only_row: false, only_col: false) ⇒ Boolean

Check whether the given input refers to a valid location on the board, regardless of placement of other pieces, rules, etc

row col

board, false otherwise

Parameters:

  • input (String)

    the user input

  • only_row (Boolean) (defaults to: false)

    optional arg for only allowing user to specify

  • only_col (Boolean) (defaults to: false)

    optional arg for only allowing user to specify

Returns:

  • (Boolean)

    true if the input can be parsed into a location on the



59
60
61
62
63
64
65
# File 'lib/boardgame_engine/board_modules.rb', line 59

def valid_board_input?(input, only_row: false, only_col: false)
  if only_row || only_col
    input.match?(/[0-#{@board.length - 1}]/)
  else
    input.match?(/[0-#{@board.length - 1}], [0-#{@board[0].length - 1}]$/)
  end
end