-
Notifications
You must be signed in to change notification settings - Fork 20
AC2 - Larissa Ault #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e4f9f23
400aa2b
02a931c
b0bf14f
1f69249
6c80a58
6149281
0bf8384
a11c425
60f7a1e
071350d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| node_modules | ||
| package.json.lock | ||
| .DS_Store | ||
| coverage | ||
| coverage | ||
| .vscode/ | ||
| .gitignore |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,71 @@ | ||
| import { Alphabet } from "./modules/Alphabet.js"; | ||
| import { Score } from "./modules/Score.js"; | ||
|
|
||
| export const drawLetters = () => { | ||
| // Implement this method for wave 1 | ||
| // initialize player hand | ||
| let hand = []; | ||
| // initialize alphabet | ||
| let alphabet = new Alphabet(); | ||
| // draw 10 letters | ||
| for (let i = 0; i < 10; ++i) { | ||
| // get array of letters (keys) from alphabet object | ||
| const keys = Object.keys(alphabet.letterCounts); | ||
| // randomly pick one letter (key) | ||
| const letter = keys[Math.floor(Math.random() * keys.length)]; | ||
| // add letter to hand | ||
| hand.push(letter); | ||
| // letter value in alphabet object | ||
| alphabet.decreaseLetterCount(letter); | ||
| } | ||
| return hand; | ||
| }; | ||
|
|
||
| export const usesAvailableLetters = (input, lettersInHand) => { | ||
| // Implement this method for wave 2 | ||
| // convert input string to array | ||
| const inputArr = [...input.toUpperCase()]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cue gwen stefanni! (nice use of spread syntax :) ) |
||
| // make mutable copy of letters in hand | ||
| let handArr = [...lettersInHand]; | ||
| for (let i = 0; i < inputArr.length; i++) { | ||
| // collect letter | ||
| const letter = inputArr[i]; | ||
| // check if input letter is in hand | ||
| if (handArr.includes(letter)) { | ||
| // collect index of letter | ||
| const index = handArr.indexOf(letter); | ||
| // remove letter from copy of letters in hand - prevent duplication | ||
| handArr.splice(index, 1); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice use of splice for the removal |
||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| }; | ||
|
|
||
| export const scoreWord = (word) => { | ||
| // Implement this method for wave 3 | ||
| // check for word input | ||
| if (typeof word === "undefined") { | ||
| return 0; | ||
| } else { | ||
| return new Score().calculateWordScore(word); | ||
| } | ||
| }; | ||
|
|
||
| export const highestScoreFrom = (words) => { | ||
| // Implement this method for wave 4 | ||
| // initialize winning word object | ||
| const winningWord = { word: "", score: 0 }; | ||
| for (let word of words) { | ||
| // collect word score | ||
| const score = scoreWord(word); | ||
| // pick word with highest score | ||
| if (score > winningWord.score) { | ||
| winningWord.word = word; | ||
| winningWord.score = score; | ||
| } else if (score === winningWord.score) { | ||
| // pick word by tie breaker criteria | ||
| winningWord.word = Score.breakTie(winningWord.word, word); | ||
| } else { | ||
| continue; | ||
| } | ||
| } | ||
| return winningWord; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| export class Alphabet { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, loving the modularization! |
||
| constructor() { | ||
| this.letterCounts = { | ||
| 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, | ||
| }; | ||
| } | ||
|
|
||
| decreaseLetterCount(letter) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this! |
||
| /** | ||
| * Decreases letter count by 1 for a given letter and updates letter in letterCounts. | ||
| * When letter count is 0, removes letter from letterCounts. | ||
| * @param {[string]} letter [letter to decrease] | ||
| */ | ||
| // decrease letter count by 1 for given letter | ||
| this.letterCounts[letter] -= 1; | ||
| // check if value is now zero | ||
| if (this.letterCounts[letter] === 0) { | ||
| // remove key from alphabet obj | ||
| delete this.letterCounts[letter]; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { Alphabet } from "./Alphabet"; | ||
|
|
||
| export class Hand extends Alphabet { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OOP! |
||
| constructor(num) { | ||
| super(); | ||
| this.hand = this.drawLettersInAlphabet(num); | ||
| } | ||
| drawLettersInAlphabet(num) { | ||
| for (let i = 0; i < num; ++i) { | ||
| const letters = Object.keys(this.letterCounts); | ||
| const randomLetter = letters[Math.floor(Math.random() * letters.length)]; | ||
| this.hand.push(randomLetter); | ||
| this.letterCounts.decreaseLetterCount(randomLetter); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| export class Score { | ||
| constructor() { | ||
| this.scoreChart = { | ||
| A: 1, | ||
| E: 1, | ||
| I: 1, | ||
| O: 1, | ||
| U: 1, | ||
| L: 1, | ||
| N: 1, | ||
| R: 1, | ||
| S: 1, | ||
| T: 1, | ||
| D: 2, | ||
| G: 2, | ||
| B: 3, | ||
| C: 3, | ||
| M: 3, | ||
| P: 3, | ||
| F: 4, | ||
| H: 4, | ||
| V: 4, | ||
| W: 4, | ||
| Y: 4, | ||
| K: 5, | ||
| J: 8, | ||
| X: 8, | ||
| Q: 10, | ||
| Z: 10, | ||
| }; | ||
| } | ||
| calculateWordScore(word) { | ||
| // initialize word score | ||
| let wordScore = 0; | ||
| // convert word string to array | ||
| const wordArr = [...word.toUpperCase()]; | ||
| // check each letter in word | ||
| wordArr.forEach((letter) => { | ||
| // look up letter score in scoreChart | ||
| const letterScore = this.scoreChart[letter]; | ||
| // add to word score | ||
| wordScore += letterScore; | ||
| }); | ||
| if (word.length >= 7) { | ||
| wordScore += 8; | ||
| } | ||
| return wordScore; | ||
| } | ||
| static breakTie(word1, word2) { | ||
| /** | ||
| * Chooses between two words of equal score with the following criteria | ||
| * in order of priority: | ||
| * (1) word of length 10 | ||
| * (2) smallest length word | ||
| * (3) word1 if lengths equal | ||
| */ | ||
| // pick smallest word length | ||
| if (word1.length === 10) { | ||
| return word1; | ||
| } else if (word2.length < word1.length || word2.length === 10) { | ||
| return word2; | ||
| } else { | ||
| // return first word | ||
| return word1; | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Way to go, breaking your code in to separate files!