Skip to content

Instantly share code, notes, and snippets.

Last active August 17, 2023 20:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
# Fetch stdgems.json if not exist
unless File.exist?('stdgems.json')
puts "Fetching stdgems.json...\n\n"
`curl -s -o stdgems.json`
require 'json'
stdgems_json ='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"
high_score ='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}"
puts "Enter your answer: "
answer = gets.chomp.to_i
if game_choices[answer - 1] == gem['description']
puts "Correct!"
score += 1
score -= 1
puts "Incorrect!"
puts "\n\n-------\n\n"
puts "Final score: #{score}"
high_score = File.write('high_score.txt', score) if high_score.nil? || score > high_score
Copy link

ericboehs commented Aug 17, 2023

To play:

curl -sL > play.rb
ruby play.rb

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