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

Lauren Brink #31

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5d67140
new module file with constants
laurenfb Nov 16, 2016
3c8dbb9
finished score function
laurenfb Nov 16, 2016
355305d
added highestScoreFrom method, though it is still pretty minimal
laurenfb Nov 16, 2016
af35fa0
highestScoreFrom method now meets all specifications
laurenfb Nov 16, 2016
fc4e03b
added a comment to explain myself better
laurenfb Nov 16, 2016
44077ce
Merge pull request #1 from laurenfb/wave-1
laurenfb Nov 16, 2016
58fb010
new file for player module yay
laurenfb Nov 16, 2016
1f52609
instantiating players with name and plays
laurenfb Nov 16, 2016
6c6386f
comment out manual testing in scrabble.js
laurenfb Nov 16, 2016
135529e
change protype functions to non-prototype functions after realizing t…
laurenfb Nov 17, 2016
b447cbb
play method for player
laurenfb Nov 17, 2016
ea8acb6
totalScore fx
laurenfb Nov 17, 2016
c90590f
totalScore fx
laurenfb Nov 17, 2016
06ecf50
highestWordScore fx & commenting out manual test stuff
laurenfb Nov 17, 2016
2ba7804
Merge pull request #2 from laurenfb/wave-2
laurenfb Nov 17, 2016
6c0376d
implement jasmine
laurenfb Nov 17, 2016
f7fa049
refactor if/else to ensure correct results if words are different len…
laurenfb Nov 17, 2016
4e90a63
tests for scrabble.helloworld, .highestscorefrom, .score
laurenfb Nov 17, 2016
44edf7d
Merge pull request #3 from laurenfb/optional-reqs
laurenfb Nov 17, 2016
1d0a53f
error handling for non-string arguments in .score
laurenfb Nov 17, 2016
0312420
add error handling in scrabble
laurenfb Nov 18, 2016
6904fe7
tests for error handling
laurenfb Nov 18, 2016
2ded72e
Merge pull request #4 from laurenfb/wave-1
laurenfb Nov 18, 2016
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
74 changes: 74 additions & 0 deletions player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var CONSTANTS = require('./scrabble_constants');
var Scrabble = require('./scrabble')

// constructor!
var Player = function(name) {
this._name = name;
this._plays = [];
};

Player.prototype = {
play: function(word){
if (this.hasWon()) {
return false;
} else {
this._plays.push(word);
};
return this._plays // currently returning the list of all plays
}, //end of play fx

totalScore: function(){
var total = 0
this._plays.forEach(function(play){
total += Scrabble.score(play);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI the forEach function is depreciated.

});
return total;
}, //end of totalScore fx

hasWon: function(){
if (this.totalScore() >= 100) {
return true;
} else {
return false;
}
}, //end of hasWon fx

highestScoringWord: function(){
return Scrabble.highestScoreFrom(this._plays);
}, //end of highestscoringword fx

highestWordScore: function(){
return Scrabble.score(this.highestScoringWord());
} //end of highestWordScore
}

// testing that we can instantiate a new Player
// var player = new Player("Edith");
// // test attributes
// // console.log("Player is: " + player._name);
// // console.log(player._name + "'s plays: " + player._plays);
// // console.log(player._name + "'s score: " + player._score);
// // console.log("Player won? " + player._won)
// // test whether we can play stuff
// player.play("cat")
// console.log(player._name + "'s plays:" + player._plays);
// player.play("bloop");
// console.log(player._name + "'s plays:" + player._plays);
// // test totalScore fx
// console.log("Total score: " + player.totalScore())
// // test hasWon fx
// console.log(player.hasWon() + ", player has not won.")
// player.play("xxxx")
// console.log("Total score: " + player.totalScore())
// player.play("xxxsssx")
// console.log(player._name + "'s plays:" + player._plays);
// console.log("Total score: " + player.totalScore())
// console.log(player.hasWon() + ", player has won!")
// // test highest scoring word fx
// console.log("highest score word is " + player.highestScoringWord())
// // test highest word score fx
// console.log("score of that word is " + player.highestWordScore())


// export this file as a module
module.exports = Player;
78 changes: 76 additions & 2 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,82 @@
var CONSTANTS = require('./scrabble_constants');

// constructor!
var Scrabble = function() {};

// YOUR CODE HERE
Scrabble.prototype.helloWorld = function() {
// this is a thing to copy from
Scrabble.helloWorld = function() {
return 'hello world!';
};

// add score method to Scrabble
Scrabble.score = function(word){

// if the word is a string and it does not contain any non-letter characters, then score it. else, return false.
if (typeof(word) == 'string' && Scrabble.checkInput(word)) {
word = word.toUpperCase();
var total = 0;
for (let char of word) {
total += CONSTANTS.SCORES[char]
};
if (word.length >= 7) {
total += 50
};
return total;
} else {
return false;
};

}

Scrabble.highestScoreFrom = function(arrayOfWords) {
var highestWord = null;
var highestWordScore = null;
var scoreArray = [];

// iterate through words, score them all
for (let word of arrayOfWords) {
// if the words aren't strings, fuhgeddaboutit
if (typeof(word) == 'string') {
scoreArray.push(Scrabble.score(word));
} else {
// turns out we need this so that scoreArray.length == arrayOfWords.length
scoreArray.push(false);
};
};

// iterate through scores, find best based on conditional
for (var i = 0; i < scoreArray.length; i++) {
if (scoreArray[i] > highestWordScore) {
highestWordScore = scoreArray[i];
highestWord = arrayOfWords[i];
}
// if the scores are tied, check to see if one of the words is longer than 7 letters. if neither is, continue on as if nothing happened (this will effectively choose the first tied score to be the winner) //
else if (scoreArray[i] == highestWordScore) {
if (highestWord.length > arrayOfWords[i].length) {
highestWord = arrayOfWords[i];
} else if (highestWord.length < 7 && arrayOfWords[i].length >= 7) {
highestWordScore = scoreArray[i]
highestWord = arrayOfWords[i];
};
};

};
if (highestWord) {
return highestWord;
} else {
return "There is no highest scoring word."
};

};

Scrabble.checkInput = function(word) {
if (word == word.match(/^[a-zA-Z]+$/)) {
return word
} else {
return false
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of a regular expression to check to see if it's a valid scrabble word.


// hashtag test-driven development. check out /spec folder to see testing on this module.
// export this file as a module
module.exports = Scrabble;
37 changes: 37 additions & 0 deletions scrabble_constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

var SCRABBLE_CONSTANTS = {
SCORES: {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},
LETTERS: [["A", "A", "A", "A", "A", "A", "A", "A", "A"],
["B", "B"],
["C", "C"],
["D", "D", "D", "D"],
["E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E"],
["F", "F"],
["G", "G", "G"],
["H", "H"],
["I", "I", "I", "I", "I", "I", "I", "I", "I"],
["J"],
["K"],
["L", "L", "L", "L"],
["M", "M"],
["N", "N", "N", "N", "N", "N"],
["O", "O", "O", "O", "O", "O", "O", "O"],
["P", "P"],
["Q"],
["R", "R", "R", "R", "R", "R"],
["S", "S", "S", "S"],
["T", "T", "T", "T", "T", "T"],
["U", "U", "U", "U"],
["V", "V"],
["W", "W"],
["X"],
["Y", "Y"],
["Z"]],
MAX_TILES: 7
}

module.exports = SCRABBLE_CONSTANTS
65 changes: 65 additions & 0 deletions spec/scrabble_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var Scrabble = require('../scrabble');
var Player = require('../player');

describe('testing Scrabble module', function() {


////////////// easy test :) //////////////////////
it('Scrabble can say hello', function() {
expect(Scrabble.helloWorld()).toBe("hello world!");
});

/////////////////// score //////////////////////////
it('scrabble can score a word correctly', function() {
expect(Scrabble.score("word")).toBe(8);
});

it('scrabble can score a bingo correctly', function() {
expect(Scrabble.score("uuuuuuu")).toBe(57);
});

it('scrabble returns false when input is not a string', function() {
expect(Scrabble.score(23948)).toBe(false);
expect(Scrabble.score([3,5, "cat"])).toBe(false);
});

it('if a word does not have only letters, scrabble returns false', function() {
expect(Scrabble.score("23948cat")).toBe(false);
expect(Scrabble.score("cat0%")).toBe(false);
});

/////////// highestScoreFrom working correctly ////////////
it('scrabble can score a list of words, none of which are bingos, correctly', function() {
expect(Scrabble.highestScoreFrom(["uuuuuu", "you", "cat", "syzygy" ])).toBe("syzygy");
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, in terms of Scrabble what's a bingo?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bingo is a word that uses all 7 letters or more! Highly recommend this book. https://www.amazon.com/Word-Freak-Heartbreak-Obsession-Competitive/dp/0142002267

it('scrabble can score a list of words, one of which is a bingo, correctly', function() {
expect(Scrabble.highestScoreFrom(["uuuuuua", "you", "cat", "syzygy" ])).toBe("uuuuuua");
});

it('scrabble can score a list of words that are tied for score and length & choose the first one as the winner', function() {
expect(Scrabble.highestScoreFrom(["uuuuuu", "iiiiii", "aaaaaa" ])).toBe("uuuuuu");
});

it('scrabble can score a list of words that are tied for score, except one is a bingo, choose the bingo rather than the one that is shorter', function() {
expect(Scrabble.highestScoreFrom(["zzzzzz", "iiiiiif" ])).toBe("iiiiiif");
});

it('list of words tied for score but not length is scored correctly, where the shortest length one is returned if nothing is a bingo', function() {
expect(Scrabble.highestScoreFrom(["aaa", "c", "id" ])).toBe("c");
});

//////////// highestScoreFrom error testing /////////
it('when passed an empty array, highestScoreFrom should let you know', function() {
expect(Scrabble.highestScoreFrom([])).toBe("There is no highest scoring word.");
});

it('when passed an array of garbage, highestScoreFrom should let you know', function() {
expect(Scrabble.highestScoreFrom(["dsfk24234", "$%#;", 230585])).toBe("There is no highest scoring word.");
});

it('when passed an array where one word is valid and the rest are garbage, it should score that word & return it as the highest scoring', function() {
expect(Scrabble.highestScoreFrom(["dsfk24234", "$%#;", 230585, "cat"])).toBe("cat");
});

});
11 changes: 11 additions & 0 deletions spec/support/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"spec_dir": "spec",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}