Make a typing game with ruby

Hello. This time I will make a typing game in Ruby. If you want to try it for the time being, please try it from here.

Then how to make it? I will write.

[1] Collect words

This time it was troublesome, so I only use 100 English words, but please add as you like. If you change it a little, you should be able to speak Japanese (although it seems to be troublesome to support Hepburn romanization) (Is it okay to set the Hash value to list?

Well, I think you can do it properly here. It ’s not that important.

[2] Get the key event

You can do it easily with ʻio / console`. Ruby is easy to do like this, isn't it?

require 'io/console'

ch = STDIN.getch
puts ch

[3] Write highlights and core parts at once

It is assumed that words contains a list of words written in [1].

require 'io/console'

miss = 0
all = 0
flag = false

while true
  word = words.sample
  puts "\e[36m#{word}\e[0m"
  i = -1
  wl = word.length
  print "\e[2m#{word}\e[0m"
  while i != (wl-1)
    i += 1
    key = STDIN.getch
    all += 1
    if key == word[i]
      print "\r#{word[0..i]}\e[2m#{word[i+1..wl]}\e[0m"
    elsif key == "\C-c"
      flag = true
      break
    else
      print "\r#{word[0...i]}\e[31m#{word[i]}\e[0m\e[2m#{word[i+1..wl]}\e[0m"
      miss += 1
      i -= 1
    end
  end
  break if flag
  puts
end

Well, it's simple to do, so you'll understand. \ r is a carriage return and \ e [~~ m is an ANSI escape sequence.

Display grades

require 'io/console'
require 'benchmark'

miss = 0
all = 0
flag = false

result = Benchmark.realtime do
  while true
    word = words.sample
    puts "\e[36m#{word}\e[0m"
    i = -1
    wl = word.length
    print "\e[2m#{word}\e[0m"
    while i != (wl-1)
      i += 1
      key = STDIN.getch
      all += 1
      if key == word[i]
        print "\r#{word[0..i]}\e[2m#{word[i+1..wl]}\e[0m"
      elsif key == "\C-c"
        flag = true
        break
      else
        print "\r#{word[0...i]}\e[31m#{word[i]}\e[0m\e[2m#{word[i+1..wl]}\e[0m"
        miss += 1
        i -= 1
      end
    end
    break if flag
    puts
  end
end


print "\e[2K\e[1A\e[2K\r"
puts "miss: #{miss}"
printf "press: %.5f/s\n" %(all / result)

Well this is also simple. The time until C-c is done with Benchmark.realtime is measured, and the number of keystrokes per second is calculated based on that. \ e [~~ K, \ e [~~ A are also ANSI escape sequences.

[4] Run!

ruby main.rb

Recommended Posts

Make a typing game with ruby
Creating a Cee-lo game with Ruby 4th Creating a game progress process
Let's make a LINE Bot with Ruby + Sinatra --Part 2
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Make a digging maze with Ruby2D
Make a slideshow tool with JavaFX
[Beginner] Try to make a simple RPG game with Java ①
Make a garbage reminder with line-bot-sdk-java
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Make a list map with LazyMap
Making a Cee-lo game with Ruby Final improvement after receiving a review
With ruby ● × Game and Othello (basic review)
Make a note of Ruby keyword arguments
Let's make a Christmas card with Processing!
Make a family todo list with Sinatra
Let's make draw poker with ruby ~ Preparation ~
I made a risky die with Ruby
Make a login function with Rails anyway
[docker] [nginx] Make a simple ALB with nginx
Extract a part of a string with Ruby
Start with a browser Ruby game programming: Introduction to Nyle-canvas (DXRuby style)
AtCoder Beginner Contest 169 A, B, C with ruby
Creating a browser automation tool with Ruby + Selenium
Let's make a search function with Rails (ransack)
Make System.out a Mock with Spock Test Framework
A simple rock-paper-scissors game with JavaFX and SceneBuilder
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
Run Scala with GraalVM & make it a native image
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
Design and implement a breakout game with a clean architecture
How to make LINE messaging function made with Ruby
[Ruby] Generate a concatenated QR code with rqrcode (Knowledge)
[Beginner] Create a competitive game with basic Java knowledge
Design and implement a breakout game with a clean architecture
[Ruby] I made a crawler with anemone and nokogiri.
[Java basics] Let's make a triangle with a for statement
Getting Started with Ruby
[Personal application work memo] Make a calendar with simple_calendar
[Ruby] Generate a concatenated QR code with rqrcode (Practice)
Make a reflection utility ②
Make a reflection utility ③
Make a reflection utility ①
Evolve Eevee with Ruby
[Ruby on Rails] Add a column with a foreign key constraint
I want to make a list with kotlin and java!
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ① ~ Hello World ~
I searched for a web framework with Gem in Ruby
Let's make a simple API with EC2 + RDS + Spring boot ①
Extract a string starting with a capital letter with a regular expression (Ruby)
Install Ruby 3.0.0 Preview 1 with a combination of Homebrew and rbenv
Make a simple CRUD with SpringBoot + JPA + Thymeleaf ⑤ ~ Template standardization ~
[Ruby] Extracting a two-dimensional array
Ruby version switching with rbenv
What is a Ruby module?
I tried DI with Ruby
Creating a calendar using Ruby
Ruby: Send email with Starttls
Format Ruby with VS Code
Integer check method with ruby
Multiplication in a Ruby array
[Java] Make it a constant