-
Notifications
You must be signed in to change notification settings - Fork 40
JS Scrabble - Angela - Octos #44
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: master
Are you sure you want to change the base?
Changes from all commits
cbf06ce
815f962
75a63f5
cf34348
6f770b4
627d4e1
5540d22
41f2ee5
5afdb3e
ae50b52
2c6f618
a3588b9
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,15 +1,155 @@ | ||
| const LETTER_VALUES = | ||
| { | ||
| "a": { "points": 1, "tiles": 9 }, | ||
| "b": { "points": 3, "tiles": 2 }, | ||
| "c": { "points": 3, "tiles": 2 }, | ||
| "d": { "points": 2, "tiles": 4 }, | ||
| "e": { "points": 1, "tiles": 12 }, | ||
| "f": { "points": 4, "tiles": 2 }, | ||
| "g": { "points": 2, "tiles": 3 }, | ||
| "h": { "points": 4, "tiles": 2 }, | ||
| "i": { "points": 1, "tiles": 9 }, | ||
| "j": { "points": 8, "tiles": 1 }, | ||
| "k": { "points": 5, "tiles": 1 }, | ||
| "l": { "points": 1, "tiles": 4 }, | ||
| "m": { "points": 3, "tiles": 2 }, | ||
| "n": { "points": 1, "tiles": 6 }, | ||
| "o": { "points": 1, "tiles": 8 }, | ||
| "p": { "points": 3, "tiles": 2 }, | ||
| "q": { "points": 10, "tiles": 1 }, | ||
| "r": { "points": 1, "tiles": 6 }, | ||
| "s": { "points": 1, "tiles": 4 }, | ||
| "t": { "points": 1, "tiles": 6 }, | ||
| "u": { "points": 1, "tiles": 4 }, | ||
| "v": { "points": 4, "tiles": 2 }, | ||
| "w": { "points": 4, "tiles": 2 }, | ||
| "x": { "points": 8, "tiles": 1 }, | ||
| "y": { "points": 4, "tiles": 2 }, | ||
| "z": { "points": 10, "tiles": 1 }, | ||
| "blank": { "tiles": 2 } | ||
| } | ||
| const MAX_LENGTH = 7; | ||
|
|
||
| const Scrabble = { | ||
|
|
||
| isRealWord(word) { | ||
| if (!word.match(/^[a-zA-Z]+$/)) { | ||
| throw "Not a valid word"; | ||
| } else if (word.length === 0) { | ||
| throw "No word given"; | ||
|
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. Instead of throwing a string, you should throw an instance of if (condition) {
throw new Error(`Invalid word ${word}`);
} |
||
| } else if (word.length > MAX_LENGTH) { | ||
| throw "Scrabble words can only be 7 letters!"; | ||
| } | ||
| }, | ||
|
|
||
| score(word) { | ||
|
|
||
| Scrabble.isRealWord(word); | ||
|
|
||
| word = word.toLowerCase().split(""); | ||
| let wordScore = 0; | ||
|
|
||
| word.forEach (function (letter) { | ||
| let points = LETTER_VALUES[letter]["points"]; | ||
| wordScore += points; | ||
| }); | ||
|
|
||
| if (word.length === 7) { | ||
| wordScore += 50; | ||
| } | ||
|
|
||
| return wordScore; | ||
| }, | ||
| breakTie(topWord, word) { | ||
| if ( topWord.length === 7 ) { | ||
| return topWord; | ||
| } else if (word.length === 7) { | ||
|
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. Good use of a helper function to clarify this logic. |
||
| return word; | ||
| } else if (word.length < topWord.length) { | ||
| return word; | ||
| } else { | ||
| return topWord; | ||
| } | ||
| }, | ||
|
|
||
| highestScoreFrom(arrayOfWords) { | ||
|
|
||
| }, | ||
| }; | ||
| if (arrayOfWords.length === 0) { | ||
| throw "No words given"; | ||
| } | ||
|
|
||
| let topWord = arrayOfWords[0]; | ||
|
|
||
| arrayOfWords.forEach (function (word) { | ||
| if ( Scrabble.score(word) > Scrabble.score(topWord)) { | ||
| topWord = word; | ||
| } else if ( Scrabble.score(word) == Scrabble.score(topWord)) { | ||
| topWord = Scrabble.breakTie(topWord, word); | ||
| } | ||
| }); | ||
| return topWord; | ||
| } | ||
| } | ||
|
|
||
| Scrabble.Player = class { | ||
|
|
||
| constructor(name) { | ||
| if (name == null) { | ||
| throw("The player needs a name!"); | ||
| } | ||
| this.name = name; | ||
| this.plays = []; | ||
| } | ||
|
|
||
| play(word) { | ||
| Scrabble.isRealWord(word); | ||
|
|
||
| if (this.hasWon()) { | ||
| return false; | ||
| } else { | ||
| this.plays.push(word); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| totalScore() { | ||
| let score = 0; | ||
| this.plays.forEach (function (word){ | ||
| let wordScore = Scrabble.score(word); | ||
| score += wordScore; | ||
| }); | ||
| return score; | ||
| } | ||
|
|
||
| hasWon() { | ||
| let score = this.totalScore(); | ||
| if (score >= 100) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| highestScoringWord() { | ||
| if (this.plays.length === 0) { | ||
| throw "No words played"; | ||
| } | ||
|
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. You should be reusing the scoring logic from |
||
|
|
||
| let max_word = this.plays[0]; | ||
| this.plays.forEach (function(word) { | ||
| if ( Scrabble.score(word) > Scrabble.score(max_word) ) { | ||
| max_word = word; | ||
| } | ||
| }); | ||
| return max_word; | ||
| } | ||
|
|
||
| highestWordScore() { | ||
| let word = this.highestScoringWord(); | ||
| let wordScore = Scrabble.score(word); | ||
|
|
||
| return wordScore; | ||
| } | ||
| }; | ||
|
|
||
|
|
||
|
|
||
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.
Nice work tying together point values and tile counts in one data structure - very clever.