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
65 changes: 21 additions & 44 deletions GemCity.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,33 @@
class GemCity
# This class represents the town of GemCity
# This is a town riddled with crime but we can find out how happy the town is
attr_reader :population
attr_accessor :officers, :thieves

=begin
This class represents the town of GemCity
This is a town riddled with crime but we can find out how happy the town is
=end
def initialize
@people, @population = {
:thieves => 5,
:Officers => 1},
50
end

def thieves thieves_number=@people[:thieves]
return @people[:thieves] = thieves_number
@thieves = 5
@officers = 1
@population = 50
end

def officers
return @people[:Officers]
def happiness_of_town
# happiness is random... people don't know what they want!
happiness = 0
population.times { happiness += rand((100 - successful_crime_rate)..100) }
happiness / 100
Copy link
Contributor

Choose a reason for hiding this comment

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

100 is used throughout this file but to someone reading it might appear as a 'magic number'. You might want to use a constant to make some of these formulas more readable

Copy link
Member

Choose a reason for hiding this comment

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

Good point 👍 If you also move it to a constant, you only have to make a single change if the magic values changes. Imagine a scenario where this is thousands of lines of code.

end

def population; return @population; end

def setOfficers officers
@people[:Officers] = officers
def successful_crime_rate
thieves <= 0 || officers > thieves ? 0 : 100 * (1 - officers / thieves.to_f)
end

def happiness_of_town
#happiness is random... people don't know what they want!
happinessVals = Array.new
happiness = 0
for index in (1..@population)
happinessVals.push(rand((100 - successful_crime_rate) .. 100))
index += 1
end
happinessVals.each do |value|
happiness += value
end
return happiness / 100
def city_demographics
{ thieves: demographics_format(thieves),
officers: demographics_format(officers),
civilians: demographics_format(population - thieves - officers) }
end

def successful_crime_rate
thieves = @people[:thieves]
officers = @people[:Officers]
if thieves <= 0
odds_percent = 0
elsif officers > thieves
odds_percent = 0
else
odds = 1 \
- officers.to_f / thieves.to_f
odds_percent = odds * 100
end
return odds_percent
def demographics_format(people)
"#{100 * people / @population}%"
end
end
end
46 changes: 23 additions & 23 deletions gem_city_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
end

it 'officers = thieves' do
city.thieves 1
city.setOfficers 1 # This line may need to be changed
city.thieves = 1
city.officers = 1 # This line may need to be changed
expect(city.successful_crime_rate).to eq(0)
end

it 'thieves = 0' do
city.thieves 0
city.thieves = 0
expect(city.successful_crime_rate).to eq(0)
end

it 'officers > thieves' do
city.thieves 1
city.setOfficers 2 # This line may need to be changed
city.thieves = 1
city.officers = 2 # This line may need to be changed
expect(city.successful_crime_rate).to eq(0)
end
end
Expand All @@ -38,26 +38,26 @@
end

it 'successful_crime_rate = 0' do
city.thieves 0
city.thieves = 0
expect(city.happiness_of_town).to eq(50)
end
end

# context 'city_demographics' do
# it 'initialize' do
# demographics = city.city_demographics
# expect(demographics[:thieves]).to eq('10%')
# expect(demographics[:officers]).to eq('2%')
# expect(demographics[:civilians]).to eq('88%')
# end

# it 'thieves = 10, officers = 25' do
# city.thieves = 10
# city.officers = 25
# demographics = city.city_demographics
# expect(demographics[:thieves]).to eq('20%')
# expect(demographics[:officers]).to eq('50%')
# expect(demographics[:civilians]).to eq('30%')
# end
# end
context 'city_demographics' do
it 'initialize' do
demographics = city.city_demographics
expect(demographics[:thieves]).to eq('10%')
expect(demographics[:officers]).to eq('2%')
expect(demographics[:civilians]).to eq('88%')
end

it 'thieves = 10, officers = 25' do
city.thieves = 10
city.officers = 25
demographics = city.city_demographics
expect(demographics[:thieves]).to eq('20%')
expect(demographics[:officers]).to eq('50%')
expect(demographics[:civilians]).to eq('30%')
end
end
end