Class: MineHunt::Board
- Inherits:
-
Object
- Object
- MineHunt::Board
- Defined in:
- lib/minehunt.rb
Instance Attribute Summary collapse
-
#explorer_x ⇒ Object
readonly
Returns the value of attribute explorer_x.
-
#explorer_y ⇒ Object
readonly
Returns the value of attribute explorer_y.
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#number_of_mines ⇒ Object
readonly
Returns the value of attribute number_of_mines.
-
#score ⇒ Object
readonly
Returns the value of attribute score.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #count_nearby_mines ⇒ Object
-
#initialize(number_of_mines = 20, height = 8, width = 16) ⇒ Board
constructor
A new instance of Board.
- #move(direction) ⇒ Object
- #update_explorer_location(direction) ⇒ Object
Constructor Details
#initialize(number_of_mines = 20, height = 8, width = 16) ⇒ Board
Returns a new instance of Board.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/minehunt.rb', line 32 def initialize( number_of_mines = 20, height = 8, width = 16 ) @height = height @width = width @explorer_x = 0 @explorer_y = 0 @score = 1 @field = {} @number_of_mines = if number_of_mines.negative? && number_of_mines > ((@height * @width) - 2) 20 else number_of_mines end @width.times do |x| @height.times do |y| @field[[x, y]] = Cell.new( open: x.zero? && y.zero? ) end end placed_mines = 0 while placed_mines < @number_of_mines coordinates = [rand( @width ), rand( @height )] next if coordinates == [0, 0] next if @field[ coordinates ].mine @field[ coordinates ].mine = true placed_mines += 1 end end |
Instance Attribute Details
#explorer_x ⇒ Object (readonly)
Returns the value of attribute explorer_x.
24 25 26 |
# File 'lib/minehunt.rb', line 24 def explorer_x @explorer_x end |
#explorer_y ⇒ Object (readonly)
Returns the value of attribute explorer_y.
24 25 26 |
# File 'lib/minehunt.rb', line 24 def explorer_y @explorer_y end |
#field ⇒ Object (readonly)
Returns the value of attribute field.
24 25 26 |
# File 'lib/minehunt.rb', line 24 def field @field end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
24 25 26 |
# File 'lib/minehunt.rb', line 24 def height @height end |
#number_of_mines ⇒ Object (readonly)
Returns the value of attribute number_of_mines.
24 25 26 |
# File 'lib/minehunt.rb', line 24 def number_of_mines @number_of_mines end |
#score ⇒ Object (readonly)
Returns the value of attribute score.
24 25 26 |
# File 'lib/minehunt.rb', line 24 def score @score end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
24 25 26 |
# File 'lib/minehunt.rb', line 24 def width @width end |
Instance Method Details
#count_nearby_mines ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/minehunt.rb', line 103 def count_nearby_mines counter = 0 [@explorer_x - 1, @explorer_x, @explorer_x + 1].each do |x| [@explorer_y - 1, @explorer_y, @explorer_y + 1].each do |y| next if x.negative? next if y.negative? next if x == @width next if y == @height next if x == @explorer_x && y == @explorer_y counter += 1 if @field[[x, y]].mine end end counter end |
#move(direction) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/minehunt.rb', line 95 def move( direction ) update_explorer_location( direction ) @score += 1 unless @field[[@explorer_x, @explorer_y]].open { dead: @field[[@explorer_x, @explorer_y]].explore, victory: @explorer_x == (@width - 1) && @explorer_y == (@height - 1) } end |
#update_explorer_location(direction) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/minehunt.rb', line 62 def update_explorer_location( direction ) case direction when 'up' @explorer_y -= 1 if @explorer_y.positive? when 'right' @explorer_x += 1 if @explorer_x < (@width - 1) when 'down' @explorer_y += 1 if @explorer_y < (@height - 1) when 'left' @explorer_x -= 1 if @explorer_x.positive? when 'up-right' if @explorer_x < (@width - 1) && @explorer_y.positive? @explorer_x += 1 @explorer_y -= 1 end when 'down-right' if @explorer_x < (@width - 1) && @explorer_y < (@height - 1) @explorer_x += 1 @explorer_y += 1 end when 'down-left' if @explorer_x.positive? && @explorer_y < (@height - 1) @explorer_x -= 1 @explorer_y += 1 end when 'up-left' if @explorer_x.positive? && @explorer_y.positive? @explorer_x -= 1 @explorer_y -= 1 end end end |