Class: Mineswiper::Board

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

Constant Summary collapse

SIZE =
24
MINE_PERCENTAGE =
10
OFFSETS =
[[-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1]]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grid = Array.new(SIZE) { Array.new(SIZE) }) ⇒ Board

Returns a new instance of Board.



12
13
14
15
16
17
# File 'lib/mineswiper/board.rb', line 12

def initialize(grid=Array.new(SIZE) { Array.new(SIZE) })
  @grid = grid
  @lost = false
  @won = false
  @all_indices = []
end

Instance Attribute Details

#all_indicesObject (readonly)

Returns the value of attribute all_indices.



10
11
12
# File 'lib/mineswiper/board.rb', line 10

def all_indices
  @all_indices
end

#gridObject

Returns the value of attribute grid.



9
10
11
# File 'lib/mineswiper/board.rb', line 9

def grid
  @grid
end

#lostObject (readonly)

Returns the value of attribute lost.



10
11
12
# File 'lib/mineswiper/board.rb', line 10

def lost
  @lost
end

#savegameObject

Returns the value of attribute savegame.



9
10
11
# File 'lib/mineswiper/board.rb', line 9

def savegame
  @savegame
end

#wonObject (readonly)

Returns the value of attribute won.



10
11
12
# File 'lib/mineswiper/board.rb', line 10

def won
  @won
end

Instance Method Details

#assign_bomb_countsObject



45
46
47
48
49
50
# File 'lib/mineswiper/board.rb', line 45

def assign_bomb_counts
  @all_indices.each do |pos|
    count_adjacent_bombs(pos)
  end
  return true
end

#count_adjacent_bombs(pos) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/mineswiper/board.rb', line 60

def count_adjacent_bombs(pos)
  row, col = pos
  neighbors = find_neighbers(pos)
  neighbors.each do |neighbor|
    row2, col2 = neighbor
    if @grid[row2][col2].bomb?
      @grid[row][col].adj_bombs += 1
    end
  end
end

#eval_move(pos) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/mineswiper/board.rb', line 118

def eval_move(pos)
  x, y = pos
  if grid[x][y].bomb
    @lost = true
  else
    reveal_titles(pos)
  end
end

#find_neighbers(pos) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mineswiper/board.rb', line 71

def find_neighbers(pos)
  row, col = pos
  neighbors = []
  OFFSETS.each do |pos|
    drow, dcol = [pos[0]+row, pos[1]+col]
    unless out_of_bounds?([drow, dcol])
      neighbors << [drow, dcol]
    end
  end
  neighbors
end

#flag_pos(pos) ⇒ Object



97
98
99
100
101
102
# File 'lib/mineswiper/board.rb', line 97

def flag_pos(pos)
  x, y = pos
  tile = @grid[x][y]
  return if tile.hidden == false
  tile.flagged? ? tile.flag = false : tile.flag_it
end

#get_indicesObject



52
53
54
55
56
57
58
# File 'lib/mineswiper/board.rb', line 52

def get_indices
  (0...SIZE).each do |row|
    (0...SIZE).each do |col|
      @all_indices << [row, col]
    end
  end
end

#in_bounds?(pos) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/mineswiper/board.rb', line 139

def in_bounds?(pos)
  pos.all? { |x| x.between?(0, SIZE-1) }
end

#lost?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/mineswiper/board.rb', line 104

def lost?
  @lost
end

#make_random_tiles(pos) ⇒ Object



35
36
37
38
# File 'lib/mineswiper/board.rb', line 35

def make_random_tiles(pos)
  bomb_status = title_roll
  Tile.new(pos, bomb_status)
end

#out_of_bounds?(pos) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/mineswiper/board.rb', line 83

def out_of_bounds?(pos)
  drow, dcol = pos
  [drow, dcol].any? { |el| el < 0 || (el > (SIZE - 1))}
end

#parse_input(input) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/mineswiper/board.rb', line 88

def parse_input(input)
  return if input == nil
  if input.include?(:flag)
    flag_pos(input[1])
  else
    eval_move(input)
  end
end

#populate_minesObject



25
26
27
28
29
30
31
32
33
# File 'lib/mineswiper/board.rb', line 25

def populate_mines
  # vals = [:bomb]
  @grid.each_index do |row|
    @grid[row].each_index do |column|
      @grid[row][column] = make_random_tiles([row, column])
    end
  end
  return true
end

#prepared_boardObject



19
20
21
22
23
# File 'lib/mineswiper/board.rb', line 19

def prepared_board
  populate_mines
  get_indices
  assign_bomb_counts
end

#reveal_titles(pos) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mineswiper/board.rb', line 127

def reveal_titles(pos)
  x, y = pos
  return if grid[x][y].flagged?
  grid[x][y].hidden = false
  return if grid[x][y].adj_bombs > 0
  neighbors = find_neighbers([x, y])
  neighbors.each do |neighbor_pos|
    x, y = neighbor_pos
    reveal_titles(neighbor_pos) unless grid[x][y].hidden == false || grid[x][y].bomb
  end
end

#title_rollObject



40
41
42
43
# File 'lib/mineswiper/board.rb', line 40

def title_roll
  roll = rand(100)
  roll <= MINE_PERCENTAGE
end

#won?Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
# File 'lib/mineswiper/board.rb', line 108

def won?
  @all_indices.each do |tile|
    x, y = tile
    if grid[x][y].hidden == true && grid[x][y].bomb == false
      return false
    end
  end
  return true
end