*.rb ⇒ ruby/, ncurses split and optimization

This commit is contained in:
Gwenhael Le Moine 2022-09-14 16:17:32 +02:00
parent c86a191aa7
commit 7841e780ba
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
2 changed files with 111 additions and 91 deletions

110
ruby/myhunt-curses.rb Executable file
View file

@ -0,0 +1,110 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'curses'
gem 'ruby-termios', require: 'termios'
end
require 'myhunt'
def display_curses( field, state = { dead: false, victory: false } )
Curses.setpos 0, 0
if state[:dead]
Curses.addstr 'You died! :('
elsif state[:victory]
Curses.addstr 'You won!! :)'
else
Curses.addstr "nearby: #{field.count_nearby_mines}"
end
field.height.times do |y|
Curses.setpos y + 2, 0
field.width.times do |x|
if [field.explorer_x, field.explorer_y] == [x, y]
Curses.addch 'o'
elsif [field.width, field.height] == [x + 1, y + 1]
Curses.addch '_'
elsif field.field[[x, y]].open
Curses.addch field.field[[x, y]].mine ? '*' : ' '
else
Curses.addch 'H'
end
end
end
Curses.refresh
end
def ncurses_main
field = MyHunt::Board.new
finished = false
Curses.init_screen
Curses.cbreak
Curses.noecho
Curses.stdscr.keypad = true
orig_termios = Termios.tcgetattr($stdin)
at_exit do
# Curses.close_screen
Termios.tcsetattr($stdin, Termios::TCSANOW, orig_termios)
end
display_curses( field )
loop do
direction = ''
ch = Curses.getch
if finished
begin
case ch
when 'r'
field = MyHunt::Board.new
finished = false
else
break
end
rescue Curses::RequestDeniedError
'prout'
end
else
begin
case ch
when '1'
direction = 'down-left'
when '2', Curses::KEY_DOWN
direction = 'down'
when '3'
direction = 'down-right'
when '4', Curses::KEY_LEFT
direction = 'left'
when '6', Curses::KEY_RIGHT
direction = 'right'
when '7'
direction = 'up-left'
when '8', Curses::KEY_UP
direction = 'up'
when '9'
direction = 'up-right'
when 'q'
break
end
rescue Curses::RequestDeniedError
'prout'
end
end
state = field.move( direction )
finished = state[:dead] || state[:victory]
display_curses( field, state )
end
exit!
end
ncurses_main

View file

@ -1,6 +1,5 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true
require 'curses'
module MyHunt module MyHunt
class Cell class Cell
@ -109,92 +108,3 @@ module MyHunt
end end
end end
end end
field = MyHunt::Board.new
def display_curses( field, state = { dead: false, victory: false } )
Curses.setpos 0, 0
if state[:dead]
Curses.addstr 'You died! :('
elsif state[:victory]
Curses.addstr 'You won!! :)'
else
Curses.addstr "nearby: #{field.count_nearby_mines}"
end
field.height.times do |y|
field.width.times do |x|
Curses.setpos y + 2, x
if [field.explorer_x, field.explorer_y] == [x, y]
Curses.addch 'o'
elsif [field.width, field.height] == [x + 1, y + 1]
Curses.addch '_'
elsif field.field[[x, y]].open
Curses.addch field.field[[x, y]].mine ? '*' : ' '
else
Curses.addch 'H'
end
end
end
Curses.refresh
end
Curses.init_screen
Curses.cbreak
Curses.noecho
Curses.stdscr.keypad = true
at_exit do
Curses.close_screen
end
finished = false
display_curses( field )
loop do
direction = ''
ch = Curses.getch
if finished
begin
case ch
when 'r'
field = MyHunt::Board.new
finished = false
else
break
end
rescue Curses::RequestDeniedError
'prout'
end
else
begin
case ch
when '1'
direction = 'down-left'
when '2', Curses::KEY_DOWN
direction = 'down'
when '3'
direction = 'down-right'
when '4', Curses::KEY_LEFT
direction = 'left'
when '6', Curses::KEY_RIGHT
direction = 'right'
when '7'
direction = 'up-left'
when '8', Curses::KEY_UP
direction = 'up'
when '9'
direction = 'up-right'
when 'q'
break
end
rescue Curses::RequestDeniedError
'prout'
end
end
state = field.move( direction )
finished = state[:dead] || state[:victory]
display_curses( field, state )
end