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

Shaunna Wiens --carets #24

Open
wants to merge 17 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
123 changes: 123 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 139 additions & 4 deletions scrabble.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,149 @@
const LETTERVALUES = {
A: [1, 9],
B: [3, 2],
C: [3, 2],
D: [2, 4],
E: [1, 12],
F: [4, 2],
G: [2, 3],
H: [4, 2],
I: [1, 9],
J: [8, 1],
K: [5, 1],
L: [1, 4],
M: [3, 2],
N: [1, 6],
O: [1, 8],
P: [3, 2],
Q: [10, 1],
R: [1, 6],
S: [1, 4],
T: [1, 6],
U: [1, 4],
V: [4, 2],
W: [4, 2],
X: [8, 1],
Y: [4, 2],
Z: [10, 1],
};

const Scrabble = {
score: function(word) {
// TODO: implement score
score(word) {
if (word.length > 7 || word.length < 1) {
throw new Error('word must be between 1 and 7 letters');
}

const wordArray = word.toUpperCase().split('');

let sum = (wordArray.length === 7) ? 50 : 0;

wordArray.forEach((letter) => {
if (letter in LETTERVALUES) {
sum += LETTERVALUES[letter][0];
} else {
throw new Error('word contains invalid characters');
}
});

return sum;
},

highestScoreFrom(words) {
if (words.length === 0 || words.constructor !== Array) {
throw new Error('no words to compare score');
}

let max = this.score(words[0]);
let winningWord = words[0];

words.forEach((word) => {
const score = this.score(word);

if (score > max) {
max = score;
winningWord = word;
} else if (score === max) {
if (word.length === 7) {
max = score;
winningWord = word;
} else if (word.length < winningWord.length && winningWord.length !== 7) {
max = score;
winningWord = word;
}
}
});
return winningWord;
},
};

Scrabble.TileBag = class {
constructor() {
this.tiles = Scrabble.TileBag.setTiles();
}

// TODO: add the highestScoreFrom method
static setTiles() {
const tiles = [];
Object.keys(LETTERVALUES).forEach((letter) => {
const quantity = LETTERVALUES[letter][1];
for (let i = 0; i < quantity; i += 1) {
tiles.push(letter);
}
});
return tiles;
}

drawTile() {
return this.tiles.splice(Math.floor(Math.random() * this.tiles.length), 1);
}
};


Scrabble.Player = class {
// TODO: implement the Player class
constructor(name) {
if (!name) {
throw new Error('player must have a name');
}
this.name = name;
this.plays = [];
}

play(word) {
if (this.hasWon()) {
return false;
}
if (!(word.match(/^[a-zA-Z]{1,7}$/))) {
throw new Error('must be a word between 1 and 7 letters');
}
this.plays.push(word);
return Scrabble.score(word);
}

totalScore() {
let totalScore = 0;
this.plays.forEach((play) => {
totalScore += Scrabble.score(play);
});
return totalScore;
}

hasWon() {
return (this.totalScore() >= 100);
}

highestScoringWord() {
if (this.plays.length === 0) {
throw new Error('no words played');
}
return Scrabble.highestScoreFrom(this.plays);
}

highestWordScore() {
if (this.plays.length === 0) {
throw new Error('no words played');
}
return Scrabble.score(this.highestScoringWord());
}
};


module.exports = Scrabble;
28 changes: 28 additions & 0 deletions spec/scrabble_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,34 @@ describe('highestScoreFrom', function() {
});
});

describe('TileBag', function() {
it ('is defined', function() {
expect(Scrabble.TileBag).toBeDefined();
});
describe('Constructor', function() {
it('creates a set of tiles', function() {
let tilebag = new Scrabble.TileBag();
expect(tilebag.tiles.length).toBe(98);
});
});

describe('drawTile', function() {
it('decreases the tilebag count by 1', function() {
let tilebag = new Scrabble.TileBag();
let tile = tilebag.drawTile();
// TODO: Find a way to test this:
// expect(tile.).toBe();
expect(tilebag.tiles.length).toBe(97);
});
it('returns a tile from the tiles', function() {
let tilebag = new Scrabble.TileBag();
let tile = tilebag.drawTile();
// TODO: Find a way to test this:
// expect(tile.).toBe();
});
});
});

describe('Player', function() {
it ('is defined', function() {
expect(Scrabble.Player).toBeDefined();
Expand Down