Skip to content

Commit

Permalink
Finally completed exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
rokesby committed Jul 16, 2024
1 parent c450b34 commit d584fb3
Show file tree
Hide file tree
Showing 4 changed files with 406 additions and 30 deletions.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/app.js"
}
]
}
90 changes: 79 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,94 @@
// TODO - Try the new ES6 Convention - https://chatgpt.com/share/8e4d1ab5-a275-4fce-b43b-37ca988b81ba

console.log('1st test message');
// console.log('1st test message');

const Scorecard = require('./src/Scorecard.js');

let myScorecard = new Scorecard();
console.log(myScorecard.getCurrentScore()); // Zero as expected.
// let myScorecard = new Scorecard();
// console.log(myScorecard.getCurrentScore()); // Zero as expected.

myScorecard.addFrame(2,5);
console.log(myScorecard.getCurrentScore()); // 7 as expected.
// myScorecard.addFrame(2,5);
// console.log(myScorecard.getCurrentScore()); // 7 as expected.

myScorecard.addFrame(3,5); // 15 as expected.
console.log(myScorecard.getCurrentScore());
console.log("Number of frames: " + myScorecard.getNumberofFrames());
// myScorecard.addFrame(3,5); // 15 as expected.
// console.log(myScorecard.getCurrentScore());
// console.log("Number of frames: " + myScorecard.getNumberofFrames());

myScorecard = new Scorecard();
console.log(myScorecard.getCurrentScore()); // 0 as expected.
// console.log("strike or spare?");
// myScorecard = new Scorecard();
// console.log(myScorecard.getCurrentScore()); // 0 as expected.
// myScorecard.addFrame(3,5);
// myScorecard.addFrame(5,5);
// myScorecard.addFrame(10,0);
// myScorecard.getFinalScore();

// myScorecard = new Scorecard();
// myScorecard.addFrame(9,5);
// console.log(myScorecard.getScore()); // 0 as expected.

// myScorecard = new Scorecard();
// myScorecard.addFrame('',5);
// console.log(myScorecard.getScore()); // 0 as expected.
// console.log(myScorecard.getScore()); // 0 as expected.


// Test strike


// scorecard14.addFrame(10,0);
// scorecard14.addFrame(10,0);
// scorecard14.addFrame(10,0);


// scorecard14.addFrame(10,0);
// scorecard14.addFrame(10,0);
// scorecard14.addFrame(10,0);

// scorecard14.addFrame(10,0);
// scorecard14.addFrame(10,0);
// scorecard14.addFrame(10,0);

// scorecard14.addFrame(10,0);

// // Bonus rolls due to strike.
// scorecard14.addFrame(10,0); // bonus round.
// scorecard14.addFrame(10,0); // bonus round.


// scorecard13.addFrame(1,1);
// scorecard13.addFrame(1,1);
// scorecard13.addFrame(1,1);


// scorecard13.addFrame(1,1);
// scorecard13.addFrame(1,1);
// scorecard13.addFrame(1,1);

// scorecard13.addFrame(1,1);
// scorecard13.addFrame(1,1);
// scorecard13.addFrame(1,1);

// scorecard13.addFrame(10,0);

// // Bonus roll due to STRIKE. Next two rolls to be included.
// scorecard13.addFrame(5,4);

// // Should be 37 instead of 42.
// // I shouldn't be adding the last 5 and 4 as a forward bonus as I'm on the last round.
// console.log(scorecard13.getFinalScore());
let scorecard9 = new Scorecard();

scorecard9.addFrame(0,0);
scorecard9.addFrame(5,5);
scorecard9.addFrame(1,8);

scorecard9.addFrame(0,0);
scorecard9.addFrame(0,0);
scorecard9.addFrame(0,0);

scorecard9.addFrame(0,0);
scorecard9.addFrame(0,0);
scorecard9.addFrame(0,0);

scorecard9.addFrame(0,0);

console.log(scorecard9.getFinalScore());
105 changes: 92 additions & 13 deletions src/Scorecard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Scorecard {

constructor() {
this._current_score = 0;
this._list_of_frames = [];
Expand All @@ -13,38 +13,117 @@ class Scorecard {
return this._list_of_frames.length;
}


_isStrike(frame) {
if (frame.score_one == 10) {
return true;
}
else {
return false;
}
}

_isSpare(frame) {
if ((frame.score_one + frame.score_two) == 10) {
return true;
}
else {
return false;
}
}


getFrameScore(frame) {
return (frame.score_one + frame.score_two);
}


getFinalScore() {
let running_total = 0;

// Go through the list of frames (maximum of 10) and return the final score.
// Useful reference : https://www.geeksforgeeks.org/how-to-loop-through-an-array-containing-multiple-objects-and-access-their-properties-in-javascript/
let game_total = 0;
let loop_counter = 0;
const LAST_NORMAL_ROUND = 10;

this._list_of_frames.forEach(thisFrame => {

loop_counter ++;
process.stdout.write("Loop : " + loop_counter + ' : ')

// Add the scores for this frame.
game_total += this.getFrameScore(thisFrame);

return 80;
}
if (loop_counter < LAST_NORMAL_ROUND) {
process.stdout.write("Check for bonus");

// Grab the next frame if we need it. We'll check for boundary conditions later on in the undefined object.
let next_frame = this._list_of_frames[loop_counter];

// TODO - Check if either of these is a strike(10)
// Count the next two scores and add as a bonus mark (unless the last round)
if (this._isStrike(thisFrame)) {

if (typeof next_frame !== "undefined") {
// game_total = game_total + next_frame.score_one; // Caused a NaN ERROR
game_total += next_frame.score_one;
game_total += next_frame.score_two;
process.stdout.write(" : Strike bonus awarded ");


// If this next_frame is a strike, then we need to add the next one too!
if (next_frame.score_one == 10 && (loop_counter <= LAST_NORMAL_ROUND-1)) {
// This a strike too!
let further_frame = this._list_of_frames[loop_counter+2];
if (typeof further_frame !== "undefined") {
game_total += further_frame.score_one;
game_total += further_frame.score_two;
process.stdout.write(" : Strike bonus awarded ");
}
}
}

} else {
// If this is not a STRIKE, then check for a SPARE.

addFrame(score1, score2) {
if (this._isSpare(thisFrame)) {
// This is a SPARE. so the next frame score is added as bonus mark.
process.stdout.write(" : Spare!");
// Get the next frame scores and add to the total (if not the last frames.)
// Check if this is the last entry in the array, if so, don't retrieve the next score. This is a bonus roll and there will not be another frame waiting to be scored.
//if (this.getNumberofFrames >= loop_counter) {
if (typeof next_frame !== 'undefined') {
game_total = game_total + next_frame.score_one; // Caused a NaN ERROR
process.stdout.write(" : Spare bonus awarded ");
}

}
}
}

console.log(" : " + game_total);

})

return game_total;
}

// TODO Confirm that no more than 10 frames are used, else the score will reset.

addFrame(score1, score2) {

// Perform some validation
if ((score1 + score2) > 10) {
throw new Error("You cannot score more than 10 with two throws");
} else {
if ((!score1) || (!score2)) {
throw new Error("One of the scores has not been defined")
if (Number.isInteger(score1) && Number.isInteger(score2)) {
this._current_score = this._current_score + score1 + score2;
}
else {
this._current_score = this._current_score + score1 + score2;
throw new Error("One of the scores has not been defined");
}

}

// The scores are valid to store in the array for scores for the final calculation.
const this_frame = {score1, score2};
// Make up the javascript object with these two attributes.
const this_frame = {score_one : score1, score_two : score2};
this._list_of_frames.push(this_frame);
}
}
Expand Down
Loading

0 comments on commit d584fb3

Please sign in to comment.