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
Let's make a smart home 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 electronic music with randomness with Ruby
Make a digging maze with Ruby2D
Make a slideshow tool with JavaFX
[Beginner] Try to make a simple RPG game with Java ①
Make a Christmas tree with swift
Make a garbage reminder with line-bot-sdk-java
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Ruby Learning # 22 Building a Guessing Game
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 Ruby Kurosawa with Ruby (Ruby + LINE Messaging API)
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 ~
Make a family todo list with Sinatra
Ruby Learning # 12 Building a Mad Libs Game
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
Make a site template easily with Rails
If you want to make a zip file with Ruby, it's rubyzip.
AtCoder Beginner Contest 169 A, B, C with ruby
Creating a browser automation tool with Ruby + Selenium
Let's make draw poker with ruby ~ test-unit preparation ~
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 made a portfolio with Ruby On Rails
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished)
How to make a factory with a model with polymorphic association
I tried to make a simple game with Javafx ① "Let's find happiness game" (unfinished version ②)
Install Ruby 3.0.0 with asdf
Design and implement a breakout game with a clean architecture
How to make LINE messaging function made with Ruby
[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.
Getting Started with Ruby
[Ruby] Generate a concatenated QR code with rqrcode (Practice)
Make a reflection utility ②
Make a reflection utility ③
11th, Classification with Ruby
Evolve Eevee with Ruby
Use Coveralls with GitHub Actions in a Ruby repository
How to divide a two-dimensional array into four with ruby
[Ruby on Rails] Add a column with a foreign key constraint
In Ruby you can define a method with any name
I want to make a list with kotlin and java!
I want to make a function 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)