-
Notifications
You must be signed in to change notification settings - Fork 23
Yordanos Dirar - ScrabbleSinatra #22
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
base: master
Are you sure you want to change the base?
Changes from all commits
153c19b
55c4082
ae78519
f2aab94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| gem 'sinatra', '~>1.4.7' | ||
| gem 'rerun', '~>0.11.0' | ||
| gem 'thin', '~>1.6.4' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| daemons (1.2.3) | ||
| eventmachine (1.2.0.1) | ||
| ffi (1.9.10) | ||
| listen (3.0.6) | ||
| rb-fsevent (>= 0.9.3) | ||
| rb-inotify (>= 0.9.7) | ||
| rack (1.6.4) | ||
| rack-protection (1.5.3) | ||
| rack | ||
| rb-fsevent (0.9.7) | ||
| rb-inotify (0.9.7) | ||
| ffi (>= 0.5.0) | ||
| rerun (0.11.0) | ||
| listen (~> 3.0) | ||
| sinatra (1.4.7) | ||
| rack (~> 1.5) | ||
| rack-protection (~> 1.4) | ||
| tilt (>= 1.3, < 3) | ||
| thin (1.6.4) | ||
| daemons (~> 1.0, >= 1.0.9) | ||
| eventmachine (~> 1.0, >= 1.0.4) | ||
| rack (~> 1.0) | ||
| tilt (2.0.2) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| rerun (~> 0.11.0) | ||
| sinatra (~> 1.4.7) | ||
| thin (~> 1.6.4) | ||
|
|
||
| BUNDLED WITH | ||
| 1.11.2 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #require_relative '../scrabble' # allows this class to access scrabble and run through irb | ||
|
|
||
| class Scrabble::Scoring | ||
|
|
||
| attr_reader :word | ||
|
|
||
| include Scrabble | ||
|
|
||
| LETTER_POINTS = { | ||
| 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], | ||
| 2 => ["D", "G"], | ||
| 3 => ["B", "C", "M", "P"], | ||
| 4 => ["F", "H", "V", "W", "Y"], | ||
| 5 => ["K"], | ||
| 8 => ["J", "X"], | ||
| 10 => ["Q", "Z"] | ||
| } | ||
|
|
||
| def initializer (word) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean |
||
| @word = word["word"] | ||
| end | ||
|
|
||
| # returns the score value for a single given word | ||
| def self.score(word) | ||
| score = 0 | ||
| tiles = 0 | ||
|
|
||
| word_array = word.upcase.split("") | ||
|
|
||
| word_array.each do |letter| | ||
| LETTER_POINTS.each do |points, letters_array| | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be replicating the functionality of |
||
| if letters_array.include? letter | ||
| score += points | ||
| tiles += 1 | ||
| end | ||
| end | ||
| end | ||
| # if all seven tiles are used the below tenary will add 50 points to the users score else return score | ||
| tiles >= 7 ? score += 50 : score | ||
| score | ||
| end | ||
|
|
||
| # returns the score value for a single given letter | ||
| def self.char_score(char) | ||
| score = 0 | ||
|
|
||
| LETTER_POINTS.each do |points, letters_array| | ||
| if letters_array.include? char.upcase | ||
| score += points | ||
| end | ||
| end | ||
| score | ||
| end | ||
|
|
||
| def self.highest_score_from(array_of_words) # array_of_words = ["cats", "fee", "no"] | ||
|
|
||
| array_of_word_values = [] | ||
| array_of_words.each do |word| | ||
| array_of_word_values << [word, self.score(word), word.length] # = [["cats", 7, 4], ["fee", 7, 3], ["no", 2, 2]] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using an array of arrays: Consider using an array of hashes so you have context of each value |
||
| end | ||
|
|
||
| highest_score_array = array_of_word_values[0] # ["cats", 7, 4] | ||
|
|
||
| array_of_word_values.each do |word_array| # ["cats", 7, 4] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use some array methods to make this a little bit easier. Now we could use Then sort by length (in case there were more than 1 word with the same score Since |
||
| # compares the score of the words | ||
| if word_array[1] == highest_score_array[1] # compare score | ||
| # compares the length of the words | ||
| if word_array[2] < highest_score_array[2] | ||
| highest_score_array[2] == 7 ? highest_score_array : highest_score_array = word_array | ||
| #highest_score_array = word_array # replace the highest score to the word_array being referenced | ||
| elsif word_array[2] >= highest_score_array[2] | ||
| if highest_score_array[2] < 7 | ||
| word_array[2] == 7 ? highest_score_array = word_array : highest_score_array | ||
| end | ||
| end | ||
| elsif word_array[1] > highest_score_array[1] | ||
| highest_score_array = word_array | ||
| end | ||
| end | ||
| return highest_score_array[0] | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
|
|
||
| # using this module as a namespace, not a mixin | ||
| module Scrabble | ||
|
|
||
|
|
||
| end | ||
|
|
||
| # putting this at the end because otherwise it will read scoring.rb before it creates | ||
| # the Scrabble module, meaning the code in scoring.rb won't work | ||
| require_relative './scoring' |
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.
Scrabblelooks like it's an empty class. Why include it here?