Skip to content

Instantly share code, notes, and snippets.

@arthur-littm
Created October 14, 2020 17:03
Show Gist options
  • Save arthur-littm/802c62e4bf5f28af73b870be38d757f2 to your computer and use it in GitHub Desktop.
Save arthur-littm/802c62e4bf5f28af73b870be38d757f2 to your computer and use it in GitHub Desktop.
class Citizen
# DATA
def initialize(first_name, last_name, age) # called when doing Citizen.new
# instance variables
@first_name = first_name
@last_name = last_name
@age = age
end
# BEHAVIOR
def can_vote?
if @age >= 18
return true
else
return false
end
end
def full_name
first = @first_name.capitalize
last = @last_name.capitalize
full_name = first + " " + last
return full_name
end
end
# john = Citizen.new('John', 'Doe', 17)
# arthur = Citizen.new('Arthur', 'Littmann', 26)
# p john.can_vote?
# #
class SlotMachine
SCORES = {
joker: 50,
star: 40,
bell: 30,
seven: 20,
cherry: 10
}
def initialize
@score = 0
end
def score(reels)
# 1. check that there are three elements the same
three_the_same = reels.uniq.length == 1
# ['joker', 'bell', 'joker']
# ['star', 'star', 'joker']
two_the_same = reels.include?('joker') && reels.uniq.length == 2
if three_the_same
key = reels[0].to_sym
return SCORES[key]
elsif two_the_same
key = reels.sort[1].to_sym
return SCORES[key] / 2
else
return 0
end
end
end
slot_machine = SlotMachine.new()
# score = slot_machine.score(['joker', 'joker', 'joker'])
# score = slot_machine.score(['joker', 'bell', 'joker'])
score = slot_machine.score(['star', 'star', 'joker'])
p score
# Joker 🤩 50 25 (2 jokers + anything)
# Star ⭐️ 40 20
# Bell 🛎 30 15
# Seven 7️⃣ 20 10
# Cherry 🍒 10 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment