-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
finish GemCity #16
Open
dommichalec
wants to merge
1
commit into
paircolumbus:master
Choose a base branch
from
dommichalec:GemCity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
finish GemCity #16
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class TypeError < StandardError; end | ||
|
||
# start GemCity class | ||
class GemCity | ||
attr_reader :thieves, :officers, :population, :civilians | ||
# This class represents the town of GemCity | ||
# This is a town riddled with crime but we can find out how happy the town is | ||
|
||
def initialize | ||
@thieves = 5 | ||
@officers = 1 | ||
@population = 50 | ||
@civilians = @population - @thieves - @officers | ||
end | ||
|
||
def thieves=(number) | ||
fail TypeError, 'Input must be an integer' unless number.is_a? Integer | ||
@thieves = number | ||
end | ||
|
||
def officers=(number) | ||
fail TypeError, 'Input must be an integer' unless number.is_a? Integer | ||
@officers = number | ||
end | ||
|
||
def civilians=(number) | ||
fail TypeError, 'Input must be an integer' unless number.is_a? Integer | ||
@civilians = number | ||
end | ||
|
||
def population=(number) | ||
fail TypeError, 'Input must be an integer' unless number.is_a? Integer | ||
@population = number | ||
end | ||
|
||
def demographics | ||
@demographics = { 'thieves' => rounded_percentage(thieves), | ||
'officers' => rounded_percentage(officers), | ||
'civilians' => rounded_percentage(civilians) } | ||
end | ||
|
||
def happiness_of_town | ||
# happiness is random... people don't know what they want! | ||
happiness_values = [] | ||
happiness = 0 | ||
counter = 0 | ||
while counter < population | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can simplify this block down to one line by using a Range and |
||
happiness_values.push(rand((100 - successful_crime_rate)..100)) | ||
counter += 1 | ||
end | ||
happiness_values.each { |value| happiness += value } | ||
happiness / 100 | ||
end | ||
|
||
def successful_crime_rate | ||
thieves <= 0 || officers > thieves ? 0 : odds_percent | ||
end | ||
|
||
private | ||
|
||
def odds_percent | ||
(1 - officers.to_f / thieves.to_f) * 100 | ||
end | ||
|
||
def rounded_percentage(person_type) | ||
((person_type.to_f / population.to_f) * 100).to_i.to_s + '%' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
require 'rspec' | ||
require_relative 'GemCity' # This line may need to be changed | ||
require_relative 'gem_city' # This line may need to be changed | ||
|
||
describe 'Gem City' do | ||
let(:city) { GemCity.new } | ||
|
@@ -15,49 +15,50 @@ | |
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 | ||
|
||
context 'happiness' do | ||
it 'on initialize' do | ||
expect((10..50).include? city.happiness_of_town).to eq(true) | ||
expect((10..50).cover?(city.happiness_of_town)).to eq(true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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.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 | ||
city.civilians = 15 | ||
demographics = city.demographics | ||
expect(demographics['thieves']).to eq('20%') | ||
expect(demographics['officers']).to eq('50%') | ||
expect(demographics['civilians']).to eq('30%') | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check out the
Integer
method: http://ruby-doc.org/core-2.0.0/Kernel.html#method-i-Integer