forked from AdaGold/js-scrabble
-
Notifications
You must be signed in to change notification settings - Fork 40
Brandy Austin - JS Scrabble - Octos #45
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
Open
brandyaustinseattle
wants to merge
6
commits into
Ada-C9:master
Choose a base branch
from
brandyaustinseattle:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd89b3d
score and highestscorefrom methods working
brandyaustinseattle ad68529
Stubbed out player class
brandyaustinseattle 6cede07
built scrabble.player class
brandyaustinseattle 214ab22
basic functions build out
brandyaustinseattle f27a734
All functions complete, addt tests in progress
brandyaustinseattle dc80e74
Basic tests for all functions passing
brandyaustinseattle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,154 @@ | ||
| const scoreTable = { | ||
| '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 findPoints = function findPoints(letter) { | ||
| let lowercase_letter = letter.toLowerCase(); | ||
| let points = scoreTable[lowercase_letter]['points']; | ||
|
|
||
| return points; | ||
| } | ||
|
|
||
| const checkWord = function permissableWord(word) { | ||
| let pattern = /[^a-zA-Z]/; | ||
|
|
||
| if (word === '') { | ||
| throw "Word cannot be empty"; | ||
| } else if (pattern.test(word)) { | ||
| throw "Must be a real word"; | ||
| } else if (word.length > 7) { | ||
| throw "More than 7 letters not allowed"; | ||
| } | ||
| } | ||
|
|
||
| const checkArrayOfWords = function checkArrayofWords(arrayOfWords) { | ||
| if (arrayOfWords.length === 0) { | ||
| throw "List of words cannot be empty"; | ||
| } | ||
|
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. Great use of helper functions through here. |
||
| } | ||
|
|
||
| // const highestScoreTieBreaker = function highestScoreTieBreaker(maxWord, word) { | ||
| // if (maxWord.length != 7 && word.length == 7) { | ||
| // maxWord = word | ||
| // } | ||
| // | ||
| // if (maxWord.length != 7 && word.length != 7 && maxWord.length > word.length) { | ||
| // maxWord = word; | ||
| // } | ||
| // } | ||
|
|
||
| const Scrabble = { | ||
| score(word) { | ||
| checkWord(word); | ||
|
|
||
| let score = 0; | ||
| if (word.length === 7) { | ||
| score += 50; | ||
| } | ||
| for (let i = 0; i < word.length; i++) { | ||
| let points = findPoints(word[i]); | ||
| score += points; | ||
| } | ||
| return score; | ||
| }, | ||
|
|
||
| highestScoreFrom(arrayOfWords) { | ||
| checkArrayOfWords(arrayOfWords); | ||
|
|
||
| let maxWord = null; | ||
| let maxPoints = null; | ||
|
|
||
| arrayOfWords.forEach(function (word) { | ||
| let score = Scrabble.score(word); | ||
|
|
||
|
|
||
| if (maxPoints === score) { | ||
|
|
||
| // highestScoreTieBreaker(maxWord, word); | ||
|
|
||
| if (maxWord.length != 7 && word.length == 7) { | ||
| maxWord = word | ||
| } | ||
| if (maxWord.length != 7 && word.length != 7 && maxWord.length > word.length) { | ||
| maxWord = word; | ||
| } | ||
|
|
||
| } else if (maxPoints < score) { | ||
| maxWord = word; | ||
| maxPoints = score; | ||
| } | ||
|
|
||
| }); | ||
| return maxWord; | ||
| } | ||
| } | ||
|
|
||
| }, | ||
| }; | ||
|
|
||
| Scrabble.Player = class { | ||
| constructor(name, plays = []) { | ||
| if (name === undefined ) { | ||
| throw "Player must have a name" | ||
| } | ||
|
|
||
| this.name = name; | ||
| this.plays = plays; | ||
| } | ||
|
|
||
| play(word) { | ||
| checkWord(word); | ||
| return (this.hasWon() ? false : this.plays.push(word)); | ||
| } | ||
|
|
||
| totalScore() { | ||
| let wordsPlayed = this.plays; | ||
| let total = 0; | ||
| wordsPlayed.forEach(function (word) { | ||
| total += Scrabble.score(word); | ||
| }) | ||
| return total; | ||
| } | ||
|
|
||
| hasWon() { | ||
| let total = this.totalScore(); | ||
| return (total >= 100); | ||
| } | ||
|
|
||
| highestScoringWord() { | ||
| let plays = this.plays; | ||
| return Scrabble.highestScoreFrom(plays); | ||
| } | ||
|
|
||
| highestWordScore() { | ||
| let word = this.highestScoringWord(); | ||
| return Scrabble.score(word); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I like that you've consolidated the points and number of tiles into one data structure - clever thinking!