diff --git a/GemCity.rb b/GemCity.rb deleted file mode 100644 index 690dc84..0000000 --- a/GemCity.rb +++ /dev/null @@ -1,56 +0,0 @@ -class GemCity - -=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 - end - - def officers - return @people[:Officers] - end - - def population; return @population; end - - def setOfficers officers - @people[:Officers] = officers - 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 - 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 - end -end \ No newline at end of file diff --git a/Gemfile b/Gemfile index 94b6f8c..6fd63b3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ source 'https://rubygems.org' gem 'rubocop', require: false -gem 'rspec' \ No newline at end of file +gem 'rspec' diff --git a/gem_city.rb b/gem_city.rb new file mode 100644 index 0000000..d73d4bf --- /dev/null +++ b/gem_city.rb @@ -0,0 +1,48 @@ +# 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 + @people = { thieves: 5, officers: 1 } + @population = 50 + end + + def thieves(thieves_number = @people[:thieves]) + @people[:thieves] = thieves_number + end + + def officers + @people[:officers] + end + + def officers=(officers) + @people[:officers] = officers + end + + def happiness_of_town + # happiness is random... people don't know what they want! + happiness_vals = [] + happiness = 0 + (1..@population).each do + happiness_vals.push(rand((100 - successful_crime_rate)..100)) + end + happiness_vals.each do |value| + happiness += value + end + happiness / 100 + end + + def successful_crime_rate + thieves = @people[:thieves] + officers = @people[:officers] + if thieves <= 0 + 0 + elsif officers > thieves + 0 + else + odds = 1 / (-officers.to_f / thieves.to_f) + odds * 100 + end + end +end