Skip to content

Instantly share code, notes, and snippets.

@matoni109
Created October 30, 2020 08:53
Show Gist options
  • Save matoni109/9359858552f2c1d0f43556798df204a3 to your computer and use it in GitHub Desktop.
Save matoni109/9359858552f2c1d0f43556798df204a3 to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'json'
def generate_grid(grid_size)
# TODO: generate random grid of letters
gen_grid = ('A'..'Z').to_a + ('A'..'N').to_a
gen_grid.sample(grid_size)
end
def run_game(attempt, grid, start_time, end_time)
url = "https://wagon-dictionary.herokuapp.com/#{attempt}"
word_back = open(url).read
word_hash = JSON.parse(word_back)
word_hash["word"]
word_hash["found"]
game_hash = {
:score => 0,
:message => "",
:time => 0
}
grid_check = grid
check = attempt.upcase
#code from stack
letter_count = grid_check.each_with_object(Hash.new(0)){ |item, hash| hash[item] += 1 }
attempt_count = attempt.upcase.split("").each_with_object(Hash.new(0)){ |item, hash| hash[item] += 1 }
# end code from stack
game_hash[:time] = end_time - start_time
if word_hash["found"] == true && check.chars.uniq.all?{|char| grid_check.include?(char)} == true
game_hash[:score] = ( attempt.length * 1.5 ) + ( 99 - game_hash[:time])
game_hash[:message] = "well done"
count = 0
attempt_count.each_pair do |key, value|
if attempt_count[key] > letter_count[key]
count += 1
end
count > 0 ? game_hash[:score] = 0 : "its odd"
count > 0 ? game_hash[:message] = "not in the grid" : "its odd"
end
elsif word_hash["found"] == false
game_hash[:score] = 0
game_hash[:message] = "not an english word" #
elsif check.chars.uniq.all?{|char| grid_check.include?(char)} == false
game_hash[:score] = 0
game_hash[:message] = "not in the grid" #
end
p game_hash
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment