require "chapmanchri_quotemachine2/version"
# module QuoteMachine
# class Error < StandardError; end
# # Your code goes here...
# end
# declare instance variables
def declare_instance_variables
@poem = []
@stanza_pick = 0
@stanza_current = 0
@section_current = 0
@section_previous = "na"
@section_next = "na"
@phrase = []
end
def set_stanza_and_section
stanza = pick_stanza
@stanza_current = stanza
@section_current = pick_section(stanza)
end
def set_phrase
@phrase[0] = @section_current
end
def set_next_section
if @section_current < (@poem[@stanza_current].length-1)
@section_next = @section_current + 1
end
@section_next
end
def set_previous_section
if @section_current > 1
@section_previous = @section_current - 1
end
@section_previous
end
def read_poem(file_name)
# text = File.read(file_name)
File.read(file_name)
end
def convert_text_to_array(text)
# text_array = text.split("\n")
text.split("\n")
end
def create_final_poem_array(text_array)
# Convert text_array into a new two dimensional array (@poem)
# with each stanza it's own element in the
# @poem array and each segment in the @poem element an element in the @poem
# element.
stanza_num = 0
section = 0
text_array.each do |line|
if line[0..1] == "" #the stanza number is after ""
if stanza_num == 0 && @poem[0] == nil
@poem[stanza_num] = [line[2..(line.length-1)]]
else
# "in writing stanza 2 or more"
stanza_num +=1
section = 0
@poem[stanza_num] = [line[2..(line.length-1)]]
end
elsif line == "" #these are blank lines which tell you a new section is beginning
section += 1
else
temp = @poem[stanza_num][section]
if temp.class == NilClass
@poem[stanza_num][section] = line
else
@poem[stanza_num][section] = @poem[stanza_num][section] + "
" + line
end
end
end
end
def pick_stanza
# stanza = rand(0...(@poem.length))
rand(0...(@poem.length))
end
def pick_section(stanza)
sections = @poem[stanza].length
# section = rand(1...(sections))
rand(1...(sections))
end
def show_current_section
"
#{show_current_section}
" + "" #the phrase is the second phrase of the stanza elsif @phrase[0] == 2 result_text = "#{show_current_section}" end end end # Builds for a Stanza with more than two sections and only one phrase if @poem[@stanza_current].length > 3 if @phrase.length == 1 if @phrase[0] == 1 result_text = "#{show_current_section}
" + "" elsif (@phrase[0] >= 2) && @phrase[0] < @poem[@stanza_current].length - 1 result_text = "" + "#{show_current_section}
" + "" elsif @phrase[0] == @poem[@stanza_current].length - 1 result_text = "#{show_current_section}" end end end # Builds for Stanza with more than two sections and more than one phrase if @poem[@stanza_current].length > 3 if @phrase.length > 1 # current_phrase is first in stanza and the second phrase is also displayed if @phrase[0] == 1 && @section_current == 1 && @phrase[1] == 2 result_text = "#{show_current_section}
#{show_next_section}
" end # current_phrase is the last phrase in the stanza and the previous phrase is also displayed if @phrase.last == @section_current && @phrase[@phrase.length - 2] == (@section_current - 1) result_text = "#{show_previous_section}
#{show_current_section}
" end # there are two phrases in array, current_phrase is not first or last stanza, # and current_phrase > second phrase if @phrase.length == 2 && @section_current != 1 && @section_current != @poem[@stanza_current].length - 1 result_text = "#{show_previous_section}
#{show_current_section}
" end # there are two phrases in array, current_phrase is not first or last, # and current_phrase < second phrase if @phrase.length == 2 && @section_current != 1 \ && @section_current != @poem[@stanza_current].length - 1 \ && @phrase.last > @section_current result_text = "#{show_current_section}
#{show_next_section}
" end # three phrases in the phrase storage array if @phrase.length == 3 result_text = "#{show_previous_section}
#{show_current_section}
#{show_next_section}
" end end end end # show result result_text end def init_poem declare_instance_variables text = read_poem('complete_verse.txt') text_array = convert_text_to_array(text) create_final_poem_array(text_array) end # run the program puts Dir.pwd # main_program def test_method "text method" end