Created
February 7, 2012 14:37
-
-
Save commuterjoy/1759973 to your computer and use it in GitHub Desktop.
Bowling
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
// allow summing of any numerical array | |
Array.prototype.sum = function(){ | |
return this.reduce(function(prev, current){ | |
return prev + current; | |
}); | |
}; | |
// hold the game - basically an array of competitors | |
var Game = function(){ | |
// array of competitors | |
this.competitors = []; | |
this.addCompetitor = function(competitor){ | |
this.competitors.push(competitor); | |
} | |
this.scores = function(i){ | |
return this.competitors[i].map(function(s){ | |
s.reduce(function(a){ | |
// ran out of time!! | |
}); | |
}) | |
} | |
this.scoreCard = function(){ | |
return this.competitors.map(function(c){ | |
return c.name + '|' + c.shots.join('|'); | |
}); | |
} | |
// would merge the scoreCardWithScores should be merged with this.scoreCard | |
this.scoreCardWithScores = function(){ | |
return this.competitors.map(function(c){ | |
return c.name + '|' + c.shots.map(function(a){ | |
return a.sum(); | |
}).join('|'); | |
}); | |
} | |
}; | |
// | |
var Competitor = function(opts){ | |
this.name = opts['name']; | |
this.shots = []; | |
this.addShot = function(score){ | |
if (this.shots.length < 10 && score.sum() < 10) // @todo could throw error here I suppose | |
this.shots.push(score); | |
} | |
}; | |
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
<html> | |
<head> | |
<title>FruitMachine QUnit Test Suite</title> | |
<!-- qunit dependencies --> | |
<script src="lib/jquery-latest.js"></script> | |
<link rel="stylesheet" href="lib/qunit.css" type="text/css" media="screen" /> | |
<script type="text/javascript" src="lib/qunit-git.js"></script> | |
<script type="text/javascript" src="lib/qunit-git.js"></script> | |
<script src="bowling.js"></script> | |
<script> | |
$(document).ready(function(){ | |
module("Array"); | |
test("array can be summed", function() { | |
equal([1,2,3].sum(), 6); | |
}); | |
module("Objects"); | |
test("game can be instantiated", function() { | |
ok( (new Competitor({})) ); | |
}); | |
test("game can be instantiated", function() { | |
ok( (new Game({})) ); | |
}); | |
test("game has competitors", function() { | |
var g = new Game(); | |
var c1 = new Competitor({}); | |
g.addCompetitor(c1); | |
equal(g.competitors.length, 1); | |
}); | |
test("competitor can submit a score", function() { | |
var c1 = new Competitor({}); | |
c1.addShot([2,1]); | |
equal(c1.shots.length, 1); | |
}); | |
test("competitor can submit many scores", function() { | |
var c1 = new Competitor({}); | |
c1.addShot([2,3]); | |
c1.addShot([2,1]); | |
equal(c1.shots.length, 2); | |
}); | |
test("competitor can not submit a score above 10", function() { | |
var c1 = new Competitor({}); | |
c1.addShot([2,10]); | |
equal(c1.shots.length, 0); | |
}); | |
test("competitor is identified by a name", function() { | |
var c1 = new Competitor({name:'Fred'}); | |
equal(c1.name, 'Fred'); | |
}); | |
test("can print score card out", function() { | |
var g = new Game(); | |
var c1 = new Competitor({name:'The dude'}); | |
g.addCompetitor(c1); | |
c1.addShot([2]); | |
c1.addShot([3]); | |
c1.addShot([3]); | |
equal(g.scoreCard(), "The dude|2|3|3"); | |
}); | |
test("can print score card out with multiple competitors", function() { | |
var g = new Game(); | |
var c1 = new Competitor({name:'dude'}); | |
var c2 = new Competitor({name:'Fred'}); | |
g.addCompetitor(c1); | |
g.addCompetitor(c2); | |
c1.addShot([3,2]); | |
c1.addShot([1,7]); | |
c2.addShot([8,1]); | |
c2.addShot([2,1]); | |
equal(g.scoreCard().join(':').toString(), "dude|3,2|1,7:Fred|8,1|2,1"); | |
}); | |
// constraint | |
test("competitor can only make 10 shots (really ten pairs of two shots)", function() { | |
var c1 = new Competitor({}); | |
[1,2,3,4,5,6,7,8,9,10,11].forEach(function(){ | |
c1.addShot([1,2]); | |
}); | |
equal(c1.shots.length, 10) | |
}); | |
// **** Scores | |
test("calculate scores", function() { | |
var g = new Game(); | |
var c1 = new Competitor({name:'dave'}); | |
g.addCompetitor(c1); | |
c1.addShot([3,2]); | |
c1.addShot([3,5]); | |
equal(g.scoreCardWithScores().toString(), "dave|5|8"); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1 id="qunit-header">QUnit example</h1> | |
<h2 id="qunit-banner"></h2> | |
<div id="qunit-testrunner-toolbar"></div> | |
<h2 id="qunit-userAgent"></h2> | |
<ol id="qunit-tests"></ol> | |
<div id="qunit-fixture">test markup, will be hidden</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment