Skip to content

Commit

Permalink
Add function for calculating grid chip count
Browse files Browse the repository at this point in the history
This function will be needed to send the chip count to the analytics
server when a game is won.
  • Loading branch information
caleb531 committed Jun 14, 2017
1 parent 6c0ae8a commit 24213f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions app/scripts/models/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ Grid.prototype.checkIfFull = function () {
});
};

// Return the total number of chips currently placed on the grid
Grid.prototype.getChipCount = function () {
return this.columns.reduce(function (sum, column) {
return sum + column.length;
}, 0);
};

// Reset the grid by removing all placed chips
Grid.prototype.resetGrid = function () {
this.columns.forEach(function (column) {
Expand Down
22 changes: 22 additions & 0 deletions test/grid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ describe('grid', function () {
expect(grid.checkIfFull()).to.be.false;
});

it('should count current number of chips when grid is empty', function () {
var grid = new Grid({
columnCount: 9,
rowCount: 8
});
expect(grid.getChipCount()).to.equal(0);
});

it('should count current number of chips', function () {
var grid = new Grid({
columnCount: 9,
rowCount: 8
});
var player = new Player({color: 'red', name: 'Bob'});
_.times(6, function (c) {
_.times(4, function () {
grid.placeChip({column: c, chip: new Chip({player: player})});
});
});
expect(grid.getChipCount()).to.equal(24);
});

it('should reset', function () {
var grid = new Grid({
columnCount: 9,
Expand Down

0 comments on commit 24213f1

Please sign in to comment.