Last active
August 17, 2023 20:17
-
-
Save ericboehs/48c51a930770b78831a297c12857b2fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To play:
curl -sL https://gist.github.com/ericboehs/48c51a930770b78831a297c12857b2fb/raw/play.rb > play.rb ruby play.rb