Skip to content

Instantly share code, notes, and snippets.

@dzsodzso63
Created June 16, 2015 17:42
Show Gist options
  • Save dzsodzso63/c4c87cb2d00e991f8b4d to your computer and use it in GitHub Desktop.
Save dzsodzso63/c4c87cb2d00e991f8b4d to your computer and use it in GitHub Desktop.
The Bowling Score Calculator kata
var bowling = {
getScore: function(scoreString){
if (scoreString.length < 11) return -1;
if (scoreString.length > 21) return -1;
if (!scoreString.match(/^([1-9]|X|\/|-)*$/)) return -1;
var score = 0;
var scorePlus = 0;
var frame = 1;
var multi = 1;
for (i = 0; i<scoreString.length; i++){
if (frame > 10) return -1;
var f1 = scoreString[i];
var frameScore = 0;
if (f1 == 'X'){
frameScore = 10;
if (scorePlus > 0){
frameScore = frameScore+(10*multi);
multi = 1;
scorePlus = scorePlus-1;
}
if (scorePlus > 0){
multi = 2;
}
scorePlus = 2;
score = score + frameScore;
if (frame==10){
var p1 = scoreString[i+1];
i++;
if (p1 == 'X'){
p1score = 10;
} else if (p1.match(/^[1-9]$/)||(p1 == '-')){
p1score = parseInt(p1)||0;
}
score = score + p1score;
if (scorePlus > 0){
score = score+p1score;
multi = 1;
scorePlus = scorePlus-1;
}
var p2 = scoreString[i+1];
i++;
if (p1 =='X' && p2 == 'X'){
p2score = 10;
} else if (p1 !='X' && p2 == '/'){
p2score = 10-p1score;
} else if (p2.match(/^[1-9]$/)){
p2score = parseInt(p2);
} else if (p2 == '-'){
} else {
return -1;
}
score = score + p2score;
if (scoreString.length > (i+1)) return -1;
}
frame = frame + 1;
} else if (f1.match(/^[1-9]$/) || (f1 == '-')){
frameScore = parseInt(f1)||0;
if (scorePlus){
score = score + (frameScore*multi);
multi = 1;
scorePlus = scorePlus-1;
}
var f2 = scoreString[i+1];
i++;
if (f2.match(/^[1-9]$/) || (f2=='-')|| (f2=='/')){
f2Score = parseInt(f2)||0;
if (f2 == '/'){
f2Score = 10 - frameScore;
if (scorePlus){
score = score + (f2Score*multi);
multi = 1;
scorePlus = scorePlus-1;
}
scorePlus = 1;
} else if (f2 == '-'){
if (scorePlus){
scorePlus = scorePlus-1;
}
} else if (f2.match(/^[1-9]$/)){
f2Score = parseInt(f2);
if ((f2Score + frameScore)>9){
return -1;
}
if (scorePlus){
score = score + (f2Score*multi);
multi = 1;
scorePlus = scorePlus-1;
}
}
} else {
return -1;
}
score = score + frameScore + f2Score;
if (frame==10){
if (scorePlus){
var p1 = scoreString[i+1];
i++;
if (p1 == 'X'){
score = score + 10;
} else if (p1.match(/^[1-9]$/)||(p1 == '-')){
score = score + parseInt(p1)||0;
} else {
return -1;
}
} else {
if (scoreString.length > (i+1)) return -1;
}
}
frame = frame + 1;
} else {
return -1;
}
}
if (frame != 11) return -1;
return score;
}
}
describe("Bowling", function() {
describe("Invalid", function() {
describe("Invalid length", function() {
bowling_helper('', -1);
bowling_helper('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', -1);
bowling_helper('XXXXXXXXXXXXX', -1);
});
describe("Invalid characters", function() {
bowling_helper('ejfbs', -1);
bowling_helper('ejfbsXXXXXXXXXXXXXXXXXXXXX', -1);
bowling_helper('12023040..12023040.r', -1);
bowling_helper('909-9-9-909-9-9-9-9-', -1);
});
describe("Invalid pins", function() {
bowling_helper('38------------------', -1);
bowling_helper('91------------------', -1);
bowling_helper('5/5/5/555/5/5/5/5/5/X', -1);
});
describe("Invalid bonus", function() {
bowling_helper('313131313131313131311', -1);
bowling_helper('3131313131313131313/XX', -1);
bowling_helper('3131313131313131313/1/', -1);
bowling_helper('XXXXXXXXXXX/', -1);
bowling_helper('XXXXXXXXX99/', -1);
bowling_helper('XXXXXXXXX991', -1);
bowling_helper('XXXXXXXXX99', -1);
});
describe("Missing bonus", function() {
bowling_helper('313233343536373839X', -1);
bowling_helper('313233343536373839XX', -1);
bowling_helper('3132333435363738393/', -1);
});
});
describe("Valid 20", function() {
bowling_helper('9-9-9-9-9-9-9-9-9-9-', 90);
bowling_helper('31313131313131313131', 40);
bowling_helper('5/5/5/545/X5/5/5/5/X', 159);
});
describe("Valid 12", function() {
bowling_helper('XXXXXXXXXXX9', 299);
bowling_helper('XXXXXXXXXX9/', 289);
bowling_helper('XXXXXXXXX9/9', 278);
bowling_helper('XXXXXXXXX9/X', 279);
bowling_helper('XXXXXXXXXXXX', 300);
bowling_helper('XXXX--XXXX--', 180);
});
describe("Valid 13", function() {
bowling_helper('XXXX--X5/XX--', 160);
});
describe("Valid 21", function() {
bowling_helper('5/5/5/5/5/5/5/5/5/5/5', 150);
bowling_helper('5/5/5/5/5/5/5/5/5/5/X', 155);
bowling_helper('5/5/5/545/5/5/5/5/5/X', 149);
});
describe("Valid 11", function() {
bowling_helper('XXXXXXXXX81', 266);
bowling_helper('XXXXXXXXX--', 240);
});
});
function bowling_helper(expression, result){
it("should evaluate '"+expression+"' to "+result, function() {
expect(bowling.getScore(expression)).toBe(result);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment