Skip to content
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

Amy Cash - Pipes - JS Scrabble #38

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fe473b5
Run NPM Install
cashmonger Nov 14, 2017
dc32db5
Write scoring function
cashmonger Nov 14, 2017
91962ce
Get basic scoring working
cashmonger Nov 14, 2017
25e303f
Finish scoring
cashmonger Nov 14, 2017
13a3b4b
Add exceptions for long/short words
cashmonger Nov 15, 2017
431a1ae
Add exceptions to scoring method
cashmonger Nov 15, 2017
c684303
Tiny change
cashmonger Nov 15, 2017
d688e40
Add highest score from 1 word array
cashmonger Nov 15, 2017
fdd663c
Empty array, nonArray and 1 item array work
cashmonger Nov 15, 2017
386c7cd
Find highest score in an array with two or more words
cashmonger Nov 15, 2017
680f5cb
Try lots of array and object methods that don't work
cashmonger Nov 15, 2017
1f83eaa
Make high score array and choose 7 letter word
cashmonger Nov 15, 2017
0fff6e0
Almost fix tie breaker
cashmonger Nov 15, 2017
4ad2170
Debugging word length selection
cashmonger Nov 15, 2017
6c929ab
Pass all tests for wave 1
cashmonger Nov 15, 2017
64afd58
Remove lots of console logs
cashmonger Nov 15, 2017
1960214
finish wave one
cashmonger Nov 15, 2017
a88890c
Update to throw error if no name
cashmonger Nov 15, 2017
9204291
Tiny change
cashmonger Nov 15, 2017
a773b3c
fix most of the logic for highest scoring word
cashmonger Nov 15, 2017
0323c05
Fix Highest Score Logic good enough for now
cashmonger Nov 15, 2017
6efddea
Add play method
cashmonger Nov 16, 2017
0bfa094
fix truthy response in play function and add haswon function
cashmonger Nov 16, 2017
1fc0de8
Work on Total Score and Has Won
cashmonger Nov 16, 2017
6433654
Debugging haswon
cashmonger Nov 16, 2017
b05627e
Functions are working, but won't pass all tests
cashmonger Nov 16, 2017
bd4586d
Finally passing all tests
cashmonger Nov 17, 2017
f8e5568
final
cashmonger Nov 17, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 124 additions & 5 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,133 @@
function UserException(message) {
this.message = message;
this.name = 'UserException'
};

const letterPoints = {
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 Scrabble = {
score: function(word) {
// TODO: implement score
}

// TODO: add the highestScoreFrom method
if(word.length > 7) {
throw new UserException("Word is Too Long");
} else if (word.length === 0) {
throw new UserException("You must enter a word to be scored");
} else if (word.match(/[^a-z]+/i)) {
throw new UserException("Word can only contain letters");
}

let total = 0;

word = word.toUpperCase();

for(let i = 0; i < word.length; i++){
total += letterPoints[word[i]];
}

if(word.length === 7){
total += 50;
}

return total;

},

tieBreak: function(a, b) {
if(a.length === 7){
return a;
} else if(b.length === 7){
return b;
} else if(a.length > b.length) {
return b;
} else {
return a;
}
},

highestScoreFrom: function(words) {

if(words.length === 0) {
throw new UserException("Array is Empty");
} else if (typeof words != 'object') {
throw new UserException("You must enter an array");
}

if(words.length === 1) {
return words[0];
} else if(words.length >= 2) {

let highestScoringWord = "";
let highestScore = 0;

words.forEach(function (word) {

if(Scrabble.score(word) > highestScore){
highestScoringWord = word;
highestScore = Scrabble.score(word);
}
else if (Scrabble.score(word) === highestScore) {
highestScoringWord = Scrabble.tieBreak(highestScoringWord, word);
}
});
return highestScoringWord;
}
}
};

Scrabble.Player = class {
// TODO: implement the Player class

Scrabble.Player = class {
constructor(name) {
if(name === undefined){
throw new UserException("Must Include a Name");
}
this.name = name;
this.plays = [];
}

totalScore() {
let playerScore = 0;
this.plays.forEach(function (word) {
playerScore += Scrabble.score(word);
});
return playerScore;
}

hasWon() {
let score = this.totalScore();
if(score >= 100) {
return true;
} else {
return false;
}
}

play(word) {
if(word.length > 7) {
throw new UserException("Word is Too Long");
} else if (word.length === 0) {
throw new UserException("You must enter a word");
} else if (word.match(/[^a-z]+/i)) {
throw new UserException("Word can only contain letters");
}

if( (this.plays.length > 0) && (this.hasWon() === true)) {
return false;
} else {
this.plays.push(word);
return true;
}
};

highestScoringWord() {
return Scrabble.highestScoreFrom(this.plays);
}

highestWordScore() {
return Scrabble.score(Scrabble.highestScoreFrom(this.plays));
}

};

module.exports = Scrabble;
4 changes: 2 additions & 2 deletions spec/scrabble_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('Player', function() {
expect(function() { player.play(44); }).toThrow();
expect(player.plays.length).toBe(0);
});

//1
it ('Returns false and does not update plays if the player has already won', function() {
let player = new Scrabble.Player('test player');
expect(player.play('zzzzzzz')).toBeTruthy(); // +120 pts
Expand All @@ -153,7 +153,7 @@ describe('Player', function() {
let player = new Scrabble.Player('test player');
expect(player.totalScore()).toBe(0);
});

//2
it ('Is updated by play', function() {
let player = new Scrabble.Player('test player');
expect(player.totalScore()).toBe(0);
Expand Down