class MineSweeper::Minefield
Attributes
field[R]
size[R]
Public Class Methods
new(size = 10)
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 9 def initialize(size = 10) set_field_size(size) create_field_with_attributes end
Public Instance Methods
blow_up_board()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 35 def blow_up_board field.flatten.each do |cell| cell.clear if cell.mine end end
flag_remaining_mines()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 45 def flag_remaining_mines field.flatten.each do |cell| cell.flag if cell.mine end end
game_over?()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 51 def game_over? lost? || won? end
lost?()
click to toggle source
GAME ENDING SCENARIOS
# File lib/mine-sweeper/minefield.rb, line 31 def lost? field.flatten.any? { |cell| cell.exploded? } end
number_of_flags()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 61 def number_of_flags field.flatten.count { |cell| cell.flagged } end
number_of_mines()
click to toggle source
PUBLIC BOARD INFORMATION
# File lib/mine-sweeper/minefield.rb, line 57 def number_of_mines field.flatten.count { |cell| cell.mine } end
render()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 14 def render field.each do |row| row.each { |cell| print cell } print "\n" end nil end
take_turn(turn)
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 22 def take_turn(turn) row = turn[:row] column = turn[:column] action = turn[:action] take_action(row, column, action) end
uncleared_cells()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 69 def uncleared_cells field.flatten.count { |cell| !(cell.cleared) } end
unflagged_mines()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 65 def unflagged_mines number_of_mines - number_of_flags end
won?()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 41 def won? uncleared_cells == number_of_mines end
Private Instance Methods
already_cleared?(row,col)
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 163 def already_cleared?(row,col) field[row][col].cleared end
auto_clear(row,col)
click to toggle source
SMART CLEARING
# File lib/mine-sweeper/minefield.rb, line 141 def auto_clear(row,col) if already_cleared?(row,col) && flags_match_mines?(row,col) valid_neighbor_coordinates(row,col).each do |cell| row,col = cell[0],cell[1] clear_with_zero_cascade(row,col) end end end
clear_with_zero_cascade(target_row,target_column)
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 150 def clear_with_zero_cascade(target_row,target_column) target_cell = field[target_row][target_column] target_cell.clear if target_cell.adjacent_mines == 0 valid_neighbor_coordinates(target_row,target_column).each do |cell| row,col = cell[0],cell[1] unless field[row][col].cleared clear_with_zero_cascade(cell[0],cell[1]) end end end end
count_nearby_flags(row,col)
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 171 def count_nearby_flags(row,col) neighbor_cells(row,col).count { |cell| cell.flagged } end
create_field_with_attributes()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 85 def create_field_with_attributes @field = Array.new(size){ Array.new(size){ Cell.new } } generate_mines generate_adjacent_mine_counts end
flags_match_mines?(row,col)
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 167 def flags_match_mines?(row,col) count_nearby_flags(row,col) == field[row][col].adjacent_mines end
generate_adjacent_mine_counts()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 111 def generate_adjacent_mine_counts field.each_with_index do |row, row_num| row.each_with_index do |cell, col_num| if cell.mine neighbor_cells(row_num,col_num).each do |neighbor| neighbor.count_adjacent_mine end end end end end
generate_mines()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 99 def generate_mines mines_left = mine_calculator until mines_left == 0 row = rand(size) col = rand(size) unless field[row][col].mine field[row][col].place_mine mines_left -= 1 end end end
mine_calculator()
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 91 def mine_calculator if size < 20 9 else 100 end end
neighbor_cells(row,col)
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 200 def neighbor_cells(row,col) valid_neighbor_coordinates(row,col).each_with_object([]) do |cell,neighbors| neighbor_row = cell[0] neighbor_col = cell[1] neighbors << field[neighbor_row][neighbor_col] end end
set_field_size(size)
click to toggle source
INITIALIZE HELPERS
# File lib/mine-sweeper/minefield.rb, line 77 def set_field_size(size) if size.class == Fixnum @size = size else raise 'Please enter an integer.' end end
take_action(row, column, action)
click to toggle source
TAKE TURN HELPERS
# File lib/mine-sweeper/minefield.rb, line 125 def take_action(row, column, action) cell = field[row][column] case action when "A" auto_clear(row,column) when "C" clear_with_zero_cascade(row,column) when "F" cell.flag if unflagged_mines > 0 when "U" cell.unflag end end
valid_cols(col)
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 184 def valid_cols(col) cols = [col] cols << (col - 1) unless col == 0 cols << (col + 1) unless col == (size - 1) cols end
valid_neighbor_coordinates(row,col)
click to toggle source
# File lib/mine-sweeper/minefield.rb, line 191 def valid_neighbor_coordinates(row,col) coords = valid_rows(row).each_with_object([]) do |row, nearby| valid_cols(col).each do |col| nearby << [row,col] end end coords - [[row,col]] end
valid_rows(row)
click to toggle source
FINDING NEIGHBOR CELLS HELPERS
# File lib/mine-sweeper/minefield.rb, line 177 def valid_rows(row) rows = [row] rows << (row - 1) unless row == 0 rows << (row + 1) unless row == (size - 1) rows end