Monday, September 15, 2008

Queneau's "A Hundred Thousand Billion Poems"

#!/usr/bin/ruby -w

# I still have to convert this procedural code to an object-oriented model, as well as
# devise a plan for how to avoid redundancy when randomly generating poems
# (i.e. I don't want the same permutation to occur twice), but this program works and is
# pretty neat-o. The user interface is only temporary, and will be taken out once the
# program can check for redundancy and terminate on its own accord (which will be
# one instance of every permutation). PS: Note sure why
# I can't get indents working in blogger; I apologize for the difficult readability
# this causes. PPS: If anyone can figure out the redundancy problem, let me know.

# "A Hundred Thousand Billion Poems"
# By: Raymond Queneau
# Translated by: Stanley Champman
# Coded by: Joshua Ware, 2008

# Opens individual text files that contain "base" sonnets, then creates
# an array of lines for each "base" sonnet."Queneau1.txt", "r") do |file|
Q1 = file.readlines()
end"Queneau2.txt", "r") do |file|
Q2 = file.readlines()
end"Queneau3.txt", "r") do |file|
Q3 = file.readlines()
end"Queneau4.txt", "r") do |file|
Q4 = file.readlines()
end"Queneau5.txt", "r") do |file|
Q5 = file.readlines()
end"Queneau6.txt", "r") do |file|
Q6 = file.readlines()
end"Queneau7.txt", "r") do |file|
Q7 = file.readlines()
end"Queneau8.txt", "r") do |file|
Q8 = file.readlines()
end"Queneau9.txt", "r") do |file|
Q9 = file.readlines()
end"Queneau10.txt", "r") do |file|
Q10 = file.readlines()

# Prompt for end-user to instantiate program or abort.

print "Type X, then ENTER for Queneau's A Hundred Thousand Billion Poems: "

new_command = gets().chomp!

# While loop that generates individual poems.

while (new_command == "X")
counter_line = 0

# While loop with embedded if/elsif that randomly generates individual lines.

while (counter_line < 14)
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
rnum = a[rand(a.size)]
if (rnum == 0)
puts Q1[counter_line]
elsif (rnum == 1)
puts Q2[counter_line]
elsif (rnum == 2)
puts Q3[counter_line]
elsif (rnum == 3)
puts Q4[counter_line]
elsif (rnum == 4)
puts Q5[counter_line]
elsif (rnum == 5)
puts Q6[counter_line]
elsif (rnum == 6)
puts Q7[counter_line]
elsif (rnum == 7)
puts Q8[counter_line]
elsif (rnum == 8)
puts Q9[counter_line]
elsif (rnum == 9)
puts Q10[counter_line]

No comments: