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

Sarah #22

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e150f03
Makes score and highes score functions
nahmisa Nov 16, 2016
fc4c067
Makes score and highest score functions
nahmisa Nov 16, 2016
5fbe7bc
Adds a player object that has a name
nahmisa Nov 16, 2016
8aec940
Adds plays to the player object
nahmisa Nov 16, 2016
35caafe
Merges
nahmisa Nov 16, 2016
e548db1
Adds play method that returns score and adds word to plays
nahmisa Nov 16, 2016
cc4de5e
Puts new Scrabble in the 'initializer' so we only make it once per pl…
nahmisa Nov 16, 2016
44fd127
Adds method to return the total score of all words player has played
nahmisa Nov 16, 2016
085d2fa
Returns the highest scoring word the player has played
nahmisa Nov 16, 2016
f90afbf
Returns score of highest scoring word
nahmisa Nov 16, 2016
9dec8f9
Adds method to decide if player has won by achieving score greater th…
nahmisa Nov 16, 2016
8026f40
Checks that players has (not) won before letting them play a word
nahmisa Nov 16, 2016
d436cd3
Fixes for handling 0 case and actually calling the method
nahmisa Nov 16, 2016
ebca151
Terminal tests
nahmisa Nov 16, 2016
a86aa62
Comments out terminal tests
nahmisa Nov 16, 2016
09f86ca
Changes scrabble to an argument passed to player so we can have multi…
nahmisa Nov 16, 2016
481a8f3
Moves variables inside of Scrabble scope
nahmisa Nov 16, 2016
c79cec2
Makes a tileBag with default tiles for every new scrabble game
nahmisa Nov 16, 2016
12a0929
Bones of tilebag...
nahmisa Nov 16, 2016
b19a055
Refactors index out of player visible tiles
nahmisa Nov 17, 2016
8efef76
Fixes tiles so they actually go into the loop and remove played tiles
nahmisa Nov 17, 2016
53df93d
Refactors because true is true and false is false already
nahmisa Nov 17, 2016
f8f1aa6
Moves a method up so the file is written in the order they are called…
nahmisa Nov 17, 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
157 changes: 153 additions & 4 deletions scrabble.js
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) {

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

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) {

Choose a reason for hiding this comment

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

It would make more sense to add this to the else if above with an && rather than having another if inside

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;

Choose a reason for hiding this comment

The 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++) {

Choose a reason for hiding this comment

The 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 = and the < for the for loop

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;