forked from AdaGold/js-scrabble
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Kelly Souza JS-Scrabble #41
Open
kellysouza
wants to merge
5
commits into
Ada-C7:master
Choose a base branch
from
kellysouza:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
efa4ae0
Score function working
kellysouza 9c4fef2
Adds highestscore from array function
kellysouza 96bb3b4
Changing score object format, simplifying score method
kellysouza 9fbad42
Wave 2 done
kellysouza ad898ab
can call highest scroing word on player or game
kellysouza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,161 @@ | ||
var Scrabble = function() {}; | ||
// HELPER METHODS | ||
var scoreChart = { | ||
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 | ||
}; | ||
|
||
var arrayOfWords = []; | ||
|
||
var scoreHelper = function (word) { | ||
word = word.toUpperCase(); | ||
var points = 0; | ||
if (word.length == 7){ | ||
points += 50; | ||
} | ||
for (var i = 0, len = word.length; i < len; i++) { | ||
points += scoreChart[word[i]]; | ||
} | ||
return points; | ||
}; | ||
|
||
var highestScoringWordHelper = function(arrayOfWords) { | ||
var highestScoringWord; | ||
var highestScore = 0; | ||
for (var i = 0; i < arrayOfWords.length; i++) { | ||
if (scoreHelper(arrayOfWords[i]) > highestScore) { | ||
highestScoringWord = arrayOfWords[i]; | ||
highestScore = scoreHelper(arrayOfWords[i]); | ||
} else if (scoreHelper(arrayOfWords[i]) == highestScore) { | ||
if (highestScoringWord.length == 7) { | ||
} else if (arrayOfWords[i].length == 7) { | ||
highestScoringWord = arrayOfWords[i]; | ||
highestScore = scoreHelper(arrayOfWords[i]); | ||
} else if (arrayOfWords.length < highestScoringWord.length){ | ||
highestScoringWord = arrayOfWords[i]; | ||
highestScore = scoreHelper(arrayOfWords[i]); | ||
} | ||
} | ||
} | ||
return highestScoringWord; | ||
}; | ||
|
||
var highestWordScoreHelper = function (arrayOfWords) { | ||
var word = highestScoringWordHelper(arrayOfWords); | ||
return scoreHelper(word); | ||
}; | ||
|
||
// SCRABBLE METHODS | ||
|
||
|
||
var Scrabble = function() { | ||
this.arrayOfWords = []; | ||
}; | ||
|
||
Scrabble.prototype.score = function(word) { | ||
this.arrayOfWords.push(word); | ||
return scoreHelper(word); | ||
}; | ||
|
||
|
||
Scrabble.prototype.highestScoreFrom = function() { | ||
return highestScoringWordHelper(this.arrayOfWords); | ||
}; | ||
|
||
|
||
// YOUR CODE HERE | ||
Scrabble.prototype.helloWorld = function() { | ||
return 'hello world!'; | ||
}; | ||
|
||
// PLAYER METHODS | ||
|
||
var Player = function (name, game) { | ||
this.name = name; | ||
this.playerWords = []; | ||
this.game = game; | ||
}; | ||
|
||
Player.prototype.plays = function () { | ||
console.log("My turn!"); | ||
}; | ||
|
||
|
||
// refactor to include a save word to game method | ||
Player.prototype.play = function (word) { | ||
if (this.hasWon()) { | ||
return console.log("Cannot play that word", this.name, "has already won!"); | ||
} else { | ||
this.playerWords.push(word); | ||
this.saveWordToGameWordArray(word); | ||
console.log(this.playerWords); | ||
} | ||
}; | ||
|
||
|
||
Player.prototype.saveWordToGameWordArray = function (word) { | ||
this.game.arrayOfWords.push(word); | ||
} | ||
|
||
Player.prototype.totalScore = function () { | ||
var playerScore = 0; | ||
var scrabble = new Scrabble(); | ||
for (var i = 0; i < this.playerWords.length; i++) { | ||
playerScore += scrabble.score(this.playerWords[i]); | ||
} | ||
return playerScore; | ||
}; | ||
|
||
Player.prototype.hasWon = function () { | ||
if (this.totalScore() >= 100) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
Player.prototype.highestScoringWord = function () { | ||
return highestScoringWordHelper(this.playerWords); | ||
}; | ||
|
||
Player.prototype.highestWordScore = function () { | ||
return highestWordScoreHelper(this.playerWords); | ||
}; | ||
|
||
|
||
module.exports = Scrabble; | ||
|
||
myScrabble = new Scrabble(); | ||
|
||
p = new Player("Kelly", myScrabble); | ||
q = new Player("Kevin", myScrabble); | ||
|
||
console.log(p.name); | ||
console.log(q.name); | ||
|
||
|
||
// p.plays(); | ||
// p.play("Hello"); | ||
// p.play("World"); | ||
// console.log(p.totalScore()); | ||
// p.play("cat"); | ||
// console.log(p.totalScore()); | ||
// p.play("ZZZZZ"); | ||
// console.log(p.totalScore()); | ||
// p.play("AA"); | ||
// console.log(p.totalScore()); | ||
// p.play("ZZZZZ"); | ||
// console.log(p.totalScore()); | ||
p.play("Hello"); | ||
p.play("WorldZ"); | ||
// p.play("ZZZZZZZ"); | ||
// console.log(p.highestScoringWord()); | ||
// console.log(p.highestWordScore()); | ||
q.play("Hello"); | ||
q.play("world"); | ||
console.log(q.totalScore()); | ||
console.log(myScrabble.helloWorld()); | ||
console.log(myScrabble.score("cat")); | ||
console.log(myScrabble.highestScoreFrom()); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could also be written
return this.totalScore() >= 100;