configurable number of mines, display it

This commit is contained in:
Gwenhael Le Moine 2022-09-14 16:38:56 +02:00
parent d9f070c92e
commit 798fb16227
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
2 changed files with 15 additions and 6 deletions

View file

@ -19,7 +19,7 @@ def display_curses( field, state = { dead: false, victory: false } )
elsif state[:victory] elsif state[:victory]
Curses.addstr 'You won!! :) ' Curses.addstr 'You won!! :) '
else else
Curses.addstr "nearby: #{field.count_nearby_mines} " Curses.addstr "#{field.number_of_mines} mines, #{field.count_nearby_mines} nearby"
end end
field.height.times do |y| field.height.times do |y|
@ -40,8 +40,8 @@ def display_curses( field, state = { dead: false, victory: false } )
Curses.refresh Curses.refresh
end end
def ncurses_main def ncurses_main( number_of_mines )
field = MyHunt::Board.new field = MyHunt::Board.new( number_of_mines )
finished = false finished = false
Curses.init_screen Curses.init_screen
@ -64,7 +64,7 @@ def ncurses_main
begin begin
case ch case ch
when 'r' when 'r'
field = MyHunt::Board.new field = MyHunt::Board.new( number_of_mines )
finished = false finished = false
else else
break break
@ -107,4 +107,7 @@ def ncurses_main
exit! exit!
end end
ncurses_main number_of_mines = 20
number_of_mines = ARGV.first.to_i if ARGV.length == 1
ncurses_main( number_of_mines )

View file

@ -22,6 +22,7 @@ module MyHunt
attr_reader :width, attr_reader :width,
:height, :height,
:field, :field,
:number_of_mines,
:explorer_x, :explorer_x,
:explorer_y :explorer_y
@ -31,6 +32,11 @@ module MyHunt
@explorer_x = 0 @explorer_x = 0
@explorer_y = 0 @explorer_y = 0
@field = {} @field = {}
@number_of_mines = if number_of_mines.negative? && number_of_mines > ((@height * @width) - 2)
20
else
number_of_mines
end
@width.times do |x| @width.times do |x|
@height.times do |y| @height.times do |y|
@ -39,7 +45,7 @@ module MyHunt
end end
placed_mines = 0 placed_mines = 0
while placed_mines < number_of_mines while placed_mines < @number_of_mines
coordinates = [rand( @width ), rand( @height )] coordinates = [rand( @width ), rand( @height )]
next if coordinates == [0, 0] next if coordinates == [0, 0]
next if @field[ coordinates ].mine next if @field[ coordinates ].mine