Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active August 17, 2023 20:17
Show Gist options
  • Save ericboehs/48c51a930770b78831a297c12857b2fb to your computer and use it in GitHub Desktop.
Save ericboehs/48c51a930770b78831a297c12857b2fb to your computer and use it in GitHub Desktop.
# Fetch stdgems.json if not exist
unless File.exist?('stdgems.json')
puts "Fetching stdgems.json...\n\n"
`curl -s -o stdgems.json https://stdgems.org/stdgems.json`
end
require 'json'
stdgems_json = File.read('stdgems.json')
stdgems = JSON.parse(stdgems_json)
game_gems = stdgems['gems'].sample(10)
score = 0
puts "Welcome to the RubyGems game!\n"
puts "Do you know your gems? Let's find out.\n\n"
puts "You will be given a gem name and four descriptions."
puts "Choose the correct description for the gem name."
puts "You will be given 10 gems to guess."
puts "You will get 1 point for each correct guess and lose 1 point for each incorrect guess.\n\n"
puts "Let's get started!\n\n"
gets
high_score = File.read('high_score.txt').to_i rescue nil
puts "High score: #{high_score || 'None yet'}"
game_gems.each do |gem|
game_choices = [gem['description'], stdgems['gems'].sample['description'], stdgems['gems'].sample['description'], stdgems['gems'].sample['description']].shuffle
puts "Current score: #{score}\n\n"
puts "What is the description for the gem named '#{gem['gem']}'?"
game_choices.each_with_index do |choice, index|
puts "#{index + 1}. #{choice}"
end
puts "Enter your answer: "
answer = gets.chomp.to_i
if game_choices[answer - 1] == gem['description']
puts "Correct!"
score += 1
else
score -= 1
puts "Incorrect!"
end
puts "\n\n-------\n\n"
end
puts "Final score: #{score}"
high_score = File.write('high_score.txt', score) if high_score.nil? || score > high_score
@ericboehs
Copy link
Author

ericboehs commented Aug 17, 2023

To play:

curl -sL https://gist.github.com/ericboehs/48c51a930770b78831a297c12857b2fb/raw/play.rb > play.rb
ruby play.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment