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

Addie's JS Scrabble #16

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
1d78724
Scrabble.score returns the score of a word
add2point71dots May 16, 2017
504d3b0
working version of Scrabble.highestScoreFrom that I may refactor later
add2point71dots May 16, 2017
590afb1
oops forgot to have highestScoreFrom actually return the word
add2point71dots May 16, 2017
1b5d636
wrote Player constructor
add2point71dots May 16, 2017
c84f7ac
player can play a word (but doesn't yet check if they've won)
add2point71dots May 16, 2017
37d46dc
can see a player's total score
add2point71dots May 16, 2017
5c034bb
can check if a player hasWon
add2point71dots May 16, 2017
bd41704
can check for player's highestScoringWord
add2point71dots May 16, 2017
dd814b0
doesn't let player play if they've already won
add2point71dots May 16, 2017
952a283
can see player's highestWordScore
add2point71dots May 16, 2017
acf98e1
put in a module exports thingy for player though I still don't know w…
add2point71dots May 16, 2017
d7f351a
nevermind undid the export player thing because I don't know how to e…
add2point71dots May 16, 2017
a9477f0
refactored highestScoreFrom method
add2point71dots May 16, 2017
2dc5d26
fixed a bug that was returning short low scoring words
add2point71dots May 16, 2017
ab5da2e
created TileBag constructor with tiles
add2point71dots May 17, 2017
f1accca
can draw tiles from a TileBag
add2point71dots May 17, 2017
27b9ff2
put more specific error messages in
add2point71dots May 17, 2017
8062ee6
took out unnecessary variable in highestScoreFrom
add2point71dots May 17, 2017
69e20fa
removed all my testing console.logs
add2point71dots May 18, 2017
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
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() {};

// YOUR CODE HERE
Scrabble.prototype.helloWorld = function() {
return 'hello world!';
Scrabble.score = function(word) {
var scores = { 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 };
var total = 0;
word = word.toLowerCase();

for (var i = 0; i < word.length; i++) {
total += scores[word[i]];
}

if (word.length == 7) {
total += 50;
}
return total;
};

Scrabble.highestScoreFrom = function(arrayOfWords) {
var highScore = -1;
var highScoreWord = "";

for (var i = 0; i < arrayOfWords.length; i++) {
var word = arrayOfWords[i];
var score = this.score(word);
var sevenBonus = (highScoreWord.length == 7);
var tie = (score == highScore);
var tieWinner = (!sevenBonus && tie && (word.length == 7 || word.length < highScoreWord.length));

if (score > highScore || tieWinner) {
highScore = score;
highScoreWord = word;
}
}
return highScoreWord;
};

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

Player.prototype.play = function(word) {
if (this.hasWon()) {
console.log("You can't play more - you've already won!");
return false;
}
this.plays.push(word);
};

Player.prototype.totalScore = function() {
var total = 0;

for (var i = 0; i < this.plays.length; i++) {
total += Scrabble.score(this.plays[i]);
}
return total;
};

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

module.exports = Scrabble;
Player.prototype.highestScoringWord = function() {
word = Scrabble.highestScoreFrom(this.plays);
if (!word) {
console.log(this.name + " hasn't played any words yet.");
return false;
}
return word;
};

Player.prototype.highestWordScore = function() {
if (this.plays.length === 0) {
console.log(this.name + " hasn't played any words yet.");
return false;
}
return Scrabble.score(this.highestScoringWord());
};

var TileBag = function() {
var bag = ["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 = TileBag.shuffle(bag);
};

TileBag.shuffle = function(tiles) {
for (var i = 0; i < tiles.length; i++) {
var j = Math.floor(Math.random() * tiles.length);
var temp = tiles[i];
tiles[i] = tiles[j];
tiles[j] = temp;
}
return tiles;
};

TileBag.prototype.drawTiles = function(num) {
var tilesDrawn = [];

if (num > 7) {
console.log("You can't drawn more than 7 tiles.");
return false;
}
if (num > this.tilesRemaining()) {
console.log("There aren't enough tiles in the bag.");
return false;
}
if (num <= 0) {
console.log("You must draw at least one tile.");
return false;
}

for (var i = 0; i < num; i++) {
tilesDrawn.push(this.tiles.pop());
}

return tilesDrawn;
};

TileBag.prototype.tilesRemaining = function(num) {
return this.tiles.length;
};