Class: Mineswiper::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/mineswiper/display.rb

Constant Summary collapse

COLORS =
{
  0 => :light_black,
  1 => :green,
  2 => :cyan,
  3 => :light_blue,
  4 => :blue,
  5 => :light_magenta,
  6 => :magenta,
  7 => :light_black,
  8 => :black
}
GLOBAL_BACKGROUND =
:light_white
KEYMAP =
{
  " " => :space,
  "f" => :f,
  "a" => :left,
  "s" => :down,
  "w" => :up,
  "d" => :right,
  "\u0003" => :ctrl_c,
}
MOVES =
{
  left: [0, -1],
  right: [0, 1],
  up: [-1, 0],
  down: [1, 0]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board) ⇒ Display

Returns a new instance of Display.



21
22
23
24
# File 'lib/mineswiper/display.rb', line 21

def initialize(board)
  @board = board
  @cursor_pos = [0, 0]
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



19
20
21
# File 'lib/mineswiper/display.rb', line 19

def board
  @board
end

Instance Method Details

#build_gridObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mineswiper/display.rb', line 26

def build_grid
  @board.grid.each_index do |i|
    rowdisplay = ""
    @board.grid[i].each_index do |j|
      output = board.grid[i][j].tilerender
      color_options = colors_for(i, j)
      rowdisplay << output.colorize(color_options) + " ".colorize(background: GLOBAL_BACKGROUND) unless output.nil?
    end
    puts rowdisplay
  end
end

#colors_for(i, j) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mineswiper/display.rb', line 38

def colors_for(i, j)
  if [i, j] == @cursor_pos
    bg = :light_red
  else
    bg = GLOBAL_BACKGROUND
  end

  if (@board.grid[i][j].hidden == false)
    color = COLORS[@board.grid[i][j].adj_bombs]
    if @board.grid[i][j].bomb?
      color = :light_red
    end
  elsif @board.grid[i][j].flagged?
    color = :light_red
  else
    color = :white
  end
  { background: bg, color: color }
end

#get_inputObject



81
82
83
84
# File 'lib/mineswiper/display.rb', line 81

def get_input
  key = KEYMAP[read_char]
  handle_key(key)
end

#handle_key(key) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mineswiper/display.rb', line 86

def handle_key(key)
  case key
  when :ctrl_c
    exit 0
  when :space
    @cursor_pos
  when :left, :right, :up, :down
    update_pos(MOVES[key])
    nil
  when :f
    [:flag, @cursor_pos]
  else
    puts key
  end
end

#read_charObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mineswiper/display.rb', line 102

def read_char
  STDIN.echo = false
  STDIN.raw!

  input = STDIN.getc.chr
  if input == "\e" then
    input << STDIN.read_nonblock(3) rescue nil
    input << STDIN.read_nonblock(2) rescue nil
  end
ensure
  STDIN.echo = true
  STDIN.cooked!

  return input
end

#renderObject



58
59
60
61
62
# File 'lib/mineswiper/display.rb', line 58

def render
  system("clear")
  puts "W-A-S-D to move the cursor - Spacebar to reveal."
  build_grid
end

#update_pos(diff) ⇒ Object



118
119
120
121
# File 'lib/mineswiper/display.rb', line 118

def update_pos(diff)
  new_pos = [@cursor_pos[0] + diff[0], @cursor_pos[1] + diff[1]]
  @cursor_pos = new_pos if @board.in_bounds?(new_pos)
end