Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Japanese Alphabets Quiz (See related posts)

This is my first Ruby script. It requires two files, hiragana.rb and katakana.rb (I'm pasting them here as well).

Its purpose is to test your knowledge of two Japanese alphabets, Hiragana and Katakana.

# Japanese Alphabets Quiz, version 1.3 :)

puts "Would you rather test your hiragana (1) or katakana (2) knowledge?"

choose = gets.chomp.to_i

if 
	choose == 1
	puts "OK, hiragana, then!"
	require 'hiragana.rb'
	lang_name = "hiragana"
else
	puts "OK, katakana, then!"
	require 'katakana.rb'
	lang_name = "katakana"
end

$score = 0 

10.times do 

	alphabet = $characters
	
	# Select a random pair of key and value from the given hash
	
	character = alphabet.sort_by{ rand }[0...1] 
	
	# Turn the result into an array
	
	pair = character.shift { |key, value| }
	
	# Get the first array value (was the hash key)
	
	question = pair.first
	
	# Ask the question
	
	puts "Which character is " + question + "?"
	
	# Gets the answer from the user, strip spaces and turn it lowercase to match the hash case anyway
	
	answer = gets.chomp.strip.downcase
	
	# Validate user input
	
	if 
		answer == pair.last
		$score += 1
		puts "Correct! " + question + " is " + pair.last + "."
	else
		puts "Wrong! " + question + " is " + pair.last + "."
	end

end

def grade (points)

	case points
	when (0..3)
		"Bad"
	when (4..6)
		"Average"
	when (7..9)
		"Excellent"
	when (10)
		"Perfect"
	end

end

puts "End of " + lang_name + " quiz. You scored " + $score.to_s + " out of 10."

puts "Your grade is " + grade( $score ) + "."


hiragana.rb
# In this hash we set the basic hiragana characters and their respective phonetic counterparts

$characters = 
	{
	'' => 'a',
	'' => 'i',
	'' => 'u',
	'' => 'e',
	'' => 'o',
	'' => 'ka',
	'' => 'ki',
	'' => 'ku',
	'' => 'ke',
	'' => 'ko',
	'' => 'sa',
	'' => 'si',
	'' => 'su',
	'' => 'se',
	'' => 'so',
	'' => 'ta',
	'' => 'ti',
	'' => 'tu',
	'' => 'te',
	'' => 'to',
	'' => 'na',
	'' => 'ni',
	'' => 'nu',
	'' => 'ne',
	'' => 'no',
	'' => 'ha',
	'' => 'hi',
	'' => 'hu',
	'' => 'he',
	'' => 'ho',
	'' => 'ma',
	'' => 'mi',
	'' => 'mu',
	'' => 'me',
	'' => 'mo',
	'' => 'ya',
	'' => 'yu',
	'' => 'yo',
	'' => 'ra',
	'' => 'ri',
	'' => 'ru',
	'' => 're',
	'' => 'ro',
	'' => 'wa',
	'' => 'wo',
	'' => 'n'
	}


# In this hash we set the basic katakana characters and their respective phonetic counterparts

$characters =
		{
		'' => 'a',
		'' => 'i',
		'' => 'u',
		'' => 'e',
		'' => 'o',
		'' => 'ka',
		'' => 'ki',
		'' => 'ku',
		'' => 'ke',
		'' => 'ko',
		'' => 'sa',
		'' => 'si',
		'' => 'su',
		'' => 'se',
		'' => 'so',
		'' => 'ta',
		'' => 'ti',
		'' => 'tu',
		'' => 'te',
		'' => 'to',
		'' => 'na',
		'' => 'ni',
		'' => 'nu',
		'' => 'ne',
		'' => 'no',
		'' => 'ha',
		'' => 'hi',
		'' => 'hu',
		'' => 'he',
		'' => 'ho',
		'' => 'ma',
		'' => 'mi',
		'' => 'mu',
		'' => 'me',
		'' => 'mo',
		'' => 'ya',
		'' => 'yi',
		'' => 'yu',
		'' => 'yo',
		'' => 'ra',
		'' => 'ri',
		'' => 'ru',
		'' => 're',
		'' => 'ro',
		'' => 'wa',
		'' => 'wy',
		'' => 'wo',
	}

You need to create an account or log in to post comments to this site.