Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 0 additions & 56 deletions GemCity.rb

This file was deleted.

2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'

gem 'rubocop', require: false
gem 'rspec'
gem 'rspec'
43 changes: 43 additions & 0 deletions gem_city.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This class represents the town of GemCity
# This is a town riddled with crime but we can find out how happy the town is
class GemCity
attr_reader :population
def initialize
@population = { thieves: 5, officers: 1 }, 50
@people = { thieves: 5, officers: 1 }, 50
end

def thieves
thieves_number = @people[:thieves]
@people[:thieves] = thieves_number
end

def officers
@people[:officers]
end

def happiness_of_town
# happiness is random... people don't know what they want!
happiness_vals = []
happiness = 0
@population.each do
happiness_vals.push(rand((100 - successful_crime_rate)..100))
end
happiness_vals.each do |value|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use Enumerable#reduce here to simplify this line

happiness += value
end
happiness / 100
end

def successful_crime_rate
thieves = @people[:thieves]
officers = @people[:officers]
if thieves <= 0 || officers > thieves
odds_percent = 0
else
odds = 1 - officers.to_f / thieves.to_f
odds_percent = odds * 100
end
odds_percent
end
end