-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.rb
executable file
·118 lines (94 loc) · 2.82 KB
/
wordle.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env ruby
require 'cgi'
require 'date'
require 'json'
require 'net/http'
MAX_TRIES = 6
WORDS_LIST_URL = 'https://2e6jmolg96.execute-api.us-east-1.amazonaws.com/default/word_of_the_day'
class String
def green
"\033[32m#{self}\033[0m"
end
def yellow
"\033[33m#{self}\033[0m"
end
def grey
"\033[37m#{self}\033[0m"
end
def black
"\033[30m#{self}\033[0m"
end
end
def block_for_color(color)
case color
when 'yellow'
"🟨"
when 'green'
"🟩"
when 'grey'
"⬜"
else
''
end
end
class Wordle
attr_reader :word_of_the_day, :letters
attr_accessor :tries, :social_share_output, :success
def initialize
response = Net::HTTP.get_response(URI.parse(WORDS_LIST_URL))
# datestamp = Date.today.to_time.utc.to_i
@tries = 0
@success = false
@social_share_output = ''
@letters = ('A'..'Z').to_a
# @word_of_the_day = words[Random.new(datestamp).rand(words.size)].upcase
@word_of_the_day = response.body.upcase
end
def play
word_of_the_day_letters = word_of_the_day.chars
puts "Guess the word of the day \nPress Ctrl + C to exit"
guess = nil
guessed_letters = []
while tries < MAX_TRIES && guess != word_of_the_day
guess = gets.chomp.upcase[0..4]
guess_chars = guess.chars
@tries += 1
output_string = ''
social_share_string = ''
guess_chars.each_with_index do |char, index|
color = 'grey'
color = 'yellow' if word_of_the_day_letters.include?(char)
color = 'green' if color == 'yellow' && char == word_of_the_day_letters[index]
output_string << "#{char} ".send(color)
social_share_string << "#{block_for_color(color)} "
end
social_share_output << "\n#{social_share_string}"
guessed_letters = (guessed_letters + guess_chars).uniq
puts output_string
letters_output = ''
letters.each do |letter|
color = 'grey'
color = 'black' if guessed_letters.include?(letter)
color = 'yellow' if color == 'black' && word_of_the_day_letters.include?(letter)
color = 'green' if color == 'yellow' && guess_chars.index(letter) == word_of_the_day_letters.index(letter)
letters_output << "#{letter} ".send(color)
end
puts letters_output
end
@success = true if guess == word_of_the_day
end
def result
if success
puts "You got the correct word in #{tries} guess(es)."
puts "\n#{social_share_output}\n\n"
puts "Share with your friends using the link below:\n"
puts "https://twitter.com/intent/tweet?url=https://deepakmahakale.com/wordle&text=Play%20wordle%20in%20the%20shell.%0A%0AMy%20score%20#{tries}/#{MAX_TRIES}%0A#{CGI.escape(social_share_output)}%0A%0A"
elsif
puts 'Better luck next time.'
end
end
end
game = Wordle.new
game.play
game.result
# exit 0