Skip to content

Commit e575ae0

Browse files
committed
70_Poetry in Lua
A working port of the poetry game in Lua. -Variables were given meaningful names -Added a limit so it did not spit out infinite poetry at lightning speed. -The variable U was changed to a boolean for readability. -Adjusted the randomizing to work with any number of phrases since Dave Ahl encouraged users to write their own content for the poems.
1 parent 5301155 commit e575ae0

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

70_Poetry/lua/poetry.lua

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
--[[
2+
Poetry
3+
From the classic book BASIC Computer Games, published by Creative Computing.
4+
5+
This program produces random verse which might loosly be considered in the Japanese Haiku style.
6+
It uses 20 phrases in four groupd of five phrases each and generally cycles through the groups in order.
7+
It inserts commas (random - 19% of the time), indentation (random - 22% of the time), and starts new paragraphs (18% probability, but at least once every 20 phrases).
8+
9+
The phrases in POETRY are somewhat suggestive of Edgar Allan Poe.
10+
11+
The Original author of this program is unkown. It was modified and reworked by Jim Bailey, Peggy Ewing, and Dave Ahl at DEC.
12+
13+
Ported to Lua by Allen West
14+
**CHANGES
15+
Added a limit so it did not spit out infinite poetry at lightning speed.
16+
The variable U was changed to a boolean for readability.
17+
Adjusted the randomizing to work with any number of phrases since Dave encouraged users to write their own content for the poems.
18+
]]
19+
20+
limit = 60
21+
22+
phrase_one = {"MIDNIGHT DREARY", "FIERY EYES", "BIRD OR FIEND", "THING OF EVIL", "PROPHET"}
23+
phrase_two = {"BEGUILING ME", "THRILLED ME", "STILL SITTING....", "NEVER FLITTING", "BURNED"}
24+
phrase_three = {"AND MY SOUL", "DARKNESS THERE", "SHALL BE LIFTED", "QUOTH THE RAVEN", "SIGN OF PARTING"}
25+
phrase_four = {"NOTHING MORE", "YET AGAIN", "SLOWLY CREEPING", "...EVERMORE", "NEVERMORE"}
26+
27+
function printintro()
28+
print("\n POETRY")
29+
print("Creative Computing Morristown, New Jersey")
30+
print("\n\n")
31+
end
32+
33+
--Insert commas, spaces, new lines,and tabs randomly
34+
function randomformatting()
35+
--Random comma
36+
if new_line == false and math.random() < 0.19 then
37+
io.write(",")
38+
new_line = false
39+
end
40+
--Random space or new line
41+
if math.random() < 0.65 then
42+
io.write(" ")
43+
new_line = false
44+
else
45+
print("")
46+
new_line = true
47+
end
48+
--Random Tabs
49+
if new_line and phrase_set % 2 ~= 0 then
50+
io.write(" ")
51+
end
52+
end
53+
54+
--Poetry printing loop
55+
function printpoetry(run_length)
56+
math.randomseed(os.time()) -- lua needs it's random function initialized to work
57+
run_count = 0 --Added so it doesn't print infinitely
58+
phrase_set = 0 --var j in original code
59+
lines_without_break = 0 --var k in original code
60+
phrase_choice = 0 --var i in original code
61+
new_line = true --var u in original code
62+
while(run_count < run_length) do
63+
if phrase_set == 0 then
64+
phrase_choice = math.random(1,#phrase_one)
65+
io.write(phrase_one[phrase_choice])
66+
elseif phrase_set == 1 then
67+
phrase_choice = math.random(1,#phrase_two)
68+
if phrase_choice == 0 or phrase_choice == 3 then
69+
new_line = false
70+
end
71+
io.write(phrase_two[phrase_choice])
72+
elseif phrase_set == 2 then
73+
phrase_choice = math.random(1,#phrase_three)
74+
if phrase_choice < #phrase_three and new_line == false then
75+
io.write(phrase_three[phrase_choice])
76+
end
77+
elseif phrase_set == 3 then
78+
phrase_choice = math.random(1,#phrase_four)
79+
io.write(phrase_four[phrase_choice])
80+
end
81+
if phrase_set == 4 then
82+
phrase_set = 0
83+
print("")
84+
if lines_without_break > 20 then
85+
new_line = true
86+
lines_without_break = 0
87+
print("\n")
88+
end
89+
else
90+
phrase_set = phrase_set + 1
91+
lines_without_break = lines_without_break + 1
92+
randomformatting()
93+
run_count = run_count + 1
94+
end
95+
end
96+
print("")
97+
end
98+
99+
printintro()
100+
printpoetry(limit)

0 commit comments

Comments
 (0)