Class: TicTacToe::TUI::Board

Inherits:
Object
  • Object
show all
Defined in:
lib/tic_tac_toe/tui/board.rb

Constant Summary collapse

SYMBOL_COLORS =
{
  "x" => Curses::COLOR_RED,
  "" => Curses::COLOR_BLUE,
  "" => Curses::COLOR_YELLOW,
  "" => Curses::COLOR_GREEN,
  "" => Curses::COLOR_MAGENTA,
}
COLOR_GRAY =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ Board

Returns a new instance of Board.



17
18
19
20
# File 'lib/tic_tac_toe/tui/board.rb', line 17

def initialize(board)
  @window = new_window(board)
  update(board)
end

Instance Attribute Details

#windowObject (readonly)

Returns the value of attribute window.



15
16
17
# File 'lib/tic_tac_toe/tui/board.rb', line 15

def window
  @window
end

Instance Method Details

#update(board) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tic_tac_toe/tui/board.rb', line 22

def update(board)
  symbols = SYMBOL_COLORS.keys
  scanner = StringScanner.new(board)

  redraw do
    loop do
      box_part = scanner.scan_until(/(?=#{Regexp.union(*symbols, /\Z/)})/)
      write_box(box_part)

      break if scanner.eos?

      symbol = scanner.getch
      write_symbol(symbol, SYMBOL_COLORS.fetch(symbol))
    end
  end
end