Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

w2d1 code review kellen h #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions minesweeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ def play
puts "Here is the minefield:\n"
@display_board.each{|row| p row}
puts ''

puts "Would you like to dig or flag? 1-Dig, 2-Flag, 3-Save, 4-Quit"
command = gets.chomp.to_i
case command
when 3 then save_game
# REV: you could do `when 4 return forfeit = "quit" `
when 4
forfeit = "quit"
return
Expand Down Expand Up @@ -66,6 +68,16 @@ def make_board(size)
row = []
9.times {row << "_"}
9.times {board << row.dup}
# REV: this may reassign 'm' to a spot that already has 'm'
# to guarantee 10 mines placed, do something like:
# mines = 10
# until mines == 0
# placement = board[rand(16)][rand(16)]
# if placement != 'm'
# board[rand(16)][rand(16)] = 'm'
# mines -= 1
# end
# end
10.times {board[rand(9)][rand(9)] = "m"}
elsif size == :large
16.times {row << "_"}
Expand All @@ -77,6 +89,8 @@ def make_board(size)

def initial_display_board
display_board = []
# REV: instead of deep_dup, a great one line method to make a board, then
# another board, is `board = Array.new(9) { Array.new(9) { '_' } }
@board.deep_dup.each do |row|
row.map! do |el|
if el == "m"
Expand Down Expand Up @@ -105,6 +119,10 @@ def is_mine?(x,y)
end

def get_neighbors(x,y)
# REV: you could just do two nested loops:
# (x-1..x+1).each do
# (y-1..y+1).each do
# ....
neighbors = [[x-1 , y-1],[x-1 , y], [x-1 , y+1],
[x , y-1],[x,y],[x , y+1],
[x+1 , y-1], [x+1 , y],[x+1 , y+1]]
Expand Down