diff --git a/scrabble.js b/scrabble.js index a7d0745..d6e4d80 100644 --- a/scrabble.js +++ b/scrabble.js @@ -1,8 +1,157 @@ -var Scrabble = function() {}; +var Scrabble = function() { + // create the hashmap + this.letterScore = { "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 }; -// YOUR CODE HERE -Scrabble.prototype.helloWorld = function() { - return 'hello world!'; + this.maxLength = 7; + this.bonus = 50; + this.tileBag = new TileBag(); +}; + +var TileBag = function() { + this.defaultTiles = ["a", "a", "a", "a", "a", "a", "a", "a", + "a", "n", "n", "n", "n", "n", "n", "b", "b", "o", "o", "o", "o", "o", + "o", "o", "o", "c", "c", "p", "p", "d", "d", "d", "d", "q", "e", "e", + "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "r", "r", "r", "r", + "r", "r", "f", "f", "s", "s", "s", "s", "g", "g", "g", "t", "t", "t", + "t", "t", "t", "h", "h", "u", "u", "u", "u", "i", "i", "i", "i", "i", + "i", "i", "i", "i", "v", "v", "j", "w", "w", "k", "x", "l", "l", "l", + "l", "y", "y", "m", "m", "z"]; +}; + +TileBag.prototype.drawTiles = function(numOfTiles) { + this.randomTiles = []; + for(var i=0; i= this.maxLength) { + score += this.bonus; + } + + return score; + } +}; + +Scrabble.prototype.highestScore = function(wordArr) { + var highScore = 0; + var highScoreWord = ""; + + for(var i=0; i highScore) { + highScore = score; + highScoreWord = word; + } else if (score == highScore) { +// if the top score is tied between multiple words, +// pick the one with the fewest letters. + if (word.length < highScoreWord.length) { + highScoreWord = word; + } + } else {} + } + + return highScoreWord; +}; + +// pass two arguments, name & game3 so that we can have multiplayer games +var Player = function(name, game = (new Scrabble())) { + this.name = name; + this.plays = []; + // Each player will have their own Scrabble + this.scrabble = game; + // a new player has the maximum number of tiles available to them + this.tiles = this.scrabble.tileBag.drawTiles(this.scrabble.maxLength); +}; + +Player.prototype.play = function(word) { + if(this.hasWon()) { + return false; + } + this.word = word; + this.plays.push(this.word); + + this.removeTiles(this.word); + this.drawTiles(); + + return this.scrabble.score(word); +}; + +Player.prototype.hasWon = function() { + return this.totalScore() > 100; +}; + +Player.prototype.removeTiles = function(word) { + this.word = word; + for(var i=0; i