Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created July 28, 2020 15:55
Show Gist options
  • Save rodloboz/b93b71911a65328d97da0a281e227b1a to your computer and use it in GitHub Desktop.
Save rodloboz/b93b71911a65328d97da0a281e227b1a to your computer and use it in GitHub Desktop.
Livecode - Day 1
# Write a game where the player has to guess a
# random price between 1 and 100 chosen by the program.
# The program should keep asking until the player
# guesses the right price.
# When the guess is right, the program displays how
# many guesses it took the player to win.
# generate random price between 1 and 100
# ask the player for a number (between 1 to 100)
# store attempt number
# store the player's guess
# compare the stored player's guess with the random price
# if the same end game
# otherwise keep asking (LOOP)
price = rand(1..100)
guess = nil
counter = 0
while guess != price do
counter += 1 # counter = counter + 1
puts "Wrong guess! Try again" unless guess.nil?
puts "Pick a number between 1 and 100:"
print "> "
guess = gets.chomp.to_i
end
puts "You win!"
puts "It took you #{counter} attempts!"
puts "Guess: #{guess}, price: #{price}"
require 'date'
def days_until_xmas(date)
# TODO: Calculate days until xmas
# Check if the date is before xmas day
#If before xmas minus date
christmas_date = Date.new(date.year,12,25)
if date > christmas_date
christmas_date = Date.new(date.year + 1,12,25)
end
(christmas_date - date).to_i
# use Date.new
# Else xmas next year minus date
end
puts "#days_until_xmas:"
if days_until_xmas(Date.new(2020, 01, 23)).class == Integer
puts "PASSED! πŸš€ - Should return an integer"
else
puts "FAIL! ❌ - Should return an integer"
end
if days_until_xmas(Date.new(2020, 12, 25)) == 0
puts "PASSED! πŸš€ - Should return 0 when date is December 25"
else
puts "FAIL! ❌ - Should return 0 when date is December 25"
end
if days_until_xmas(Date.new(2020, 12, 26)) == 364
puts "PASSED! πŸš€ - Should return 364 when date is December 26"
else
puts "FAIL! ❌ - Should return 0 when date is December 26"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment