class MinesweeperGame

Attributes

board[RW]

Public Class Methods

new(board, rows, columns) click to toggle source
# File lib/minesweeper_game.rb, line 5
def initialize(board, rows, columns)
  @board = board
  @rows = rows
  @columns = columns
  @game_over = false
  @win = false
end

Public Instance Methods

flag_tile(row, column) click to toggle source
# File lib/minesweeper_game.rb, line 86
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.num_played += 1 unless @board.board["(#{row}, #{column})"].been_played
    @board.board["(#{row}, #{column})"].been_played = true
    if @board.num_played == @board.win_value
      @win = true
    end
  end
end
game_over?() click to toggle source
# File lib/minesweeper_game.rb, line 99
def game_over?
  @game_over
end
play_the_game() click to toggle source
# 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 "Numbers greater than the number of rows or cols will be truncated to the max row/col"
  puts
  player_choice = gets.chomp

  if /^(help|h)\z/.match(player_choice.downcase) then print_help_message end

  # Now match the player response with a regular expression
  while /^([pfPF]) (\d{1,2}), (\d{1,2})\z/.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 "Numbers greater than the number of rows or cols will be truncated to the max row/col"
    puts
    player_choice = gets.chomp
    if /^(help|h)\z/.match(player_choice.downcase) then print_help_message end
  end
  match = /^([pfPF]) (\d{1,2}), (\d{1,2})\z/.match(player_choice)
  row = match[2].to_i
  col = match[3].to_i

  if row > @rows then row = @rows end
  if col > @columns then col = @columns end

  # 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' || match[1] == 'P'
    play_tile(row - 1, col - 1)
  elsif match[1] == 'f' || match[1] == 'F'
    flag_tile(row - 1, col - 1)
  end
  puts
  print_the_board
end
play_tile(row, column) click to toggle source
# File lib/minesweeper_game.rb, line 70
def play_tile(row, column)
  if @board.board["(#{row}, #{column})"].is_bomb?
    @game_over = true
  else
    @board.num_played += 1 unless @board.board["(#{row}, #{column})"].been_played || @board.board["(#{row}, #{column})"].adjacent_bombs == 0
    @board.board["(#{row}, #{column})"].been_played = true
    if @board.board["(#{row}, #{column})"].adjacent_bombs == 0
      @board.board["(#{row}, #{column})"].play_adjacent_zeroes(@board)
    end
    if @board.num_played == @board.win_value
      @win = true
    end
  end
  
end
print_ending_board() click to toggle source
print_the_board() click to toggle source
win?() click to toggle source
# File lib/minesweeper_game.rb, line 103
def win?
  @win
end

Private Instance Methods

print_help_message() click to toggle source