added score

This commit is contained in:
Gwenhael Le Moine 2022-10-31 22:49:04 +01:00
parent 9fc8946428
commit b78ee1ff09
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
2 changed files with 13 additions and 10 deletions

View file

@ -13,20 +13,20 @@ require 'myhunt'
def display_curses( field, def display_curses( field,
state = { dead: false, victory: false }, state = { dead: false, victory: false },
theme = { explorer: '', theme = { explorer: '()',
dead_explorer: '☠', dead_explorer: '☠ ',
door: '', door: '=>',
mine: '', mine: '<>',
cell: '█', cell: '█',
visited_cell: ' ' } ) visited_cell: ' ' } )
Curses.setpos 0, 0 Curses.setpos 0, 0
if state[:dead] if state[:dead]
Curses.addstr 'You died! :( ' Curses.addstr "You died! :(, score #{field.score} "
elsif state[:victory] elsif state[:victory]
Curses.addstr 'You won!! :) ' Curses.addstr "You won!! :), score #{field.score} "
else else
Curses.addstr "#{field.number_of_mines} mines, #{field.count_nearby_mines} nearby" Curses.addstr "#{field.number_of_mines} mines, #{field.count_nearby_mines} nearby, score #{field.score}"
end end
field.height.times do |y| field.height.times do |y|

View file

@ -24,13 +24,15 @@ module MyHunt
:field, :field,
:number_of_mines, :number_of_mines,
:explorer_x, :explorer_x,
:explorer_y :explorer_y,
:score
def initialize( number_of_mines = 20, height = 8, width = 16 ) def initialize( number_of_mines = 20, height = 8, width = 16 )
@height = height @height = height
@width = width @width = width
@explorer_x = 0 @explorer_x = 0
@explorer_y = 0 @explorer_y = 0
@score = 1
@field = {} @field = {}
@number_of_mines = if number_of_mines.negative? && number_of_mines > ((@height * @width) - 2) @number_of_mines = if number_of_mines.negative? && number_of_mines > ((@height * @width) - 2)
20 20
@ -90,6 +92,7 @@ module MyHunt
def move( direction ) def move( direction )
update_explorer_location( direction ) update_explorer_location( direction )
@score += 1 unless @field[[@explorer_x, @explorer_y]].open
{ dead: @field[[@explorer_x, @explorer_y]].explore, { dead: @field[[@explorer_x, @explorer_y]].explore,
victory: @explorer_x == (@width - 1) && @explorer_y == (@height - 1) } victory: @explorer_x == (@width - 1) && @explorer_y == (@height - 1) }