Created
April 2, 2019 08:47
-
-
Save kencoba/5cb65480642cdc68924606f0cc41c719 to your computer and use it in GitHub Desktop.
Touch Typing with Ruby
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
# tt.rb | |
def interval () | |
start_time = (Time.now.to_f * 1000).to_i | |
lambda { | |
end_time = (Time.now.to_f * 1000).to_i | |
end_time - start_time | |
} | |
end | |
def word (level) | |
chars = "fjdkslaghvmcxzbnrueiwoqpty;:,./!\"#\$%&'()=[]{}" | |
vals = chars.split('') | |
n = [] | |
(0 .. 4).each do | |
n << rand(0 .. level) | |
end | |
w = n.map{|i| vals[i]} | |
w.join | |
end | |
def problem_line (level) | |
w = [] | |
(0 .. 7).each do | |
w << word(level) | |
end | |
w.join(' ') | |
end | |
def difference (problem_line, enter_line) | |
vec = problem_line.split('').zip(enter_line.split('')) | |
result = vec.map{|v| | |
if v[0] == v[1] then | |
' ' | |
else | |
v[0] | |
end | |
} | |
result.join | |
end | |
def count_wrong (diff_line) | |
diff_line.length - diff_line.count(' ') | |
end | |
def main (initial_level) | |
min_level = 2 | |
if min_level <= initial_level then | |
level = initial_level | |
else | |
level = min_level | |
end | |
loop do | |
printf("level: %d\n", level) | |
problem_line = problem_line(level) | |
puts problem_line | |
inter = interval | |
enter_line = gets | |
diff_line = difference(problem_line, enter_line) | |
puts diff_line | |
wrongs = count_wrong(diff_line) | |
if enter_line == "" then | |
else | |
if wrongs < 2 && inter.call <= 25000 then | |
level=level+1 | |
end | |
if 3 <= wrongs then | |
if 2 < level then | |
level = level - 1 | |
end | |
end | |
end | |
end | |
end | |
main 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment