-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add source maps; add trap location generator
- Loading branch information
Showing
6 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,25 @@ | ||
class LocationGenerator { | ||
getRandomInt(max) { | ||
return Math.floor(Math.random() * Math.floor(max)); | ||
} | ||
|
||
generateLocations(width, height, trapCount) { | ||
const traps = {}; | ||
|
||
for (let n = 0; n < trapCount; n++) { | ||
const x = this.getRandomInt(width); | ||
const y = this.getRandomInt(height); | ||
|
||
if (traps[x]) { | ||
traps[x].push(y); | ||
} | ||
else { | ||
traps[x] = [y]; | ||
} | ||
} | ||
|
||
return traps; | ||
} | ||
} | ||
|
||
export default LocationGenerator; |
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,52 @@ | ||
/* global describe, it */ | ||
|
||
import { expect } from 'chai'; | ||
import Generator from '../../src/services/trap-location-generator'; | ||
|
||
describe('trap location generator', function() { | ||
it('returns new trap locations', function() { | ||
|
||
// Arrange | ||
|
||
const count = 3; | ||
const height = 3; | ||
const width = 3; | ||
|
||
// Act | ||
|
||
const generator = new Generator(); | ||
const locations = generator.generateLocations(width, height, count); | ||
|
||
// Assert | ||
|
||
const keys = Object.keys(locations); | ||
expect(keys.length).to.be.greaterThan(0); | ||
|
||
keys.forEach(xValue => { | ||
expect(Number(xValue), 'x co-ordinate should be at least 0').to.be.at.least(0); | ||
|
||
locations[xValue].forEach(yValue => { | ||
expect(yValue, 'y co-ordinate should be at least 0').to.be.at.least(0); | ||
}); | ||
}); | ||
}); | ||
|
||
it('returns different locations each time', function() { | ||
|
||
// Arrange | ||
|
||
const count = 3; | ||
const height = 3; | ||
const width = 3; | ||
|
||
// Act | ||
|
||
const generator = new Generator(); | ||
const locations1 = generator.generateLocations(width, height, count); | ||
const locations2 = generator.generateLocations(width, height, count); | ||
|
||
// Assert | ||
|
||
expect(locations1).not.to.deep.equal(locations2); | ||
}); | ||
}); |
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