Skip to content

Instantly share code, notes, and snippets.

@NNBnh
Created October 20, 2022 07:11
Show Gist options
  • Save NNBnh/06da7115e83de61c8c95b61351e41322 to your computer and use it in GitHub Desktop.
Save NNBnh/06da7115e83de61c8c95b61351e41322 to your computer and use it in GitHub Desktop.
require "tty-reader"
class HighLow
@@DESK = [?A, *'2'..'10', ?J, ?Q, ?K].product(%w[♠ ♥ ♦ ♣])
def initialize
@deck = @@DESK.shuffle
@score = 0
end
attr_reader :deck
attr_reader :score
def play
prompt = TTY::Reader.new
last = nil
@deck.each do
current = [?A, *'2'..'10', ?J, ?Q, ?K].index(_1)
unless last.nil?
correct = nil
while correct.nil? do
case prompt.read_keypress
when "\e[B" then correct = current <= last
when "\e[A" then correct = current >= last
end
end
@score += 1 if correct
end
puts " #{@score.to_s.ljust(2)} | #{_1} #{_2}"
last = current
end
@score
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment