diff --git a/src/adagrams.js b/src/adagrams.js index 7ec5afc7..0038f61b 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,15 +1,138 @@ -export const drawLetters = () => { - // Implement this method for wave 1 +const letterPool = { + 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 usesAvailableLetters = (input, lettersInHand) => { - // Implement this method for wave 2 +const scoreChart = { + 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 alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; -export const scoreWord = (word) => { - // Implement this method for wave 3 -}; +// ------------ Wave 05--------------------------- +class Adagrams { + static drawLetters() { + let availableLetters = { ...letterPool }; + const lettersInHand = []; -export const highestScoreFrom = (words) => { - // Implement this method for wave 4 -}; + while (lettersInHand.length < 10) { + const letter = alphabet[Math.floor(Math.random() * alphabet.length)]; + + if (availableLetters[letter] > 0) { + availableLetters[letter]--; + lettersInHand.push(letter); + } + } + return lettersInHand; + } + + static usesAvailableLetters(input, lettersInHand) { + const drawnDict = {}; + lettersInHand.forEach((letter) => { + if (letter in drawnDict) { + drawnDict[letter]++; + } else { + drawnDict[letter] = 1; + } + }); + const word = [...input.toUpperCase()]; + + for (const letter of word) { + if (drawnDict[letter] == 0 || !drawnDict[letter]) { + return false; + } else { + drawnDict[letter]--; + } + } + + return true; + } + + static scoreWord(word) { + const wordList = [...word.toUpperCase()]; + let score = 0; + if (wordList.length >= 7) { + score += 8; + } + + wordList.forEach((letter) => (score += scoreChart[letter])); + + return score; + } + + static highestScoreFrom(words) { + let highestScore = 0; + let winningWord = ""; + + words.forEach((word) => { + const score = Adagrams.scoreWord(word); + + if (score > highestScore) { + highestScore = score; + winningWord = word; + } else if (score === highestScore) { + if (word.length === 10 && winningWord.length != 10) { + winningWord = word; + } else if ( + word.length < winningWord.length && + winningWord.length != 10 + ) { + winningWord = word; + } + } + }); + + const winner = { word: winningWord, score: highestScore }; + return winner; + } +} + +export default Adagrams; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 1a0dc94e..7587da97 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -1,10 +1,4 @@ -import { - drawLetters, - usesAvailableLetters, - scoreWord, - highestScoreFrom, -} from "adagrams"; - +import Adagrams from "adagrams"; const LETTER_POOL = { A: 9, B: 2, @@ -36,14 +30,16 @@ const LETTER_POOL = { describe("Adagrams", () => { describe("drawLetters", () => { + // const newAdagram = new Adagrams() + it("draws ten letters from the letter pool", () => { - const drawn = drawLetters(); + const drawn = Adagrams.drawLetters(); expect(drawn).toHaveLength(10); }); it("returns an array, and each item is a single-letter string", () => { - const drawn = drawLetters(); + const drawn = Adagrams.drawLetters(); expect(Array.isArray(drawn)).toBe(true); drawn.forEach((l) => { @@ -53,7 +49,7 @@ describe("Adagrams", () => { it("does not draw a letter too many times", () => { for (let i = 0; i < 1000; i++) { - const drawn = drawLetters(); + const drawn = Adagrams.drawLetters(); const letter_freq = {}; for (let letter of drawn) { if (letter in letter_freq) { @@ -73,9 +69,9 @@ describe("Adagrams", () => { describe("usesAvailableLetters", () => { it("returns true if the submitted letters are valid against the drawn letters", () => { const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; - const word = "DOG"; - const isValid = usesAvailableLetters(word, drawn); + const word = "DOG"; + const isValid = Adagrams.usesAvailableLetters(word, drawn); expect(isValid).toBe(true); }); @@ -83,7 +79,8 @@ describe("Adagrams", () => { const drawn = ["D", "O", "X", "X", "X", "X", "X", "X", "X", "X"]; const word = "DOG"; - const isValid = usesAvailableLetters(word, drawn); + const isValid = Adagrams.usesAvailableLetters(word, drawn); + console.log(isValid); expect(isValid).toBe(false); }); @@ -91,7 +88,7 @@ describe("Adagrams", () => { const drawn = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"]; const word = "GOOD"; - const isValid = usesAvailableLetters(word, drawn); + const isValid = Adagrams.usesAvailableLetters(word, drawn); expect(isValid).toBe(false); }); }); @@ -99,7 +96,7 @@ describe("Adagrams", () => { describe("scoreWord", () => { const expectScores = (wordScores) => { Object.entries(wordScores).forEach(([word, score]) => { - expect(scoreWord(word)).toBe(score); + expect(Adagrams.scoreWord(word)).toBe(score); }); }; @@ -120,7 +117,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,24 +132,24 @@ 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") }; + const correct = { word: "XXXX", score: Adagrams.scoreWord("XXXX") }; - expect(highestScoreFrom(words)).toEqual(correct); + expect(Adagrams.highestScoreFrom(words)).toEqual(correct); }); it("accurately finds best scoring word even if not sorted", () => { const words = ["XXX", "XXXX", "X", "XX"]; - const correct = { word: "XXXX", score: scoreWord("XXXX") }; + const correct = { word: "XXXX", score: Adagrams.scoreWord("XXXX") }; - throw "Complete test by adding an assertion"; + expect(Adagrams.highestScoreFrom(words)).toEqual(correct); }); describe("in case of tied score", () => { const expectTie = (words) => { - const scores = words.map((word) => scoreWord(word)); + const scores = words.map((word) => Adagrams.scoreWord(word)); const highScore = scores.reduce((h, s) => (h < s ? s : h), 0); const tiedWords = scores.filter((s) => s == highScore); @@ -162,37 +161,37 @@ describe("Adagrams", () => { const words = ["AAAAAAAAAA", "BBBBBB"]; const correct = { word: "AAAAAAAAAA", - score: scoreWord("AAAAAAAAAA"), + score: Adagrams.scoreWord("AAAAAAAAAA"), }; expectTie(words); - expect(highestScoreFrom(words)).toEqual(correct); - expect(highestScoreFrom(words.reverse())).toEqual(correct); + expect(Adagrams.highestScoreFrom(words)).toEqual(correct); + expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct); }); it("selects the word with fewer letters when neither are 10 letters", () => { const words = ["MMMM", "WWW"]; - const correct = { word: "WWW", score: scoreWord("WWW") }; + const correct = { word: "WWW", score: Adagrams.scoreWord("WWW") }; expectTie(words); - expect(highestScoreFrom(words)).toEqual(correct); - expect(highestScoreFrom(words.reverse())).toEqual(correct); + expect(Adagrams.highestScoreFrom(words)).toEqual(correct); + expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(correct); }); it("selects the first word when both have same length", () => { const words = ["AAAAAAAAAA", "EEEEEEEEEE"]; const first = { word: "AAAAAAAAAA", - score: scoreWord("AAAAAAAAAA"), + score: Adagrams.scoreWord("AAAAAAAAAA"), }; const second = { word: "EEEEEEEEEE", - score: scoreWord("EEEEEEEEEE"), + score: Adagrams.scoreWord("EEEEEEEEEE"), }; expectTie(words); - expect(highestScoreFrom(words)).toEqual(first); - expect(highestScoreFrom(words.reverse())).toEqual(second); + expect(Adagrams.highestScoreFrom(words)).toEqual(first); + expect(Adagrams.highestScoreFrom(words.reverse())).toEqual(second); }); }); }); diff --git a/test/demo/model.test.js b/test/demo/model.test.js index 49bf9599..a5f2a89b 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.skip("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);