diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..80043d7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,123 @@ +{ + "name": "js-scrabble", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "add-matchers": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/add-matchers/-/add-matchers-0.5.0.tgz", + "integrity": "sha1-UCGQ5HUM1XIWGDkyaLYaFXNm52U=" + }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" + }, + "brace-expansion": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.8.tgz", + "integrity": "sha1-wHshHHyVLsH479Uad+8NHTmQopI=", + "requires": { + "balanced-match": "1.0.0", + "concat-map": "0.0.1" + } + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" + }, + "eslint-plugin-jasmine": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-jasmine/-/eslint-plugin-jasmine-2.9.1.tgz", + "integrity": "sha1-IuGaWfFvOl9kOgSroEQ40OMEcDA=" + }, + "exit": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz", + "integrity": "sha1-BjJjj42HfMghB9MKD/8aF8uhzQw=" + }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + }, + "glob": { + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", + "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", + "requires": { + "fs.realpath": "1.0.0", + "inflight": "1.0.6", + "inherits": "2.0.3", + "minimatch": "3.0.4", + "once": "1.4.0", + "path-is-absolute": "1.0.1" + } + }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "requires": { + "once": "1.4.0", + "wrappy": "1.0.2" + } + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "jasmine": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/jasmine/-/jasmine-2.8.0.tgz", + "integrity": "sha1-awicChFXax8W3xG4AUbZHU6Lij4=", + "requires": { + "exit": "0.1.2", + "glob": "7.1.2", + "jasmine-core": "2.8.0" + } + }, + "jasmine-core": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-2.8.0.tgz", + "integrity": "sha1-vMl5rh+f0FcB5F5S5l06XWPxok4=" + }, + "jasmine-expect": { + "version": "3.8.1", + "resolved": "https://registry.npmjs.org/jasmine-expect/-/jasmine-expect-3.8.1.tgz", + "integrity": "sha512-klARdR5AVX9nZhHhYDlbDYgxgi6kl9DGS0vguhaioKoSwr8HL1uOQ7FFUBASq/sqBL/s2nkh/1efqZ2ugcknFA==", + "requires": { + "add-matchers": "0.5.0" + } + }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "requires": { + "brace-expansion": "1.1.8" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "requires": { + "wrappy": "1.0.2" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + } + } +} diff --git a/scrabble.js b/scrabble.js index 7a1f161..44ef6c6 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,14 +1,149 @@ +const LETTERVALUES = { + A: [1, 9], + B: [3, 2], + C: [3, 2], + D: [2, 4], + E: [1, 12], + F: [4, 2], + G: [2, 3], + H: [4, 2], + I: [1, 9], + J: [8, 1], + K: [5, 1], + L: [1, 4], + M: [3, 2], + N: [1, 6], + O: [1, 8], + P: [3, 2], + Q: [10, 1], + R: [1, 6], + S: [1, 4], + T: [1, 6], + U: [1, 4], + V: [4, 2], + W: [4, 2], + X: [8, 1], + Y: [4, 2], + Z: [10, 1], +}; + const Scrabble = { - score: function(word) { - // TODO: implement score + score(word) { + if (word.length > 7 || word.length < 1) { + throw new Error('word must be between 1 and 7 letters'); + } + + const wordArray = word.toUpperCase().split(''); + + let sum = (wordArray.length === 7) ? 50 : 0; + + wordArray.forEach((letter) => { + if (letter in LETTERVALUES) { + sum += LETTERVALUES[letter][0]; + } else { + throw new Error('word contains invalid characters'); + } + }); + + return sum; + }, + + highestScoreFrom(words) { + if (words.length === 0 || words.constructor !== Array) { + throw new Error('no words to compare score'); + } + + let max = this.score(words[0]); + let winningWord = words[0]; + + words.forEach((word) => { + const score = this.score(word); + + if (score > max) { + max = score; + winningWord = word; + } else if (score === max) { + if (word.length === 7) { + max = score; + winningWord = word; + } else if (word.length < winningWord.length && winningWord.length !== 7) { + max = score; + winningWord = word; + } + } + }); + return winningWord; + }, +}; + +Scrabble.TileBag = class { + constructor() { + this.tiles = Scrabble.TileBag.setTiles(); } - // TODO: add the highestScoreFrom method + static setTiles() { + const tiles = []; + Object.keys(LETTERVALUES).forEach((letter) => { + const quantity = LETTERVALUES[letter][1]; + for (let i = 0; i < quantity; i += 1) { + tiles.push(letter); + } + }); + return tiles; + } + drawTile() { + return this.tiles.splice(Math.floor(Math.random() * this.tiles.length), 1); + } }; + Scrabble.Player = class { - // TODO: implement the Player class + constructor(name) { + if (!name) { + throw new Error('player must have a name'); + } + this.name = name; + this.plays = []; + } + + play(word) { + if (this.hasWon()) { + return false; + } + if (!(word.match(/^[a-zA-Z]{1,7}$/))) { + throw new Error('must be a word between 1 and 7 letters'); + } + this.plays.push(word); + return Scrabble.score(word); + } + + totalScore() { + let totalScore = 0; + this.plays.forEach((play) => { + totalScore += Scrabble.score(play); + }); + return totalScore; + } + + hasWon() { + return (this.totalScore() >= 100); + } + + highestScoringWord() { + if (this.plays.length === 0) { + throw new Error('no words played'); + } + return Scrabble.highestScoreFrom(this.plays); + } + + highestWordScore() { + if (this.plays.length === 0) { + throw new Error('no words played'); + } + return Scrabble.score(this.highestScoringWord()); + } }; + module.exports = Scrabble; diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js index c195a03..1195944 100644 --- a/spec/scrabble_spec.js +++ b/spec/scrabble_spec.js @@ -97,6 +97,34 @@ describe('highestScoreFrom', function() { }); }); +describe('TileBag', function() { + it ('is defined', function() { + expect(Scrabble.TileBag).toBeDefined(); + }); + describe('Constructor', function() { + it('creates a set of tiles', function() { + let tilebag = new Scrabble.TileBag(); + expect(tilebag.tiles.length).toBe(98); + }); + }); + + describe('drawTile', function() { + it('decreases the tilebag count by 1', function() { + let tilebag = new Scrabble.TileBag(); + let tile = tilebag.drawTile(); + // TODO: Find a way to test this: + // expect(tile.).toBe(); + expect(tilebag.tiles.length).toBe(97); + }); + it('returns a tile from the tiles', function() { + let tilebag = new Scrabble.TileBag(); + let tile = tilebag.drawTile(); + // TODO: Find a way to test this: + // expect(tile.).toBe(); + }); + }); +}); + describe('Player', function() { it ('is defined', function() { expect(Scrabble.Player).toBeDefined();