From 0241fe430e42e7039b344ccc0e588406c5002225 Mon Sep 17 00:00:00 2001 From: Kate Date: Sun, 4 Dec 2022 14:44:09 -0800 Subject: [PATCH 01/12] 0 --- src/adagrams.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 7ec5afc7..0e4bc1da 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,15 +1,108 @@ +let letterDist = { + A: 9, + N: 6, + B: 2, + O: 8, + C: 2, + P: 2, + D: 4, + Q: 1, + E: 12, + R: 6, + F: 2, + S: 4, + G: 3, + T: 6, + H: 2, + U: 4, + I: 9, + V: 2, + J: 1, + W: 2, + K: 1, + X: 1, + L: 4, + Y: 2, + M: 2, + Z: 1, +}; + +//* create score dictionary +let letterScore = {}; +//* create score dictionary +const buildScore = (letters, score) => { + for (const letter of letters) { + letterScore[letter] = score; + } +}; +//* create score dictionary +buildScore(["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], 1); +buildScore(["D", "G"], 2); +buildScore(["B", "C", "M", "P"], 3); +buildScore(["F", "H", "V", "W", "Y"], 4); +buildScore(["K"], 5); +buildScore(["J", "X"], 8); +buildScore(["Q", "Z"], 10); + +//calculate total numbers of letters in pool +let totalLetters = 0; +for (const value of Object.values(letterDist)) { + totalLetters += value; +} +//create letter pool from letter distribution +let letterPool = []; +for (const letter of Object.keys(letterDist)) { + for (let i in letterDist) { + letterPool.push(letter); + } +} + export const drawLetters = () => { - // Implement this method for wave 1 + let hand = []; + for (let i = 1; (i = 10); i++) { + let drawnLetter = Math.random() * totalLetters; + delete letterPool[drawnLetter]; + hand.push(letterPool[drawnLetter]); + } }; export const usesAvailableLetters = (input, lettersInHand) => { - // Implement this method for wave 2 + for (let letter of input) { + if (lettersInHand.includes(letter)) { + i = lettersInHand.indexOf(letter); + fruits.splice(i, 1); + } else { + return false; + } + return true; + } }; export const scoreWord = (word) => { - // Implement this method for wave 3 + score = 0; + for (let letter of word) { + score += letterScore[letter]; + } + return score; }; export const highestScoreFrom = (words) => { // Implement this method for wave 4 + let max = 0; + let highestScoreWord = ""; + for (const word in words) { + if (scoreWord(word) > max) { + max = scoreWord(word); + highestScoreWord = word; + } else if (scoreWord(word) === max) { + if (word.length < highestScoreWord.length) { + highestScoreWord = word; + } + } + } + output = { + word: highestScoreWord, + score: max, + }; + return output; }; From aaa41f907c79f3faa74876c5063ff6b54a318791 Mon Sep 17 00:00:00 2001 From: Kate Date: Sun, 4 Dec 2022 15:27:27 -0800 Subject: [PATCH 02/12] add codes inside function --- src/adagrams.js | 118 ++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 0e4bc1da..efcfeebc 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,68 +1,51 @@ -let letterDist = { - A: 9, - N: 6, - B: 2, - O: 8, - C: 2, - P: 2, - D: 4, - Q: 1, - E: 12, - R: 6, - F: 2, - S: 4, - G: 3, - T: 6, - H: 2, - U: 4, - I: 9, - V: 2, - J: 1, - W: 2, - K: 1, - X: 1, - L: 4, - Y: 2, - M: 2, - Z: 1, -}; +export const drawLetters = () => { + const letterDist = { + A: 9, + N: 6, + B: 2, + O: 8, + C: 2, + P: 2, + D: 4, + Q: 1, + E: 12, + R: 6, + F: 2, + S: 4, + G: 3, + T: 6, + H: 2, + U: 4, + I: 9, + V: 2, + J: 1, + W: 2, + K: 1, + X: 1, + L: 4, + Y: 2, + M: 2, + Z: 1, + }; -//* create score dictionary -let letterScore = {}; -//* create score dictionary -const buildScore = (letters, score) => { - for (const letter of letters) { - letterScore[letter] = score; + let letterPool = []; + //create letter pool from letter distribution + for (const letter of Object.keys(letterDist)) { + for (let i = 0; (i = letterDist[letter]); i++) { + letterPool.push(letter); + } } -}; -//* create score dictionary -buildScore(["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], 1); -buildScore(["D", "G"], 2); -buildScore(["B", "C", "M", "P"], 3); -buildScore(["F", "H", "V", "W", "Y"], 4); -buildScore(["K"], 5); -buildScore(["J", "X"], 8); -buildScore(["Q", "Z"], 10); - -//calculate total numbers of letters in pool -let totalLetters = 0; -for (const value of Object.values(letterDist)) { - totalLetters += value; -} -//create letter pool from letter distribution -let letterPool = []; -for (const letter of Object.keys(letterDist)) { - for (let i in letterDist) { - letterPool.push(letter); + //calculate total numbers of letters in pool + let totalLetters = 0; + for (const value of Object.values(letterDist)) { + totalLetters += value; } -} -export const drawLetters = () => { + //draw letters from pool into hand let hand = []; for (let i = 1; (i = 10); i++) { - let drawnLetter = Math.random() * totalLetters; - delete letterPool[drawnLetter]; - hand.push(letterPool[drawnLetter]); + let drawnLetter = letterPool[Math.random() * totalLetters]; + hand.push(hand[drawnLetter]); } }; @@ -79,6 +62,23 @@ export const usesAvailableLetters = (input, lettersInHand) => { }; export const scoreWord = (word) => { + // 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 score = 0; for (let letter of word) { score += letterScore[letter]; From ee43b0cab784f711e93d8f738a3d52208d6f1eb8 Mon Sep 17 00:00:00 2001 From: Kate Date: Sun, 4 Dec 2022 18:35:58 -0800 Subject: [PATCH 03/12] passed 3 tests --- src/adagrams.js | 99 +++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index efcfeebc..87123b2e 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,59 +1,54 @@ -export const drawLetters = () => { - const letterDist = { - A: 9, - N: 6, - B: 2, - O: 8, - C: 2, - P: 2, - D: 4, - Q: 1, - E: 12, - R: 6, - F: 2, - S: 4, - G: 3, - T: 6, - H: 2, - U: 4, - I: 9, - V: 2, - J: 1, - W: 2, - K: 1, - X: 1, - L: 4, - Y: 2, - M: 2, - Z: 1, - }; +const letterDist = { + A: 9, + N: 6, + B: 2, + O: 8, + C: 2, + P: 2, + D: 4, + Q: 1, + E: 12, + R: 6, + F: 2, + S: 4, + G: 3, + T: 6, + H: 2, + U: 4, + I: 9, + V: 2, + J: 1, + W: 2, + K: 1, + X: 1, + L: 4, + Y: 2, + M: 2, + Z: 1, +}; +export const drawLetters = () => { let letterPool = []; //create letter pool from letter distribution - for (const letter of Object.keys(letterDist)) { - for (let i = 0; (i = letterDist[letter]); i++) { + for (let letter in letterDist) { + for (let i = 0; i < letterDist[letter] + 1; i++) { letterPool.push(letter); } } - //calculate total numbers of letters in pool - let totalLetters = 0; - for (const value of Object.values(letterDist)) { - totalLetters += value; - } - - //draw letters from pool into hand let hand = []; - for (let i = 1; (i = 10); i++) { - let drawnLetter = letterPool[Math.random() * totalLetters]; - hand.push(hand[drawnLetter]); + for (let i = 1; i < 11; i++) { + let randomIndex = Math.random() * letterPool.length; + let drawnLetter = letterPool[randomIndex]; + hand.push(drawnLetter); } + return hand; }; export const usesAvailableLetters = (input, lettersInHand) => { for (let letter of input) { if (lettersInHand.includes(letter)) { - i = lettersInHand.indexOf(letter); - fruits.splice(i, 1); + let i = lettersInHand.indexOf(letter); + lettersInHand.splice(i, 1); } else { return false; } @@ -64,22 +59,22 @@ export const usesAvailableLetters = (input, lettersInHand) => { export const scoreWord = (word) => { // create dictionary with score for each letter let letterScore = {}; - const BuildScoreDict = (letters, score) => { + 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); + 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 - score = 0; + let score = 0; for (let letter of word) { score += letterScore[letter]; } From af78b52dc89fe13a33f831b9e15e57d9acc12915 Mon Sep 17 00:00:00 2001 From: Kate Date: Mon, 5 Dec 2022 00:44:51 -0800 Subject: [PATCH 04/12] passed 7 tests --- src/adagrams.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 87123b2e..d13120e7 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -37,7 +37,7 @@ export const drawLetters = () => { } let hand = []; for (let i = 1; i < 11; i++) { - let randomIndex = Math.random() * letterPool.length; + let randomIndex = parseInt(Math.random() * letterPool.length); let drawnLetter = letterPool[randomIndex]; hand.push(drawnLetter); } @@ -52,8 +52,8 @@ export const usesAvailableLetters = (input, lettersInHand) => { } else { return false; } - return true; } + return true; }; export const scoreWord = (word) => { @@ -75,6 +75,7 @@ export const scoreWord = (word) => { // calculate score let score = 0; + word = word.toUpperCase(); for (let letter of word) { score += letterScore[letter]; } From 0adffedeedfcf9d885ce0cc88d22beab230357de Mon Sep 17 00:00:00 2001 From: Kate Date: Mon, 5 Dec 2022 01:55:52 -0800 Subject: [PATCH 05/12] passed 6 tests --- src/adagrams.js | 67 +++++++++++++++++++++++-------------------- test/adagrams.test.js | 7 +++-- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index d13120e7..847fa205 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -63,42 +63,47 @@ export const scoreWord = (word) => { 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); + 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 - let score = 0; word = word.toUpperCase(); - for (let letter of word) { - score += letterScore[letter]; + + let points = 0; + if (word.length === 0) { + return points; + } else + for (let letter of word) { + points += letterScore[letter]; + } + if (word.length > 7) { + points += 8; } - return score; + return points; }; export const highestScoreFrom = (words) => { - // Implement this method for wave 4 - let max = 0; - let highestScoreWord = ""; - for (const word in words) { - if (scoreWord(word) > max) { - max = scoreWord(word); - highestScoreWord = word; - } else if (scoreWord(word) === max) { - if (word.length < highestScoreWord.length) { - highestScoreWord = word; - } - } - } - output = { - word: highestScoreWord, - score: max, - }; - return output; + // let max = 0; + // let highestScoreWord = ""; + // for (const word of words) { + // if (scoreWord(word) > max) { + // max = scoreWord(word); + // highestScoreWord = word; + // } else if (scoreWord(word) === max) { + // if (word.length < highestScoreWord.length) { + // highestScoreWord = word; + // } + // } + // } + // const output = { + // word: highestScoreWord, + // score: max, + // }; + // return output; }; diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 1a0dc94e..1320e425 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -120,7 +120,10 @@ describe("Adagrams", () => { }); it("returns a score of 0 if given an empty input", () => { - throw "Complete test"; + // throw "Complete test"; + expectScores({ + "": 0, + }); }); it("adds an extra 8 points if word is 7 or more characters long", () => { @@ -133,7 +136,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") }; From 1352011e7c57688b3f8c71a66608f3faf18ed82d Mon Sep 17 00:00:00 2001 From: Kate Date: Mon, 5 Dec 2022 02:02:44 -0800 Subject: [PATCH 06/12] passed 8/15 --- src/adagrams.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 847fa205..269a5f4c 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -63,28 +63,30 @@ export const scoreWord = (word) => { 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); }; + + 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 - for (let letter of word) { - points += letterScore[letter]; - } - if (word.length > 7) { + } else if (word.length > 7) { points += 8; } + + for (let letter of word) { + points += letterScore[letter]; + } + return points; }; From 603c7f7472040c1a352c6173c13c26421a5d2291 Mon Sep 17 00:00:00 2001 From: Kate Date: Mon, 5 Dec 2022 02:05:15 -0800 Subject: [PATCH 07/12] pass 9/15 --- src/adagrams.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 269a5f4c..fca34551 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -79,14 +79,12 @@ export const scoreWord = (word) => { let points = 0; if (word.length === 0) { return points; - } else if (word.length > 7) { + } else if (word.length > 6) { points += 8; } - for (let letter of word) { points += letterScore[letter]; } - return points; }; From 3f4f0c396204ecb875ed860d11d9587bcad6a7d6 Mon Sep 17 00:00:00 2001 From: Kate Date: Mon, 5 Dec 2022 02:18:00 -0800 Subject: [PATCH 08/12] pass 12/15 --- src/adagrams.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index fca34551..0d4328cb 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -36,9 +36,10 @@ export const drawLetters = () => { } } let hand = []; - for (let i = 1; i < 11; i++) { + for (let i = 0; i < 10; i++) { let randomIndex = parseInt(Math.random() * letterPool.length); let drawnLetter = letterPool[randomIndex]; + letterPool.splice(randomIndex, 1); hand.push(drawnLetter); } return hand; @@ -89,21 +90,21 @@ export const scoreWord = (word) => { }; export const highestScoreFrom = (words) => { - // let max = 0; - // let highestScoreWord = ""; - // for (const word of words) { - // if (scoreWord(word) > max) { - // max = scoreWord(word); - // highestScoreWord = word; - // } else if (scoreWord(word) === max) { - // if (word.length < highestScoreWord.length) { - // highestScoreWord = word; - // } - // } - // } - // const output = { - // word: highestScoreWord, - // score: max, - // }; - // return output; + let max = 0; + let highestScoreWord = ""; + for (const word of words) { + if (scoreWord(word) > max) { + max = scoreWord(word); + highestScoreWord = word; + } else if (scoreWord(word) === max) { + if (word.length < highestScoreWord.length) { + highestScoreWord = word; + } + } + } + const output = { + word: highestScoreWord, + score: max, + }; + return output; }; From af609fc1f4dd6604c3d43561baa5a27b54817ff8 Mon Sep 17 00:00:00 2001 From: Kate Date: Mon, 5 Dec 2022 22:51:15 -0800 Subject: [PATCH 09/12] 13/15 passed --- src/adagrams.js | 14 ++++- test/demo/model.test.js | 118 +++++++++++++++++++++------------------- 2 files changed, 73 insertions(+), 59 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 0d4328cb..8390c30c 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -29,16 +29,18 @@ const letterDist = { export const drawLetters = () => { let letterPool = []; + let hand = []; + //create letter pool from letter distribution for (let letter in letterDist) { for (let i = 0; i < letterDist[letter] + 1; i++) { letterPool.push(letter); } } - let hand = []; + for (let i = 0; i < 10; i++) { - let randomIndex = parseInt(Math.random() * letterPool.length); - let drawnLetter = letterPool[randomIndex]; + const randomIndex = parseInt(Math.random() * letterPool.length); + const drawnLetter = letterPool[randomIndex]; letterPool.splice(randomIndex, 1); hand.push(drawnLetter); } @@ -100,6 +102,12 @@ export const highestScoreFrom = (words) => { if (word.length < highestScoreWord.length) { highestScoreWord = word; } + if (word.length === 10 && highestScoreWord.length != 10) { + highestScoreWord = word; + } + if (word.length != 10 && highestScoreWord.length === 10) { + highestScoreWord = word; + } } } const output = { 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); From 896887e2b92b0d2d34e8bc4e656399499b3a3bba Mon Sep 17 00:00:00 2001 From: Kate Date: Mon, 5 Dec 2022 23:28:50 -0800 Subject: [PATCH 10/12] pass 14/15 --- src/adagrams.js | 18 ++++++++++-------- test/adagrams.test.js | 3 +-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index 8390c30c..d4ee7791 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -94,19 +94,21 @@ export const scoreWord = (word) => { export const highestScoreFrom = (words) => { let max = 0; let highestScoreWord = ""; - for (const word of words) { + for (let word of words) { if (scoreWord(word) > max) { max = scoreWord(word); highestScoreWord = word; } else if (scoreWord(word) === max) { - if (word.length < highestScoreWord.length) { - highestScoreWord = word; - } - if (word.length === 10 && highestScoreWord.length != 10) { - highestScoreWord = word; - } - if (word.length != 10 && highestScoreWord.length === 10) { + 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; } } } diff --git a/test/adagrams.test.js b/test/adagrams.test.js index 1320e425..fc35fcf9 100644 --- a/test/adagrams.test.js +++ b/test/adagrams.test.js @@ -120,7 +120,6 @@ describe("Adagrams", () => { }); it("returns a score of 0 if given an empty input", () => { - // throw "Complete test"; expectScores({ "": 0, }); @@ -148,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", () => { From d923055e50ff15401fa72e238a5139f932e02970 Mon Sep 17 00:00:00 2001 From: Kate Date: Tue, 6 Dec 2022 15:38:46 -0800 Subject: [PATCH 11/12] passed all 15 tests --- src/adagrams.js | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/adagrams.js b/src/adagrams.js index d4ee7791..2b38fd9a 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -1,29 +1,29 @@ const letterDist = { A: 9, - N: 6, B: 2, - O: 8, C: 2, - P: 2, D: 4, - Q: 1, E: 12, - R: 6, F: 2, - S: 4, G: 3, - T: 6, H: 2, - U: 4, I: 9, - V: 2, J: 1, - W: 2, K: 1, - X: 1, L: 4, - Y: 2, 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, }; @@ -31,19 +31,21 @@ export const drawLetters = () => { let letterPool = []; let hand = []; - //create letter pool from letter distribution + // create letter pool from letter distribution for (let letter in letterDist) { - for (let i = 0; i < letterDist[letter] + 1; i++) { + for (let i = 0; i < letterDist[letter]; i++) { letterPool.push(letter); } } - - for (let i = 0; i < 10; i++) { - const randomIndex = parseInt(Math.random() * letterPool.length); - const drawnLetter = letterPool[randomIndex]; - letterPool.splice(randomIndex, 1); + console.log(letterPool); + // 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; }; From 3a1aae5bdade069237055ad1297a916f906c3e93 Mon Sep 17 00:00:00 2001 From: Kate Date: Tue, 6 Dec 2022 15:40:49 -0800 Subject: [PATCH 12/12] passed 15/15 --- src/adagrams.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adagrams.js b/src/adagrams.js index 2b38fd9a..678be290 100644 --- a/src/adagrams.js +++ b/src/adagrams.js @@ -37,7 +37,7 @@ export const drawLetters = () => { letterPool.push(letter); } } - console.log(letterPool); + // for (let i = 0; i < 10; i++) { while (hand.length < 10) { let randomIndex = parseInt(Math.random() * letterPool.length);