From c82b8a217fed3453eb9efdb07b755b81f29bd164 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Thu, 21 Feb 2019 16:21:24 -0800 Subject: [PATCH 01/13] Wrote the entire program so far --- lib/adagrams.rb | 156 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..3086019 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,156 @@ +require "pry" +letter_distribution = { + "a" => 9, + "b" => 2, + "c" => 2, + "d" => 4, + "e" => 12, + "f" => 2, + "g" => 3, + "h" => 2, + "i" => 9, + "j" => 1, + "k" => 1, + "l" => 4, + "m" => 2, + "n" => 6, + "o" => 8, + "p" => 2, + "q" => 1, + "r" => 6, + "s" => 4, + "t" => 6, + "u" => 4, + "v" => 2, + "w" => 2, + "x" => 1, + "y" => 2, + "z" => 1, +} + +# populating letter pool +@letter_pool = [] +letter_distribution.each do |str, n| + n.times do + @letter_pool << str.downcase + end +end + +def draw_letters + @hand = @letter_pool.shuffle[0..9] + print "Here's your hand: #{@hand}" +end + +def uses_available_letters?(input, letters_in_hand) + if input.length > 10 + puts "Too many letters! Try again: " + input = gets.chomp.downcase + end + until (input.chars.all? { |char| @hand.include? char }) + puts "Don't work, try again: " + input = gets.chomp.downcase + end + n = 0 + unless input.chars.length == n + input.chars.each do |letter| + hand_count = @hand.count(letter) + input_count = input.count(letter) + if hand_count >= input_count + n += 1 + else + puts "no" + input = gets.chomp.downcase + #accept input, start loop over + end + end + end + puts "Congrats, your adagram is: #{input}" + return input +end + +def score_word(word) + score_chart = { + "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, + } + if word.length > 6 + total_points = 8 + else + total_points = 0 + end + + word.each_char do |letter| + total_points += (score_chart[letter]) + end + return total_points +end + +def highest_score_from_words(words) + highest_score = 0 + winning_word = {:word => "", :score => 0} + words.each do |word| + if score_word(word) > highest_score + highest_score = score_word(word) + winning_word[:word] = word + winning_word[:score] = highest_score + elsif score_word(word) == highest_score + if word.length == 10 + winning_word[:word] = word + elsif word.length < winning_word[:word].length + winning_word[:word] = word + elsif word.length == winning_word[:word].length + break + end + end + end + puts "The word with the highest score is: #{winning_word}" + return winning_word +end + +########### Starting the game ############ +puts "Insert number of players" +num_players = gets.chomp.to_i + +player_results = [] +num_players.times do + player_word = {} + draw_letters + puts "\n\nInsert your Adagram: " + input = gets.chomp.downcase + uses_available_letters?(input, @hand) + total_points = score_word(input) + player_word[:word] = input + player_word[:score] = total_points + player_results << player_word +end + +played_words = [] +player_results.each do |word| + played_words << word[:word] +end + +highest_score_from_words(played_words) From def1b94622b2e13016623e314b1506792dbb97e1 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 13:37:45 -0800 Subject: [PATCH 02/13] commented out erroneous code --- lib/adagrams.rb | 229 ++++++++++++++++++++++++------------------------ 1 file changed, 116 insertions(+), 113 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 3086019..157006b 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,113 +1,116 @@ require "pry" -letter_distribution = { - "a" => 9, - "b" => 2, - "c" => 2, - "d" => 4, - "e" => 12, - "f" => 2, - "g" => 3, - "h" => 2, - "i" => 9, - "j" => 1, - "k" => 1, - "l" => 4, - "m" => 2, - "n" => 6, - "o" => 8, - "p" => 2, - "q" => 1, - "r" => 6, - "s" => 4, - "t" => 6, - "u" => 4, - "v" => 2, - "w" => 2, - "x" => 1, - "y" => 2, - "z" => 1, -} # populating letter pool -@letter_pool = [] -letter_distribution.each do |str, n| - n.times do - @letter_pool << str.downcase - end -end def draw_letters - @hand = @letter_pool.shuffle[0..9] - print "Here's your hand: #{@hand}" + letter_distribution = { + "a" => 9, + "b" => 2, + "c" => 2, + "d" => 4, + "e" => 12, + "f" => 2, + "g" => 3, + "h" => 2, + "i" => 9, + "j" => 1, + "k" => 1, + "l" => 4, + "m" => 2, + "n" => 6, + "o" => 8, + "p" => 2, + "q" => 1, + "r" => 6, + "s" => 4, + "t" => 6, + "u" => 4, + "v" => 2, + "w" => 2, + "x" => 1, + "y" => 2, + "z" => 1, + } + letter_pool = [] + letter_distribution.each do |str, n| + n.times do + letter_pool << str.downcase + end + end + hand = letter_pool.shuffle[0..9] + # print "Here's your hand: #{hand}" + return hand end def uses_available_letters?(input, letters_in_hand) - if input.length > 10 - puts "Too many letters! Try again: " - input = gets.chomp.downcase - end - until (input.chars.all? { |char| @hand.include? char }) - puts "Don't work, try again: " - input = gets.chomp.downcase - end + # if input.length > 10 + # puts "Too many letters! Try again: " + # input = gets.chomp.downcase + # end + # until (input.chars.all? { |char| letters_in_hand.include? char }) + # puts "Don't work, try again: " + # input = gets.chomp.downcase + # end n = 0 unless input.chars.length == n input.chars.each do |letter| - hand_count = @hand.count(letter) + hand_count = letters_in_hand.count(letter) input_count = input.count(letter) - if hand_count >= input_count - n += 1 - else - puts "no" - input = gets.chomp.downcase - #accept input, start loop over - end - end + # if hand_count >= input_count + # n += 1 + # else + # puts "no" + # input = gets.chomp.downcase + # #accept input, start loop over + # end + # end end - puts "Congrats, your adagram is: #{input}" + # puts "Congrats, your adagram is: #{input}" return input end -def score_word(word) - score_chart = { - "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, - } - if word.length > 6 - total_points = 8 - else - total_points = 0 - end +# def score_word(word) +# score_chart = { +# "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, +# } +# word.downcase! +# if word.length > 6 +# total_points = 8 +# else +# total_points = 0 +# end - word.each_char do |letter| - total_points += (score_chart[letter]) - end - return total_points -end +# word.each_char do |letter| +# total_points += (score_chart[letter]) + +# end +# return total_points +# end def highest_score_from_words(words) highest_score = 0 @@ -127,30 +130,30 @@ def highest_score_from_words(words) end end end - puts "The word with the highest score is: #{winning_word}" + # puts "The word with the highest score is: #{winning_word}" return winning_word end ########### Starting the game ############ -puts "Insert number of players" -num_players = gets.chomp.to_i +# puts "Insert number of players" +# num_players = gets.chomp.to_i -player_results = [] -num_players.times do - player_word = {} - draw_letters - puts "\n\nInsert your Adagram: " - input = gets.chomp.downcase - uses_available_letters?(input, @hand) - total_points = score_word(input) - player_word[:word] = input - player_word[:score] = total_points - player_results << player_word -end +# player_results = [] +# num_players.times do +# player_word = {} +# draw_letters +# puts "\n\nInsert your Adagram: " +# input = gets.chomp.downcase +# uses_available_letters?(input, @hand) +# total_points = score_word(input) +# player_word[:word] = input +# player_word[:score] = total_points +# player_results << player_word +# end -played_words = [] -player_results.each do |word| - played_words << word[:word] -end +# played_words = [] +# player_results.each do |word| +# played_words << word[:word] +# end -highest_score_from_words(played_words) +# highest_score_from_words(played_words) From 6f91cf00647dc89dee7d01d2da787cbb1fbef426 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 13:39:00 -0800 Subject: [PATCH 03/13] commented out erroneous code --- lib/adagrams.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 157006b..739e081 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -107,8 +107,6 @@ def uses_available_letters?(input, letters_in_hand) # word.each_char do |letter| # total_points += (score_chart[letter]) - -# end # return total_points # end From 29a4055aa6894cfe81d8b41266676f7ac202047a Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 13:39:43 -0800 Subject: [PATCH 04/13] commented out erroneous code --- lib/adagrams.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 739e081..157006b 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -107,6 +107,8 @@ def uses_available_letters?(input, letters_in_hand) # word.each_char do |letter| # total_points += (score_chart[letter]) + +# end # return total_points # end From 00f9da74d9b0490ef714763f007ce5d06891644e Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 13:42:07 -0800 Subject: [PATCH 05/13] updated code to pass draw letters method and uses available letters method --- lib/adagrams.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 157006b..9258caa 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -56,14 +56,14 @@ def uses_available_letters?(input, letters_in_hand) input.chars.each do |letter| hand_count = letters_in_hand.count(letter) input_count = input.count(letter) - # if hand_count >= input_count - # n += 1 - # else - # puts "no" - # input = gets.chomp.downcase - # #accept input, start loop over - # end - # end + if hand_count >= input_count + n += 1 + else + puts "no" + input = gets.chomp.downcase + #accept input, start loop over + end + end end # puts "Congrats, your adagram is: #{input}" return input From 45193b0fd5d1a87377746b7cf23a3630fc6b530a Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 14:10:37 -0800 Subject: [PATCH 06/13] fixed uses_available_letters? method --- lib/adagrams.rb | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 9258caa..05daf97 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -43,30 +43,27 @@ def draw_letters end def uses_available_letters?(input, letters_in_hand) - # if input.length > 10 - # puts "Too many letters! Try again: " - # input = gets.chomp.downcase - # end - # until (input.chars.all? { |char| letters_in_hand.include? char }) - # puts "Don't work, try again: " - # input = gets.chomp.downcase - # end - n = 0 - unless input.chars.length == n - input.chars.each do |letter| - hand_count = letters_in_hand.count(letter) - input_count = input.count(letter) - if hand_count >= input_count - n += 1 - else - puts "no" - input = gets.chomp.downcase - #accept input, start loop over - end + if input.length > 10 + puts "Too many letters! Try again: " + input = gets.chomp.downcase + end + if (input.chars.all? { |char| letters_in_hand.include? char }) + valid_input = true + else + valid_input = false + end + + input.chars.each do |letter| + hand_count = letters_in_hand.count(letter) + input_count = input.count(letter) + if hand_count >= input_count + valid_input = true + else + valid_input = false + break end end - # puts "Congrats, your adagram is: #{input}" - return input + return valid_input end # def score_word(word) From 42042f6eb35bc6627adaa811418aa5026a9779f5 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 14:12:49 -0800 Subject: [PATCH 07/13] fixed score_word method --- lib/adagrams.rb | 81 ++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 05daf97..7bab086 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -66,48 +66,47 @@ def uses_available_letters?(input, letters_in_hand) return valid_input end -# def score_word(word) -# score_chart = { -# "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, -# } -# word.downcase! -# if word.length > 6 -# total_points = 8 -# else -# total_points = 0 -# end - -# word.each_char do |letter| -# total_points += (score_chart[letter]) +def score_word(word) + score_chart = { + "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, + } + word.downcase! + if word.length > 6 + total_points = 8 + else + total_points = 0 + end -# end -# return total_points -# end + word.each_char do |letter| + total_points += (score_chart[letter]) + end + return total_points +end def highest_score_from_words(words) highest_score = 0 From f1e8f42a18a8059c677fd143a67e399c36f42247 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 14:33:15 -0800 Subject: [PATCH 08/13] in a tie, prefers the 10-letter word --- lib/adagrams.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 7bab086..defd9e8 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -108,21 +108,21 @@ def score_word(word) return total_points end -def highest_score_from_words(words) +def highest_score_from(words) highest_score = 0 winning_word = {:word => "", :score => 0} words.each do |word| if score_word(word) > highest_score highest_score = score_word(word) - winning_word[:word] = word + winning_word[:word] = word.upcase winning_word[:score] = highest_score elsif score_word(word) == highest_score if word.length == 10 - winning_word[:word] = word - elsif word.length < winning_word[:word].length - winning_word[:word] = word - elsif word.length == winning_word[:word].length - break + winning_word[:word] = word.upcase + elsif (word.length < winning_word[:word].length) && (winning_word[:word].length != 10) + winning_word[:word] = word.upcase + # elsif word.length == winning_word[:word].length + # break end end end From 200927859faef2130fc2a6dd34ce34a17fd5cb26 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 15:00:41 -0800 Subject: [PATCH 09/13] passed all tests --- lib/adagrams.rb | 34 +++++----------------------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index defd9e8..8d2f261 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -95,7 +95,7 @@ def score_word(word) "y" => 4, "z" => 10, } - word.downcase! + word = word.downcase if word.length > 6 total_points = 8 else @@ -117,39 +117,15 @@ def highest_score_from(words) winning_word[:word] = word.upcase winning_word[:score] = highest_score elsif score_word(word) == highest_score - if word.length == 10 + if word.length == 10 && !(word.length == winning_word[:word].length) winning_word[:word] = word.upcase - elsif (word.length < winning_word[:word].length) && (winning_word[:word].length != 10) + elsif word.length == 10 && (word.length == winning_word[:word]) + winning_word[:word].downcase = winning_word[:word].downcase + elsif (word.length <= winning_word[:word].length) && (winning_word[:word].length != 10) winning_word[:word] = word.upcase - # elsif word.length == winning_word[:word].length - # break end end end # puts "The word with the highest score is: #{winning_word}" return winning_word end - -########### Starting the game ############ -# puts "Insert number of players" -# num_players = gets.chomp.to_i - -# player_results = [] -# num_players.times do -# player_word = {} -# draw_letters -# puts "\n\nInsert your Adagram: " -# input = gets.chomp.downcase -# uses_available_letters?(input, @hand) -# total_points = score_word(input) -# player_word[:word] = input -# player_word[:score] = total_points -# player_results << player_word -# end - -# played_words = [] -# player_results.each do |word| -# played_words << word[:word] -# end - -# highest_score_from_words(played_words) From 4f35479f56b586fefa9bf0a0c6afdff857dfbe6f Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 15:03:56 -0800 Subject: [PATCH 10/13] "removed erroneous comments" --- lib/adagrams.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 8d2f261..46a557e 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,7 +1,5 @@ require "pry" -# populating letter pool - def draw_letters letter_distribution = { "a" => 9, @@ -38,7 +36,6 @@ def draw_letters end end hand = letter_pool.shuffle[0..9] - # print "Here's your hand: #{hand}" return hand end @@ -126,6 +123,5 @@ def highest_score_from(words) end end end - # puts "The word with the highest score is: #{winning_word}" return winning_word end From 8dc8f70f127b26904e4211e69759b663c4b3e0ab Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 15:05:01 -0800 Subject: [PATCH 11/13] removed erroneous comments --- lib/adagrams.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 46a557e..8ad63fc 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,5 +1,7 @@ require "pry" +# populating letter pool + def draw_letters letter_distribution = { "a" => 9, @@ -123,5 +125,6 @@ def highest_score_from(words) end end end + # puts "The word with the highest score is: #{winning_word}" return winning_word end From 91e169bdd5b3be7da52ef2f54b06cbfb9368bc9a Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 15:05:11 -0800 Subject: [PATCH 12/13] removed erroneous comments --- lib/adagrams.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 8ad63fc..46a557e 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,7 +1,5 @@ require "pry" -# populating letter pool - def draw_letters letter_distribution = { "a" => 9, @@ -125,6 +123,5 @@ def highest_score_from(words) end end end - # puts "The word with the highest score is: #{winning_word}" return winning_word end From 71b78c8711cfddfeec7965cb5653edec2fe43586 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Fri, 22 Feb 2019 15:06:28 -0800 Subject: [PATCH 13/13] removed require pry --- lib/adagrams.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 46a557e..80c6bed 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,5 +1,3 @@ -require "pry" - def draw_letters letter_distribution = { "a" => 9,