Skip to content

Instantly share code, notes, and snippets.

@shiopon01
Created February 26, 2018 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiopon01/367412e67e781ed6fbff26150f59d23a to your computer and use it in GitHub Desktop.
Save shiopon01/367412e67e781ed6fbff26150f59d23a to your computer and use it in GitHub Desktop.
typingGame
require 'io/console'
require 'benchmark'
class Enword
def initialize
@words = []
#名詞(身の回りの身近なもの)
@words.concat(["egg", "bag", "rose", "chair", "bat", "fish", "notebook", "pencil", "dog", "desk", "watch", "mitt", "milk", "flower", "door", "boat", "piano", "orange", "bird", "sheep", "cup", "bus", "apple", "fruit", "car", "cake", "picture", "cat", "stamp", "plane", "book", "racket", "glass", "bed", "letter", "tape", "cap", "mail", "box", "bread", "doll", "table", "tree", "pen", "map", "cow", "pot", "camera", "hand", "lemon"])
#名詞(場所、建物)
@words.concat(["yard", "bank", "library", "hospital", "hotel", "village", "kitchen", "wall", "park", "girl", "father", "doctor", "pilot", "man", "cook", "sister", "mother", "city", "window", "church", "school", "country", "office", "garden", "town", "brother", "woman", "citizen", "parent", "student", "clerk", "family", "teacher", "player", "floor", "store", "station", "house", "field", "japan", "gate", "room", "aunt", "boy", "son", "nurse", "friend", "uncle", "singer", "daughter"])
#名詞(日課、運動、時、月、曜日)
@words.concat(["evening", "morning", "night", "noon", "tomorrow", "time", "afternoon", "diary", "meal", "breakfast", "lunch", "dinner", "supper", "walk", "work", "basketball", "baseball", "tennis", "homework", "season", "spring", "summer", "autumn", "fall", "winter", "minute", "week", "month", "year", "hour", "day", "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december", "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"])
end
def easy # 5文字以下の単語
@words.select{|n| n.length <= 5 }.sample(10)
end
def normal # すべての単語から
@words.sample(10)
end
def hard # 8文字以上の単語
@words.select{|n| n.length >= 8 }.sample(10)
end
end
# 初期設定
@ok = 0
@type = 0
enword = Enword.new()
puts '英単語タイピングゲーム(10単語)'
puts '[1: かんたん ] (5文字以下)'
puts '[2: ふつう ] (全単語)'
puts '[3: むずかしい ] (8文字以上)'
puts '[0: 終了 ]'
print '> '
# ゲームモード選択
while c = STDIN.getch
case c
when '1'
puts 'かんたんで開始'
qwords = enword.easy
break
when '2'
puts 'ふつうで開始'
qwords = enword.normal
break
when '3'
puts 'むずかしいで開始'
qwords = enword.hard
break
when '0'
puts '終了'
exit
when ?\C-c
puts '終了'
exit
end
end
# カウントダウン
puts ' 3 3 3'
sleep(1)
puts ' 2 2 2'
sleep(1)
puts ' 1 1 1'
sleep(1)
puts ''
# 開始
result = Benchmark.realtime do
10.times do |n|
word = qwords[n].split('')
if n+1 < 10
puts "#{n+1} . #{qwords[n]}"
print ' > '
elsif n+1 >= 10
puts "#{n+1}. #{qwords[n]}"
print ' > '
end
word.each do |w|
while c = STDIN.getch do
@type += 1
if c == w
print c
@ok += 1
break
end
exit if c == ?\C-c
end
end
puts ''
puts ''
end
end
# 結果発表だどん
wps = @type.to_f/result
wps = wps.to_s.match(/.*\.../)
result = result.to_s.match(/.*\.../)
puts "総タイプ数 : #{@type} 回"
puts "正解タイプ数: #{@ok} 回"
puts "ミスタイプ数: #{@type - @ok} 回"
puts "経過時間 : #{result} 秒"
puts "1秒あたりのタイプ速度: #{wps} 打"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment