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

Miriam Cortes (parens) #42

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
120 changes: 116 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,120 @@
var Scrabble = function() {};
var Scrabble = function() {
parseWord = function(word) {
this.word = word
this.wordAsArray = this.word.toUpperCase().split('');
return this.wordAsArray;
},

// YOUR CODE HERE
Scrabble.prototype.helloWorld = function() {
return 'hello world!';
letterPoints = function(letter) {
// this.wordScore = 0;
if ( ["A","E","I","O","U","L","N","R","S","T"].indexOf(letter) != -1 ) {
return 1;
} else if ( ["D","G"].indexOf( letter ) != -1 ) {
return 2;
} else if ( ["B","C","M","P"].indexOf( letter ) != -1 ) {
return 3;
} else if ( ["F","H","V","W","Y"].indexOf( letter ) != -1 ) {
return 4;
} else if ( ["K"].indexOf( letter ) != -1 ) {
return 5;
} else if ( ["J","X"].indexOf( letter ) != -1 ) {
return 8;
} else if ( ["Q","Z"].indexOf( letter ) != -1 ) {
return 10;
} else {
return console.log("idk! that's not a letter...wtf");
}
}
};

var ValidWordLength = function(word) {
if (word == "") {
console.log( "That's an empty string, dummy." );
return false;
} else if (word.length > 7) {
console.log( word + " is too long for this game." );
return false;
} else {
return true;
};
};

Scrabble.prototype.score = function(word) {
this.word = word;
this.points = 0
if ( ValidWordLength(this.word) ) {
parseWord(this.word).forEach(function(letter) {
this.points += letterPoints(letter);
});
}

//50 bonus points for using all 7 letters
if ( this.word.length == 7 ) {
this.points += 50;
}
return this.points;
};

Scrabble.prototype.highestScoreFrom = function( array ) {
var max = 0
var maxWord = ""
array.forEach ( function (word) {
if ( score(word) > max ) {
max = score(word)
maxWord = word
} else if ( score(word) == max ) {
if ( word.length < maxWord.length ) {
maxWord = word;
}
}
});
return maxWord
};

var Player = function(name) {
Scrabble.call(this);
this.name = name;
this.plays = []; //if i'm defining the array here then why does it say it's undefined when I try to push it in??
this.playerWon = false;
this.totalScore = 0;
};

Player.prototype.play = function(word) {
if ( this.playerWon ) {return console.log("You've already won. Why are you still playing??");}
this.plays.push(word)
return (new Scrabble()).score(word)
};

// totalScore = function() {
// this.plays.forEach(function(word) {
// a = new Scrabble()[score(word)]
// this.totalScore += a
// });
// return this.totalScore
// }
//
// hasWon = function() {
// this.playerWon = true if (this.totalScore > 100)
// return this.playerWon
// }
//
// highestScoringWord = function() {
// return new Scrabble()[score(this.plays)]
// }
//
// highestScoringWordScore = function() {
// return new Scrabble()[score(this.highestScoringWord)]
// }
// };


module.exports = Scrabble;

var newGame = new Scrabble();
// newGame[score('')];
// newGame[score("one")]
console.log(newGame.score("poOp"));
// newGame[score("po&")]

var Miriam = new Player("Miriam");
console.log(Miriam.play("one"));
137 changes: 137 additions & 0 deletions scrabble_take2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
var Scrabble = function() {}

Scrabble.prototype.score = function (word) {
if ( word.length > 7 ) {
return console.log("That word is too long for a real scrabble round. You've been Scrabbled!");
}
var wordPoints = 0
var parseWord = function(word) {
return wordAsArray = word.toLowerCase().split('');
}

if ( word.length == 7 ) {
wordPoints += 50
}

parseWord(word).forEach( function(letter) {
if( ["a","e","i","o","u","l","n","r","s","t"].indexOf( letter ) != -1 ) {
wordPoints += 1;
} else if ( ["d","g"].indexOf( letter ) != -1 ) {
wordPoints += 2;
} else if ( ["b","c","m","p"].indexOf( letter ) != -1 ) {
wordPoints += 3;
} else if ( ["f","h","v","w","y"].indexOf( letter ) != -1 ) {
wordPoints += 4;
} else if ( ["k"].indexOf( letter ) != -1 ) {
wordPoints += 5
} else if ( ["j","x"].indexOf( letter ) != -1 ) {
wordPoints += 8;
} else if ( ["q","z"].indexOf( letter ) != -1 ) {
wordPoints += 10;
} else {
return console.log("idk! that's not a letter...wtf??");
}
});

return wordPoints
};

Scrabble.prototype.highestScoreFrom = function (arrayOfWords) {
max = 0
maxWord = ""
arrayOfWords.forEach( function(word) {
currentWordScore = Scrabble.prototype.score(word)
if ( currentWordScore > max ) {
max = currentWordScore
max_word = word
} else if ( currentWordScore == max ) {
if ( word.length < max_word.length ) {
max_word = word
}
}
})
return max_word
};


///////// TESTING STUFF OUT /////////
// var newGame = new Scrabble()
// // returns the total score value for the given word. The word is input as a string (case insensitive).
// console.log(newGame.score("poop")); //8
// console.log(newGame.score("fuzz")); //25
// console.log(newGame.score("guzzle")); //25
// //if the top score is tied between multiple words, pick the one with the fewest letters.
// console.log(newGame.highestScoreFrom(["poop", "fuzz","guzzle"])); //fuzz
// //If the top score is tied between multiple words and one used all seven letters, choose the one with seven letters over the one with fewer tiles.
// console.log(newGame.highestScoreFrom(["poop", "fuzz","jimjams"])); // jimjams
// //If the there are multiple words that are the same score and same length, pick the first one in supplied list.
// console.log(newGame.highestScoreFrom(["jibb", "jauk","jamb"])); // jibb


var Player = function(playerName) {
this.playerName = playerName
this.won = false
this.arrayOfWords = []
this.playerTotalScore = 0
}

Player.prototype.name = function () {
return this.playerName
};

Player.prototype.plays = function () {
return this.arrayOfWords
};

Player.prototype.play = function (word) {
if ( this.won ) { return false }

return this.arrayOfWords.push(word)
};

Player.prototype.totalScore = function () {
for ( var i=0; i<this.arrayOfWords.length; i++ ) {
thisWordsScore = ( new Scrabble() ).score(this.arrayOfWords[i])
this.playerTotalScore += thisWordsScore
}
if ( this.playerTotalScore > 100 ) {
this.won = true
}
return this.playerTotalScore
};

Player.prototype.hasWon = function () {
return this.won
};

Player.prototype.highestScoringWord = function () {
return ( new Scrabble() ).highestScoreFrom(this.arrayOfWords)
};

Player.prototype.highestWordScore = function () {
return ( new Scrabble() ).score(this.highestScoringWord())
};

var miriam = new Player("Miriam")
// name: property which returns the value of the player's name
console.log(miriam.name()); //Miriam
// plays: property which returns an Array of the words played by the player
console.log(miriam.plays()); //[]
// play(word): Function which adds the input word to the plays Array
console.log(miriam.play("poop")); //1
console.log(miriam.play("words")); //2
console.log(miriam.play("jibb")); //3
console.log(miriam.plays()); //["poop","words","jibb"]
// totalScore(): Function which sums up and returns the score of the players words
console.log(miriam.totalScore()); //32
// hasWon(): Function which returns true if the player has over 100 points, otherwise returns false
console.log(miriam.hasWon()); //false
console.log(miriam.play("pazazz"));
console.log(miriam.play("pizzaz"));
console.log(miriam.plays());
console.log(miriam.totalScore());
console.log(miriam.hasWon()); //true
// highestScoringWord(): Function which returns the highest scoring word the user has played
console.log(miriam.highestScoringWord());
// highestWordScore(): Function which returns the highestScoringWord score
console.log(miriam.highestWordScore());