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

Scrabble Sinatra #18

Open
wants to merge 9 commits into
base: cas/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
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,29 @@ build/

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org/"
ruby "2.2.3"

gem "sinatra"

# group: development do
# gem "pry"
# end
20 changes: 20 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
GEM
remote: https://rubygems.org/
specs:
rack (1.6.4)
rack-protection (1.5.3)
rack
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
tilt (2.0.1)

PLATFORMS
ruby

DEPENDENCIES
sinatra

BUNDLED WITH
1.10.6
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "./scrabble-site.rb"
run ScrabbleSite
67 changes: 67 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "./lib/scrabble.rb"
require "./lib/tile_bag"

class Player
attr_accessor :name, :player_words, :tile_tray

#initializes a new player
def initialize(name)
@name = name
@player_words =[]
#@bag = nil
@tile_tray = []
end

#returns the array of words a player has already played
def plays
return @player_words
end

#takes a word as a parameter and checks if it is a string
def play(word)
if word.class != String
raise ArgumentError, "Input must be a string."
end
#if word is not a string it returns false, and if it is pushes the word to an array of all guessed words
won? ? (return false) : (@player_words.push(word))
end

#takes all of the words guessed by a player and calls the score method on each of them, then sums up all those scores
def total_score
total_score = 0
@player_words.each do |word|
total_score += Scrabble.score(word)
end
return total_score
end

#returns true is a player has scored a hundred or more points
def won?
total_score >= 100 ? (return true) : (return false)
end

#returns the maximum of the array of player_words based on score of each word
def highest_scoring_word
high_word = @player_words.max_by { |word| Scrabble.score(word) }
return high_word
end

#returns the score of the returned word of highest_scoring_word method
def highest_word_score
return Scrabble.score(highest_scoring_word)
end


#draws new tiles for the player based on TileBag draw_tiles method. This method requires a parameter, which will be a TileBag instance. We then set that tilebag instance equal to a variable and call Tilebags draw_tiles method on it. The parameter for that method is the difference between 7 (the max number of tiles a player can have) and the tile_tray, or how many tiles they already have. This returns new tiles from the Tilebag method, which we then push onto the player's tile tray and flatten into one array.
def draw_tiles(tile_bag = TileBag.new)
new_player_tiles = tile_bag.draw_tiles(7-@tile_tray.length)
@tile_tray.push(new_player_tiles)
return @tile_tray.flatten!
end

#returns everything in the tile_tray of the player
def tiles
return @tile_tray
end

end
118 changes: 118 additions & 0 deletions lib/scrabble.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
class Scrabble
attr_accessor :player_words, :score

def initialize
end

# Returns the total score value for the given word
def self.score(word)
# Check that the given word is a string
if word.class != String
raise ArgumentError, "Input must be a string."
else
# Check that each character of the given string is alphabetical
# word.length.times do |letter|
# if !("a".."z").to_a.include? word[letter]
# raise ArgumentError, "Input must be an alphabetical string."
# end
# end
end

@score = 0
# If word length is 7, add 50 points
word.length == 7 ? (@score += 50) : (@score += 0)
# For each letter in the given word, update the score value
word.length.times do |index|
case word[index].upcase
when "A","E","I","O","U","L","N","R","S","T"
@score += 1
when "D","G"
@score += 2
when "B","C","M","P"
@score += 3
when "F","H","V","W","Y"
@score += 4
when "K"
@score += 5
when "J", "X"
@score += 8
when "Q", "Z"
@score += 10
end
end
return @score
end

# Returns the word from a given array of words that will have th highest score
def self.highest_score_from(array_of_words)
best_words = []
# Sort the array by score
sorted_array = array_of_words.sort_by {|word| score(word)}
# Inspired by Jennie and Jenna's solution
# Map the sorted array to a score array
sorted_score_array = sorted_array.map do |word|
score(word)
end
# Determine the max score
max_score = sorted_score_array.max
# For each score value, if it equals the max score, put the word in the best words array
sorted_score_array.length.times do |i|
if sorted_score_array[i] == max_score
best_words.push(sorted_array[i])
end
end
# Sort best words by length
# After this you have an array of all the words with max_score sorted by word length
best_words.sort_by! do |word|
word.length
end
# If there is a 7 letter word, return that
# Otherwise, return the shortest word that hits max score
if best_words[-1].length == 7
return best_words[-1]
else
return best_words[0]
end
end
end

# ------------ ORIGINAL SOLUTION ------------------------ #
# We rewrote our solution after looking at the solutions of our classmates. We think the new solution is more straightforward but the below solution works too.
# at_max = true
# best_words = []
# index = -1
#
# # While you are still at the maximum score
# while at_max == true
# # If you are at the first index, check if the score of the value at that index is equal to the value next to it in the array. If it is, push it into best_words and break the loo
# if index.abs >= sorted_array.length
# if score(sorted_array[index]) == score(sorted_array[index+1])
# best_words.push(sorted_array[index])
# at_max = false
# end
# else
# # Otherwise, push the word into the best words array
# # If the next value (moving backwards) has a lower score, break out the while loop (and don't add any more words to best_words)
# best_words.push(sorted_array[index])
# if score(sorted_array[index]) > score(sorted_array[index-1])
# at_max = false
# end
# end
# # decrement index
# index -= 1
# end
# # Sort the array by word length
# best_words.sort_by! {|word| word.length}

# best_words.each do |word|
# # Handling the first value
# if word == best_words[-1]
# return word
# end
# word_index = best_words.index(word)
# if word.length < best_words[word_index+1].length
# return word
# end
# end
# return best_words[0]
# --------------- END OF ORIGINAL SOLUTION -------------------------- #
Loading