diff --git a/src/adagrams.js b/src/adagrams.js index 7ec5afc7..678be290 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,15 +1,122 @@ +const letterDist = { + A: 9, + B: 2, + C: 2, + D: 4, + E: 12, + F: 2, + G: 3, + H: 2, + I: 9, + J: 1, + K: 1, + L: 4, + M: 2, + N: 6, + O: 8, + P: 2, + Q: 1, + R: 6, + S: 4, + T: 6, + U: 4, + V: 2, + W: 2, + X: 1, + Y: 2, + Z: 1, +}; + export const drawLetters = () => { - // Implement this method for wave 1 + let letterPool = []; + let hand = []; + + // create letter pool from letter distribution + for (let letter in letterDist) { + for (let i = 0; i < letterDist[letter]; i++) { + letterPool.push(letter); + } + } + + // for (let i = 0; i < 10; i++) { + while (hand.length < 10) { + let randomIndex = parseInt(Math.random() * letterPool.length); + let drawnLetter = letterPool[randomIndex]; + hand.push(drawnLetter); + letterPool.splice(randomIndex, 1); + } + + return hand; }; export const usesAvailableLetters = (input, lettersInHand) => { - // Implement this method for wave 2 + for (let letter of input) { + if (lettersInHand.includes(letter)) { + let i = lettersInHand.indexOf(letter); + lettersInHand.splice(i, 1); + } else { + return false; + } + } + return true; }; export const scoreWord = (word) => { - // Implement this method for wave 3 + // create dictionary with score for each letter + let letterScore = {}; + const buildScoreDict = (letters, score) => { + for (const letter of letters) { + letterScore[letter] = score; + } + }; + + buildScoreDict(["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], 1); + buildScoreDict(["D", "G"], 2); + buildScoreDict(["B", "C", "M", "P"], 3); + buildScoreDict(["F", "H", "V", "W", "Y"], 4); + buildScoreDict(["K"], 5); + buildScoreDict(["J", "X"], 8); + buildScoreDict(["Q", "Z"], 10); + + // calculate score + word = word.toUpperCase(); + + let points = 0; + if (word.length === 0) { + return points; + } else if (word.length > 6) { + points += 8; + } + for (let letter of word) { + points += letterScore[letter]; + } + return points; }; export const highestScoreFrom = (words) => { - // Implement this method for wave 4 + let max = 0; + let highestScoreWord = ""; + for (let word of words) { + if (scoreWord(word) > max) { + max = scoreWord(word); + highestScoreWord = word; + } else if (scoreWord(word) === max) { + if (word.length != 10 && highestScoreWord.length != 10) { + if (word.length < highestScoreWord.length) { + highestScoreWord = word; + } + } else if (word.length === 10 && highestScoreWord.length != 10) { + highestScoreWord = word; + } else if (word.length != 10 && highestScoreWord.length === 10) { + highestScoreWord = highestScoreWord; + } else if (word.length === highestScoreWord) { + highestScoreWord = highestScoreWord; + } + } + } + const output = { + word: highestScoreWord, + score: max, + }; + return output; }; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 1a0dc94e..fc35fcf9 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -120,7 +120,9 @@ describe("Adagrams", () => { }); it("returns a score of 0 if given an empty input", () => { - throw "Complete test"; + expectScores({ + "": 0, + }); }); it("adds an extra 8 points if word is 7 or more characters long", () => { @@ -133,7 +135,7 @@ describe("Adagrams", () => { }); }); - describe.skip("highestScoreFrom", () => { + describe("highestScoreFrom", () => { it("returns a hash that contains the word and score of best word in an array", () => { const words = ["X", "XX", "XXX", "XXXX"]; const correct = { word: "XXXX", score: scoreWord("XXXX") }; @@ -145,7 +147,7 @@ describe("Adagrams", () => { const words = ["XXX", "XXXX", "X", "XX"]; const correct = { word: "XXXX", score: scoreWord("XXXX") }; - throw "Complete test by adding an assertion"; + expect(highestScoreFrom(words)).toEqual(correct); }); describe("in case of tied score", () => { diff --git a/test/demo/model.test.js b/test/demo/model.test.js index 49bf9599..9616ec8b 100644 --- a/test/demo/model.test.js +++ b/test/demo/model.test.js @@ -1,48 +1,45 @@ -import Model from 'demo/model'; -import Adagrams from 'demo/adagrams'; +import Model from "demo/model"; +import Adagrams from "demo/adagrams"; -describe.skip('Game Model', () => { +describe("Game Model", () => { const config = { - players: [ - 'Player A', - 'Player B', - ], + players: ["Player A", "Player B"], rounds: 3, time: 60, // Seconds }; - describe('constructor', () => { - it('creates a new Model instance', () => { + describe("constructor", () => { + it("creates a new Model instance", () => { const model = new Model(config); expect(model).toBeInstanceOf(Model); }); - it('requires a config parameter', () => { + it("requires a config parameter", () => { expect(() => { const model = new Model(); }).toThrow(/config/); }); - it('initializes the round number to zero', () => { + it("initializes the round number to zero", () => { const model = new Model(config); expect(model.round).toBe(0); }); - it('initializes the current player to null', () => { + it("initializes the current player to null", () => { const model = new Model(config); expect(model.currentPlayer).toBe(null); }); - it('initializes the letter bank to null', () => { + it("initializes the letter bank to null", () => { const model = new Model(config); expect(model.letterBank).toBe(null); }); - it('initializes the plays history', () => { + it("initializes the plays history", () => { const model = new Model(config); expect(model.plays).toBeInstanceOf(Object); @@ -54,14 +51,14 @@ describe.skip('Game Model', () => { }); }); - describe('.currentPlayerName()', () => { - it('is defined', () => { + describe(".currentPlayerName()", () => { + it("is defined", () => { const model = new Model(config); expect(model.currentPlayerName).toBeDefined(); }); - it('returns the name of the current player when game is on-going', () => { + it("returns the name of the current player when game is on-going", () => { const model = new Model(config); model.nextRound(); @@ -69,21 +66,21 @@ describe.skip('Game Model', () => { expect(model.currentPlayerName()).toEqual(model.config.players[0]); }); - it('returns null when the game is not on-going', () => { + it("returns null when the game is not on-going", () => { const model = new Model(config); expect(model.currentPlayerName()).toBe(null); }); }); - describe('.nextRound', () => { - it('is defined', () => { + describe(".nextRound", () => { + it("is defined", () => { const model = new Model(config); expect(model.nextRound).toBeDefined(); }); - it('increments the round number', () => { + it("increments the round number", () => { const model = new Model(config); const roundBefore = model.round; @@ -92,7 +89,7 @@ describe.skip('Game Model', () => { expect(model.round).toBe(roundBefore + 1); }); - it('initializes the current player number to first player', () => { + it("initializes the current player number to first player", () => { const model = new Model(config); model.nextRound(); @@ -100,7 +97,7 @@ describe.skip('Game Model', () => { expect(model.currentPlayer).toBe(0); }); - it('initializes the round play history for first player', () => { + it("initializes the round play history for first player", () => { const model = new Model(config); model.nextRound(); @@ -113,7 +110,7 @@ describe.skip('Game Model', () => { }); }); - it('draws a new hand of letters', () => { + it("draws a new hand of letters", () => { const model = new Model(config); model.nextRound(); @@ -125,8 +122,8 @@ describe.skip('Game Model', () => { }); }); - describe('returns game state', () => { - it('gameOver', () => { + describe("returns game state", () => { + it("gameOver", () => { const model = new Model({ ...config, rounds: 1 }); const gameState = model.nextRound(); @@ -138,8 +135,8 @@ describe.skip('Game Model', () => { expect(gameOverState.gameOver).toBe(true); }); - it('winner', () => { - const model = new Model({ ...config, rounds : 2 }); + it("winner", () => { + const model = new Model({ ...config, rounds: 2 }); // Start game, no one has won yet let gameState = model.nextRound(); @@ -147,7 +144,7 @@ describe.skip('Game Model', () => { // First player plays a word let p1Score = 0; - let word = model.letterBank.slice(0, 5).join(''); + let word = model.letterBank.slice(0, 5).join(""); p1Score += model.playWord(word); // Second player does not play @@ -157,7 +154,7 @@ describe.skip('Game Model', () => { expect(gameState.winner).toBe(null); // First player plays another word - word = model.letterBank.slice(0, 5).join(''); + word = model.letterBank.slice(0, 5).join(""); p1Score += model.playWord(word); // Second player does not play again @@ -173,7 +170,7 @@ describe.skip('Game Model', () => { }); }); - describe('.nextTurn', () => { + describe(".nextTurn", () => { const getModel = () => { const model = new Model(config); model.nextRound(); @@ -181,13 +178,13 @@ describe.skip('Game Model', () => { return model; }; - it('is defined', () => { + it("is defined", () => { const model = getModel(); expect(model.nextTurn).toBeDefined(); }); - it('increments the current player index', () => { + it("increments the current player index", () => { const model = getModel(); const origPlayer = model.currentPlayer; @@ -198,8 +195,8 @@ describe.skip('Game Model', () => { expect(model.currentPlayer).toBe(origPlayer + 2); }); - describe('returns round state', () => { - it('roundOver', () => { + describe("returns round state", () => { + it("roundOver", () => { const model = getModel(); const roundState = model.nextTurn(); @@ -208,7 +205,7 @@ describe.skip('Game Model', () => { expect(config.players.length).toBeGreaterThan(1); expect(roundState).toBeInstanceOf(Object); - expect(roundState).toHaveProperty('roundOver'); + expect(roundState).toHaveProperty("roundOver"); expect(roundState.roundOver).toBe(false); // Advance to the final turn @@ -221,7 +218,7 @@ describe.skip('Game Model', () => { expect(roundOverState.roundOver).toBe(true); }); - it('winner', () => { + it("winner", () => { const model = getModel(); const roundState = model.nextTurn(); @@ -236,7 +233,7 @@ describe.skip('Game Model', () => { }); // Play a word as the last player - const word = model.letterBank.slice(0, 5).join(''); + const word = model.letterBank.slice(0, 5).join(""); const score = model.playWord(word); // Complete the final turn, round is over and winner should be set @@ -253,7 +250,7 @@ describe.skip('Game Model', () => { }); }); - describe('.playWord', () => { + describe(".playWord", () => { const getModel = () => { const model = new Model(config); model.nextRound(); @@ -265,18 +262,18 @@ describe.skip('Game Model', () => { return [...(model.plays[player][round - 1] || [])]; }; - it('is defined', () => { + it("is defined", () => { const model = getModel(); expect(model.playWord).toBeDefined(); }); - describe('for valid words', () => { + describe("for valid words", () => { const getWord = (model) => { - return model.letterBank.slice(0, 5).join(''); + return model.letterBank.slice(0, 5).join(""); }; - it('it returns the word score', () => { + it("it returns the word score", () => { const model = getModel(); const word = getWord(model); const score = Adagrams.scoreWord(word); @@ -284,21 +281,28 @@ describe.skip('Game Model', () => { expect(model.playWord(word)).toBe(score); }); - it('adds word to plays history for current player', () => { + it("adds word to plays history for current player", () => { const model = getModel(); const player = model.currentPlayerName(); const origPlays = getPlays(model, player, model.round); const word1 = getWord(model); model.playWord(word1); - expect(getPlays(model, player, model.round)).toEqual([...origPlays, word1]); + expect(getPlays(model, player, model.round)).toEqual([ + ...origPlays, + word1, + ]); const word2 = getWord(model); model.playWord(word2); - expect(getPlays(model, player, model.round)).toEqual([...origPlays, word1, word2]); + expect(getPlays(model, player, model.round)).toEqual([ + ...origPlays, + word1, + word2, + ]); }); - it('validates word case-insensitively', () => { + it("validates word case-insensitively", () => { const model = getModel(); const word = getWord(model); const score = Adagrams.scoreWord(word); @@ -307,27 +311,29 @@ describe.skip('Game Model', () => { }); }); - describe('for invalid words', () => { + describe("for invalid words", () => { const getWord = (model) => { const letter = model.letterBank[0]; - return letter.repeat(model.letterBank.filter((l) => { - return l === letter; - }).length + 1); + return letter.repeat( + model.letterBank.filter((l) => { + return l === letter; + }).length + 1 + ); }; - it('it returns null', () => { + it("it returns null", () => { const model = getModel(); const word = getWord(model); expect(model.playWord(word)).toBe(null); - expect(model.playWord('123')).toBe(null); - expect(model.playWord('')).toBe(null); + expect(model.playWord("123")).toBe(null); + expect(model.playWord("")).toBe(null); }); - it('does not add word to history', () => { + it("does not add word to history", () => { const model = getModel(); const word = getWord(model); - const origPlays = {...model.plays}; + const origPlays = { ...model.plays }; model.playWord(word);