-
Notifications
You must be signed in to change notification settings - Fork 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
Lauren Brink #31
base: master
Are you sure you want to change the base?
Lauren Brink #31
Changes from all commits
5d67140
3c8dbb9
355305d
af35fa0
fc4e03b
44077ce
58fb010
1f52609
6c6386f
135529e
b447cbb
ea8acb6
c90590f
06ecf50
2ba7804
6c0376d
f7fa049
4e90a63
44edf7d
1d0a53f
0312420
6904fe7
2ded72e
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 |
---|---|---|
@@ -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); | ||
}); | ||
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; |
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 | ||
} | ||
} | ||
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. 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; |
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 |
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"); | ||
}); | ||
|
||
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. Just curious, in terms of Scrabble what's a 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. 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"); | ||
}); | ||
|
||
}); |
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 | ||
} |
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.
Just FYI the forEach function is depreciated.