-- Sorry for the mess. Here's the start of my Globals.
oldpad = Controls.read()
-- This prototype is used to temporarily hold my generated sentence so it can be outputted to screen or file without making a whole new sentence on accident.
thispattern = nil
backuppattern = nil
toggle = nil
oldfav = nil
favsread = io.open("norris_favs.txt", "r")
green = Color.new(0, 255, 0)
blue = Color.new(0, 0, 255)
red = Color.new(255, 0, 0)
grey = Color.new(200, 200, 200)
mainback = Image.load("srsgmain.png")

-- Function for my main screen display.
function main()
screen:blit(0,0,mainback)
screen:print(140, 170, "<up> Generate a fact", green)
screen:print(140, 180, "<right> Save to favorites", green)
screen:print(140, 190, "<down> Back one sentence", grey)
screen:print(140, 200, "<left> Review Chuck favs", blue)
screen:print(140, 210, "", blue)
screen:print(140, 220, "<cross> Return to Categories", red)
screen:print(140, 230, "<menu> Exit SRSG", red)
screen:print(140, 240, "", red)
end

-- Prototype for my pattern table which seems to need to be in my main loop.
norrispatternArray={}
-- Currently all of my data is hardcoded. Here's the word lists seperated by word type.
attacknounArray={"roundhouse", "kick", "roundhouse-kick", "punch", "karate-chop", "murdalizing-blow", "super-kick"}
attackverb3rdpresArray={"roundhouses", "kicks", "roundhouse-kicks", "punches", "karate-chops", "murdalizes", "headbutts", "super-kicks"}
attackverb3rdpastArray={"roundhoused", "kicked", "roundhouse-kicked", "punched", "karate-chopped", "murdalized", "headbutted", "super-kicked"}
deadlybodypartArray={"fist", "arm", "leg", "tongue", "elbow", "foot", "shin", "thigh", "bicep", "balls", "hand"}
weakbodypartArray={"spleen", "liver", "kidney", "phalanges", "jugular", "aorta", "solar plexus", "diaphragm", "tibia", "femur", "mandibles", "teeth", "madulla oblongata", "pelvis", "hip", "brain", "ass", "balls", "back", "ribs", "face", "nose", "fingers", "toes", "ear hole", "appendix", "heart"}
attackadjArray={"amazing", "incredible", "lightning fast", "faster than light", "godlike", "insane", "kickass", "powerful", "supernova'ish", "cataclysmic", "devastating", "furious", "explosive", "mind blowing", "mind splitting", "badass"}
chuckArray={"Chuck", "Chuck Norris", "Mr. Chuck Norris", "Mr. Norris", "The Chuckster", "Lord Norris of Chuckingham", "Chuck 'blood and tears' Norris"}
animalsArray={"lions", "tigers", "bears", "mountain lions", "dragons", "gorillas", "pythons", "anacondas", "eagles", "griffins", "sphinxes", "boars", "cheetas", "mammoths", "werewolves", "tasmanian devils", "wombats", "bunny rabbits", "baby chicks", "sparrows" }
numbersArray={"10", "25", "37", "42", "50", "63", "75", "88", "91", "100", "116", "129", "131", "1,000", "20,000", "55,387", "79,726", "5,000,000", "1 billion", "27 bajillion", "infinity", "infinity-squared"}
famousfighterArray={"Rocky", "Mr. T", "himself", "Arnold Schwarzenegger", "Mr. Clean", "The Green Giant", "Mr. Belvedere", "Steven Seagal", "Jean Claude Van Damme", "Micky Mouse", "Jesus", "Muhammad", "Buddah", "Ghandi" }
holidayArray={"Christmas", "Chanukah", "Easter", "birthday", "New Year's", "MLKJr's Day", "Valentine's Day", "4th of July", "Kwanza", "Presidents' Day", "Halloween", "St. Patrick's Day", "day"}
impossibleArray={ "also prevent forrest fires", "divide by zero", "run OS X on a PC", "play Civ 4 with 8Mb of RAM", "be everywhere at once", "please all of the people all of the time", "keep a good horse down", "do that on television", "tickle himself", "take it all with him when he dies. IF he could die. (He can't)", "have his cake and eat it too", "hurry love", "stop a tidal wave with a fork"}
traveledArray={"traveled", "flew", "ran", "roundhoused himself", "leaped", "jumped", "thought himself"}
backArray={"back", "forward"}
actionArray={"sit on the toilet", "eat a hamburger", "have sex", "drink some kool-aid", "make another movie", "take his driver's license picture", "stick a q-tip in his ear", "zip up his pants"}
emoverbprogArray={"laughing", "crying", "screaming", "dancing", "giving him high-fives", "doing brodies", "pointing and laughing", "thinking about his pain"}

screen:clear()
main()
screen:print(70,65, "Welcome to the Chuck Norris Fact Generator.", green)
screen:print(100,75, "Please choose an option to begin...", green)
screen.flip()

-- This is my function for generating random numbers. The random generator is seeded by the os.time
-- and is then fed into the random generator, one item at a time, AND returns an interger based on
-- (found withing the range of) the number of items in the respective table.
function rand(numer) 
randnumer = math.random(numer)
return randnumer
end

while true do
-- seeding my random generator within my main loop to allow constantly updated seeds
math.randomseed(os.time())
pad = Controls.read()

-- Here's my sentence patterns. They reference a category's table and randomly select an entry.
-- Each random number is restricted to the available table entries by using table.getn to set the
-- upper range of the randomly generated numbers. Later on in the code the patterns themselves are randomly
-- selected. Each pattern will create a basic sentence and the details are the only things that
-- actually change. Depending on how many patterns you have and how well you categorize your
-- word lists, your sentence quality and variety can rise or fall. The comment under each line is
-- a sentence that could be made by each pattern. These help me streamline the generation by
-- reminding me quickly what types of words should fit into each category.
norrispatternArray[1]=chuckArray[rand(table.getn(chuckArray))].." once "..attackverb3rdpastArray[rand(table.getn(attackverb3rdpastArray))].." "..famousfighterArray[rand(table.getn(famousfighterArray))].." in the "..weakbodypartArray[rand(table.getn(weakbodypartArray))]..". It was so "..attackadjArray[rand(table.getn(attackadjArray))].."!"
--Chuck once kicked Rocky in the ass. It was so amazing!
norrispatternArray[2]="Every "..holidayArray[rand(table.getn(holidayArray))]..", "..chuckArray[rand(table.getn(chuckArray))].." "..attackverb3rdpresArray[rand(table.getn(attackverb3rdpresArray))].." "..numbersArray[rand(table.getn(numbersArray))].." "..animalsArray[rand(table.getn(animalsArray))].." and gives them as gifts."
--Every Christmas Chuck kicks 23 bears and gives them as gifts.
norrispatternArray[3]=chuckArray[rand(table.getn(chuckArray))].." can "..attacknounArray[rand(table.getn(attacknounArray))].." "..numbersArray[rand(table.getn(numbersArray))].." "..animalsArray[rand(table.getn(animalsArray))].." with his "..deadlybodypartArray[rand(table.getn(deadlybodypartArray))].." tied behind his back."
--Chuck can super-kick 23 bears with his hand tied behind his back.
norrispatternArray[4]=chuckArray[rand(table.getn(chuckArray))].." "..traveledArray[rand(table.getn(traveledArray))].." "..backArray[rand(table.getn(backArray))].." in time and "..attackverb3rdpastArray[rand(table.getn(attackverb3rdpastArray))].." "..famousfighterArray[rand(table.getn(famousfighterArray))].." just as he was about to "..actionArray[rand(table.getn(actionArray))]..". I couldn't stop "..emoverbprogArray[rand(table.getn(emoverbprogArray))].."!"
--Chuck traveled back in time and kicked Rocky just as he was about to scratch his ear. I couldn't stop laughing!
norrispatternArray[5]=chuckArray[rand(table.getn(chuckArray))].." can "..impossibleArray[rand(table.getn(impossibleArray))].."!"
--Chuck can also prevent forrest fires.



if pad:up() and oldpad:up() ~= pad:up() then
-- Here's my pattern being randomly selected and assigned to a temporary variable holder. I have also added
-- in a second holder to take the old value and preserve it till another sentence is created.
toggle = true
backuppattern = thispattern
thispattern = norrispatternArray[rand(table.getn(norrispatternArray))]
screen:clear()
main()
screen:print(20,40,string.sub(thispattern,1,53),green)
screen:print(20,50,string.sub(thispattern,54,105),green)
screen:print(20,60,string.sub(thispattern,106,157),green)
-- For some reason, having this in my main loop outside of an event causes flickering. Anyone
-- else have this problem? The tutorials I read suggested otherwise, so I was pretty happy when
-- I finally figured out that I could stop the flickering my placing my screen.flips in my events. 
screen.flip()
end

-- This is for going back a sentence. I want to allow the program to toggle back and forth between
-- the current and the last generated one, but for some reason there is a problem with changing from
-- the old sentence, back to the current one. I think it is a flipping problem, but it might be my
-- long list of conditions.
if pad:down() and oldpad:down() ~= pad:down() and thispattern ~= nil and toggle == false then
toggle = true
screen:clear()
main()
screen:print(20,40,string.sub(thispattern,1,53),green)
screen:print(20,50,string.sub(thispattern,54,105),green)
screen:print(20,60,string.sub(thispattern,106,157),green)
screen.flip()
end

if pad:down() and oldpad:down() ~= pad:down() and backuppattern ~= nil and toggle == true then
toggle = false
screen:clear()
main()
screen:print(20,40,string.sub(backuppattern,1,53),grey)
screen:print(20,50,string.sub(backuppattern,54,105),grey)
screen:print(20,60,string.sub(backuppattern,106,157),grey)
screen.flip()
end

-- Can't see something that's not there.
if pad:down() and oldpad:down() ~= pad:down() and backuppattern == nil then
screen:clear()
main()
screen:print(20,40,"Please generate 2 sentences first...",red)
screen.flip()
end

-- This saves extremely funny sentences to a file called norris_favs.txt. It appends to the 
-- end of the file and inserts a line break before each sentence. I added in conditions to prevent
-- a line return from being inserted at the top of a blank file. I realize I could probably do this
-- with fewer lines of code, but since it works, I'd rather not take the time to change it right now. =)
if pad:right() and oldpad:right() ~= pad:right() and thispattern ~= nil and toggle == true then
favscheck = io.open ("norris_favs.txt", "r")
favsblank = favscheck:read()
favscheck:close()
if favsblank == nil then
favs = io.open("norris_favs.txt", "a")
favs:write(thispattern)
favs:close()
screen:clear()
main()
screen:print(20,40,"Written to norris_favs.txt...",red)
screen:print(20,50,string.sub(thispattern,1,53),green)
screen:print(20,60,string.sub(thispattern,54,105),green)
screen:print(20,70,string.sub(thispattern,106,157),green)
screen.flip()
else
favs = io.open("norris_favs.txt", "a")
favs:write("\n"..thispattern)
favs:close()
screen:clear()
main()
screen:print(20,40,"Written to norris_favs.txt...",red)
screen:print(20,50,string.sub(thispattern,1,53),green)
screen:print(20,60,string.sub(thispattern,54,105),green)
screen:print(20,70,string.sub(thispattern,106,157),green)
screen.flip()
end
end

if pad:right() and oldpad:right() ~= pad:right() and thispattern ~= nil and toggle == false then
favscheck = io.open ("norris_favs.txt", "r")
favsblank = favscheck:read()
favscheck:close()
if favsblank == nil then
favs = io.open("norris_favs.txt", "a")
favs:write(backuppattern)
favs:close()
screen:clear()
main()
screen:print(20,40,"Written to norris_favs.txt...",red)
screen:print(20,50,string.sub(backuppattern,1,53),grey)
screen:print(20,60,string.sub(backuppattern,54,105),grey)
screen:print(20,70,string.sub(backuppattern,106,157),grey)
screen.flip()
else
favs = io.open("norris_favs.txt", "a")
favs:write("\n"..backuppattern)
favs:close()
screen:clear()
main()
screen:print(20,40,"Written to norris_favs.txt...",red)
screen:print(20,50,string.sub(backuppattern,1,53),grey)
screen:print(20,60,string.sub(backuppattern,54,105),grey)
screen:print(20,70,string.sub(backuppattern,106,157),grey)
screen.flip()
end
end

-- Again, you can't write somethign that's not there.
if pad:right() and oldpad:right() ~= pad:right() and thispattern == nil then
screen:clear()
main()
screen:print(20,40,"Please generate a sentence first...",red)
screen.flip()
end

if pad:left() and oldpad:left() ~= pad:left() then
oldfav = favsread:read()
if oldfav == nil then
favsread:close()
favsread = io.open("norris_favs.txt", "r")
screen:clear()
main()
screen:print(20,40,"End of file. Please start again.",red)
screen.flip()
else
screen:clear()
main()
screen:print(20,40,string.sub(oldfav,1,53),blue)
screen:print(20,50,string.sub(oldfav,54,105),blue)
screen:print(20,60,string.sub(oldfav,106,157),blue)
screen.flip()
end
end

screen.waitVblankStart()
oldpad = pad
if Controls.read():cross() then break end
-- The end
end
favsread:close()


