From 49faceca23359e5eef71d62a9f442d2a7d0fc269 Mon Sep 17 00:00:00 2001 From: Kate Mangubat Date: Mon, 10 Feb 2020 22:23:49 -0800 Subject: [PATCH 01/13] adagrams wave 1 completed --- adagrams.rb | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 adagrams.rb diff --git a/adagrams.rb b/adagrams.rb new file mode 100644 index 0000000..7f5e54f --- /dev/null +++ b/adagrams.rb @@ -0,0 +1,144 @@ +# Our first job is to build a hand of 10 letters for the user. To do so, add a method called draw_letters in adagrams.rb. This method should have the following properties: + +# No parameters +# Returns an array of ten strings + # Each string should contain exactly one letter + # These represent the hand of letters that the player has drawn +# The letters should be randomly drawn from a pool of letters + # This letter pool should reflect the distribution of letters as described in the table below + # There are only 2 available C letters, so draw_letters cannot ever return more than 2 Cs + # Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z +# Invoking this method should not change the pool of letters (USING METHOD CALLED ARRAY.SAMPLE HERE) + # Imagine that the user returns their hand to the pool before drawing new letters + +LETTER_INFO ={ + "A" => { + :qty => 9, + :score => 1 + }, + "B" => { + :qty => 2, + :score => 3 + }, + "C" => { + :qty => 2, + :score => 3 + }, + "D" => { + :qty => 4, + :score => 2 + }, + "E" => { + :qty => 12, + :score => 1 + }, + "F" => { + :qty => 2, + :score => 4 + }, + "G" => { + :qty => 3, + :score => 2 + }, + "H" => { + :qty => 2, + :score => 4 + }, + "I" => { + :qty => 9, + :score => 1 + }, + "J" => { + :qty => 1, + :score => 8 + }, + "K" => { + :qty => 1, + :score => 5 + }, + "L" => { + :qty => 4, + :score => 1 + }, + "M" => { + :qty => 2, + :score => 3 + }, + "N" => { + :qty => 6, + :score => 1 + }, + "O" => { + :qty => 8, + :score => 1 + }, + "P" => { + :qty => 2, + :score => 3 + }, + "Q" => { + :qty => 1, + :score => 10 + }, + "R" => { + :qty => 6, + :score => 1 + }, + "S" => { + :qty => 4, + :score => 1 + }, + "T" => { + :qty => 6, + :score => 1 + }, + "U" => { + :qty => 4, + :score => 1 + }, + "V" => { + :qty => 2, + :score => 4 + }, + "W" => { + :qty => 2, + :score => 4 + }, + "X" => { + :qty => 1, + :score => 8 + }, + "Y" => { + :qty => 2, + :score => 4 + }, + "Z" => { + :qty => 1, + :score => 10 + } +} + +#Global Variables to use later outside the scope +HAND_SIZE = 10 #Setting hand size to global variable will let us manually change it to a different size in the future + +#Creating letter pool array by looping through letter info hash. +LETTER_POOL = [] #Made this a global variable LETTER_POOL in order to use outside of this block of code and our functions can use it +LETTER_INFO.each do |key, value| + number_of_letters = value[:qty] + i = 0 + while i < number_of_letters do + LETTER_POOL.push(key) + i +=1 + end +end + +# puts $letter_pool + +def draw_letters() #Global variable LETTER_POOL would need to be passed as a paramter but reqs said no parameters so made it global + letters = [] #array of letters + letters = LETTER_POOL.sample(HAND_SIZE) #using .sample to randomly select letters WITHOUT changing letter_pool + return letters #returning the array of letters /// can also simply return LETTER_POOL.sample(10) +end + +puts draw_letters() + From 4ec75a74cb80c3b365b9d8846bf0389f297559a1 Mon Sep 17 00:00:00 2001 From: Kate Mangubat Date: Tue, 11 Feb 2020 16:16:01 -0800 Subject: [PATCH 02/13] current adagrams.rb from415pm --- .vscode/launch.json | 14 ++++ adagrams.rb | 144 --------------------------------- lib/adagrams.rb | 189 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 203 insertions(+), 144 deletions(-) create mode 100644 .vscode/launch.json delete mode 100644 adagrams.rb diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2207bc3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Debug Local File", + "type": "Ruby", + "request": "launch", + "program": "${file}" + } + ] +} \ No newline at end of file diff --git a/adagrams.rb b/adagrams.rb deleted file mode 100644 index 7f5e54f..0000000 --- a/adagrams.rb +++ /dev/null @@ -1,144 +0,0 @@ -# Our first job is to build a hand of 10 letters for the user. To do so, add a method called draw_letters in adagrams.rb. This method should have the following properties: - -# No parameters -# Returns an array of ten strings - # Each string should contain exactly one letter - # These represent the hand of letters that the player has drawn -# The letters should be randomly drawn from a pool of letters - # This letter pool should reflect the distribution of letters as described in the table below - # There are only 2 available C letters, so draw_letters cannot ever return more than 2 Cs - # Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z -# Invoking this method should not change the pool of letters (USING METHOD CALLED ARRAY.SAMPLE HERE) - # Imagine that the user returns their hand to the pool before drawing new letters - -LETTER_INFO ={ - "A" => { - :qty => 9, - :score => 1 - }, - "B" => { - :qty => 2, - :score => 3 - }, - "C" => { - :qty => 2, - :score => 3 - }, - "D" => { - :qty => 4, - :score => 2 - }, - "E" => { - :qty => 12, - :score => 1 - }, - "F" => { - :qty => 2, - :score => 4 - }, - "G" => { - :qty => 3, - :score => 2 - }, - "H" => { - :qty => 2, - :score => 4 - }, - "I" => { - :qty => 9, - :score => 1 - }, - "J" => { - :qty => 1, - :score => 8 - }, - "K" => { - :qty => 1, - :score => 5 - }, - "L" => { - :qty => 4, - :score => 1 - }, - "M" => { - :qty => 2, - :score => 3 - }, - "N" => { - :qty => 6, - :score => 1 - }, - "O" => { - :qty => 8, - :score => 1 - }, - "P" => { - :qty => 2, - :score => 3 - }, - "Q" => { - :qty => 1, - :score => 10 - }, - "R" => { - :qty => 6, - :score => 1 - }, - "S" => { - :qty => 4, - :score => 1 - }, - "T" => { - :qty => 6, - :score => 1 - }, - "U" => { - :qty => 4, - :score => 1 - }, - "V" => { - :qty => 2, - :score => 4 - }, - "W" => { - :qty => 2, - :score => 4 - }, - "X" => { - :qty => 1, - :score => 8 - }, - "Y" => { - :qty => 2, - :score => 4 - }, - "Z" => { - :qty => 1, - :score => 10 - } -} - -#Global Variables to use later outside the scope -HAND_SIZE = 10 #Setting hand size to global variable will let us manually change it to a different size in the future - -#Creating letter pool array by looping through letter info hash. -LETTER_POOL = [] #Made this a global variable LETTER_POOL in order to use outside of this block of code and our functions can use it -LETTER_INFO.each do |key, value| - number_of_letters = value[:qty] - i = 0 - while i < number_of_letters do - LETTER_POOL.push(key) - i +=1 - end -end - -# puts $letter_pool - -def draw_letters() #Global variable LETTER_POOL would need to be passed as a paramter but reqs said no parameters so made it global - letters = [] #array of letters - letters = LETTER_POOL.sample(HAND_SIZE) #using .sample to randomly select letters WITHOUT changing letter_pool - return letters #returning the array of letters /// can also simply return LETTER_POOL.sample(10) -end - -puts draw_letters() - diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..85ffd9b 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,189 @@ +# Our first job is to build a hand of 10 letters for the user. To do so, add a method called draw_letters in adagrams.rb. This method should have the following properties: + +# No parameters +# Returns an array of ten strings + # Each string should contain exactly one letter + # These represent the hand of letters that the player has drawn +# The letters should be randomly drawn from a pool of letters + # This letter pool should reflect the distribution of letters as described in the table below + # There are only 2 available C letters, so draw_letters cannot ever return more than 2 Cs + # Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z +# Invoking this method should not change the pool of letters (USING METHOD CALLED ARRAY.SAMPLE HERE) + # Imagine that the user returns their hand to the pool before drawing new letters + +LETTER_INFO ={ + "A" => { + :qty => 9, + :score => 1 + }, + "B" => { + :qty => 2, + :score => 3 + }, + "C" => { + :qty => 2, + :score => 3 + }, + "D" => { + :qty => 4, + :score => 2 + }, + "E" => { + :qty => 12, + :score => 1 + }, + "F" => { + :qty => 2, + :score => 4 + }, + "G" => { + :qty => 3, + :score => 2 + }, + "H" => { + :qty => 2, + :score => 4 + }, + "I" => { + :qty => 9, + :score => 1 + }, + "J" => { + :qty => 1, + :score => 8 + }, + "K" => { + :qty => 1, + :score => 5 + }, + "L" => { + :qty => 4, + :score => 1 + }, + "M" => { + :qty => 2, + :score => 3 + }, + "N" => { + :qty => 6, + :score => 1 + }, + "O" => { + :qty => 8, + :score => 1 + }, + "P" => { + :qty => 2, + :score => 3 + }, + "Q" => { + :qty => 1, + :score => 10 + }, + "R" => { + :qty => 6, + :score => 1 + }, + "S" => { + :qty => 4, + :score => 1 + }, + "T" => { + :qty => 6, + :score => 1 + }, + "U" => { + :qty => 4, + :score => 1 + }, + "V" => { + :qty => 2, + :score => 4 + }, + "W" => { + :qty => 2, + :score => 4 + }, + "X" => { + :qty => 1, + :score => 8 + }, + "Y" => { + :qty => 2, + :score => 4 + }, + "Z" => { + :qty => 1, + :score => 10 + } +} + +#Global Variables to use later outside the scope +HAND_SIZE = 10 #Setting hand size to global variable will let us manually change it to a different size in the future + +#Creating letter pool array by looping through letter info hash. +LETTER_POOL = [] #Made this a global variable LETTER_POOL in order to use outside of this block of code and our functions can use it +LETTER_INFO.each do |key, value| + number_of_letters = value[:qty] + i = 0 + while i < number_of_letters do + LETTER_POOL.push(key) + i +=1 + end +end + +# puts $letter_pool + +def draw_letters() #Global variable LETTER_POOL would need to be passed as a paramter but reqs said no parameters so made it global + letters_in_hand = [] #array of letters + letters_in_hand = LETTER_POOL.sample(HAND_SIZE) #using .sample to randomly select letters WITHOUT changing letter_pool + return letters_in_hand #returning the array of letters /// can also simply return LETTER_POOL.sample(10) +end + +letters_in_hand = draw_letters() + +puts letters_in_hand + +# Wave 2 +# Next, we need a way to check if an input word (a word a player submits) only uses characters that are contained within a collection (or hand) of drawn letters. Essentially, we need a way to check if the word is, indeed, an anagram of some or all of the given letters in the hand. + +# To do so, add a method called uses_available_letters? in adagrams.rb. This method should have the following properties: + +# Has two parameters: + # input, the first parameter, describes some input word, and is a string + # letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter +# Returns either true or false +# Returns true if every letter in the input word is available (in the right quantities) in the letters_in_hand +# Returns false if not; if there is a letter in input that is not present in the letters_in_hand or has too much of compared to the letters_in_hand + +puts "What's your word?" +user_input = gets.chomp.upcase + +def uses_available_letters(letters, hand) + word_characters = letters.split("") + puts word_characters + word_characters.each do |letter| + puts letter + if !hand.include?(letter) + return false + end + end + return true +end + +puts uses_available_letters(user_input, letters_in_hand) + +# Wave 3 +# We want a method that returns the score of a given word as defined by the Adagrams game. + +# Name this method score_word in adagrams.rb. This method should have the following properties: + +# Has one parameter: word, which is a string of characters +# Returns an integer representing the number of points +# Each letter within word has a point value. The number of points of each letter is summed up to represent the total score of word +# Each letter's point value is described in the table below +# If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points + +def score_word (word) + return +end \ No newline at end of file From 8d4afd6e64c4ad0f565c0203da74b379a682a8c7 Mon Sep 17 00:00:00 2001 From: Kate Mangubat Date: Tue, 11 Feb 2020 23:14:34 -0800 Subject: [PATCH 03/13] Wave 3 complete version as of 11:14pm --- lib/adagrams.rb | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 85ffd9b..4dd2d35 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -158,20 +158,22 @@ def draw_letters() #Global variable LETTER_POOL would need to be passed as a par puts "What's your word?" user_input = gets.chomp.upcase - -def uses_available_letters(letters, hand) - word_characters = letters.split("") - puts word_characters +your_words = [] +def uses_available_letters(input, letters_in_hand) + word_characters = input.split("") word_characters.each do |letter| - puts letter - if !hand.include?(letter) - return false + if !letters_in_hand.include?(letter) + puts "You're cheating!" + return false + else + letters_in_hand.delete_at(letters_in_hand.index(letter)) + # your_words << input end end return true end -puts uses_available_letters(user_input, letters_in_hand) +isUserInputValid = uses_available_letters(user_input, letters_in_hand) # Wave 3 # We want a method that returns the score of a given word as defined by the Adagrams game. @@ -185,5 +187,18 @@ def uses_available_letters(letters, hand) # If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points def score_word (word) - return -end \ No newline at end of file + score = 0 + if (word.length >= 7) && (word.length <= 10) + score = 8 + end + word.split("").each do |letter| + score = LETTER_INFO[letter][:score] + score + end + return score +end + +if isUserInputValid + puts "You're score is #{score_word(user_input)}." +else + puts "Your input is not valid, therefore not scored." +end From fbd533fd12f6b29b11ebd8a1f542066089b99a59 Mon Sep 17 00:00:00 2001 From: Kate Mangubat Date: Tue, 11 Feb 2020 23:46:35 -0800 Subject: [PATCH 04/13] added Optional section to adagrams --- .vscode/launch.json | 8 ++++++++ lib/adagrams.rb | 24 +++++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 2207bc3..eba80f5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,14 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Listen for rdebug-ide", + "type": "Ruby", + "request": "attach", + "remoteHost": "127.0.0.1", + "remotePort": "1234", + "remoteWorkspaceRoot": "${workspaceRoot}" + }, { "name": "Debug Local File", "type": "Ruby", diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 4dd2d35..6672d39 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -159,7 +159,7 @@ def draw_letters() #Global variable LETTER_POOL would need to be passed as a par puts "What's your word?" user_input = gets.chomp.upcase your_words = [] -def uses_available_letters(input, letters_in_hand) +def uses_available_letters?(input, letters_in_hand) word_characters = input.split("") word_characters.each do |letter| if !letters_in_hand.include?(letter) @@ -173,7 +173,7 @@ def uses_available_letters(input, letters_in_hand) return true end -isUserInputValid = uses_available_letters(user_input, letters_in_hand) +isUserInputValid = uses_available_letters?(user_input, letters_in_hand) # Wave 3 # We want a method that returns the score of a given word as defined by the Adagrams game. @@ -196,9 +196,27 @@ def score_word (word) end return score end - +#Checking ONE time, prints only if true. if isUserInputValid puts "You're score is #{score_word(user_input)}." else puts "Your input is not valid, therefore not scored." end + + + +#Optional +require 'csv' +ENGLISH_DICTIONARY = CSV.parse(File.read("../assets/dictionary-english.csv"), headers: true) + +def is_in_english_dict? (input) + #https://www.rubyguides.com/2018/10/parse-csv-ruby/ + #method looking at the column which is list of words in the dictionary + return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) +end +#Checking ONE time, prints only if true. +if isUserInputValid + puts "Your word is in the Engligh Dictionary: #{is_in_english_dict?(user_input)}." +else + puts "Your input is not valid, therefore not checked in English dictionary." +end \ No newline at end of file From 0672fac86b69abd774c0d26a9846e0f07161c549 Mon Sep 17 00:00:00 2001 From: thenora Date: Wed, 12 Feb 2020 10:20:01 -0800 Subject: [PATCH 05/13] commented out the optional to make tests work --- lib/adagrams.rb | 204 ++++++++++++++++++++++++------------------------ 1 file changed, 102 insertions(+), 102 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 6672d39..3589ea7 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -2,120 +2,120 @@ # No parameters # Returns an array of ten strings - # Each string should contain exactly one letter - # These represent the hand of letters that the player has drawn +# Each string should contain exactly one letter +# These represent the hand of letters that the player has drawn # The letters should be randomly drawn from a pool of letters - # This letter pool should reflect the distribution of letters as described in the table below - # There are only 2 available C letters, so draw_letters cannot ever return more than 2 Cs - # Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z +# This letter pool should reflect the distribution of letters as described in the table below +# There are only 2 available C letters, so draw_letters cannot ever return more than 2 Cs +# Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z # Invoking this method should not change the pool of letters (USING METHOD CALLED ARRAY.SAMPLE HERE) - # Imagine that the user returns their hand to the pool before drawing new letters +# Imagine that the user returns their hand to the pool before drawing new letters -LETTER_INFO ={ +LETTER_INFO = { "A" => { - :qty => 9, - :score => 1 + :qty => 9, + :score => 1, }, "B" => { - :qty => 2, - :score => 3 + :qty => 2, + :score => 3, }, "C" => { - :qty => 2, - :score => 3 + :qty => 2, + :score => 3, }, "D" => { - :qty => 4, - :score => 2 + :qty => 4, + :score => 2, }, "E" => { - :qty => 12, - :score => 1 + :qty => 12, + :score => 1, }, "F" => { - :qty => 2, - :score => 4 + :qty => 2, + :score => 4, }, "G" => { - :qty => 3, - :score => 2 + :qty => 3, + :score => 2, }, "H" => { - :qty => 2, - :score => 4 + :qty => 2, + :score => 4, }, "I" => { - :qty => 9, - :score => 1 + :qty => 9, + :score => 1, }, "J" => { - :qty => 1, - :score => 8 + :qty => 1, + :score => 8, }, "K" => { - :qty => 1, - :score => 5 + :qty => 1, + :score => 5, }, "L" => { - :qty => 4, - :score => 1 + :qty => 4, + :score => 1, }, "M" => { - :qty => 2, - :score => 3 + :qty => 2, + :score => 3, }, "N" => { - :qty => 6, - :score => 1 + :qty => 6, + :score => 1, }, "O" => { - :qty => 8, - :score => 1 + :qty => 8, + :score => 1, }, "P" => { - :qty => 2, - :score => 3 + :qty => 2, + :score => 3, }, "Q" => { - :qty => 1, - :score => 10 + :qty => 1, + :score => 10, }, "R" => { - :qty => 6, - :score => 1 + :qty => 6, + :score => 1, }, "S" => { - :qty => 4, - :score => 1 + :qty => 4, + :score => 1, }, "T" => { - :qty => 6, - :score => 1 + :qty => 6, + :score => 1, }, "U" => { - :qty => 4, - :score => 1 + :qty => 4, + :score => 1, }, "V" => { - :qty => 2, - :score => 4 + :qty => 2, + :score => 4, }, "W" => { - :qty => 2, - :score => 4 + :qty => 2, + :score => 4, }, "X" => { - :qty => 1, - :score => 8 + :qty => 1, + :score => 8, }, "Y" => { - :qty => 2, - :score => 4 + :qty => 2, + :score => 4, }, "Z" => { - :qty => 1, - :score => 10 - } + :qty => 1, + :score => 10, + }, } #Global Variables to use later outside the scope @@ -126,9 +126,9 @@ LETTER_INFO.each do |key, value| number_of_letters = value[:qty] i = 0 - while i < number_of_letters do - LETTER_POOL.push(key) - i +=1 + while i < number_of_letters + LETTER_POOL.push(key) + i += 1 end end @@ -150,8 +150,8 @@ def draw_letters() #Global variable LETTER_POOL would need to be passed as a par # To do so, add a method called uses_available_letters? in adagrams.rb. This method should have the following properties: # Has two parameters: - # input, the first parameter, describes some input word, and is a string - # letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter +# input, the first parameter, describes some input word, and is a string +# letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter # Returns either true or false # Returns true if every letter in the input word is available (in the right quantities) in the letters_in_hand # Returns false if not; if there is a letter in input that is not present in the letters_in_hand or has too much of compared to the letters_in_hand @@ -159,18 +159,19 @@ def draw_letters() #Global variable LETTER_POOL would need to be passed as a par puts "What's your word?" user_input = gets.chomp.upcase your_words = [] + def uses_available_letters?(input, letters_in_hand) - word_characters = input.split("") - word_characters.each do |letter| - if !letters_in_hand.include?(letter) - puts "You're cheating!" - return false - else - letters_in_hand.delete_at(letters_in_hand.index(letter)) - # your_words << input - end + word_characters = input.split("") + word_characters.each do |letter| + if !letters_in_hand.include?(letter) + puts "You're cheating!" + return false + else + letters_in_hand.delete_at(letters_in_hand.index(letter)) + # your_words << input end - return true + end + return true end isUserInputValid = uses_available_letters?(user_input, letters_in_hand) @@ -186,37 +187,36 @@ def uses_available_letters?(input, letters_in_hand) # Each letter's point value is described in the table below # If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points -def score_word (word) - score = 0 - if (word.length >= 7) && (word.length <= 10) - score = 8 - end - word.split("").each do |letter| - score = LETTER_INFO[letter][:score] + score - end - return score +def score_word(word) + score = 0 + if (word.length >= 7) && (word.length <= 10) + score = 8 + end + word.split("").each do |letter| + score = LETTER_INFO[letter][:score] + score + end + return score end + #Checking ONE time, prints only if true. if isUserInputValid - puts "You're score is #{score_word(user_input)}." + puts "You're score is #{score_word(user_input)}." else - puts "Your input is not valid, therefore not scored." + puts "Your input is not valid, therefore not scored." end - - #Optional -require 'csv' -ENGLISH_DICTIONARY = CSV.parse(File.read("../assets/dictionary-english.csv"), headers: true) - -def is_in_english_dict? (input) - #https://www.rubyguides.com/2018/10/parse-csv-ruby/ - #method looking at the column which is list of words in the dictionary - return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) -end -#Checking ONE time, prints only if true. -if isUserInputValid - puts "Your word is in the Engligh Dictionary: #{is_in_english_dict?(user_input)}." -else - puts "Your input is not valid, therefore not checked in English dictionary." -end \ No newline at end of file +# require 'csv' +# ENGLISH_DICTIONARY = CSV.parse(File.read("../assets/dictionary-english.csv"), headers: true) + +# def is_in_english_dict? (input) +# #https://www.rubyguides.com/2018/10/parse-csv-ruby/ +# #method looking at the column which is list of words in the dictionary +# return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) +# end +# #Checking ONE time, prints only if true. +# if isUserInputValid +# puts "Your word is in the Engligh Dictionary: #{is_in_english_dict?(user_input)}." +# else +# puts "Your input is not valid, therefore not checked in English dictionary." +# end From e0a7a59cf6e5d29184683b632e8a5eba69ff714a Mon Sep 17 00:00:00 2001 From: thenora Date: Wed, 12 Feb 2020 10:42:44 -0800 Subject: [PATCH 06/13] added .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b4d43e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +secrets.txt \ No newline at end of file From 284ef5e065c28fc54c0af616d977822585fe59c8 Mon Sep 17 00:00:00 2001 From: Kate Mangubat Date: Wed, 12 Feb 2020 15:50:47 -0800 Subject: [PATCH 07/13] corrected wave 2 code to pass test to not change letters in hand --- .github/PULL_REQUEST_TEMPLATE | 12 ++++++------ lib/adagrams.rb | 13 ++++++++----- test/adagrams_test.rb | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE index fbaf41c..eaa2ebb 100644 --- a/.github/PULL_REQUEST_TEMPLATE +++ b/.github/PULL_REQUEST_TEMPLATE @@ -6,9 +6,9 @@ Congratulations! You're submitting your assignment. Please reflect on the assign Feature | Feedback --- | --- -What are the components that make up a method? | -What are the advantages of using git when collaboratively working on one code base? | -What kind of relationship did you and your pair have with the unit tests? | -Does your code use any methods from the `Enumerable` mixin? If so, where and why was it helpful? | -What was one method you and your pair used to debug code? | -What are two discussion points that you and your pair discussed when giving/receiving feedback from each other that you would be willing to share? | +What are the components that make up a method? | The structure of a method is indicated by "def/return/end". The def contains the signature of the method that describes the action/purpose of the method with the parameters, or variables we are going to use inside our block of code that lives in the method. Need to be cognizant that the variable we define in the signature matches what is used in the block. The return allows you to exit the method and gives us the results. If a return is not indicated, then the return will be nil. To use the method, we need to invoke it by calling the method and supplying an argumnet for that particular parameter. | +What are the advantages of using git when collaboratively working on one code base? | Using git helped keep track of changes that either collaborator made. Instead of emailing files back and forth or cutting and pasting via Slack, the entire file lives in a safe house rather than locally where things are bound to happen. It is especially useful to be able to go back to previous versions that worked at the end of the night when the current morning version does not appear to be working. | +What kind of relationship did you and your pair have with the unit tests? | If speaking specifically to the waves of the project, we had a learning moment to make sure the tests were running at each wave. While the code was being written, being aware of the variable names was also important in developing the rest of the code. Additionally, since it was the first project, it was a "getting to know you, your style of tackling group work, and agreeing on the flow of how to carry out the project" which functioned well. Being open, respectful, and receptive of a partner's working style, personality, and understanding their guess vs ask tendancies helped to navigate this new journey. | +Does your code use any methods from the `Enumerable` mixin? If so, where and why was it helpful? | Yes, we used .include? because it allowed us to supply a return true if the given string from the user's word contains the given characters in the list supplied. | +What was one method you and your pair used to debug code? | We utilized the error messages provided in the terminal to track our errors. In the future, we would like to be more proficient in using the Debugger which would be useful in seeing what is happening in our data structures and loops. | +What are two discussion points that you and your pair discussed when giving/receiving feedback from each other that you would be willing to share? | diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 6672d39..91e0580 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -159,14 +159,16 @@ def draw_letters() #Global variable LETTER_POOL would need to be passed as a par puts "What's your word?" user_input = gets.chomp.upcase your_words = [] + def uses_available_letters?(input, letters_in_hand) + current_hand = letters_in_hand.dup word_characters = input.split("") word_characters.each do |letter| - if !letters_in_hand.include?(letter) + if !current_hand.include?(letter) puts "You're cheating!" return false else - letters_in_hand.delete_at(letters_in_hand.index(letter)) + current_hand.delete_at(current_hand.index(letter)) # your_words << input end end @@ -187,18 +189,19 @@ def uses_available_letters?(input, letters_in_hand) # If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points def score_word (word) + word score = 0 if (word.length >= 7) && (word.length <= 10) score = 8 end word.split("").each do |letter| - score = LETTER_INFO[letter][:score] + score + score = LETTER_INFO[letter.upcase][:score] + score end return score end #Checking ONE time, prints only if true. if isUserInputValid - puts "You're score is #{score_word(user_input)}." + puts "Your score is #{score_word(user_input)}." else puts "Your input is not valid, therefore not scored." end @@ -207,7 +210,7 @@ def score_word (word) #Optional require 'csv' -ENGLISH_DICTIONARY = CSV.parse(File.read("../assets/dictionary-english.csv"), headers: true) +ENGLISH_DICTIONARY = CSV.parse(File.read("/Users/katemangubat/Developer/projects/adagrams/assets/dictionary-english.csv"), headers: true) def is_in_english_dict? (input) #https://www.rubyguides.com/2018/10/parse-csv-ruby/ diff --git a/test/adagrams_test.rb b/test/adagrams_test.rb index 90ec44d..e1dfd42 100644 --- a/test/adagrams_test.rb +++ b/test/adagrams_test.rb @@ -90,7 +90,7 @@ expect(score_word("XXXXXXXXX")).must_equal 80 end end - +#WAVE 4 tests 12-18 describe 'highest_score_from method' do it 'returns a hash that contains the word and score of best word in an array' do words = ['X', 'XX', 'XXX', 'XXXX'] From 80c288b4f2dfdf8b7adebe5d0e6c7f65b401df9a Mon Sep 17 00:00:00 2001 From: Kate Mangubat Date: Wed, 12 Feb 2020 16:19:12 -0800 Subject: [PATCH 08/13] corrected csv library issue 420pm --- lib/adagrams.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 91e0580..d750b2b 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -210,7 +210,7 @@ def score_word (word) #Optional require 'csv' -ENGLISH_DICTIONARY = CSV.parse(File.read("/Users/katemangubat/Developer/projects/adagrams/assets/dictionary-english.csv"), headers: true) +ENGLISH_DICTIONARY = CSV.parse(File.read("./assets/dictionary-english.csv"), headers: true) def is_in_english_dict? (input) #https://www.rubyguides.com/2018/10/parse-csv-ruby/ From c249a1e10185c077e58f7fb15143c30f42e54da0 Mon Sep 17 00:00:00 2001 From: thenora Date: Wed, 12 Feb 2020 19:31:41 -0800 Subject: [PATCH 09/13] added collection of approved word, and some new game functionality --- lib/adagrams.rb | 151 +++++++++++++++++++++++++----------------------- 1 file changed, 79 insertions(+), 72 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index cb45e66..33c88b1 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -132,94 +132,101 @@ end end -# puts $letter_pool - -def draw_letters() #Global variable LETTER_POOL would need to be passed as a paramter but reqs said no parameters so made it global - letters_in_hand = [] #array of letters - letters_in_hand = LETTER_POOL.sample(HAND_SIZE) #using .sample to randomly select letters WITHOUT changing letter_pool - return letters_in_hand #returning the array of letters /// can also simply return LETTER_POOL.sample(10) -end +def play_game + puts "Welcome to Adagrams." + puts "Try to make words with your letters." + puts "Your letters are:" + + def draw_letters() #Global variable LETTER_POOL would need to be passed as a paramter but reqs said no parameters so made it global + letters_in_hand = [] #array of letters + letters_in_hand = LETTER_POOL.sample(HAND_SIZE) #using .sample to randomly select letters WITHOUT changing letter_pool + return letters_in_hand #returning the array of letters /// can also simply return LETTER_POOL.sample(10) + end -letters_in_hand = draw_letters() + letters_in_hand = draw_letters() -puts letters_in_hand + puts letters_in_hand.join -# Wave 2 -# Next, we need a way to check if an input word (a word a player submits) only uses characters that are contained within a collection (or hand) of drawn letters. Essentially, we need a way to check if the word is, indeed, an anagram of some or all of the given letters in the hand. + # Wave 2 + # Next, we need a way to check if an input word (a word a player submits) only uses characters that are contained within a collection (or hand) of drawn letters. Essentially, we need a way to check if the word is, indeed, an anagram of some or all of the given letters in the hand. -# To do so, add a method called uses_available_letters? in adagrams.rb. This method should have the following properties: + # To do so, add a method called uses_available_letters? in adagrams.rb. This method should have the following properties: -# Has two parameters: -# input, the first parameter, describes some input word, and is a string -# letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter -# Returns either true or false -# Returns true if every letter in the input word is available (in the right quantities) in the letters_in_hand -# Returns false if not; if there is a letter in input that is not present in the letters_in_hand or has too much of compared to the letters_in_hand + # Has two parameters: + # input, the first parameter, describes some input word, and is a string + # letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter + # Returns either true or false + # Returns true if every letter in the input word is available (in the right quantities) in the letters_in_hand + # Returns false if not; if there is a letter in input that is not present in the letters_in_hand or has too much of compared to the letters_in_hand -puts "What's your word?" -user_input = gets.chomp.upcase -@words = [] + puts "What's your word?" + user_input = gets.chomp.upcase + @your_words = [] -def uses_available_letters?(input, letters_in_hand) - current_hand = letters_in_hand.dup - word_characters = input.split("") - word_characters.each do |letter| - if !current_hand.include?(letter) - puts "You're cheating!" - return false - else - current_hand.delete_at(current_hand.index(letter)) + def uses_available_letters?(input, letters_in_hand) + current_hand = letters_in_hand.dup + word_characters = input.split("") + word_characters.each do |letter| + if !current_hand.include?(letter) + puts "You're cheating!" + return false + else + current_hand.delete_at(current_hand.index(letter)) + end end + return true end - @words << input - return true -end -isUserInputValid = uses_available_letters?(user_input, letters_in_hand) + isUserInputValid = uses_available_letters?(user_input, letters_in_hand) -# Wave 3 -# We want a method that returns the score of a given word as defined by the Adagrams game. + # Wave 3 + # We want a method that returns the score of a given word as defined by the Adagrams game. -# Name this method score_word in adagrams.rb. This method should have the following properties: + # Name this method score_word in adagrams.rb. This method should have the following properties: -# Has one parameter: word, which is a string of characters -# Returns an integer representing the number of points -# Each letter within word has a point value. The number of points of each letter is summed up to represent the total score of word -# Each letter's point value is described in the table below -# If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points + # Has one parameter: word, which is a string of characters + # Returns an integer representing the number of points + # Each letter within word has a point value. The number of points of each letter is summed up to represent the total score of word + # Each letter's point value is described in the table below + # If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points -def score_word(word) - word - score = 0 - if (word.length >= 7) && (word.length <= 10) - score = 8 + def score_word(word) + word + score = 0 + if (word.length >= 7) && (word.length <= 10) + score = 8 + end + word.split("").each do |letter| + score = LETTER_INFO[letter.upcase][:score] + score + end + return score end - word.split("").each do |letter| - score = LETTER_INFO[letter.upcase][:score] + score + + words = [] + #Checking ONE time, prints only if true. + if isUserInputValid + puts "Your score is #{score_word(user_input)}." + words << user_input + else + puts "Your input is not valid, therefore not scored." end - return score -end -#Checking ONE time, prints only if true. -if isUserInputValid - puts "Your score is #{score_word(user_input)}." -else - puts "Your input is not valid, therefore not scored." + #Optional + require "csv" + # ENGLISH_DICTIONARY = CSV.parse(File.read("./assets/dictionary-english.csv"), headers: true) + + # def is_in_english_dict?(input) + # #https://www.rubyguides.com/2018/10/parse-csv-ruby/ + # #method looking at the column which is list of words in the dictionary + # return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) + # end + + # #Checking ONE time, prints only if true. + # if isUserInputValid + # puts "Your word is in the Engligh Dictionary: #{is_in_english_dict?(user_input)}." + # else + # puts "Your input is not valid, therefore not checked in English dictionary." + # end end -# #Optional -# require "csv" -# ENGLISH_DICTIONARY = CSV.parse(File.read("./assets/dictionary-english.csv"), headers: true) - -# def is_in_english_dict?(input) -# #https://www.rubyguides.com/2018/10/parse-csv-ruby/ -# #method looking at the column which is list of words in the dictionary -# return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) -# end - -# #Checking ONE time, prints only if true. -# if isUserInputValid -# puts "Your word is in the Engligh Dictionary: #{is_in_english_dict?(user_input)}." -# else -# puts "Your input is not valid, therefore not checked in English dictionary." -# end +play_game From c1e5c8d7744541744d21390f83a5457f880045d2 Mon Sep 17 00:00:00 2001 From: Kate Mangubat Date: Thu, 13 Feb 2020 15:11:47 -0800 Subject: [PATCH 10/13] completed ADAgrams code 311pm --- lib/adagrams.rb | 211 +++++++++++++++++++++++++++++------------------- 1 file changed, 129 insertions(+), 82 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 33c88b1..a80a7f7 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -1,16 +1,5 @@ -# Our first job is to build a hand of 10 letters for the user. To do so, add a method called draw_letters in adagrams.rb. This method should have the following properties: - -# No parameters -# Returns an array of ten strings -# Each string should contain exactly one letter -# These represent the hand of letters that the player has drawn -# The letters should be randomly drawn from a pool of letters -# This letter pool should reflect the distribution of letters as described in the table below -# There are only 2 available C letters, so draw_letters cannot ever return more than 2 Cs -# Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z -# Invoking this method should not change the pool of letters (USING METHOD CALLED ARRAY.SAMPLE HERE) -# Imagine that the user returns their hand to the pool before drawing new letters - +# Globals +HAND_SIZE = 10 #Setting hand size to global variable will let us manually change it to a different size in the future LETTER_INFO = { "A" => { :qty => 9, @@ -118,9 +107,6 @@ }, } -#Global Variables to use later outside the scope -HAND_SIZE = 10 #Setting hand size to global variable will let us manually change it to a different size in the future - #Creating letter pool array by looping through letter info hash. LETTER_POOL = [] #Made this a global variable LETTER_POOL in order to use outside of this block of code and our functions can use it LETTER_INFO.each do |key, value| @@ -132,26 +118,29 @@ end end -def play_game - puts "Welcome to Adagrams." - puts "Try to make words with your letters." - puts "Your letters are:" +# Methods - def draw_letters() #Global variable LETTER_POOL would need to be passed as a paramter but reqs said no parameters so made it global +# Wave 1 +# Our first job is to build a hand of 10 letters for the user. To do so, add a method called draw_letters in adagrams.rb. This method should have the following properties: +# No parameters +# Returns an array of ten strings +# Each string should contain exactly one letter +# These represent the hand of letters that the player has drawn +# The letters should be randomly drawn from a pool of letters +# This letter pool should reflect the distribution of letters as described in the table below +# There are only 2 available C letters, so draw_letters cannot ever return more than 2 Cs +# Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z +# Invoking this method should not change the pool of letters (USING METHOD CALLED ARRAY.SAMPLE HERE) +# Imagine that the user returns their hand to the pool before drawing new letters +def draw_letters() #Global variable LETTER_POOL would need to be passed as a paramter but reqs said no parameters so made it global letters_in_hand = [] #array of letters letters_in_hand = LETTER_POOL.sample(HAND_SIZE) #using .sample to randomly select letters WITHOUT changing letter_pool return letters_in_hand #returning the array of letters /// can also simply return LETTER_POOL.sample(10) - end - - letters_in_hand = draw_letters() - - puts letters_in_hand.join +end - # Wave 2 +# Wave 2 # Next, we need a way to check if an input word (a word a player submits) only uses characters that are contained within a collection (or hand) of drawn letters. Essentially, we need a way to check if the word is, indeed, an anagram of some or all of the given letters in the hand. - # To do so, add a method called uses_available_letters? in adagrams.rb. This method should have the following properties: - # Has two parameters: # input, the first parameter, describes some input word, and is a string # letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter @@ -159,74 +148,132 @@ def draw_letters() #Global variable LETTER_POOL would need to be passed as a par # Returns true if every letter in the input word is available (in the right quantities) in the letters_in_hand # Returns false if not; if there is a letter in input that is not present in the letters_in_hand or has too much of compared to the letters_in_hand - puts "What's your word?" - user_input = gets.chomp.upcase - @your_words = [] - - def uses_available_letters?(input, letters_in_hand) +def uses_available_letters?(input, letters_in_hand) current_hand = letters_in_hand.dup - word_characters = input.split("") + word_characters = input.upcase.split("") word_characters.each do |letter| - if !current_hand.include?(letter) - puts "You're cheating!" - return false - else - current_hand.delete_at(current_hand.index(letter)) - end + if !current_hand.include?(letter) + puts "You're cheating!" + return false + else + current_hand.delete_at(current_hand.index(letter)) + end end return true - end - - isUserInputValid = uses_available_letters?(user_input, letters_in_hand) - - # Wave 3 - # We want a method that returns the score of a given word as defined by the Adagrams game. +end - # Name this method score_word in adagrams.rb. This method should have the following properties: - # Has one parameter: word, which is a string of characters - # Returns an integer representing the number of points - # Each letter within word has a point value. The number of points of each letter is summed up to represent the total score of word - # Each letter's point value is described in the table below - # If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points +# Wave 3 +# We want a method that returns the score of a given word as defined by the Adagrams game. +# Name this method score_word in adagrams.rb. This method should have the following properties: +# Has one parameter: word, which is a string of characters +# Returns an integer representing the number of points +# Each letter within word has a point value. The number of points of each letter is summed up to represent the total score of word +# Each letter's point value is described in the table below +# If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points - def score_word(word) - word +def score_word(word) score = 0 if (word.length >= 7) && (word.length <= 10) - score = 8 + score = 8 end word.split("").each do |letter| - score = LETTER_INFO[letter.upcase][:score] + score + score = LETTER_INFO[letter.upcase][:score] + score end return score - end +end - words = [] - #Checking ONE time, prints only if true. - if isUserInputValid - puts "Your score is #{score_word(user_input)}." - words << user_input - else - puts "Your input is not valid, therefore not scored." - end +# Wave 4 +# After several hands have been drawn, words have been submitted, checked, scored, and played, we want a way to find the highest scoring word. This method looks at the array of words and calculates which of these words has the highest score, applies any tie-breaking logic, and returns the winning word in a special data structure. +# Add a method called highest_score_from in adagrams.rb. This method should have the following properties: +# Has one parameter: words, which is an array of strings +# Returns a single hash that represents the data of a winning word and its score. The hash should have the following keys: +# :word, whose value is a string of a word +# :score, whose value is the score of that word + +# In the case of tie in scores, use these tie-breaking rules: +# prefer the word with the fewest letters... +# ...unless one word has 10 letters. If the top score is tied between multiple words and one is 10 letters long, choose the one with 10 letters over the one with fewer tiles +# If the there are multiple words that are the same score and the same length, pick the first one in the supplied list + +def highest_score_from (words) + word_hash = { + #:word => "hello", + #:score => score_word("hello") + } + #words = ["tree", "the", "they", "them"] + highest_score = 0 + highest_word = "" + words.each do |word| + word_score = score_word(word) + if word_score > highest_score #check for highest score + highest_score = word_score + highest_word = word + elsif word_score == highest_score #tie break block until 218 + if highest_word.length != 10 + if (word.length == 10) #If the there are multiple words that are the same score and the same length, pick the first one in the supplied list. Setting the first word as the winner. + highest_word = word + elsif (word.length < highest_word.length) + highest_word = word + end + end + end + end + word_hash = {:word => highest_word, :score => highest_score} + return word_hash +end + + +# Optional: Wave 5 +# We want to be able to verify that a word that a player submits is a valid word against the English dictionary. +# We have the English dictionary available as a CSV file. We want to write a method that checks an input word against the words listed in the CSV file. If the word is found in the CSV file, then the word is valid and can be played. +# Add a method called is_in_english_dict? in adagrams.rb. This method should have the following properties: +# Has one parameter: input, which is a string +# Returns a boolean +# true, if input is in the provided English dictionary +# false, if input is not in the provided English dictionary +# Uses the English dictionary found in assets/dictionary-english.csv - #Optional - require "csv" - # ENGLISH_DICTIONARY = CSV.parse(File.read("./assets/dictionary-english.csv"), headers: true) - - # def is_in_english_dict?(input) - # #https://www.rubyguides.com/2018/10/parse-csv-ruby/ - # #method looking at the column which is list of words in the dictionary - # return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) - # end - - # #Checking ONE time, prints only if true. - # if isUserInputValid - # puts "Your word is in the Engligh Dictionary: #{is_in_english_dict?(user_input)}." - # else - # puts "Your input is not valid, therefore not checked in English dictionary." - # end +require "csv" + +# https://stackoverflow.com/a/29148595 +puts __dir__ +ENGLISH_DICTIONARY = CSV.parse(File.read(__dir__+"/../assets/dictionary-english.csv"), headers: true) #relative to ADAGRAMS not the workspace +def is_in_english_dict?(input) + #https://www.rubyguides.com/2018/10/parse-csv-ruby/ + #method looking at the column which is list of words in the dictionary + return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) +end + +def play_game + puts "Welcome to Adagrams." + puts "Try to make words with your letters." + puts "Your letters are:" + + letters_in_hand = draw_letters() + puts letters_in_hand.join + + puts "What's your word?" + user_input = gets.chomp.upcase + @your_words = [] + + isUserInputValid = uses_available_letters?(user_input, letters_in_hand) + words = [] + #Checking ONE time, prints only if true. + if isUserInputValid + puts "Your score is #{score_word(user_input)}." + words << user_input + else + puts "Your input is not valid, therefore not scored." + end + + #Checking ONE time, prints only if true. + if isUserInputValid + puts "Your word is in the Engligh Dictionary: #{is_in_english_dict?(user_input)}." + else + puts "Your input is not valid, therefore not checked in English dictionary." + end end play_game + From ddf0f1c0d6438ed7c0841c5df03265b43b506408 Mon Sep 17 00:00:00 2001 From: thenora Date: Thu, 13 Feb 2020 15:54:46 -0800 Subject: [PATCH 11/13] added loop to play again --- lib/adagrams.rb | 177 +++++++++++++++++++++++++----------------------- 1 file changed, 93 insertions(+), 84 deletions(-) diff --git a/lib/adagrams.rb b/lib/adagrams.rb index a80a7f7..90eaeae 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -132,37 +132,37 @@ # Since there are 12 Es but only 1 Z, it should be 12 times as likely for the user to draw an E as a Z # Invoking this method should not change the pool of letters (USING METHOD CALLED ARRAY.SAMPLE HERE) # Imagine that the user returns their hand to the pool before drawing new letters + def draw_letters() #Global variable LETTER_POOL would need to be passed as a paramter but reqs said no parameters so made it global - letters_in_hand = [] #array of letters - letters_in_hand = LETTER_POOL.sample(HAND_SIZE) #using .sample to randomly select letters WITHOUT changing letter_pool - return letters_in_hand #returning the array of letters /// can also simply return LETTER_POOL.sample(10) + letters_in_hand = [] #array of letters + letters_in_hand = LETTER_POOL.sample(HAND_SIZE) #using .sample to randomly select letters WITHOUT changing letter_pool + return letters_in_hand #returning the array of letters /// can also simply return LETTER_POOL.sample(10) end # Wave 2 - # Next, we need a way to check if an input word (a word a player submits) only uses characters that are contained within a collection (or hand) of drawn letters. Essentially, we need a way to check if the word is, indeed, an anagram of some or all of the given letters in the hand. - # To do so, add a method called uses_available_letters? in adagrams.rb. This method should have the following properties: - # Has two parameters: - # input, the first parameter, describes some input word, and is a string - # letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter - # Returns either true or false - # Returns true if every letter in the input word is available (in the right quantities) in the letters_in_hand - # Returns false if not; if there is a letter in input that is not present in the letters_in_hand or has too much of compared to the letters_in_hand +# Next, we need a way to check if an input word (a word a player submits) only uses characters that are contained within a collection (or hand) of drawn letters. Essentially, we need a way to check if the word is, indeed, an anagram of some or all of the given letters in the hand. +# To do so, add a method called uses_available_letters? in adagrams.rb. This method should have the following properties: +# Has two parameters: +# input, the first parameter, describes some input word, and is a string +# letters_in_hand, the second parameter, describes an array of drawn letters in a hand. You can expect this to be an array of ten strings, with each string representing a letter +# Returns either true or false +# Returns true if every letter in the input word is available (in the right quantities) in the letters_in_hand +# Returns false if not; if there is a letter in input that is not present in the letters_in_hand or has too much of compared to the letters_in_hand def uses_available_letters?(input, letters_in_hand) - current_hand = letters_in_hand.dup - word_characters = input.upcase.split("") - word_characters.each do |letter| - if !current_hand.include?(letter) - puts "You're cheating!" - return false - else - current_hand.delete_at(current_hand.index(letter)) - end + current_hand = letters_in_hand.dup + word_characters = input.upcase.split("") + word_characters.each do |letter| + if !current_hand.include?(letter) + puts "You're cheating!" + return false + else + current_hand.delete_at(current_hand.index(letter)) end - return true + end + return true end - # Wave 3 # We want a method that returns the score of a given word as defined by the Adagrams game. # Name this method score_word in adagrams.rb. This method should have the following properties: @@ -173,14 +173,14 @@ def uses_available_letters?(input, letters_in_hand) # If the length of the word is 7, 8, 9, or 10, then the word gets an additional 8 points def score_word(word) - score = 0 - if (word.length >= 7) && (word.length <= 10) - score = 8 - end - word.split("").each do |letter| - score = LETTER_INFO[letter.upcase][:score] + score - end - return score + score = 0 + if (word.length >= 7) && (word.length <= 10) + score = 8 + end + word.split("").each do |letter| + score = LETTER_INFO[letter.upcase][:score] + score + end + return score end # Wave 4 @@ -196,34 +196,33 @@ def score_word(word) # ...unless one word has 10 letters. If the top score is tied between multiple words and one is 10 letters long, choose the one with 10 letters over the one with fewer tiles # If the there are multiple words that are the same score and the same length, pick the first one in the supplied list -def highest_score_from (words) - word_hash = { - #:word => "hello", - #:score => score_word("hello") +def highest_score_from(words) + word_hash = { + #:word => "hello", + #:score => score_word("hello") } - #words = ["tree", "the", "they", "them"] - highest_score = 0 - highest_word = "" - words.each do |word| - word_score = score_word(word) - if word_score > highest_score #check for highest score - highest_score = word_score - highest_word = word - elsif word_score == highest_score #tie break block until 218 - if highest_word.length != 10 - if (word.length == 10) #If the there are multiple words that are the same score and the same length, pick the first one in the supplied list. Setting the first word as the winner. - highest_word = word - elsif (word.length < highest_word.length) - highest_word = word - end - end + #words = ["tree", "the", "they", "them"] + highest_score = 0 + highest_word = "" + words.each do |word| + word_score = score_word(word) + if word_score > highest_score #check for highest score + highest_score = word_score + highest_word = word + elsif word_score == highest_score #tie break block until 218 + if highest_word.length != 10 + if (word.length == 10) #If the there are multiple words that are the same score and the same length, pick the first one in the supplied list. Setting the first word as the winner. + highest_word = word + elsif (word.length < highest_word.length) + highest_word = word end + end end - word_hash = {:word => highest_word, :score => highest_score} - return word_hash + end + word_hash = { :word => highest_word, :score => highest_score } + return word_hash end - # Optional: Wave 5 # We want to be able to verify that a word that a player submits is a valid word against the English dictionary. # We have the English dictionary available as a CSV file. We want to write a method that checks an input word against the words listed in the CSV file. If the word is found in the CSV file, then the word is valid and can be played. @@ -237,43 +236,53 @@ def highest_score_from (words) require "csv" # https://stackoverflow.com/a/29148595 -puts __dir__ -ENGLISH_DICTIONARY = CSV.parse(File.read(__dir__+"/../assets/dictionary-english.csv"), headers: true) #relative to ADAGRAMS not the workspace +ENGLISH_DICTIONARY = CSV.parse(File.read(__dir__ + "/../assets/dictionary-english.csv"), headers: true) #relative to ADAGRAMS not the workspace + def is_in_english_dict?(input) - #https://www.rubyguides.com/2018/10/parse-csv-ruby/ - #method looking at the column which is list of words in the dictionary - return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) + #https://www.rubyguides.com/2018/10/parse-csv-ruby/ + #method looking at the column which is list of words in the dictionary + return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) +end + +def play_again_or_quit + puts "Would you like to play again? Type Y to play again." + should_continue = gets.chomp.upcase + + if should_continue == "Y" + play_game + end end def play_game - puts "Welcome to Adagrams." - puts "Try to make words with your letters." - puts "Your letters are:" - - letters_in_hand = draw_letters() - puts letters_in_hand.join + puts "Welcome to Adagrams." + puts "Try to make words with your letters." + puts "Your letters are:" - puts "What's your word?" - user_input = gets.chomp.upcase - @your_words = [] - - isUserInputValid = uses_available_letters?(user_input, letters_in_hand) - words = [] - #Checking ONE time, prints only if true. - if isUserInputValid - puts "Your score is #{score_word(user_input)}." - words << user_input - else - puts "Your input is not valid, therefore not scored." - end + letters_in_hand = draw_letters() + puts letters_in_hand.join - #Checking ONE time, prints only if true. - if isUserInputValid - puts "Your word is in the Engligh Dictionary: #{is_in_english_dict?(user_input)}." - else - puts "Your input is not valid, therefore not checked in English dictionary." - end + puts "What's your word?" + user_input = gets.chomp.upcase + @your_words = [] + + isUserInputValid = uses_available_letters?(user_input, letters_in_hand) + words = [] + #Checking ONE time, prints only if true. + if isUserInputValid + puts "Your score is #{score_word(user_input)}." + words << user_input + else + puts "Your input is not valid, therefore not scored." + end + + #Checking ONE time, prints only if true. + if isUserInputValid + puts "Your word is in the Engligh Dictionary: #{is_in_english_dict?(user_input)}." + else + puts "Your input is not valid, therefore not checked in English dictionary." + end + + play_again_or_quit end play_game - From bb5a0593939eeb4714f27994595c0b6028d3d1ee Mon Sep 17 00:00:00 2001 From: Kate Mangubat Date: Fri, 14 Feb 2020 15:06:49 -0800 Subject: [PATCH 12/13] Completing pull request for ADAGRAMS & submit. --- .github/PULL_REQUEST_TEMPLATE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE index eaa2ebb..b43332b 100644 --- a/.github/PULL_REQUEST_TEMPLATE +++ b/.github/PULL_REQUEST_TEMPLATE @@ -11,4 +11,4 @@ What are the advantages of using git when collaboratively working on one code ba What kind of relationship did you and your pair have with the unit tests? | If speaking specifically to the waves of the project, we had a learning moment to make sure the tests were running at each wave. While the code was being written, being aware of the variable names was also important in developing the rest of the code. Additionally, since it was the first project, it was a "getting to know you, your style of tackling group work, and agreeing on the flow of how to carry out the project" which functioned well. Being open, respectful, and receptive of a partner's working style, personality, and understanding their guess vs ask tendancies helped to navigate this new journey. | Does your code use any methods from the `Enumerable` mixin? If so, where and why was it helpful? | Yes, we used .include? because it allowed us to supply a return true if the given string from the user's word contains the given characters in the list supplied. | What was one method you and your pair used to debug code? | We utilized the error messages provided in the terminal to track our errors. In the future, we would like to be more proficient in using the Debugger which would be useful in seeing what is happening in our data structures and loops. | -What are two discussion points that you and your pair discussed when giving/receiving feedback from each other that you would be willing to share? | +What are two discussion points that you and your pair discussed when giving/receiving feedback from each other that you would be willing to share? | We strongly agreed that reviewing the project in its entirety using the whiteboard to assist is critical. This would be an opportunity to ask questions and clarify any parts of the project. We have a deeper understanding of our work habits, feelings of not letting our partner down that would lead to overcompensating and taking away from the pair programming experience all together, and making good use of our TAs. | From d96984de9b65a90b71dd5630082af6f0c3572136 Mon Sep 17 00:00:00 2001 From: thenora Date: Sat, 4 Apr 2020 01:15:26 -0700 Subject: [PATCH 13/13] sync --- lib/adagrams.rb | 2 +- lib/example_code.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 lib/example_code.rb diff --git a/lib/adagrams.rb b/lib/adagrams.rb index 90eaeae..b754abf 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -239,7 +239,7 @@ def highest_score_from(words) ENGLISH_DICTIONARY = CSV.parse(File.read(__dir__ + "/../assets/dictionary-english.csv"), headers: true) #relative to ADAGRAMS not the workspace def is_in_english_dict?(input) - #https://www.rubyguides.com/2018/10/parse-csv-ruby/ + # https://www.rubyguides.com/2018/10/parse-csv-ruby/ #method looking at the column which is list of words in the dictionary return ENGLISH_DICTIONARY.by_col[0].include?(input.downcase) end diff --git a/lib/example_code.rb b/lib/example_code.rb new file mode 100644 index 0000000..689a937 --- /dev/null +++ b/lib/example_code.rb @@ -0,0 +1,20 @@ +# A method looks at the array of words and calculates which of these words has the highest score. +def highest_score_from(words) + maximum_score = words.map { |word| score_word(word) }.max + highest = words.select { |word| score_word(word) == maximum_score } + if highest.length == 1 + winning_word = highest.first + else + highest_lengths = highest.map { |i| i.length } + if highest_lengths.any? { |x| x == 10 } + index_of_length_10 = highest_lengths.find_index(10) + winning_word = highest[index_of_length_10] + else + winning_word = highest[highest_lengths.find_index(highest_lengths.min)] + end + end + results = Hash.new + results[:score] = maximum_score + results[:word] = winning_word + return results +end