Merge pull request #704 from arjieng/main

Ruby version for Hangman And Poetry
This commit is contained in:
Jeff Atwood
2022-04-09 00:25:00 -05:00
committed by GitHub
2 changed files with 421 additions and 0 deletions

280
44_Hangman/ruby/hangman.rb Normal file
View File

@@ -0,0 +1,280 @@
class Canvas
BUFFER = []
def initialize width = 12, height = 12, fill = " "
for i in (0...height) do
line = []
for i in (0...width) do
line << ""
end
BUFFER << line
end
clear
end
def render
lines = []
for line in BUFFER do
lines << line.join("")
end
return lines.join("\n")
end
def put s, x, y
BUFFER[y][x] = s[0]
end
private
def clear fill = " "
for row in BUFFER do
for x in (0...(row.length)) do
row[x] = fill
end
end
end
end
def init_gallows canvas
for i in (0...12) do
canvas.put("X", 0, i)
end
for i in (0...7) do
canvas.put("X", i, 0)
end
canvas.put("X", 6, 1)
end
def draw_head canvas
canvas.put("-", 5, 2)
canvas.put("-", 6, 2)
canvas.put("-", 7, 2)
canvas.put("(", 4, 3)
canvas.put(".", 5, 3)
canvas.put(".", 7, 3)
canvas.put(")", 8, 3)
canvas.put("-", 5, 4)
canvas.put("-", 6, 4)
canvas.put("-", 7, 4)
end
def draw_body canvas
for i in (5...9) do
canvas.put("X", 6, i)
end
end
def draw_right_arm canvas
for i in (3...8) do
canvas.put("\\", i - 1, i)
end
end
def draw_left_arm canvas
canvas.put("/", 10, 3)
canvas.put("/", 9, 4)
canvas.put("/", 8, 5)
canvas.put("/", 7, 6)
end
def draw_right_leg canvas
canvas.put("/", 5, 9)
canvas.put("/", 4, 10)
end
def draw_left_leg canvas
canvas.put("\\", 7, 9)
canvas.put("\\", 8, 10)
end
def draw_left_hand canvas
canvas.put("\\", 10, 2)
end
def draw_right_hand canvas
canvas.put("/", 2, 2)
end
def draw_left_foot canvas
canvas.put("\\", 9, 11)
canvas.put("-", 10, 11)
end
def draw_right_foot canvas
canvas.put("-", 2, 11)
canvas.put("/", 3, 11)
end
PHASES = [
["First, we draw a head", 'draw_head'],
["Now we draw a body.", 'draw_body'],
["Next we draw an arm.", 'draw_right_arm'],
["this time it's the other arm.", 'draw_left_arm'],
["Now, let's draw the right leg.", 'draw_right_leg'],
["This time we draw the left leg.", 'draw_left_leg'],
["Now we put up a hand.", 'draw_left_hand'],
["Next the other hand.", 'draw_right_hand'],
["Now we draw one foot", 'draw_left_foot'],
["Here's the other foot -- you're hung!!", 'draw_right_foot'],
]
WORDS = [
"GUM",
"SIN",
"FOR",
"CRY",
"LUG",
"BYE",
"FLY",
"UGLY",
"EACH",
"FROM",
"WORK",
"TALK",
"WITH",
"SELF",
"PIZZA",
"THING",
"FEIGN",
"FIEND",
"ELBOW",
"FAULT",
"DIRTY",
"BUDGET",
"SPIRIT",
"QUAINT",
"MAIDEN",
"ESCORT",
"PICKAX",
"EXAMPLE",
"TENSION",
"QUININE",
"KIDNEY",
"REPLICA",
"SLEEPER",
"TRIANGLE",
"KANGAROO",
"MAHOGANY",
"SERGEANT",
"SEQUENCE",
"MOUSTACHE",
"DANGEROUS",
"SCIENTIST",
"DIFFERENT",
"QUIESCENT",
"MAGISTRATE",
"ERRONEOUSLY",
"LOUDSPEAKER",
"PHYTOTOXIC",
"MATRIMONIAL",
"PARASYMPATHOMIMETIC",
"THIGMOTROPISM",
]
def play_game guess_target
wrong_guesses = 0
guess_progress = ["-"] * guess_target.length
guess_list = []
gallows = Canvas.new
init_gallows(gallows)
guess_count = 0
while true
puts "Here are the letters you used:"
puts "#{guess_list.join(",")}\n"
puts "#{guess_progress.join("")}\n"
guess_letter = ""
guess_word = ""
while guess_letter == ""
print "What is your guess? "
guess_letter = gets.chomp!.upcase[0]
if !guess_letter.match?(/[[:alpha:]]/)
guess_letter = ""
puts "Only letters are allowed!"
elsif guess_list.include?(guess_letter)
guess_letter = ""
puts "You guessed that letter before!"
end
end
guess_list << guess_letter
guess_count += 1
if guess_target.include?(guess_letter)
indices = (0...guess_target.length).find_all { |i| guess_target[i,1] == guess_letter }
for i in indices do
guess_progress[i] = guess_letter
end
if guess_progress.join("") == guess_target
puts "You found the word!"
break
else
puts "\n#{guess_progress.join("")}\n"
while guess_word == ""
print "What is your guess for the word? "
guess_word = gets.chomp!.upcase
if !guess_word.match?(/[[:alpha:]]/)
guess_word = ""
puts "Only words are allowed!"
end
end
if guess_word == guess_target
puts "Right!! It took you #{guess_count} guesses!"
break
end
end
else
comment, draw_bodypart = PHASES[wrong_guesses]
puts comment
method(draw_bodypart).call(gallows)
puts gallows.render()
wrong_guesses += 1
puts "Sorry, that letter isn't in the word."
if wrong_guesses == 10
puts "Sorry, you lose. The word was #{guess_target}"
break
end
end
end
end
def main
puts "#{(" " * 32)}HANGMAN"
shuffled = WORDS.shuffle(random: Random.new(1))
current_word = 0
word_count = shuffled.length
keep_playing = true
while keep_playing
play_game(WORDS[current_word])
current_word += 1
if current_word == word_count
puts "You did all the words!!"
keep_playing = false
else
print "Want another word? (yes or no) "
a = gets.chomp!.upcase
keep_playing = true if a == 'Y' || a == 'y' || a == 'Yes' || a == 'YES' || a == 'yes'
end
end
puts "It's been fun! Bye for now."
end
if __FILE__ == $0
main
end

141
70_Poetry/ruby/poetry.rb Normal file
View File

@@ -0,0 +1,141 @@
PAGE_WIDTH = 64
class State
attr_accessor :u, :i, :j, :k, :phrase, :line
def initialize
self.u = 0
self.i = 0
self.j = 0
self.k = 0
self.phrase = 1
self.line = ""
end
end
def print_centered msg
spaces = " " * ((PAGE_WIDTH - msg.length).fdiv(2))
print(spaces + msg)
end
def process_phrase_1 state
line_1_options = [
"MIDNIGHT DREARY",
"FIERY EYES",
"BIRD OR FIEND",
"THING OF EVIL",
"PROPHET"
]
state.line = state.line + line_1_options[state.i]
return state.line
end
def process_phrase_2 state
line_2_options = [
["BEGUILING ME", 2],
["THRILLED ME", nil],
["STILL SITTING....", nil],
["NEVER FLITTING", 2],
["BURNED", nil]
]
words, u_modifier = line_2_options[state.i]
state.line += words
if !u_modifier.nil?
state.u = u_modifier
end
end
def process_phrase_3 state
phrases = [
[false, "AND MY SOUL"],
[false, "DARKNESS THERE"],
[false, "SHALL BE LIFTED"],
[false, "QUOTH THE RAVEN"],
[true, "SIGN OF PARTING"]
]
only_if_u, words = phrases[state.i]
if !only_if_u || state.u > 0
state.line = state.line + words
end
end
def process_phrase_4 state
phrases = [
"NOTHING MORE",
"YET AGAIN",
"SLOWLY CREEPING",
"...EVERMORE",
"NEVERMORE"
]
state.line += phrases[state.i]
end
def maybe_comma state
if state.line.length > 0 && state.line[-1] == "."
return
end
if state.u != 0 && Random.rand <= 0.19
state.line += ", "
state.u = 2
end
if Random.rand <= 0.65
state.line += " "
state.u += 1
else
puts state.line
state.line = ""
state.u = 0
end
end
def pick_phrase state
state.i = Random.rand(0..4)
state.j += 1
state.k += 1
if state.u <= 0 && (state.j % 2) != 0
state.line += (" " * 5)
end
state.phrase = state.j + 1
end
def main
print_centered("POETRY")
state = State.new
phrase_processors = {
'1' => 'process_phrase_1',
'2' => 'process_phrase_2',
'3' => 'process_phrase_3',
'4' => 'process_phrase_4'
}
while true
if state.phrase >= 1 && state.phrase <= 4
method(phrase_processors[state.phrase.to_s]).call(state)
maybe_comma state
elsif state.phrase == 5
state.j = 0
puts state.line
state.line = ""
if state.k > 20
puts ""
state.u = 0
state.k = 0
else
state.phrase = 2
next
end
end
pick_phrase state
end
end
if __FILE__ == $0
main
end