diff --git a/lib/adagrams.rb b/lib/adagrams.rb index e69de29..d512ce8 100644 --- a/lib/adagrams.rb +++ b/lib/adagrams.rb @@ -0,0 +1,97 @@ +def draw_letters + letter_pool = { 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_array = [] + letter_pool.each do |letter, quantity| + quantity.times do + letter_array << letter.to_s.downcase + end + end + return letter_array.sample(10) +end + +def uses_available_letters?(input, letters_in_hand) + size = 0 + input_array = input.upcase.split(//) + + letters_in_hand.each_with_index do |letter, index| + if input_array.include?(letter) + size += 1 + letters_in_hand.delete(index) + end + end + + if size == input_array.length + return true + else + return false + end +end + +def score_word(word) + points = 0 + word.upcase.split(//).each do |letter| + case letter + when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" + points += 1 + when "D", "G" + points += 2 + when "B", "C", "M", "P" + points += 3 + when "F", "H", "V", "W", "Y" + points += 4 + when "K" + points += 5 + when "J", "X" + points += 8 + when "Q", "Z" + points += 10 + end + end + + if word.length > 6 && word.length < 11 + points += 8 + end + + return points +end + +def highest_score_from(words) + highest_score = Hash.new(0) + + words.each do |word| + highest_score[word] = score_word(word) + end + + max_words = { + word: [], + score: [], + } + + highest_score.each do |k, v| + if v == highest_score.values.max + max_words[:word] << k + max_words[:score] = v + end + end + + best_word = {} + + min_length = 10 + if max_words[:word].length == 1 + best_word[:word] = max_words[:word][0] + best_word[:score] = max_words[:score] + else + max_words[:word].each do |word| + if word.length == 10 + best_word[:word] = word + best_word[:score] = max_words[:score] + return best_word + elsif word.length < min_length + min_length = word.length + best_word[:word] = word + best_word[:score] = max_words[:score] + end + end + end + return best_word +end \ No newline at end of file diff --git a/specs/adagrams_spec.rb b/specs/adagrams_spec.rb index ae2ccd0..b1adf14 100644 --- a/specs/adagrams_spec.rb +++ b/specs/adagrams_spec.rb @@ -1,20 +1,20 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require 'minitest/skip_dsl' +require "minitest/autorun" +require "minitest/reporters" +require "minitest/skip_dsl" -require_relative '../lib/adagrams' +require_relative "../lib/adagrams" # Get that nice colorized output Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -describe 'Adagrams' do - describe 'draw_letters method' do - it 'draws ten letters from the letter pool' do +describe "Adagrams" do + describe "draw_letters method" do + it "draws ten letters from the letter pool" do drawn_letters = draw_letters expect(drawn_letters.size).must_equal 10 end - it 'returns an array, and each item is a single-letter string' do + it "returns an array, and each item is a single-letter string" do drawn_letters = draw_letters expect(drawn_letters.size).must_equal 10 @@ -26,81 +26,79 @@ end end - describe 'uses_available_letters? method' do - - it 'returns true if the submitted letters are valid against the drawn letters' do - drawn_letters = ['D', 'O', 'G', 'X', 'X', 'X', 'X', 'X', 'X', 'X'] - test_word = 'DOG' + describe "uses_available_letters? method" do + it "returns true if the submitted letters are valid against the drawn letters" do + drawn_letters = ["D", "O", "G", "X", "X", "X", "X", "X", "X", "X"] + test_word = "DOG" is_valid = uses_available_letters? test_word, drawn_letters expect(is_valid).must_equal true end - it 'returns false word contains letters not in the drawn letters' do - drawn_letters = ['D', 'O', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'] - test_word = 'DOG' + it "returns false word contains letters not in the drawn letters" do + drawn_letters = ["D", "O", "X", "X", "X", "X", "X", "X", "X", "X"] + test_word = "DOG" is_valid = uses_available_letters? test_word, drawn_letters expect(is_valid).must_equal false end - it 'returns false word contains repeated letters more than in the drawn letters' do - drawn_letters = ['A', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'] - test_word = 'AAA' + it "returns false word contains repeated letters more than in the drawn letters" do + drawn_letters = ["A", "X", "X", "X", "X", "X", "X", "X", "X", "X"] + test_word = "AAA" is_valid = uses_available_letters? test_word, drawn_letters expect(is_valid).must_equal false end - end - describe 'score_word method' do - it 'returns an accurate numerical score according to the score chart' do + describe "score_word method" do + it "returns an accurate numerical score according to the score chart" do expect(score_word("A")).must_equal 1 expect(score_word("DOG")).must_equal 5 expect(score_word("WHIMSY")).must_equal 17 end - it 'returns a score regardless of input case' do + it "returns a score regardless of input case" do expect(score_word("a")).must_equal 1 expect(score_word("dog")).must_equal 5 expect(score_word("wHiMsY")).must_equal 17 end - it 'returns a score of 0 if given an empty input' do + it "returns a score of 0 if given an empty input" do expect(score_word("")).must_equal 0 end - it 'adds an extra 8 points if the word is 7 or more characters long' do + it "adds an extra 8 points if the word is 7 or more characters long" do expect(score_word("XXXXXXX")).must_equal 64 expect(score_word("XXXXXXXX")).must_equal 72 expect(score_word("XXXXXXXXX")).must_equal 80 end end - 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'] + 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"] best_word = highest_score_from words - expect(best_word[:word]).must_equal 'XXXX' + expect(best_word[:word]).must_equal "XXXX" expect(best_word[:score]).must_equal 32 end - it 'accurately finds best scoring word even if not sorted' do - words = ['XXX', 'XXXX', 'XX', 'X'] + it "accurately finds best scoring word even if not sorted" do + words = ["XXX", "XXXX", "XX", "X"] best_word = highest_score_from words - expect(best_word[:word]).must_equal 'XXXX' + expect(best_word[:word]).must_equal "XXXX" expect(best_word[:score]).must_equal 32 end - it 'in case of tied score, prefers the word with fewer letters' do + it "in case of tied score, prefers the word with fewer letters" do # the character 'M' is worth 3 points, 'W' is 4 points - words = ['MMMM', 'WWW'] + words = ["MMMM", "WWW"] # verify both have a score of 12 expect(score_word(words.first)).must_equal 12 @@ -108,13 +106,13 @@ best_word = highest_score_from words - expect(best_word[:word]).must_equal 'WWW' + expect(best_word[:word]).must_equal "WWW" expect(best_word[:score]).must_equal 12 end - it 'in case of tied score, prefers the word with fewer letters regardless of order' do + it "in case of tied score, prefers the word with fewer letters regardless of order" do # the character 'M' is worth 3 points, 'W' is 4 points - words = ['WWW', 'MMMM'] + words = ["WWW", "MMMM"] # verify both have a score of 12 expect(score_word(words.first)).must_equal 12 @@ -122,13 +120,13 @@ best_word = highest_score_from words - expect(best_word[:word]).must_equal 'WWW' + expect(best_word[:word]).must_equal "WWW" expect(best_word[:score]).must_equal 12 end - it 'in case of tied score, prefers most the word with 10 letters' do + it "in case of tied score, prefers most the word with 10 letters" do # the character 'A' is worth 1 point, 'B' is 3 points - words = ['AAAAAAAAAA', 'BBBBBB'] + words = ["AAAAAAAAAA", "BBBBBB"] # verify both have a score of 10 expect(score_word(words.first)).must_equal 18 @@ -136,13 +134,13 @@ best_word = highest_score_from words - expect(best_word[:word]).must_equal 'AAAAAAAAAA' + expect(best_word[:word]).must_equal "AAAAAAAAAA" expect(best_word[:score]).must_equal 18 end - it 'in case of tied score, prefers most the word with 10 letters regardless of order' do + it "in case of tied score, prefers most the word with 10 letters regardless of order" do # the character 'A' is worth 1 point, 'B' is 3 points - words = ['BBBBBB', 'AAAAAAAAAA'] + words = ["BBBBBB", "AAAAAAAAAA"] # verify both have a score of 10 expect(score_word(words.first)).must_equal 18 @@ -150,13 +148,13 @@ best_word = highest_score_from words - expect(best_word[:word]).must_equal 'AAAAAAAAAA' + expect(best_word[:word]).must_equal "AAAAAAAAAA" expect(best_word[:score]).must_equal 18 end - it 'in case of tied score and same length words, prefers the first word' do + it "in case of tied score and same length words, prefers the first word" do # the character 'A' is worth 1 point, 'E' is 1 point - words = ['AAAAAAAAAA', 'EEEEEEEEEE'] + words = ["AAAAAAAAAA", "EEEEEEEEEE"] # verify both have a score of 10 expect(score_word(words.first)).must_equal 18 diff --git a/wave-1-game.rb b/wave-1-game.rb index 159f620..5e94554 100644 --- a/wave-1-game.rb +++ b/wave-1-game.rb @@ -1,4 +1,5 @@ require_relative 'lib/adagrams' +require 'pry' def display_welcome_message puts "Welcome to Adagrams!"