forked from AdaGold/js-scrabble
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Kiera - jsscrabble - Octos #28
Open
Krashaune
wants to merge
9
commits into
Ada-C9:master
Choose a base branch
from
Krashaune: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
9 commits
Select commit
Hold shift + click to select a range
6bc7845
Created data structure for letter scores. Added to score method and o…
Krashaune b903a9d
Completed score function for wave 1. all tests are passing.
Krashaune 78905df
made progress on highestscore from method.
Krashaune ec19c38
completed highestScoreFrom function. all 7 tests are passing.
Krashaune 3be879e
created play function. passing 2 of 3 tests. making progress on total…
Krashaune ec259e5
completed totalScore function all tests are passing.
Krashaune 3cb4f18
completed hasWon function, filled in test specs and they're all passing.
Krashaune a9f8533
completed highestScoringWord function. created tests for HSW and they…
Krashaune db74c6d
Added highestwordscore function and two passing functionality tests.
Krashaune 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 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,14 +1,152 @@ | ||
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 REGEX = /[A-Z]/ | ||
|
||
const Scrabble = { | ||
score(word) { | ||
let wordUp = word.toUpperCase() | ||
let wordSplits = wordUp.split(''); | ||
|
||
let totalScore = 0; | ||
if (wordSplits.length > 7 || wordSplits.length === 0) { | ||
throw 'Invalid word'; | ||
} | ||
|
||
if (wordSplits.length === 7) { | ||
totalScore += 50; | ||
} | ||
|
||
wordSplits.forEach(function(letter) { | ||
if (REGEX.test(letter)) { | ||
totalScore += SCORECHART[letter]; | ||
} else { | ||
throw 'Not a valid word'; | ||
} | ||
|
||
}); | ||
|
||
return totalScore | ||
}, | ||
|
||
highestScoreFrom(arrayOfWords) { | ||
if (arrayOfWords.length === 0) { | ||
throw 'Unable to calculate highest score without any words.'; | ||
} | ||
if (Array.isArray(arrayOfWords) !== true) { | ||
throw 'Unable to calculate highest score without an array of words'; | ||
} | ||
if (arrayOfWords.length === 1){ | ||
return arrayOfWords[0]; | ||
} else { | ||
let word_score = {}; | ||
arrayOfWords.forEach(function(word) { | ||
word_score[word] = Scrabble.score(word); | ||
}); | ||
|
||
let highest_score = 0; | ||
let winWord = null; | ||
for(const word in word_score) { | ||
if (word_score[word] > highest_score) { | ||
highest_score = word_score[word]; | ||
winWord = word; | ||
} if (word_score[word] === highest_score && winWord.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. These conditionals are structured like you want them to be nested in the else ifs, but they're all independent conditions. Recommend switching to |
||
return winWord; | ||
} if (word_score[word] === highest_score && word.length === 7) { | ||
return word; | ||
} if (word_score[word] === highest_score && winWord.length < word.length) { | ||
return winWord; | ||
} if (word_score[word] === highest_score && winWord.length > word.length) { | ||
return word; | ||
} | ||
} //for loop | ||
return winWord; | ||
} | ||
}, | ||
}; | ||
|
||
Scrabble.Player = class { | ||
constructor(name) { | ||
this.name = name; | ||
this.plays = []; | ||
if (name === undefined) { | ||
throw 'Player must have a name'; | ||
} | ||
} | ||
|
||
play(word) { | ||
if (this.hasWon()){ | ||
return false; | ||
} else { | ||
if (word === undefined) { | ||
throw 'Must play a word'; | ||
} | ||
if (REGEX.test(word.toUpperCase)!== true){ | ||
throw 'Must play a real word'; | ||
} | ||
this.plays.push(word); | ||
} | ||
return this.plays; | ||
|
||
} | ||
|
||
totalScore() { | ||
let points = 0; | ||
this.plays.forEach(function(word) { | ||
let wScore = Scrabble.score(word); | ||
points += wScore; | ||
}); | ||
return points; | ||
} | ||
|
||
hasWon(){ | ||
if (this.totalScore() >= 100) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
highestScoringWord() { | ||
if (this.plays.length === 0) { | ||
throw 'Cannot calculate highest scoring word without a played word'; | ||
} | ||
return Scrabble.highestScoreFrom(this.plays); | ||
} | ||
|
||
highestWordScore(){ | ||
if (this.plays.length === 0) { | ||
throw 'Cannot calculate highest word score without a played word'; | ||
} | ||
return Scrabble.score(this.highestScoringWord()); | ||
|
||
} | ||
|
||
}; | ||
|
||
|
This file contains 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 the way you're storing the scores in an object so you don't need to recalculate the scores each time