Created
October 20, 2022 07:11
-
-
Save NNBnh/06da7115e83de61c8c95b61351e41322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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