Class: GameBoard

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows, cols, bomb_chance) ⇒ GameBoard

Returns a new instance of GameBoard.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
61
62
63
# File 'lib/game_board.rb', line 9

def initialize(rows, cols, bomb_chance)
  @board = {}
  @num_played = 0
  @win_value = rows * cols
  # This sets the board's values and then checks for some of the bombs
  rows.times do |row|
    cols.times do |column|
      if rand < bomb_chance
        @board["(#{row}, #{column})"] = GameTile.new(self, nil, nil, nil, nil, nil, nil, nil, nil, true)
        @win_value -= 1
      else
        @board["(#{row}, #{column})"] = GameTile.new(self, nil, nil, nil, nil, nil, nil, nil, nil, false)
      end


      # Set the left pointers to GameTile objects that have already been created.
      if column > 0
        @board["(#{row}, #{column})"].adjacent["left"] = @board["(#{row}, #{column - 1})"]
      end
     
      # Set the up_left, up, and up_right pointers to objects that have been created. Edge 
      # cases are automatically handled because Hashes return nil for keys that do not 
      # exist in the hash.
      if row > 0
        @board["(#{row}, #{column})"].adjacent["up"] = @board["(#{row - 1}, #{column})"]
        @board["(#{row}, #{column})"].adjacent["up_left"] = @board["(#{row - 1}, #{column - 1})"]
        @board["(#{row}, #{column})"].adjacent["up_right"] = @board["(#{row - 1}, #{column + 1})"]
      end
    end
  end

  # Now finish the bomb checking 
  rows.times do |row|
    cols.times do |column|

      # Set the right pointer object based on objects that have already been created.
      if column < cols - 1
        @board["(#{row}, #{column})"].adjacent["right"] = @board["(#{row}, #{column + 1})"]
      end

      if row < rows - 1
        @board["(#{row}, #{column})"].adjacent["down_left"] = @board["(#{row + 1}, #{column - 1})"]
        @board["(#{row}, #{column})"].adjacent["down"] = @board["(#{row + 1}, #{column})"]
        @board["(#{row}, #{column})"].adjacent["down_right"] = @board["(#{row + 1}, #{column + 1})"]
      end

    end
  end

  # Search for bombs
  @board.each do |key, value|
    value.find_adjacent_bombs
    value.find_adjacent_zeroes
  end
end

Instance Attribute Details

#boardObject

Returns the value of attribute board.



7
8
9
# File 'lib/game_board.rb', line 7

def board
  @board
end

#colsObject

Returns the value of attribute cols.



7
8
9
# File 'lib/game_board.rb', line 7

def cols
  @cols
end

#num_playedObject

Returns the value of attribute num_played.



7
8
9
# File 'lib/game_board.rb', line 7

def num_played
  @num_played
end

#rowsObject

Returns the value of attribute rows.



7
8
9
# File 'lib/game_board.rb', line 7

def rows
  @rows
end

#win_valueObject

Returns the value of attribute win_value.



7
8
9
# File 'lib/game_board.rb', line 7

def win_value
  @win_value
end