From cab483e9e2da54c7604bd1ad7adca8f691fdada4 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Tue, 15 May 2018 15:52:40 -0700 Subject: [PATCH 01/13] Initialized Scrabble prototype to get the score function. Score function working. --- scrabble.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/scrabble.js b/scrabble.js index e95f352..8b6528f 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,16 +1,59 @@ -const Scrabble = { - score(word) { +const Scrabble = function () { + this.letterValues = { "A": 1, "B":3, "C":3, "D": 2, "E": 1, "F":4, "G":2, "H":4, "I":1, "J":8, "K":5, "L":1, + "M": 3, "N": 1, "O": 1, "P": 3, "Q": 10, "R": 1, "S": 1, "T": 1, "U": 1, "V": 4, "W": 4, "X": 8, + "Y": 4, "Z": 10 }; +}; - }, - highestScoreFrom(arrayOfWords) { +Scrabble.prototype = { + score: function(word) { + + if (/^[a-zA-Z]+$/.test(word)) { + + word = word.toUpperCase(); + + let wordScore = (word.length == 7)? 50 : 0; + + for (let i = 0; i < word.length; i+=1) { + + wordScore += this.letterValues[word.charAt(i)]; + } + return wordScore; + + } else { + throw "Please enter a valid input"; + } }, -}; -Scrabble.Player = class { + // highestScoreFrom: function (arrayOfWords) { + // if (Array.isArray(arrayOfWords)) { + // let highestScore = 0, + // // iterate through words array + // arrayOfWords.forEach (function(word) { + // wordScore = this.score(word); + // if (wordScore > highestScore) { + // highestScore = wordScore; + // winner = word; + // } else if (wordScore == highestScore) { + // // winner = function to find the winner; + // } + // }); + // + // return winner; + // } + // }, }; +// Scrabble.Player = class { +// +// }; + module.exports = Scrabble; + + +let mygame = new Scrabble(); + +console.log(mygame.score("ZZzzZZ")); From 2af2e4626d9fd5044b8546cf8f03b3b56a18de28 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 14:47:25 -0700 Subject: [PATCH 02/13] Refactored functions to make tests pass for function score. --- scrabble.js | 54 ++++++++++++++++++++---------------------- specs/scrabble.spec.js | 6 ++--- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/scrabble.js b/scrabble.js index 8b6528f..7441dff 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,12 +1,10 @@ -const Scrabble = function () { - this.letterValues = { "A": 1, "B":3, "C":3, "D": 2, "E": 1, "F":4, "G":2, "H":4, "I":1, "J":8, "K":5, "L":1, - "M": 3, "N": 1, "O": 1, "P": 3, "Q": 10, "R": 1, "S": 1, "T": 1, "U": 1, "V": 4, "W": 4, "X": 8, - "Y": 4, "Z": 10 }; -}; +const Scrabble = { -Scrabble.prototype = { - score: function(word) { + score(word) { + const letterValues = { "A": 1, "B":3, "C":3, "D": 2, "E": 1, "F":4, "G":2, "H":4, "I":1, "J":8, "K":5, "L":1, + "M": 3, "N": 1, "O": 1, "P": 3, "Q": 10, "R": 1, "S": 1, "T": 1, "U": 1, "V": 4, "W": 4, "X": 8, + "Y": 4, "Z": 10 }; if (/^[a-zA-Z]+$/.test(word)) { @@ -16,34 +14,34 @@ Scrabble.prototype = { for (let i = 0; i < word.length; i+=1) { - wordScore += this.letterValues[word.charAt(i)]; + wordScore += letterValues[word.charAt(i)]; } return wordScore; } else { - throw "Please enter a valid input"; + throw "Please enter a valid input. Word must only contain letters from A-Z"; } }, - // highestScoreFrom: function (arrayOfWords) { - // if (Array.isArray(arrayOfWords)) { - // let highestScore = 0, - // // iterate through words array - // arrayOfWords.forEach (function(word) { - // wordScore = this.score(word); - // if (wordScore > highestScore) { - // highestScore = wordScore; - // winner = word; - // } else if (wordScore == highestScore) { - // // winner = function to find the winner; - // } - // }); - // - // return winner; - // } - // }, +// highestScoreFrom: function (arrayOfWords) { +// if (Array.isArray(arrayOfWords)) { +// let highestScore = 0, +// // iterate through words array +// arrayOfWords.forEach (function(word) { +// wordScore = this.score(word); +// if (wordScore > highestScore) { +// highestScore = wordScore; +// winner = word; +// } else if (wordScore == highestScore) { +// // winner = function to find the winner; +// } +// }); +// +// return winner; +// } +// }, }; // Scrabble.Player = class { @@ -54,6 +52,6 @@ Scrabble.prototype = { module.exports = Scrabble; -let mygame = new Scrabble(); +// let mygame = new Scrabble(); -console.log(mygame.score("ZZzzZZ")); +console.log(Scrabble.score("ZZzzZZ")); diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index b562037..78fa7bc 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -11,17 +11,17 @@ describe('score', () => { expect(Scrabble.score('pig')).toBe(6); }); - test.skip('adds 50 points for a 7-letter word', () => { + test('adds 50 points for a 7-letter word', () => { expect(Scrabble.score('academy')).toBe(65); }); - test.skip('throws on bad characters', () => { + test('throws on bad characters', () => { expect(() => { Scrabble.score('char^'); }).toThrow(); }); - test.skip('handles all upper- and lower-case letters', () => { + test('handles all upper- and lower-case letters', () => { expect(Scrabble.score('dog')).toBe(5); expect(Scrabble.score('DOG')).toBe(5); expect(Scrabble.score('DoG')).toBe(5); From 5563f030d67e343a44c9a71efeb79b9c7454f694 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 14:56:11 -0700 Subject: [PATCH 03/13] All tests for score method are passing. --- scrabble.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scrabble.js b/scrabble.js index 7441dff..10c4d79 100644 --- a/scrabble.js +++ b/scrabble.js @@ -16,7 +16,7 @@ const Scrabble = { wordScore += letterValues[word.charAt(i)]; } - + return wordScore; } else { @@ -52,6 +52,4 @@ const Scrabble = { module.exports = Scrabble; -// let mygame = new Scrabble(); - console.log(Scrabble.score("ZZzzZZ")); From d43422c1ccfc96f698c0bd501c4bfe7690ac0b14 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 15:05:00 -0700 Subject: [PATCH 04/13] Added condition of word lenght to be less than or equal to 7. Lasts tests for score method passing --- scrabble.js | 52 ++++++++++++++++++++++++++---------------- specs/scrabble.spec.js | 4 ++-- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/scrabble.js b/scrabble.js index 10c4d79..5ab91ff 100644 --- a/scrabble.js +++ b/scrabble.js @@ -6,7 +6,7 @@ const Scrabble = { "M": 3, "N": 1, "O": 1, "P": 3, "Q": 10, "R": 1, "S": 1, "T": 1, "U": 1, "V": 4, "W": 4, "X": 8, "Y": 4, "Z": 10 }; - if (/^[a-zA-Z]+$/.test(word)) { + if (/^[a-zA-Z]+$/.test(word) && word.length <= 7) { word = word.toUpperCase(); @@ -16,7 +16,7 @@ const Scrabble = { wordScore += letterValues[word.charAt(i)]; } - + return wordScore; } else { @@ -24,24 +24,36 @@ const Scrabble = { } }, - -// highestScoreFrom: function (arrayOfWords) { -// if (Array.isArray(arrayOfWords)) { -// let highestScore = 0, -// // iterate through words array -// arrayOfWords.forEach (function(word) { -// wordScore = this.score(word); -// if (wordScore > highestScore) { -// highestScore = wordScore; -// winner = word; -// } else if (wordScore == highestScore) { -// // winner = function to find the winner; -// } -// }); -// -// return winner; -// } -// }, + // highestScoreFrom(arrayOfWords) { + // if (arrayOfWords.length === 0 || arrayOfWords.constructor !== Array) { + // throw new Error('Array of words must not be empty'); + // } else if (arrayOfWords.length === 1) { + // return arrayOfWords[0]; + // } else { + // const winningWord = arrayOfWords.reduce((leftWord, rightWord) => { + // const scoreLeft = Scrabble.score(leftWord); + // const scoreRight = Scrabble.score(rightWord); + // + // if (scoreLeft > scoreRight) { + // return leftWord; + // } else if (scoreRight > scoreLeft) { + // return rightWord; + // } + // + // if (rightWord.length === 7) { + // return rightWord; + // } else if (leftWord.length === 7) { + // return leftWord; + // } + // + // if (leftWord.length < rightWord.length || leftWord.length === rightWord.length) { + // return leftWord; + // } // else + // return rightWord; + // }); + // return winningWord; + // } + // }, }; // Scrabble.Player = class { diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 78fa7bc..b19511d 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -27,11 +27,11 @@ describe('score', () => { expect(Scrabble.score('DoG')).toBe(5); }); - test.skip('does not allow words > 7 letters', () => { + test('does not allow words > 7 letters', () => { expect(() => { Scrabble.score('abcdefgh'); }).toThrow(); }); - test.skip('does not allow empty words', () => { + test('does not allow empty words', () => { expect(() => { Scrabble.score(''); }).toThrow(); }); }); From 1e4f930c82ef99c5a73361c7fe211e744e2d3ee0 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 16:08:12 -0700 Subject: [PATCH 05/13] Added highestScorefrom method and all tests passing. --- scrabble.js | 70 +++++++++++++++++++++++------------------- specs/scrabble.spec.js | 14 ++++----- 2 files changed, 46 insertions(+), 38 deletions(-) diff --git a/scrabble.js b/scrabble.js index 5ab91ff..dfdbad9 100644 --- a/scrabble.js +++ b/scrabble.js @@ -20,40 +20,44 @@ const Scrabble = { return wordScore; } else { - throw "Please enter a valid input. Word must only contain letters from A-Z"; + throw "Word must only contain letters from A-Z and have less than or equal to 7 characters"; } }, - // highestScoreFrom(arrayOfWords) { - // if (arrayOfWords.length === 0 || arrayOfWords.constructor !== Array) { - // throw new Error('Array of words must not be empty'); - // } else if (arrayOfWords.length === 1) { - // return arrayOfWords[0]; - // } else { - // const winningWord = arrayOfWords.reduce((leftWord, rightWord) => { - // const scoreLeft = Scrabble.score(leftWord); - // const scoreRight = Scrabble.score(rightWord); - // - // if (scoreLeft > scoreRight) { - // return leftWord; - // } else if (scoreRight > scoreLeft) { - // return rightWord; - // } - // - // if (rightWord.length === 7) { - // return rightWord; - // } else if (leftWord.length === 7) { - // return leftWord; - // } - // - // if (leftWord.length < rightWord.length || leftWord.length === rightWord.length) { - // return leftWord; - // } // else - // return rightWord; - // }); - // return winningWord; - // } - // }, + highestScoreFrom(arrayOfWords) { + + if (arrayOfWords.length === 0 || arrayOfWords.constructor !== Array) { + + throw new Error('Array of words can not be empty'); + + } else if (arrayOfWords.length === 1) { + + return arrayOfWords[0]; + + } else { + + const winningWord = arrayOfWords.reduce((word1, word2) => { + const scoreWord1 = Scrabble.score(word1); + const scoreWord2 = Scrabble.score(word2); + + if (scoreWord1 > scoreWord2 || word1.length === 7) { + return word1; + + } else if (scoreWord2 > scoreWord1 || word2.length === 7) { + return word2; + } + + if (word1.length === word2.length || word1.length < word2.length) { + return word1; + } + + return word2; + + }); + + return winningWord; + } + }, }; // Scrabble.Player = class { @@ -65,3 +69,7 @@ module.exports = Scrabble; console.log(Scrabble.score("ZZzzZZ")); +console.log(Scrabble.score("baby")); +console.log(Scrabble.highestScoreFrom(["ZZzzZZ", "zzzzzzz", "pedro"])); +console.log(Scrabble.highestScoreFrom(["QQQQQQQ", "zzzzzzz", "pedro"])); +console.log(Scrabble.score("QQQQQQQ")); diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index b19511d..5c05066 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -37,20 +37,20 @@ describe('score', () => { }); describe('highestScoreFrom', () => { - test.skip('is defined', () => { + test('is defined', () => { expect(Scrabble.highestScoreFrom).toBeDefined(); }); - test.skip('throws if no words were passed', () => { + test('throws if no words were passed', () => { expect(() => { Scrabble.highestScoreFrom([]); }).toThrow(); expect(() => { Scrabble.highestScoreFrom('not array'); }).toThrow(); }); - test.skip('returns the only word in a length-1 array', () => { + test('returns the only word in a length-1 array', () => { expect(Scrabble.highestScoreFrom(['dog'])).toBe('dog'); }); - test.skip('returns the highest word if there are two words', () => { + test('returns the highest word if there are two words', () => { // Check score assumptions expect(Scrabble.score('dog')).toBe(5); expect(Scrabble.score('pig')).toBe(6); @@ -60,7 +60,7 @@ describe('highestScoreFrom', () => { expect(Scrabble.highestScoreFrom(['pig', 'dog'])).toBe('pig'); }); - test.skip('if tied, prefer a word with 7 letters', () => { + test('if tied, prefer a word with 7 letters', () => { const loser = 'zzzzzz'; const winner = 'iiiiddd'; @@ -73,7 +73,7 @@ describe('highestScoreFrom', () => { expect(Scrabble.highestScoreFrom([winner, loser])).toBe(winner); }); - test.skip('if tied and no word has 7 letters, prefers the word with fewer letters', () => { + test('if tied and no word has 7 letters, prefers the word with fewer letters', () => { // Check score assumptions expect(Scrabble.score('dog')).toBe(5); expect(Scrabble.score('goat')).toBe(5); @@ -83,7 +83,7 @@ describe('highestScoreFrom', () => { expect(Scrabble.highestScoreFrom(['goat', 'dog'])).toBe('dog'); }); - test.skip('returns the first word of a tie with same letter count', () => { + test('returns the first word of a tie with same letter count', () => { // Check score assumptions expect(Scrabble.score('i')).toBe(1); expect(Scrabble.score('dog')).toBe(5); From 471166ac2db1295517ea12daa761ccb263603e43 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 16:45:40 -0700 Subject: [PATCH 06/13] Added pseudocode for the Player class of the Scrabble object. --- scrabble.js | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/scrabble.js b/scrabble.js index dfdbad9..0ff9868 100644 --- a/scrabble.js +++ b/scrabble.js @@ -27,7 +27,7 @@ const Scrabble = { highestScoreFrom(arrayOfWords) { if (arrayOfWords.length === 0 || arrayOfWords.constructor !== Array) { - + throw new Error('Array of words can not be empty'); } else if (arrayOfWords.length === 1) { @@ -60,9 +60,42 @@ const Scrabble = { }, }; -// Scrabble.Player = class { -// -// }; +Scrabble.Player = class { + constructor(name) { + this.name = name; + this.plays =[]; + + } + + play() { + //adds the input words to the plays Array + // return false if player has already won + + } + + totalScore() { + + //sums up and returns the score of the players words + + } + + hasWon() { + // returns true if the player has over 100 points, otherwise returns false + } + + highestScoringWord() { + + // returns the highest scoring word the user has played + + } + + highestWordScore() { + + // returns the highestScoringWord score + + } + +}; module.exports = Scrabble; From 98cb20baf2659ba2b14d93172b19a8056c9ced01 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 16:59:47 -0700 Subject: [PATCH 07/13] Started the constructor method and play(word) method. Still need one test to pass for the constructor. --- scrabble.js | 15 ++++++++++++++- specs/scrabble.spec.js | 10 +++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/scrabble.js b/scrabble.js index 0ff9868..0f91c1f 100644 --- a/scrabble.js +++ b/scrabble.js @@ -67,12 +67,21 @@ Scrabble.Player = class { } - play() { + play(word) { //adds the input words to the plays Array // return false if player has already won + if (word === " "|| typeof word !== 'string') { + throw new Error('Please enter a valid word'); + } + if (this.hasWon()) { + return false; + } + this.plays.push(word); + return true; } + totalScore() { //sums up and returns the score of the players words @@ -81,6 +90,10 @@ Scrabble.Player = class { hasWon() { // returns true if the player has over 100 points, otherwise returns false + if (this.totalScore() >= 100 ) { + return true; + } + return false; } highestScoringWord() { diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 5c05066..9463cc6 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -98,19 +98,19 @@ describe('highestScoreFrom', () => { }); describe('Player', () => { - test.skip('is defined', () => { + test('is defined', () => { expect(Scrabble.Player).toBeDefined(); }); describe('Constructor', () => { - test.skip('Creates a new player', () => { + test('Creates a new player', () => { const name = 'test name'; const player = new Scrabble.Player(name); expect(player.name).toBe(name); }); - test.skip('Requires a name', () => { + test('Requires a name', () => { expect(() => { new Scrabble.Player(); }).toThrow(); @@ -118,7 +118,7 @@ describe('Player', () => { }); describe('play', () => { - test.skip('Records the played word', () => { + test('Records the played word', () => { const word = 'dog'; const player = new Scrabble.Player('test player'); @@ -130,7 +130,7 @@ describe('Player', () => { expect(player.plays[0]).toBe(word); }); - test.skip('Requires a real word', () => { + test('Requires a real word', () => { const player = new Scrabble.Player('test player'); expect(player.plays.length).toBe(0); From 1281e518fefdd847bb64bdad75dcffa91bfdf31f Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 21:52:45 -0700 Subject: [PATCH 08/13] Added conditional in constructor method. Tests for constructor method are passing. --- scrabble.js | 24 ++++++++++++++++-------- specs/scrabble.spec.js | 10 +++++----- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/scrabble.js b/scrabble.js index 0f91c1f..3192a97 100644 --- a/scrabble.js +++ b/scrabble.js @@ -62,7 +62,13 @@ const Scrabble = { Scrabble.Player = class { constructor(name) { - this.name = name; + if (name.length > 0 || typeof name === 'string') { + this.name = name; + } + else { + throw new Error('Please enter name of the player'); + } + this.plays =[]; } @@ -90,22 +96,24 @@ Scrabble.Player = class { hasWon() { // returns true if the player has over 100 points, otherwise returns false - if (this.totalScore() >= 100 ) { - return true; - } - return false; + // if (this.totalScore() >= 100 ) { + // return true; + // } + // return false; + let won = (this.totalScore() >= 100) ? true:false; + return won; } highestScoringWord() { // returns the highest scoring word the user has played - + return Scrabble.highestScoreFrom(this.plays); } highestWordScore() { - // returns the highestScoringWord score - + // returns the highestScoringWord score + return Scrabble.score(this.highestScoringWord()); } }; diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 9463cc6..daa889a 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -142,7 +142,7 @@ describe('Player', () => { expect(player.plays.length).toBe(0); }); - test.skip('Returns false and does not update plays if the player has already won', () => { + test('Returns false and does not update plays if the player has already won', () => { const player = new Scrabble.Player('test player'); expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts @@ -181,17 +181,17 @@ describe('Player', () => { }); describe('hasWon', () => { - test.skip('returns false when score < 100', () => { + test('returns false when score < 100', () => { }); - test.skip('returns true when score == 100', () => { + test('returns true when score == 100', () => { }); - test.skip('returns true when score > 100', () => { + test('returns true when score > 100', () => { }); @@ -200,7 +200,7 @@ describe('Player', () => { describe('highestScoringWord', () => { // Tie-breaking logic is already described in the tests // for highestWordFrom, so we will not repeat it here. - test.skip('returns the highest scoring word played', () => { + test('returns the highest scoring word played', () => { }); From 09f26af57712ff2ca8031a850ffccd35114e14c8 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 21:54:19 -0700 Subject: [PATCH 09/13] Added code to hasWon, highestScoringWord, and highestWordScore methods. --- scrabble.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/scrabble.js b/scrabble.js index 3192a97..53e82ef 100644 --- a/scrabble.js +++ b/scrabble.js @@ -95,24 +95,18 @@ Scrabble.Player = class { } hasWon() { - // returns true if the player has over 100 points, otherwise returns false - // if (this.totalScore() >= 100 ) { - // return true; - // } - // return false; + let won = (this.totalScore() >= 100) ? true:false; return won; } highestScoringWord() { - // returns the highest scoring word the user has played return Scrabble.highestScoreFrom(this.plays); } highestWordScore() { - // returns the highestScoringWord score return Scrabble.score(this.highestScoringWord()); } From 208a894f34d964eb11fa7b3cabad2d89f3947da2 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 22:57:17 -0700 Subject: [PATCH 10/13] All tests passing. Only missing highestScoringWord tests to be done. --- scrabble.js | 9 +++++---- specs/scrabble.spec.js | 46 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/scrabble.js b/scrabble.js index 53e82ef..fdfbb5c 100644 --- a/scrabble.js +++ b/scrabble.js @@ -74,8 +74,6 @@ Scrabble.Player = class { } play(word) { - //adds the input words to the plays Array - // return false if player has already won if (word === " "|| typeof word !== 'string') { throw new Error('Please enter a valid word'); @@ -90,8 +88,11 @@ Scrabble.Player = class { totalScore() { - //sums up and returns the score of the players words - + let total = 0; + for (let word of this.plays) { + total += Scrabble.score(word); + } + return total; } hasWon() { diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index daa889a..21b1276 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -155,13 +155,13 @@ describe('Player', () => { }); describe('totalScore', () => { - test.skip('Is zero if the player has not played anything', () => { + test('Is zero if the player has not played anything', () => { const player = new Scrabble.Player('test player'); expect(player.totalScore()).toBe(0); }); - test.skip('Is updated by play', () => { + test('Is updated by play', () => { // Arrange const player = new Scrabble.Player('test player'); const words = [{word: 'dog', score: 5}, {word: 'cat', score: 5}, {word: 'goat', score: 5}]; @@ -182,18 +182,47 @@ describe('Player', () => { describe('hasWon', () => { test('returns false when score < 100', () => { + const player = new Scrabble.Player('test player'); + + player.play('alcohol'); + + + expect(player.totalScore()).toBe(62); + expect(player.hasWon()).toBe(false); + + + player.play('cookie'); + + + expect(player.totalScore()).toBe(74); + expect(player.hasWon()).toBe(false); }); + test('returns true when score == 100', () => { + const player = new Scrabble.Player('test player'); + + player.play('QQQQQ'); + player.play('ZZZZZ'); + expect(player.totalScore()).toBe(100); + expect(player.hasWon()).toBe(true); + }); test('returns true when score > 100', () => { + const player = new Scrabble.Player('test player'); + + player.play('squeeze'); + player.play('jukebox'); + expect(player.totalScore()).toBe(152); + expect(player.hasWon()).toBe(true); + }); }); @@ -201,13 +230,20 @@ describe('Player', () => { // Tie-breaking logic is already described in the tests // for highestWordFrom, so we will not repeat it here. test('returns the highest scoring word played', () => { + const player = new Scrabble.Player('test player'); + player.play('QQQQQ'); + player.play('KKKKK'); - }); - - test.skip('throws an error if no words have been played', () => { + expect(Scrabble.score('QQQQQ')).toBe(50); + expect(Scrabble.score('KKKKK')).toBe(25); + expect(player.highestScoringWord()).toBe('QQQQQ'); + }); + test('throws an error if no words have been played', () => { + const player = new Scrabble.Player('test player'); + expect(() => { player.highestScoringWord() }).toThrow(); }); }); From dcda1db69f951b740940510c941d7f541cc75a92 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Wed, 16 May 2018 23:02:04 -0700 Subject: [PATCH 11/13] Added tests for highestWordScore method. All tests passing. --- specs/scrabble.spec.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 21b1276..5678e04 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -248,13 +248,18 @@ describe('Player', () => { }); describe('highestWordScore', () => { - test.skip('returns the score of the highest scoring word played', () => { + test('returns the score of the highest scoring word played', () => { + const player = new Scrabble.Player('test player'); + player.play('QQQQQ'); + player.play('KKKKK'); + expect(player.highestWordScore()).toBe(50); }); - test.skip('throws an error if no words have been played', () => { - + test('throws an error if no words have been played', () => { + const player = new Scrabble.Player('test player'); + expect(() => { player.highestScoringWord() }).toThrow(); }); }); From 430ab5198e6f89b915b12650404ef1f353b391b9 Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Fri, 18 May 2018 00:10:32 -0700 Subject: [PATCH 12/13] Added the optional of the tilebag. --- scrabble.js | 43 ++++++++++++++++++++++--- specs/scrabble.spec.js | 73 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/scrabble.js b/scrabble.js index fdfbb5c..5875b54 100644 --- a/scrabble.js +++ b/scrabble.js @@ -113,12 +113,45 @@ Scrabble.Player = class { }; +// optional enhancements + +Scrabble.TilesBag = class { + constructor() { + this.tiles = ['A','A','A','A','A','A','A','A','A','B','B','C','C','D','D','D','D', + 'E','E','E','E','E','E','E','E','E','E','E','E','F','F','G','G','G','H','H','I','I','I','I','I','I', + 'I','I','I','J','K','L','L','L','L','M','M','N','N','N','N','N','N,','O','O','O','O','O','O','O','O', + 'P','P','Q','R','R','R','R','R','R','S','S','S','S','T','T','T','T','T','T','U','U','U','U','V','V', + 'W','W','X','Y','Y','Z']; + } + + drawTiles(number) { + + if (number > this.tilesRemaining()) { + throw new Error('No enough tiles'); + } + let hand = []; + for (let i = 0; i < number; i+=1) { + let rand = Math.floor(Math.random() * this.tiles.length); + hand.push(this.tiles[rand]); + this.tiles.splice(rand, 1); + // splice removes items from an array. changes the original array + } + return hand; + } + tilesRemaining() { + if (this.tiles.length === 0) { + return 'No more tiles in the bag' + } + return this.tiles.length; + } + +}; module.exports = Scrabble; -console.log(Scrabble.score("ZZzzZZ")); -console.log(Scrabble.score("baby")); -console.log(Scrabble.highestScoreFrom(["ZZzzZZ", "zzzzzzz", "pedro"])); -console.log(Scrabble.highestScoreFrom(["QQQQQQQ", "zzzzzzz", "pedro"])); -console.log(Scrabble.score("QQQQQQQ")); +// console.log(Scrabble.score("ZZzzZZ")); +// console.log(Scrabble.score("baby")); +// console.log(Scrabble.highestScoreFrom(["ZZzzZZ", "zzzzzzz", "pedro"])); +// console.log(Scrabble.highestScoreFrom(["QQQQQQQ", "zzzzzzz", "pedro"])); +// console.log(Scrabble.score("QQQQQQQ")); diff --git a/specs/scrabble.spec.js b/specs/scrabble.spec.js index 5678e04..893f64c 100644 --- a/specs/scrabble.spec.js +++ b/specs/scrabble.spec.js @@ -264,3 +264,76 @@ describe('Player', () => { }); }); }); + +// optional enchancements tests +describe('TileBag', () => { + test('is defined', () => { + expect(Scrabble.TilesBag).toBeDefined(); + }); + + describe('constructor', () => { + test('creates a new tile bag', () => { + const tileBag = new Scrabble.TilesBag(); + + expect(tileBag.tiles.length).toEqual(98); + }); + + }); + + describe('drawTiles', () => { + test('returns a collection of tiles', () => { + const tileBag = new Scrabble.TilesBag(); + let tiles = tileBag.drawTiles(5); + + expect(tiles.length).toEqual(5); + + }); + + test('throws error if not enough tiles', () => { + const tileBag = new Scrabble.TilesBag(); + + // a loop to use tiles + for (let i = 0; i < 10; i+=1) { + tileBag.drawTiles(9); + } + + // try to get more tiles than the amount available + expect(() => { tileBag.drawTiles(10); }).toThrow(); + }); + + test('removes the tile from the array of tiles', () => { + const tileBag = new Scrabble.TilesBag(); + + tileBag.drawTiles(2); + + expect(tileBag.tiles.length).toEqual(96); + }); + + + }); + + describe('tilesRemaining', () => { + test('returns the amount of tiles remaining in the tiles bag', () => { + const tileBag = new Scrabble.TilesBag(); + + tileBag.drawTiles(4); + + expect(tileBag.tilesRemaining()).toEqual(94); + }); + + test('returns "No more tiles in the bag" if no tiles left in the bag', () => { + const tileBag = new Scrabble.TilesBag(); + + // a loop to use all the tiles + for (let i = 0; i < 14; i++) { + tileBag.drawTiles(7); + } + + expect(tileBag.tilesRemaining()).toEqual('No more tiles in the bag'); + }); + + + }); + + +}); From 12b5c38ba416dd53f01a7734926ebab258afdb3a Mon Sep 17 00:00:00 2001 From: Brenda Rios Date: Fri, 18 May 2018 09:29:01 -0700 Subject: [PATCH 13/13] Added To Do comments. --- scrabble.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scrabble.js b/scrabble.js index 5875b54..f593719 100644 --- a/scrabble.js +++ b/scrabble.js @@ -26,7 +26,7 @@ const Scrabble = { highestScoreFrom(arrayOfWords) { - if (arrayOfWords.length === 0 || arrayOfWords.constructor !== Array) { + if (arrayOfWords.length === 0) { throw new Error('Array of words can not be empty'); @@ -113,7 +113,10 @@ Scrabble.Player = class { }; -// optional enhancements +// Optional Enhancements + +// TODO: I need to improve the TilesBag. The way I made the tiles in the constructor +// I know is not the best way. Scrabble.TilesBag = class { constructor() {