Class: MinesweeperGame
- Inherits:
-
Object
- Object
- MinesweeperGame
- Defined in:
- lib/minesweeper_game.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
Returns the value of attribute board.
Instance Method Summary collapse
- #flag_tile(row, column) ⇒ Object
- #game_over? ⇒ Boolean
-
#initialize(board, rows, columns) ⇒ MinesweeperGame
constructor
A new instance of MinesweeperGame.
- #play_the_game ⇒ Object
- #play_tile(row, column) ⇒ Object
- #print_the_board ⇒ Object
- #win? ⇒ Boolean
Constructor Details
#initialize(board, rows, columns) ⇒ MinesweeperGame
Returns a new instance of MinesweeperGame.
5 6 7 8 9 10 11 |
# File 'lib/minesweeper_game.rb', line 5 def initialize(board, rows, columns) @board = board @rows = rows @columns = columns @game_over = false @win = false end |
Instance Attribute Details
#board ⇒ Object
Returns the value of attribute board.
3 4 5 |
# File 'lib/minesweeper_game.rb', line 3 def board @board end |
Instance Method Details
#flag_tile(row, column) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/minesweeper_game.rb', line 74 def flag_tile(row, column) if @board.board["(#{row}, #{column})"].been_flagged @board.board["(#{row}, #{column})"].been_flagged = false else @board.board["(#{row}, #{column})"].been_flagged = true @board.board["(#{row}, #{column})"].been_played = true @board.num_played += 1 if @board.num_played == @board.win_value @win = true end end end |
#game_over? ⇒ Boolean
87 88 89 |
# File 'lib/minesweeper_game.rb', line 87 def game_over? @game_over end |
#play_the_game ⇒ Object
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 |
# File 'lib/minesweeper_game.rb', line 32 def play_the_game puts "Please put two numbers corresponding to the row and column that you'd like to play in the form" puts "<pf> <row>, <column>" puts player_choice = gets.chomp # Now match the player response with a regular expression while /([pf]) (\d), (\d)$/.match(player_choice) == nil puts "Please put two numbers corresponding to the row and column that you'd like to play in the form" puts "<pf> <row>, <column>" puts player_choice = gets.chomp end match = /([pf]) (\d), (\d)$/.match(player_choice) # A 'p' character indicates that the player wants to "play" the tile. # An 'f' character indicates that the player would like to flag the # tile because they believe there to be a bomb there. Flagged tiles # may still be played after they have been flagged. if match[1] == 'p' play_tile(match[2].to_i - 1, match[3].to_i - 1) elsif match[1] == 'f' flag_tile(match[2].to_i - 1, match[3].to_i - 1) end puts print_the_board end |
#play_tile(row, column) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/minesweeper_game.rb', line 59 def play_tile(row, column) if @board.board["(#{row}, #{column})"].is_bomb? @game_over = true else @board.board["(#{row}, #{column})"].been_played = true @board.num_played += 1 if @board.board["(#{row}, #{column})"].adjacent_bombs == 0 @board.board["(#{row}, #{column})"].play_adjacent_zeroes end if @board.num_played == @board.win_value @win = true end end end |
#print_the_board ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/minesweeper_game.rb', line 13 def print_the_board @rows.times do |row| @columns.times do |column| if !@board.board["(#{row}, #{column})"].been_played print "[ ] " elsif @board.board["(#{row}, #{column})"].been_flagged print "[|>] " else if @board.board["(#{row}, #{column})"].adjacent_bombs > 0 print "[ " + @board.board["(#{row}, #{column})"].adjacent_bombs.to_s + "] " else print "[--] " end end end puts end end |
#win? ⇒ Boolean
91 92 93 |
# File 'lib/minesweeper_game.rb', line 91 def win? @win end |