class RubyTerminalGames::Board

Constants

BOTTOM_SEPARATOR
CENTER_SEPARATOR
EMPTY_CHAR
HORIZONTAL_DASHED_LINE
HORIZONTAL_LINE
LEFT_BOTTOM_CORNER
LEFT_SEPARATOR
LEFT_TOP_CORNER
RIGHT_BOTTOM_CORNER
RIGHT_SEPARATOR
RIGHT_TOP_CORNER
TOP_SEPARATOR
VERTICAL_DASHED_LINE
VERTICAL_LINE

Attributes

cols[RW]
height[RW]
rows[RW]
width[RW]

Public Class Methods

new(width: nil, height: nil) click to toggle source
# File lib/ruby_terminal_games/board.rb, line 19
def initialize(width: nil, height: nil)
  @rows, @cols = STDOUT.winsize
  @width = width || @cols
  @height = height || @rows
end

Public Instance Methods

clear!() click to toggle source
# File lib/ruby_terminal_games/board.rb, line 25
def clear!
  move_cursor(1, 1)
  rows.times { write(EMPTY_CHAR * cols) }
end
draw_border!() click to toggle source
# File lib/ruby_terminal_games/board.rb, line 39
def draw_border!
  (0..height).each do |i|
    write(VERTICAL_LINE, row: i, col: 0)
    write(VERTICAL_LINE, row: i, col: width)
  end

  (0..width).each do |i|
    write(HORIZONTAL_LINE, row: 0, col: i)
    write(HORIZONTAL_LINE, row: height, col: i)
  end

  write(LEFT_TOP_CORNER, row: 0, col: 0)
  write(RIGHT_BOTTOM_CORNER, row: height, col: width)
  write(RIGHT_TOP_CORNER, row: 0, col: width)
  write(LEFT_BOTTOM_CORNER, row: height, col: 0)
end
move_cursor(row, col) click to toggle source
# File lib/ruby_terminal_games/board.rb, line 35
def move_cursor(row, col)
  write("\e[#{row};#{col}H")
end
write(text, row: nil, col: nil) click to toggle source
# File lib/ruby_terminal_games/board.rb, line 30
def write(text, row: nil, col: nil)
  move_cursor(row, col) if (row && col)
  STDOUT.write(text)
end