Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#This program simulates the Adagrams game
def draw_letters
alphabet = {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}
hand = []
only_letters = alphabet.keys
# Builds the user letter pool
10.times do
tile = only_letters[rand(only_letters.size)]
# Checks the availability of the letter
if alphabet[tile.to_sym] > 0
alphabet[tile] -= 1
elsif alphabet[tile] == 0
tile = only_letters[rand(only_letters.size)]
end
hand << tile.to_s
end
return hand
puts hand
end

def uses_available_letters?(input, letters_in_hand)
input = input.upcase.split("")
# Creates a duplicate to maintain integrity of original letters in hand
comparison_check = letters_in_hand.dup
input.each do |letter|
# Checks if the input word complies with available letters
if comparison_check.include?(letter)
comparison_check.delete(letter)
else
return false
end
end
return true
end

def score_word(word)
word_letters = word.upcase.split("")
letter_scores = {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}
score = 0
word_letters.each do |letter|
score += letter_scores[letter.to_sym]
end
# Adds 8 more points based on word length in compliance with game rules
if word_letters.length >= 7
score += 8
end
return score
end

def highest_score_from(words)
final_array = []
words.each do |word|
score = score_word(word)
final_array << {word: word, score: score}
end
highest_score = 0
winner_word = ""
winner = nil
final_array.each do |pair|
# Declares the winner if any word is 10 characters long
if pair[:word].length == 10
winner = pair
break
# Finds the winner pair based on its score
elsif pair[:score] > highest_score
highest_score = pair[:score]
winner_word = pair[:word]
winner = pair
# Finds the winner pair when scores are equal based on word length
elsif pair[:word].length < winner_word.length && pair[:score] == highest_score
winner_word = pair[:word]
winner = pair
# Finds the winner pair when scores are equal based on word order
elsif pair[:word].length == winner_word.length && pair[:score] == highest_score
winner = winner
end
end
return winner
p winner
end

def in_dict?(input)
CSV.open("assets/dictionary-english.csv", "r").each do |word|
return true if word.include?(input.downcase)
end
return false
end

24 changes: 22 additions & 2 deletions wave-1-game.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
#Adagrams: Corinna & Olga -- YAY
require_relative 'lib/adagrams'

#comment

def display_welcome_message
puts "Welcome to Adagrams!"
puts "Let's draw 10 letters from the letter pool..."
end

def display_drawn_letters(letters)
puts "You have drawn the letters:"
puts letters.join(', ')
puts "You have drawn the letters: "
puts letters.join(", ")
end

def run_game
display_welcome_message
display_drawn_letters(draw_letters)
end

def draw_letters
alphabet = {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}
hand = []
10.times do |letter, count|
tile = alphabet.keys.sample().to_sym
if alphabet[tile] > 0
alphabet[tile] -= 1
elsif alphabet[tile] == 0
tile = alphabet.key.sample()
end
hand << tile.to_s
end
return hand
# puts alphabet
puts hand
end

run_game
43 changes: 40 additions & 3 deletions wave-2-game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def display_needs_valid_input_message
display_game_instructions
end

def display_score(score)
def display_score(score_word)
puts "Your submitted anagram scored #{score} points"
end

Expand All @@ -32,9 +32,27 @@ def display_goodbye_message
end

def get_user_input
gets.chomp
gets.chomp.upcase
end

def uses_available_letters?(get_user_input, letters_in_hand)
word_letters = Array.new(get_user_input.split(""))
word_letters.difference(letters_in_hand).empty?
end

def score_word(word)
word_letters = Array.new(word.split(""))
letter_scores = {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}
score = 0
word_letters.each do |letter|
#p letter.to_sym
score += letter_scores[letter.to_sym]
end
return score
end

#puts score_word(get_user_input)

def run_game
display_welcome_message

Expand All @@ -50,16 +68,35 @@ def run_game

user_input_word = get_user_input

while ( !(uses_available_letters?(user_input_word, letter_bank)) )
while ( (uses_available_letters?(user_input_word, letter_bank)) == false)
display_needs_valid_input_message
user_input_word = get_user_input
end

#display_score

display_retry_instructions
should_continue = get_user_input == "y"
end

display_goodbye_message
end

def draw_letters
alphabet = {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}
hand = []
10.times do |letter, count|
tile = alphabet.keys.sample().to_sym
if alphabet[tile] > 0
alphabet[tile] -= 1
elsif alphabet[tile] == 0
tile = alphabet.key.sample()
end
hand << tile.to_s
end
return hand
# puts alphabet
puts hand
end

run_game
37 changes: 35 additions & 2 deletions wave-3-game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def display_needs_valid_input_message
display_game_instructions
end

def score_word(word)
word_letters = Array.new(word.split(""))
letter_scores = {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}
score = 0
word_letters.each do |letter|
#p letter.to_sym
score += letter_scores[letter.to_sym]
end
return score
end

def display_score(score)
puts "Your submitted anagram scored #{score} points"
end
Expand All @@ -32,7 +43,12 @@ def display_goodbye_message
end

def get_user_input
gets.chomp
gets.chomp.upcase
end

def uses_available_letters?(get_user_input, letters_in_hand)
word_letters = Array.new(get_user_input.split(""))
word_letters.difference(letters_in_hand).empty?
end

def run_game
Expand Down Expand Up @@ -60,10 +76,27 @@ def run_game
display_score(score)

display_retry_instructions
should_continue = get_user_input == "y"
should_continue = get_user_input == "Y"
end

display_goodbye_message
end

def draw_letters
alphabet = {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}
hand = []
10.times do |letter, count|
tile = alphabet.keys.sample().to_sym
if alphabet[tile] > 0
alphabet[tile] -= 1
elsif alphabet[tile] == 0
tile = alphabet.key.sample()
end
hand << tile.to_s
end
return hand
# puts alphabet
puts hand
end

run_game
70 changes: 67 additions & 3 deletions wave-4-game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ def display_needs_valid_input_message
display_game_instructions
end

def score_word(word)
word_letters = Array.new(word.split(""))
letter_scores = {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}
score = 0
word_letters.each do |letter|
#p letter.to_sym
score += letter_scores[letter.to_sym]
end
if word_letters.length >= 7
score += 8
end
return score

end

def display_score(score)
puts "Your submitted anagram scored #{score} points"
end
Expand All @@ -37,7 +52,12 @@ def display_goodbye_message
end

def get_user_input
gets.chomp
gets.chomp.upcase
end

def uses_available_letters?(get_user_input, letters_in_hand)
word_letters = Array.new(get_user_input.split(""))
word_letters.difference(letters_in_hand).empty?
end

def run_game
Expand All @@ -63,16 +83,60 @@ def run_game
end

score = score_word(user_input_word)
played_words << user_input_word
played_words << [user_input_word, score]

display_score(score)

display_retry_instructions
should_continue = get_user_input == "y"
should_continue = get_user_input == "Y"
end

display_highest_score(highest_score_from(played_words))
display_goodbye_message
end

def highest_score_from(words)
final_array = []
words.each do |word, score|
final_array << {word: word, score: score}
end
# word with fewest letters
# first one submitted if length and score are same
highest_score = 0
highest_score_word = ""
winner = nil
final_array.each do |word_score_hash|
if word_score_hash[:word].length == 10
winner = word_score_hash
break
elsif word_score_hash[:score] > highest_score
highest_score = word_score_hash[:score]
winner = word_score_hash
elsif word_score_hash[:score] == highest_score && word_score_hash[:word].length < highest_score_word.length
highest_score_word = word_score_hash[:word]
winner = word_score_hash
end
end
return winner
puts winner
end


def draw_letters
alphabet = {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}
hand = []
10.times do |letter, count|
tile = alphabet.keys.sample().to_sym
if alphabet[tile] > 0
alphabet[tile] -= 1
elsif alphabet[tile] == 0
tile = alphabet.key.sample()
end
hand << tile.to_s
end
return hand
# puts alphabet
puts hand
end

run_game