class Mineswiper::Board
Constants
- MINE_PERCENTAGE
- OFFSETS
- SIZE
Attributes
all_indices[R]
grid[RW]
lost[R]
savegame[RW]
won[R]
Public Class Methods
new(grid=Array.new(SIZE) { Array.new(SIZE) })
click to toggle source
# 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
Public Instance Methods
assign_bomb_counts()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# File lib/mineswiper/board.rb, line 139 def in_bounds?(pos) pos.all? { |x| x.between?(0, SIZE-1) } end
lost?()
click to toggle source
# File lib/mineswiper/board.rb, line 104 def lost? @lost end
make_random_tiles(pos)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/mineswiper/board.rb, line 19 def prepared_board populate_mines get_indices assign_bomb_counts end
reveal_titles(pos)
click to toggle source
# 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()
click to toggle source
# File lib/mineswiper/board.rb, line 40 def title_roll roll = rand(100) roll <= MINE_PERCENTAGE end
won?()
click to toggle source
# 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