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

Brenda Rios - Scrabble - Octos #32

Open
wants to merge 13 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
146 changes: 145 additions & 1 deletion scrabble.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,160 @@

const Scrabble = {
const Scrabble = {

score(word) {
const letterValues = { "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 };

if (/^[a-zA-Z]+$/.test(word) && word.length <= 7) {

word = word.toUpperCase();

let wordScore = (word.length == 7)? 50 : 0;

for (let i = 0; i < word.length; i+=1) {

wordScore += letterValues[word.charAt(i)];
}

return wordScore;

} else {
throw "Word must only contain letters from A-Z and have less than or equal to 7 characters";
}

Choose a reason for hiding this comment

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

It might be a little cleaner to invert this conditional:

if (!(/^[a-zA-Z]+$/.test(word) && word.length <= 7)) {
  throw new Error("Word must only contain letters from A-Z and have less than or equal to 7 characters");
}
// ... lines 11 through 20 ...

Choose a reason for hiding this comment

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

If you throw a regular string like you do on line 23, you don't get backtrace information! You should throw an instance of Error instead (i.e. throw new Error("Word must..."))

},

highestScoreFrom(arrayOfWords) {

if (arrayOfWords.length === 0) {

throw new Error('Array of words can not be empty');

} else if (arrayOfWords.length === 1) {

return arrayOfWords[0];

} else {

const winningWord = arrayOfWords.reduce((word1, word2) => {
const scoreWord1 = Scrabble.score(word1);
const scoreWord2 = Scrabble.score(word2);

Choose a reason for hiding this comment

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

Good use of reduce here!

if (scoreWord1 > scoreWord2 || word1.length === 7) {
return word1;

} else if (scoreWord2 > scoreWord1 || word2.length === 7) {
return word2;
}

if (word1.length === word2.length || word1.length < word2.length) {
return word1;
}

return word2;

});

return winningWord;
}
},
};

Scrabble.Player = class {
constructor(name) {
if (name.length > 0 || typeof name === 'string') {
this.name = name;
}
else {
throw new Error('Please enter name of the player');
}

this.plays =[];

}

play(word) {

if (word === " "|| typeof word !== 'string') {
throw new Error('Please enter a valid word');
}
if (this.hasWon()) {
return false;
}
this.plays.push(word);
return true;
}


totalScore() {

let total = 0;
for (let word of this.plays) {
total += Scrabble.score(word);
}
return total;
}

hasWon() {

let won = (this.totalScore() >= 100) ? true:false;
return won;
}

highestScoringWord() {

return Scrabble.highestScoreFrom(this.plays);
}

highestWordScore() {

return Scrabble.score(this.highestScoringWord());
}

};

// Optional Enhancements

// TODO: I need to improve the TilesBag. The way I made the tiles in the constructor
// I know is not the best way.

Scrabble.TilesBag = class {
constructor() {
this.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'];
}

drawTiles(number) {

if (number > this.tilesRemaining()) {
throw new Error('No enough tiles');
}
let hand = [];
for (let i = 0; i < number; i+=1) {
let rand = Math.floor(Math.random() * this.tiles.length);
hand.push(this.tiles[rand]);
this.tiles.splice(rand, 1);
// splice removes items from an array. changes the original array
}
return hand;
}

tilesRemaining() {
if (this.tiles.length === 0) {
return 'No more tiles in the bag'
}
return this.tiles.length;
}

};
module.exports = Scrabble;


// console.log(Scrabble.score("ZZzzZZ"));
// console.log(Scrabble.score("baby"));
// console.log(Scrabble.highestScoreFrom(["ZZzzZZ", "zzzzzzz", "pedro"]));
// console.log(Scrabble.highestScoreFrom(["QQQQQQQ", "zzzzzzz", "pedro"]));
// console.log(Scrabble.score("QQQQQQQ"));
Loading