Skip to content
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
65 changes: 65 additions & 0 deletions player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// var Scrabble = require('./scrabble');
var Player = function(name, game) {
this.name = name ;
this.game = game
this.plays = [];
};

Player.prototype = {
play: function(word) {
this.plays.push(word)
//need to return false if already won
},

totalScore: function() {
var self = this
var score = 0
// Function which sums up and returns the score of the players words
this.plays.forEach(function(word) {
score += self.game.scoreWord(word)
})
console.log("Your total score is: " + score)
return score
},

hasWon: function() {
var self = this
var score = self.totalScore()
if (score >= 100) {
console.log("You won with a score of: " + score)
return score
} else {
console.log("You haven't won yet. Your current score is: " + score)
return score
}
},

highestScoringWord: function() {
var self = this
var highest_score_word = self.game.highestScoreFrom(this.plays)
console.log("the highest scoring word is: " + highest_score_word)
return highest_score_word
},

highestScoringWordScore: function () {
var self = this
var highest_scoring_word = self.highestScoringWord()
var highest_word_score = self.game.scoreWord(highest_scoring_word)
console.log("The score of the highest scoring word is: " + highest_word_score)
return highest_word_score
}

};

var Scrabble = require('./scrabble') // do this here to pass in an instance of scrabble every time a player is created
var gameOne = new Player("fido", new Scrabble())
gameOne.play("yep");
gameOne.play("jazzily");
gameOne.totalScore();
gameOne.highestScoringWord();
gameOne.highestScoringWordScore();
gameOne.hasWon();
console.log(gameOne)

module.exports = Player;
//telling it to send the player to the scrabble (whereever it's required)
89 changes: 85 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,89 @@
var Scrabble = function() {};
var Scrabble = function() {
tile_scores = {
"a": 1,
"e": 1,
"i": 1,
"o": 1,
"u": 1,
"l": 1,
"n": 1,
"r": 1,
"s": 1,
"t": 1,
"d": 2,
"g": 2,
"b": 3,
"c": 3,
"m": 3,
"p": 3,
"f": 4,
"h": 4,
"v": 4,
"w": 4,
"y": 4,
"k": 5,
"j": 8,
"x": 8,
"q": 10,
"z": 10,
}
};

// could also do Scrabble.prototype.scoreWord = function(word) {
Scrabble.prototype = {
scoreWord: function(word) {
var score = 0,
word_length = word.length

for(var i = 0; i < word_length; i++)
score += tile_scores[word.charAt(i)];

if (word_length == 7) {
score += 50;
}
// console.log(score)
return score
},

highestScoreFrom: function(arrayOfWords) {
var highest_scored_word = 0
var highest_scored_word_length = highest_scored_word.length
var self = this
arrayOfWords.forEach(function(word) {
if (self.scoreWord(word) == self.scoreWord(highest_scored_word)) {
var current_word_length = word.length
if (highest_scored_word_length == current_word_length) {
highest_scored_word = highest_scored_word
} else if (highest_scored_word_length > current_word_length) {
highest_scored_word = word
} else if (highest_scored_word_length == 7) {
highest_scored_word = highest_scored_word
}
}
else if (self.scoreWord(word) >= self.scoreWord(highest_scored_word)) {
highest_scored_word = word;
}
})
console.log(highest_scored_word)
return highest_scored_word
}

// YOUR CODE HERE
Scrabble.prototype.helloWorld = function() {
return 'hello world!';
};

//second option

// var scoreHash = {}
// // var self = this
// arrayOfWords.forEach(function(word) {
// scoreHash[word] = Scrabble.prototype.scoreWord(word)
// })
// // console.log(scoreHash)
// return


var gameOne = new Scrabble
gameOne.scoreWord("yes")
gameOne.highestScoreFrom(["yes", "no", "qqqqqqq", "zzzzzzz" ])
// console.log(gameOne)

module.exports = Scrabble;