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

Completed primary requirements #6

Open
wants to merge 15 commits into
base: ald/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
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,28 @@ 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
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
source 'https://rubygems.org'
ruby "2.2.3"

gem "sinatra"

group :development do
gem "pry"
end

group :test do
gem "rspec"
gem "simplecov"
end
51 changes: 51 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.0)
diff-lcs (1.2.5)
docile (1.1.5)
json (1.8.3)
method_source (0.8.2)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.4)
rack-protection (1.5.3)
rack
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
simplecov (0.10.0)
docile (~> 1.1.0)
json (~> 1.8)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
slop (3.6.0)
tilt (2.0.1)

PLATFORMS
ruby

DEPENDENCIES
pry
rspec
simplecov
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-sinatra'
run ScrabbleSinatra
1 change: 1 addition & 0 deletions css/layout.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

46 changes: 46 additions & 0 deletions lib/highest_score.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'pry'
require "./lib/score.rb"

module Scrabble
class ScrabbleGame


def self.score_multiple_words(array_of_words)
wordscore_hash = {}
array_of_words.each do |w|
score = self.score(w)
wordscore_hash[w] = score
end
return wordscore_hash
end

def self.string_to_multiple_word_scores(string_of_words)
word_array = string_of_words.split(" ")
Scrabble::ScrabbleGame.score_multiple_words(word_array)
end

def self.highest_score_from(array_of_words)
wordscore_hash = Scrabble::ScrabbleGame.score_multiple_words(array_of_words)
top_scoring_words_hash = {}
wordscore_hash.each do |k, v|
if v == wordscore_hash.values.max
top_scoring_words_hash[k] = v
end
end
#make a hash from the top scoring words that instead of containing scores for values, contains the length of the word.
word_lengths_hash = {}
top_scoring_words_hash.each do |k, v|
word_lengths_hash[k] = k.length
end

#return the word associated with the shortest length
#if the value of the key is equal to the min, return just one
word_lengths_hash.each do |k,v|
if v == word_lengths_hash.values.min
puts "#{k}"
return k
end
end
end
end
end
55 changes: 55 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'pry'
require "./lib/score.rb"
require "./lib/highest_score.rb"
require "./lib/tilebag.rb"

module Scrabble

class Player
attr_accessor :name, :words_played, :player_score, :tiles

def initialize(name)
@name = name.capitalize
@words_played = []
@player_score = 0
@tiles = []
end

def play_word(word)
if won?
puts "You've won!"
return "player won"
else
@words_played.push(word)
return @words_played
end
end

def won?
if @player_score > 100
return true
end
end

def total_score
sum = 0
@words_played.each do |word|
sum += (ScrabbleGame.score(word))
end
sum
end

def highest_scoring_word
ScrabbleGame.highest_score_from(@words_played)
end

def highest_word_score
ScrabbleGame.score(highest_scoring_word)
end

def draw_tiles(tilebag)
num = 7 - @tiles.length
@tiles += (tilebag.draw_tiles(num))
end
end
end
53 changes: 53 additions & 0 deletions lib/score.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Scrabble

class ScrabbleGame

LETTER_VALUES = {
"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
}

def self.score(word)

letters = word.downcase.split("")

word_score = 0

letters.each do |letter|
word_score += LETTER_VALUES[letter]
end

if letters.length == 8
word_score += 50
puts "BONUS! You used all seven letters! You get 50 extra points!"
end

return word_score
end

end
end
64 changes: 64 additions & 0 deletions lib/tilebag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'pry'
require './lib/player.rb'

module Scrabble

class TileBag

attr_accessor :quantity

def initialize

@quantity = {}

@quantity["a"] = 9
@quantity["b"] = 2
@quantity["c"] = 2
@quantity["d"] = 4
@quantity["e"] = 12
@quantity["f"] = 2
@quantity["g"] = 3
@quantity["h"] = 2
@quantity["i"] = 9
@quantity["j"] = 1
@quantity["k"] = 1
@quantity["l"] = 4
@quantity["m"] = 2
@quantity["n"] = 6
@quantity["o"] = 8
@quantity["p"] = 2
@quantity["q"] = 1
@quantity["r"] = 6
@quantity["s"] = 4
@quantity["t"] = 6
@quantity["u"] = 4
@quantity["v"] = 2
@quantity["w"] = 2
@quantity["x"] = 1
@quantity["y"] = 2
@quantity["z"] = 1
end

# turn @quantity into an array, shuffle, pop, and return x amount of letters

def draw_tiles(num)
if num > 7
return []
else
letters_array = []
num.times do
letters_array.push(@quantity.keys.shuffle.pop)
end
letters_array.each do |letter|
@quantity[letter] -= 1
end
return letters_array
end
end

def tiles_remaining
sum = @quantity.values.inject(0) {|sum, value| sum + value}
sum
end
end
end
38 changes: 38 additions & 0 deletions scrabble-sinatra.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "sinatra"
require "./lib/highest_score.rb"
require "./lib/player.rb"
require "./lib/score.rb"
require "./lib/tilebag.rb"

class ScrabbleSinatra < Sinatra::Base

get "/" do
erb :index
end

get "/score" do
@page_name = "Score a Word"
erb :score
end

post "/score" do
@page_name = "Score a Word"
@word = params[:word]
@score = Scrabble::ScrabbleGame.score(@word)
erb :score
end

get "/score_multiple" do
@page_name = "Score some Words"
erb :score_multiple
end

post "/score_multiple" do
@page_name = "Score some Words"
words = params[:words]
@words_with_scores = Scrabble::ScrabbleGame.string_to_multiple_word_scores(words)
erb :score_multiple
end


end
20 changes: 20 additions & 0 deletions spec/highest_score_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "spec_helper"
require "./lib/highest_score.rb"

describe Scrabble::ScrabbleGame do

describe "self.highest_score_from" do
before :each do
@word_array1 = ["aaa", "bbb"]
@word_array2 = ["bb", "aaaaaa", "a", "b", "ki", "fg"]
end

it "Can look at this method with rspec" do
puts "Rspec can see in here"
end

it "If there is more than word with a top score, return the shortest word that is listed first in the array" do
expect(Scrabble::ScrabbleGame.highest_score_from(@word_array2)).to be == "bb"
end
end
end
Loading