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,
state = { dead: false, victory: false },
theme = { explorer: '',
dead_explorer: '☠',
door: '',
mine: '',
cell: '█',
visited_cell: ' ' } )
theme = { explorer: '()',
dead_explorer: '☠ ',
door: '=>',
mine: '<>',
cell: '█',
visited_cell: ' ' } )
Curses.setpos 0, 0
if state[:dead]
Curses.addstr 'You died! :( '
Curses.addstr "You died! :(, score #{field.score} "
elsif state[:victory]
Curses.addstr 'You won!! :) '
Curses.addstr "You won!! :), score #{field.score} "
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
field.height.times do |y|

View file

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