-
Notifications
You must be signed in to change notification settings - Fork 21
Fairly simple, demographics implemented, all green #8
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
Open
sampriddy
wants to merge
8
commits into
paircolumbus:master
Choose a base branch
from
sampriddy:master
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d50e207
Commit initial solutoin
8987056
Get rid of getter and setter functions, everything is broken
2e63b9c
Refactor for conciseness
0ed3b84
Add GemCity#city_demographics method
eb0f9c0
Uncomment demographics tests, all green
df3d842
Fix as many RuboCop complaints as possible
5da9db8
Final refactoring
76a22da
Remove a few more @ signs
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 contains hidden or 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,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 | ||
| 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 | ||
This file contains hidden or 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
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.
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
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.
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.