Class: MineSweeper::Minefield
- Inherits:
-
Object
- Object
- MineSweeper::Minefield
- Defined in:
- lib/mine-sweeper/minefield.rb
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #blow_up_board ⇒ Object
- #flag_remaining_mines ⇒ Object
- #game_over? ⇒ Boolean
-
#initialize(size = 10) ⇒ Minefield
constructor
A new instance of Minefield.
-
#lost? ⇒ Boolean
GAME ENDING SCENARIOS.
- #number_of_flags ⇒ Object
-
#number_of_mines ⇒ Object
PUBLIC BOARD INFORMATION.
- #render ⇒ Object
- #take_turn(turn) ⇒ Object
- #uncleared_cells ⇒ Object
- #unflagged_mines ⇒ Object
- #won? ⇒ Boolean
Constructor Details
#initialize(size = 10) ⇒ Minefield
Returns a new instance of Minefield.
9 10 11 12 |
# File 'lib/mine-sweeper/minefield.rb', line 9 def initialize(size = 10) set_field_size(size) create_field_with_attributes end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
7 8 9 |
# File 'lib/mine-sweeper/minefield.rb', line 7 def field @field end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
7 8 9 |
# File 'lib/mine-sweeper/minefield.rb', line 7 def size @size end |
Instance Method Details
#blow_up_board ⇒ Object
35 36 37 38 39 |
# 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 ⇒ Object
45 46 47 48 49 |
# 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? ⇒ Boolean
51 52 53 |
# File 'lib/mine-sweeper/minefield.rb', line 51 def game_over? lost? || won? end |
#lost? ⇒ Boolean
GAME ENDING SCENARIOS
31 32 33 |
# File 'lib/mine-sweeper/minefield.rb', line 31 def lost? field.flatten.any? { |cell| cell.exploded? } end |
#number_of_flags ⇒ Object
61 62 63 |
# File 'lib/mine-sweeper/minefield.rb', line 61 def number_of_flags field.flatten.count { |cell| cell.flagged } end |
#number_of_mines ⇒ Object
PUBLIC BOARD INFORMATION
57 58 59 |
# File 'lib/mine-sweeper/minefield.rb', line 57 def number_of_mines field.flatten.count { |cell| cell.mine } end |
#render ⇒ Object
14 15 16 17 18 19 20 |
# 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) ⇒ Object
22 23 24 25 26 27 |
# 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 ⇒ Object
69 70 71 |
# File 'lib/mine-sweeper/minefield.rb', line 69 def uncleared_cells field.flatten.count { |cell| !(cell.cleared) } end |
#unflagged_mines ⇒ Object
65 66 67 |
# File 'lib/mine-sweeper/minefield.rb', line 65 def unflagged_mines number_of_mines - number_of_flags end |
#won? ⇒ Boolean
41 42 43 |
# File 'lib/mine-sweeper/minefield.rb', line 41 def won? uncleared_cells == number_of_mines end |