Class: GameTile

Inherits:
Object
  • Object
show all
Defined in:
lib/game_tile.rb

Overview

An object oriented take on the minesweeper game that’s done in 131. Since it is un-Rubylike to use 2d arrays, I’m going to try and make the game using a more object oriented approach.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board, up_left, up, up_right, left, right, down_left, down, down_right, is_bomb) ⇒ GameTile

Returns a new instance of GameTile.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/game_tile.rb', line 10

def initialize(board, up_left, up, up_right, left, right, down_left, down, down_right, is_bomb)
  @adjacent = {
    "up_left" => up_left,
    "up" => up,
    "up_right" => up_right,
    "left" => left,
    "right" => right,
    "down_left" => down_left,
    "down" => down,
    "down_right" => down_right
  }
  @is_bomb = is_bomb
  @adjacent_bombs = 0
  @adjacent_zeroes = {}
  @been_played = false
  @been_flagged = false
  @board = board
end

Instance Attribute Details

#adjacentObject

Returns the value of attribute adjacent.



8
9
10
# File 'lib/game_tile.rb', line 8

def adjacent
  @adjacent
end

#adjacent_bombsObject

Returns the value of attribute adjacent_bombs.



8
9
10
# File 'lib/game_tile.rb', line 8

def adjacent_bombs
  @adjacent_bombs
end

#adjacent_zeroesObject

Returns the value of attribute adjacent_zeroes.



8
9
10
# File 'lib/game_tile.rb', line 8

def adjacent_zeroes
  @adjacent_zeroes
end

#been_flaggedObject

Returns the value of attribute been_flagged.



8
9
10
# File 'lib/game_tile.rb', line 8

def been_flagged
  @been_flagged
end

#been_playedObject

Returns the value of attribute been_played.



8
9
10
# File 'lib/game_tile.rb', line 8

def been_played
  @been_played
end

Instance Method Details

#find_adjacent_bombsObject

Method will iterate through the surrounding GameTiles to check how many bombs there are. The method returns the number of bombs that are adjacent to itself.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/game_tile.rb', line 31

def find_adjacent_bombs
  @adjacent.each do |key, value|
    begin
      if value.is_bomb?
        @adjacent_bombs += 1
      end
    rescue
      # This rescue catches the NoMethodError that arises when trying to call the find_adjacent_bombs 
      # method on an edge cell. The error arises when trying to access the is_bomb attribute of a 
      # nil class. Hence, the NoMethodError.
    end
  end
end

#find_adjacent_zeroesObject

This method will “play” all of the adjacent cells that have zero mines surrounding them. The minesweeper game that comes standard with most computers has this behavior.



47
48
49
50
51
52
53
54
55
56
# File 'lib/game_tile.rb', line 47

def find_adjacent_zeroes
  @adjacent.each do |key, value|
    begin  
      if value.adjacent_bombs == 0
        @adjacent_zeroes[key] = value
      end
    rescue
    end
  end 
end

#is_bomb?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/game_tile.rb', line 70

def is_bomb?
  @is_bomb
end

#play_adjacent_zeroesObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/game_tile.rb', line 58

def play_adjacent_zeroes
  @adjacent_zeroes.each do |key, value|
    value.adjacent_zeroes.each do |key1, value1|
      if !value1.been_played  
        value1.been_played = true
        @board.num_played += 1
      end
    end
  end

end