Class: Mineswiper::Board
- Inherits:
-
Object
- Object
- Mineswiper::Board
- 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
-
#all_indices ⇒ Object
readonly
Returns the value of attribute all_indices.
-
#grid ⇒ Object
Returns the value of attribute grid.
-
#lost ⇒ Object
readonly
Returns the value of attribute lost.
-
#savegame ⇒ Object
Returns the value of attribute savegame.
-
#won ⇒ Object
readonly
Returns the value of attribute won.
Instance Method Summary collapse
- #assign_bomb_counts ⇒ Object
- #count_adjacent_bombs(pos) ⇒ Object
- #eval_move(pos) ⇒ Object
- #find_neighbers(pos) ⇒ Object
- #flag_pos(pos) ⇒ Object
- #get_indices ⇒ Object
- #in_bounds?(pos) ⇒ Boolean
-
#initialize(grid = Array.new(SIZE) { Array.new(SIZE) }) ⇒ Board
constructor
A new instance of Board.
- #lost? ⇒ Boolean
- #make_random_tiles(pos) ⇒ Object
- #out_of_bounds?(pos) ⇒ Boolean
- #parse_input(input) ⇒ Object
- #populate_mines ⇒ Object
- #prepared_board ⇒ Object
- #reveal_titles(pos) ⇒ Object
- #title_roll ⇒ Object
- #won? ⇒ Boolean
Constructor Details
Instance Attribute Details
#all_indices ⇒ Object (readonly)
Returns the value of attribute all_indices.
10 11 12 |
# File 'lib/mineswiper/board.rb', line 10 def all_indices @all_indices end |
#grid ⇒ Object
Returns the value of attribute grid.
9 10 11 |
# File 'lib/mineswiper/board.rb', line 9 def grid @grid end |
#lost ⇒ Object (readonly)
Returns the value of attribute lost.
10 11 12 |
# File 'lib/mineswiper/board.rb', line 10 def lost @lost end |
#savegame ⇒ Object
Returns the value of attribute savegame.
9 10 11 |
# File 'lib/mineswiper/board.rb', line 9 def savegame @savegame end |
#won ⇒ Object (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_counts ⇒ Object
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_indices ⇒ Object
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
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
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
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_mines ⇒ Object
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_board ⇒ Object
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_roll ⇒ Object
40 41 42 43 |
# File 'lib/mineswiper/board.rb', line 40 def title_roll roll = rand(100) roll <= MINE_PERCENTAGE end |
#won? ⇒ 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 |