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

Nina Mutty - Scrabble #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
71 changes: 71 additions & 0 deletions player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var Scrabble = require('./scrabble');


var Player = function(name) {
this.name = name;
this.plays = [];
};


// Put all of this in prototype
Player.prototype = {
play: function(add) {
return this.plays.push(add);

Choose a reason for hiding this comment

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

What about if the player has already won?

Check requirements.

},

totalScore: function() {
total = 0;
scrabble = new Scrabble();
for(var i=0; i < this.plays.length; i++) {

Choose a reason for hiding this comment

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

Shouldn't there be some vars in these variable declarations?

total += scrabble.score(this.plays[i]);
};
return total;
},

hasOne: function() {
if (this.totalScore(this.plays) >= 100) {
return true;
} else {
return false;
};
},

highestScoringWord: function() {
scrabble = new Scrabble();
return scrabble.highestScoreFrom(this.plays);
},

highestWordScore: function() {
scrabble = new Scrabble();
return scrabble.score(scrabble.highestScoreFrom(this.plays));
}

Choose a reason for hiding this comment

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

Good use of existing functions.

};

// Player.prototype.plays = function() {
// return plays
// }



console.log("####################################")
console.log("########## PLAYER TESTS ##########");
console.log("####################################")
nina = new Player("Nina");
console.log(nina.name);
console.log(nina.plays);

nina.play("cat");
nina.play("dog");
nina.play("fish");
console.log(nina.plays);
console.log("Total Score: " + nina.totalScore());
console.log("Has One: " + nina.hasOne());
nina.play("ZZZQZZZ");
console.log("Total Score: " + nina.totalScore());
console.log("Has One: " + nina.hasOne());
console.log("Highest Scoring Word: " + nina.highestScoringWord());
console.log("Highest Word Score: " + nina.highestWordScore());



module.exports = Player;
77 changes: 73 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,77 @@
var Scrabble = function() {};
var Scrabble = function() {
this.POINT_LIST = [
["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
["D", "G"],
["B", "C", "M", "P"],
["F", "H", "V", "W", "Y"],
["K"],
["J", "X"],
["Q", "Z"]
]
};

Scrabble.prototype = {
score: function(word) {
var word = word.toUpperCase();
var totalScore = 0;
if (word.length == 7) { totalScore += 50; }
for (var i=0; i < word.length; i++) {
if (this.POINT_LIST[0].includes(word[i])) {
totalScore += 1;
} else if (this.POINT_LIST[1].includes(word[i])) {
totalScore += 2;
} else if (this.POINT_LIST[2].includes(word[i])) {
totalScore += 3;
} else if (this.POINT_LIST[3].includes(word[i])) {
totalScore += 4;
} else if (this.POINT_LIST[4].includes(word[i])) {
totalScore += 5;
} else if (this.POINT_LIST[5].includes(word[i])) {
totalScore += 8;
} else if (this.POINT_LIST[6].includes(word[i])) {
totalScore += 10;
};

Choose a reason for hiding this comment

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

Interesting, so this just skips anything that's not a letter.

};
return totalScore
},

// YOUR CODE HERE
Scrabble.prototype.helloWorld = function() {
return 'hello world!';
highestScoreFrom: function(arrayOfWords) {
var highestScore = 0;
var word = "";
for (var i = 0; i < arrayOfWords.length; i++) {
if (this.score(arrayOfWords[i]) > highestScore) {
highestScore = this.score(arrayOfWords[i]);
word = arrayOfWords[i];
} else if (this.score(arrayOfWords[i]) == highestScore) {
if (arrayOfWords[i].length == 7 || arrayOfWords[i].length < word.length) {
if (word.length != 7 ) {
word = arrayOfWords[i];
}
};
};
};
if (word === "") {
word = "No High Score";
};
return word;
}
};


module.exports = Scrabble;



console.log("####################################")
console.log("########## SCRABBLE TESTS ##########");
console.log("####################################")

var s = new Scrabble();
console.log(s.score("ZZZ"));
console.log(s.highestScoreFrom(["ZZZ", "QQQ"])); // 'ZZZ'
console.log(s.highestScoreFrom(["cat", "aaaaa"])); // 'cat'
console.log(s.highestScoreFrom(["cat", "rat"])); // 'cat'
console.log(s.highestScoreFrom(["aaaaad", "aaaaaaa"])); // 'aaaaaaa'
console.log(s.score(""));
console.log("Cat: " + s.score("cat"));
console.log(s.highestScoreFrom(["", ""]));

Choose a reason for hiding this comment

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

Good work testing things.

30 changes: 30 additions & 0 deletions tilebag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var Scrabble = require('./scrabble');


var Tilebag = function() {
this.BEGIN_TILES = ["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"];

this.tiles = this.BEGIN_TILES;
};


Tilebag.prototype = {

drawTiles: function(num = 1) {
pulledTiles = [];
for (var i=0; i < num; i++) {
randomNumber = Math.floor(Math.random() * this.tiles.length);
pulledTiles.push(this.tiles.splice(randomNumber, 1)[0]);
}

Choose a reason for hiding this comment

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

Good use of push and slice to take letters out of the tilebag and put them into pulledTiles.

return pulledTiles;
}
};

var a = new Tilebag();
// console.log(shuffle(a.tiles));

console.log("First: " + a.drawTiles(20));
console.log("Tiles: " + a.tiles);
console.log("Second: " + a.drawTiles(20));

module.exports = Tilebag;