diff --git a/player.js b/player.js new file mode 100644 index 0000000..7c02f4e --- /dev/null +++ b/player.js @@ -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; diff --git a/scrabble.js b/scrabble.js index a7d0745..72392d1 100644 --- a/scrabble.js +++ b/scrabble.js @@ -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 + } +} + +// hashtag test-driven development. check out /spec folder to see testing on this module. +// export this file as a module module.exports = Scrabble; diff --git a/scrabble_constants.js b/scrabble_constants.js new file mode 100644 index 0000000..10a9fd2 --- /dev/null +++ b/scrabble_constants.js @@ -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 diff --git a/spec/scrabble_spec.js b/spec/scrabble_spec.js new file mode 100644 index 0000000..63c45e4 --- /dev/null +++ b/spec/scrabble_spec.js @@ -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"); + }); + + 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"); + }); + +}); diff --git a/spec/support/jasmine.json b/spec/support/jasmine.json new file mode 100644 index 0000000..3ea3166 --- /dev/null +++ b/spec/support/jasmine.json @@ -0,0 +1,11 @@ +{ + "spec_dir": "spec", + "spec_files": [ + "**/*[sS]pec.js" + ], + "helpers": [ + "helpers/**/*.js" + ], + "stopSpecOnExpectationFailure": false, + "random": false +}