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
-
#clear_diag_path?(start_location, end_location) ⇒ Boolean
Check whether there exists a clear diagonal path from start to a destination.
-
#clear_horz_path?(start_location, end_location) ⇒ Boolean
Check whether there exists an unblocked horizontal path from start to a destination.
-
#clear_vert_path?(start_location, end_location) ⇒ Boolean
Check whether there exists an unblocked vertical path from start to a destination.
-
#consecutive?(num, row: true, col: true, diagonal: true) ⇒ Boolean
Check whether there are consecutive pieces on the board.
-
#display(show_row: false, show_col: false) ⇒ void
Print a visual representation of the board.
-
#generate_board(row, col) ⇒ Array<Array>
Make a 2D Array representing the board.
-
#get_piece_at(location) ⇒ Piece
Accessor for getting piece at a location on the board.
-
#parse_input(input) ⇒ Array<Integer, Integer>
Parse an input from the user that has a corresponding board location.
-
#set_piece_at(location, piece) ⇒ void
Setter for setting a piece at a location on the board.
-
#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.
Instance Method Details
#clear_diag_path?(start_location, end_location) ⇒ Boolean
Check whether there exists a clear diagonal path from start to a 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
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
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
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
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
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
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
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
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
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 |