-
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
Sarah #22
base: master
Are you sure you want to change the base?
Sarah #22
Changes from all commits
e150f03
fc4c067
5fbe7bc
8aec940
35caafe
e548db1
cc4de5e
44fd127
085d2fa
f90afbf
9dec8f9
8026f40
d436cd3
ebca151
a86aa62
09f86ca
481a8f3
c79cec2
12a0929
b19a055
8efef76
53df93d
f8f1aa6
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 |
---|---|---|
@@ -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<numOfTiles; i++) { | ||
// tiles are formatted like: [number, original index] | ||
var tile = sample(this.defaultTiles); | ||
// get random tiles from the bag, only push the letter to the player's tiles | ||
this.randomTiles.push(tile[0]); | ||
// remove those tiles from the bag so they cannot be drawn again | ||
// the index 1 of tile is the random index that cause it to be drawn... | ||
// this is the index in defaultTiles | ||
this.defaultTiles.splice(tile[1],1); | ||
} | ||
return this.randomTiles; | ||
|
||
}; | ||
|
||
// helper function to wrap ugly random | ||
function sample(array) { | ||
var index = Math.floor ( Math.random() * array.length ); | ||
return [array[index], index]; | ||
} | ||
|
||
Scrabble.prototype.score = function(word) { | ||
this.word = word.toLowerCase(); // lowercase input for comparing | ||
|
||
if (word === "") { | ||
return 0; | ||
} else { | ||
var score = 0; | ||
for(var i=0; i<this.word.length; i++) { | ||
score += this.letterScore[this.word[i]]; | ||
} | ||
|
||
if (this.word.length >= this.maxLength) { | ||
score += this.bonus; | ||
} | ||
|
||
return score; | ||
} | ||
}; | ||
|
||
Scrabble.prototype.highestScore = function(wordArr) { | ||
var highScore = 0; | ||
var highScoreWord = ""; | ||
|
||
for(var i=0; i<wordArr.length; i++) { | ||
var word = wordArr[i]; | ||
var score = this.score(word); | ||
|
||
if (score > 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) { | ||
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. It would make more sense to add this to the |
||
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; | ||
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 the inline conditional |
||
}; | ||
|
||
Player.prototype.removeTiles = function(word) { | ||
this.word = word; | ||
for(var i=0; i<this.word.length; i++) { | ||
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. Watch your spacing here, it is more pleasing to add spaces between the |
||
var letter = this.word[i]; | ||
// remove those tiles from the player's tiles so they cannot be played again | ||
// I think we will have to loop through all the player's tiles to find | ||
// the one that matches a partiular letter. Most is 7. | ||
// This is less than ideal, but because things keep getting reshuffled and | ||
// we don't have any information from the letter about where it is in the arr. | ||
// this is an okay solution for now :( | ||
this.tiles.splice(this.tiles.indexOf(letter),1); | ||
} | ||
return this.tiles; | ||
}; | ||
|
||
Player.prototype.drawTiles = function() { | ||
var tilesDrawn = this.scrabble.tileBag.drawTiles(this.scrabble.maxLength - this.tiles.length); | ||
for(var i=0; i<tilesDrawn.length; i++) { | ||
this.tiles.push(tilesDrawn[i]); | ||
} | ||
return this.tiles; | ||
}; | ||
|
||
Player.prototype.totalScore = function() { | ||
if (this.plays == []){ | ||
return 0; | ||
} | ||
var total = 0; | ||
for(var i=0; i<this.plays.length; i++) { | ||
total += this.scrabble.score(this.plays[i]); | ||
} | ||
return total; | ||
}; | ||
|
||
Player.prototype.highestScoringWord = function() { | ||
return this.scrabble.highestScore(this.plays); | ||
}; | ||
|
||
Player.prototype.highestWordScore = function() { | ||
return this.scrabble.score(this.highestScoringWord()); | ||
}; | ||
|
||
module.exports = Scrabble; |
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.
I might argue that the greater than case would be an error rather than the bonus