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

finished wave 3 minimum requirements! (w/ Sabrina aka best partner ever) #4

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4514a3b
set up envirnoment with All The Folders & Stuff
laurenfb Aug 30, 2016
0d25cd4
added first test
wilsab30 Aug 30, 2016
1934838
added Scoring class and self.score method, plus a test for that
laurenfb Aug 30, 2016
95e35b3
write tests and method bingo to add 50 pts for 7-letter word
laurenfb Aug 30, 2016
c01db08
trying to push spec updates again
laurenfb Aug 30, 2016
89a92f7
Added check_input method and test for check_input.
wilsab30 Aug 30, 2016
f893441
added to check_input method to verify string argument
laurenfb Aug 30, 2016
3ec9b20
rearrange tests and add first test for highest_score_method
laurenfb Aug 30, 2016
9bb5870
Added highest score from method
wilsab30 Aug 30, 2016
3857182
start work on tests & method for tiebreaker
laurenfb Aug 30, 2016
7959225
Added tie method
wilsab30 Aug 31, 2016
10c02ff
added self.tie and self.determine tie, added tests for both
laurenfb Aug 31, 2016
70428fb
Added tiebreaker method
wilsab30 Aug 31, 2016
cf408f2
OMG WE FINALLY FINISHED WAVE 1
laurenfb Aug 31, 2016
ab848be
Added player class
wilsab30 Aug 31, 2016
94a8e75
added play(word) and tests for such
laurenfb Aug 31, 2016
51dc387
Added highest word and score methods
wilsab30 Aug 31, 2016
114b7aa
added total_score method
laurenfb Aug 31, 2016
3a2f59f
Added won method
wilsab30 Aug 31, 2016
f3caba9
added final tests and added won? to play method
laurenfb Aug 31, 2016
d38e272
finished Wave 2
laurenfb Aug 31, 2016
8116f35
Added Tilebag class
wilsab30 Aug 31, 2016
6cb9ec5
added LETTER_ARRAY constant
laurenfb Sep 1, 2016
bd1ddb0
added and passing tests for LETTER_ARRAY constant
laurenfb Sep 1, 2016
bc59de7
added test for Tilebag and empty initialize method
laurenfb Sep 1, 2016
365fd79
Added draw tile method
wilsab30 Sep 1, 2016
06a02ee
a whole bunch of stuff
laurenfb Sep 1, 2016
ff145d5
Added test for draw_tiles
wilsab30 Sep 2, 2016
4355b22
finished wave 3, with a slight complication
laurenfb Sep 2, 2016
1d4f31d
Added instance variables to Player class
wilsab30 Sep 2, 2016
5ba0f6b
added a comment
laurenfb Sep 16, 2016
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
18 changes: 17 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,37 @@
/InstalledFiles
/pkg/
/spec/reports/
/spec/examples.txt
/test/tmp/
/test/version_tmp/
/tmp/

# Used by dotenv library to load environment variables.
# .env

## Specific to RubyMotion:
.dat*
.repl_history
build/
*.bridgesupport
build-iPhoneOS/
build-iPhoneSimulator/

## Specific to RubyMotion (use of CocoaPods):
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
# vendor/Pods/

## Documentation cache and generated files:
/.yardoc/
/_yardoc/
/doc/
/rdoc/

## Environment normalisation:
## Environment normalization:
/.bundle/
/vendor/bundle
/lib/bundler/man/
Expand All @@ -33,3 +48,4 @@ build/

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
.DS_Store
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib", "specs"]
t.warning = false
t.verbose = false
t.test_files = FileList['specs/*_spec.rb']
puts "Running TestTask"
end

task default: :test do
puts "Running my Rakefile"
end
83 changes: 83 additions & 0 deletions lib/Scrabble_Player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require_relative '../scrabble'
require_relative '../lib/tilebag'


class Scrabble::Player
attr_reader :name, :plays

def initialize(name)
# we want the player to use a string for a name
raise ArgumentError unless name.class == String

@name = name
@plays = []
@player_tiles = [] # now this is empty but later when we are more advanced we will initialize it with 7 tiles.
end

# allows the player to plays a word as long as they have not already won
def play(word)
if won?
return false
else
@plays << word
score = Scrabble::Scoring.score(word)
return score
end
end

# method that returns the word that a player has played with the highest score
def highest_score_word
highest_word = Scrabble::Scoring.highest_score_from(@plays)
return highest_word
end

# method that returns the score of a player's highest scoring word
def highest_word_score
highest_score = Scrabble::Scoring.score(highest_score_word)
return highest_score
end

# method that checks what the total score is
def total_score
score = 0
@plays.each do |word|
score += Scrabble::Scoring.score(word)
end
return score
end

# method that checks if player has 100+ points; if so, they've won
def won?
if total_score >= 100
return true
else
return false
end
end


def tiles
return @player_tiles
end

def player_draw_tiles(tile_bag)
# puts "#{@player_tiles} <---- this is before hand"

@player_tiles << tile_bag.draw_tiles(number_to_draw)
# puts "#{@player_tiles} <---- this is after the tiles are drawn"

return @player_tiles.flatten!
end

def number_to_draw
number_to_draw = Scrabble::MAX_TILES - @player_tiles.length
return number_to_draw
end

end # end of class


player = Scrabble::Player.new("Lauren7")
bag_seven = Scrabble::Tilebag.new

player.player_draw_tiles(bag_seven)
110 changes: 110 additions & 0 deletions lib/scrabble_scoring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require_relative '../scrabble'


class Scrabble::Scoring

def self.score(word)
check_input(word)
letter_array = word.upcase.split('')
score = 0
# this adds 50 if word length is >= 7 by calling bingo.
score += bingo(word)

# iterate through array and add score
letter_array.each do |letter|
score += Scrabble::SCORE_HASH[letter.to_sym]
end
return score
end

# this determines whether the word uses all 7 tiles
def self.bingo(word)
if word.length >= 7
return 50 # this adds the 50 points
else
return 0 # if not, then add 0 points
end
end


def self.check_input(word)
# this will only allow the word to be a string
raise ArgumentError unless word.class == String

# this checks to be sure the word only contains letters
if word =~ /^[a-zA-Z]+$/
return word
else
raise ArgumentError
end
end

# this method finds the highest score from an array of words
def self.highest_score_from(array_of_words)
# make sure the argument is an array
raise ArgumentError unless array_of_words.class == Array

word_hash = {}

array_of_words.each do |element|
word_hash[element] = score(element)
end

if !determine_tie(word_hash)
# this returns the word that has the highest score if there is no tie.
return word_hash.key(word_hash.values.max)
else
tie(word_hash)
end

end

def self.determine_tie(hash)
# determines if there are any ties.
if hash.values.length != hash.values.uniq.length
return true
else
return false
end
end

def self.tie(word_hash)
# make an empty array, will be made of the words that are tied
tie_words = []
# iterates through hash and adds word to the array if the value is one of the max values
word_hash.each { |key, value| tie_words << key if value == word_hash.values.max }
winner = tiebreaker(tie_words)
return winner
end

def self.tiebreaker(tie_array)
#returns winning word if words are same length.
two_d_array = []
tie_array.each do |word|
two_d_array << [word, word.length]
end
max = 7
new_array = two_d_array.select {|word| word[1] >= max}
if new_array.length == 0
find_winner_non7(tie_array)
else
return new_array[0][0] # returns the first word played has 7 or greater letters
end
end

def self.find_winner_non7(tie_array)
winning_word = ""
winning_length = 10000
tie_array.each do |word|
if word.length < winning_length
winning_length = word.length
winning_word = word
end
end
return winning_word
end


end # end of Scoring class
#
# puts Scrabble::Scoring.tiebreaker(["sabrina", "knitting"])
48 changes: 48 additions & 0 deletions lib/tilebag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require_relative '../scrabble'

class Scrabble::Tilebag
attr_reader :tiles

def initialize
@tiles = clone_constant
end

def clone_constant
clone_array = []
Scrabble::LETTER_ARRAY.each do |small_array|
clone_array << small_array.clone
end
return clone_array
end

def draw_tiles(num)
rack = @tiles.flatten.sample(num)

# for each drawn letter in rack, this finds the letter's array in @tiles,
# and pops it off the end of that mini-array
rack.each do |letter|
@tiles.assoc(letter).pop
end

return rack
end

def tiles_remaining
return @tiles.flatten.length
end

end
#
#joe = Scrabble::Tilebag.new

# rack = joe.draw_tiles(7)
# ap rack
# joe.tiles.each do |small_array|
# puts small_array.length
# end
# puts Scrabble::LETTER_ARRAY.length
# # puts "****************"
# # ap joe.tiles.flatten.length
#
#
# # puts rack.length
47 changes: 47 additions & 0 deletions scrabble.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# let's add a comment to this to test if github username changing worked!

#require_relative './lib/Scrabble_scoring'
#require_relative './lib/Scrabble_Player'
#require_relative './lib/tilebag'
require 'awesome_print'


module Scrabble

SCORE_HASH = {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}


LETTER_ARRAY = [["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"]]


MAX_TILES = 7

MAX_IN_BAG = LETTER_ARRAY.flatten.length
end
Loading