Liberty BASIC Community Forum
« Mad Lib Game Contest »

Welcome Guest. Please Login or Register.
Mar 29th, 2017, 9:41pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Mad Lib Game Contest  (Read 521 times)
Admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 14484
xx Mad Lib Game Contest
« Thread started on: Mar 4th, 2012, 09:37am »

You'll find info about our Rosetta Code project on the Open Source board. If you haven't investigated, you'll want to do so. There are currently 249 solved programming tasks, many of which provide code that is useful in various situations. It's a great library of helpful code!

One unsolved task is Mad Libs.

Mad Libs

http://rosettacode.org/wiki/Mad_Libs

Quote:
Mad Libs is a phrasal template word game where one player prompts another for a list of words to substitute for blanks in a story, usually with funny results.


Have a look at the solutions to the task in other languages to get a feel for the kinds of stories to use.

Let's have a contest for this task! If you want to participate, please read the Rosetta Code page linked above. Please keep your code as clear and concise as possible.

At the end of a week or so (or when we have several entries) we'll vote on the best solution to the task and post it on the Open Source board for tweaking.


To enter, simply post your code on this thread. smiley

User IP Logged

Admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 14484
xx Re: Mad Lib Game Contest
« Reply #1 on: Mar 5th, 2012, 07:47am »

This code is several years old. It was written in Run BASIC. I removed the 4 HTML statemens. I also modified one line that made use of Run BASIC's (and presumably LB5's) ability to double quote characters to avoid the need for chr$(34) notation.

Paste it into Liberty BASIC and run it to see how a Mad-Lib game works. Look at the entries on Rosetta Code.

Now, write your own entry, please! Or at least tweak this one so that it better conforms to the others on RC.

Code:
dim ans$(10)
[again]
ans$(0)="checkers"
ans$(1)="red"
ans$(2)="huge"
ans$(3)="slowly"
ans$(4)="nose"
ans$(5)="broccoli"
ans$(6)="baseball bat"
ans$(7)="mitten"
ans$(8)="cow"
ans$(9)="chair"
ans$(10)="throw"

print "Welcome to Mad-Libs!"
print
print "Fill in the answers, then read the story you've written!"
print:print
input "Type the name of a game.";ans$(0)
input "Type the name of a color.";ans$(1)
input "Type an adjective describing size.";ans$(2)
input "Type an adverb describing speed.";ans$(3)
input "Type the name of a part of the body.";ans$(4)
input "Type the name of a food.";ans$(5)
input "Type the name of a piece of sports equipment.";ans$(6)
input "Type the name of a piece of clothing.";ans$(7)
input "Type the name of an animal.";ans$(8)
input "Type the name of a piece of furniture.";ans$(9)
print "A transitive verb is one that requires a subject and an object."
input "Type a transitive verb.";ans$(10)
print:print

print "Bob decided to clean out his closet. "
print
print "As soon as he opened the door, a ";ans$(6);" fell onto his ";ans$(4);"! "
print "His ";ans$(4);" turned a bright shade of ";ans$(1);". Bob was NOT happy. "
print
print "He set the ";ans$(6);" aside and ";ans$(3);" reached into the closet and extracted a ";ans$(9);". "
print "Bob wondered how THAT got into the closet. He didn't recall placing it there. "
print
print "He returned to the task and grabbed ";ans$(5);" from the top shelf."
print "That made Bob hungry, so he started to walk to the kitchen."
print "He thought better of it and tossed the ";ans$(5);" onto the ";ans$(9);" and "
print "continued cleaning the closet. "
print
print "Bob crouched down to inspect the pile of stuff on the floor of the closet."
print "He was shocked to find a ";ans$(7);", which he ";ans$(3);" dragged into the room. "
print "He hadn't worn that ";ans$(7);" in many years and he wondered if it still fit."
print "The ";ans$(7);" was ";ans$(2);"."
print "He decided to try it on his pet ";ans$(8);". "
print "The ";ans$(8);" looked silly wearing the ";ans$(7);" so Bob went to the window "
print "so he could ";ans$(10);" the ";ans$(7);"."
print
print "Bob slammed the closet door and said to himself, "
print "'That's enough cleaning for today! I'm going to play ";ans$(0);".'"

input "Would you like to play again?";again$
if instr(upper$(again$), "Y") > 0 then
goto [again]
else
end
end if
 
« Last Edit: Mar 5th, 2012, 07:47am by Admin » User IP Logged

tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1609
xx Re: Mad Lib Game Contest
« Reply #2 on: Mar 5th, 2012, 08:34am »

*I... must... not... look... at Alyce code... before coding own version*

And it is hard wink
User IP Logged

The existence of bug reports means somebody is using the software and actually cares to report back to you that he is having a problem with it, instead of just deleting it from their hard disk.
(Janusz Marcin Gorycki)
Admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 14484
xx Re: Mad Lib Game Contest
« Reply #3 on: Mar 5th, 2012, 08:55am »

Haha, Anatoly! You are a far better coder than I am. You have no need to look at an inferior program!

I look forward to seeing your code!
User IP Logged

meerkat
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 18
xx Re: Mad Lib Game Contest
« Reply #4 on: Mar 5th, 2012, 1:14pm »

This may work.
Code:

template$ = "<name> went for a walk in the park. " _
+ " <he or she> found a <noun>. <name> decided to take it home."
[loop]
i	= i + 1
i 	= instr(template$,"<",i)
if i <> 0 then
	j	= instr(template$,">",i)
	a$	= mid$(template$,i,j-i + 1)
	print "Give me a name for ";a$
	input n$
	template$ = strRep$(template$,a$,n$)
	goto [loop]
end if
print template$
wait
FUNCTION strRep$(str$,rep$,with$)
ln	= len(rep$)
ln1	= ln - 1
i	= 1
while i <= len(str$)
    if mid$(str$,i,ln) = rep$ then
        strRep$ = strRep$ + with$
        i = i + ln1
    else
        strRep$ = strRep$ + mid$(str$,i,1)
    end if
i = i + 1
WEND
END FUNCTION
 
« Last Edit: Mar 6th, 2012, 08:11am by meerkat » User IP Logged

Dan Teel
Guru
ImageImageImageImageImage


member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1116
xx Re: Mad Lib Game Contest
« Reply #5 on: Mar 6th, 2012, 12:58am »

Very similiar to yours
Code:
temp$="<name> went for a walk in the park. <he or she> found a <noun>. <name> decided to take it home."

while word$(temp$,2,"<")<>""
    what$=word$(word$(temp$,2,"<"),1,">")
    input "";what$;": ";rep$
    temp$=strrep$(temp$,"<";what$;">",rep$)
wend

print temp$

function strrep$(str$,what$,with$)
    for i=1 to len(str$)
        if mid$(str$,i,len(what$))=what$ then
            strrep$=strrep$+with$
            i=i+len(what$)-1
        else
            strrep$=strrep$+mid$(str$,i,1)
        end if
    next i
end function
 
« Last Edit: Mar 6th, 2012, 6:17pm by Dan Teel » User IP Logged

ZPtr.net
tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1609
xx Re: Mad Lib Game Contest
« Reply #6 on: Mar 6th, 2012, 05:43am »

Ok, another approach.
Somehow using strRep$ (or similar) just never occured to me :(
And
word$(word$(temp$,2,"<"),1,">")
is sure nifty trick I managed to forget.

Code:
CrLf$=chr$(13)+chr$(10)
txt$=""
txt$=txt$+      "<name> went for a walk in the park. <he or she>"
txt$=txt$+CrLf$+"found a <noun>. <name> decided to take it home."

'print txt$

keys$=""        '01<key1>02<key2> etc...
dim value(99)   'NN
N=0

txt2$=""
p2=0

while 1
    p1=instr(txt$, "<", p2)
    if not(p1) then exit while
    txt2$=txt2$+mid$(txt$, p2+1,(p1-p2-1))
    p2=instr(txt$, ">", p2+1)
    key$=mid$(txt$, p1,(p2-p1+1))
    k=instr(keys$, key$)
    if k then
        NN=val(mid$(keys$, k-2, 2))
        repl$=value$(NN)
    else
        N=N+1
        input "Enter replacement for ";key$;" :";repl$
        value$(N)=repl$
        keys$=keys$+using("##",N);key$ 
    end if
    txt2$=txt2$+ repl$ 
wend
txt2$=txt2$+mid$(txt$, p2+1)
print "-------------------------"
'print keys$
print txt2$
 
User IP Logged

The existence of bug reports means somebody is using the software and actually cares to report back to you that he is having a problem with it, instead of just deleting it from their hard disk.
(Janusz Marcin Gorycki)
meerkat
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 18
xx Re: Mad Lib Game Contest
« Reply #7 on: Mar 6th, 2012, 10:08am »

This may work better
Code:
template$ = "<name> went for a walk in the park. " _
+ " <he or she> found a <noun>. <name> decided to take it home."
i    = instr(template$,"<",i)
while i
    j    = instr(template$,">",i)
    a$    = mid$(template$,i,j-i + 1)
    print "Give me a name for ";a$;
    input n$
     k   = instr(template$,a$)
    while k
        template$ = left$(template$,k - 1) + n$ + mid$(template$,k + len(a$))
         k   = instr(template$,a$)
    wend
    i    = instr(template$,"<",i)
wend

print template$
wait
 
« Last Edit: Mar 7th, 2012, 08:27am by meerkat » User IP Logged

tenochtitlanuk
Board Moderator

member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1138
xx Re: Mad Lib Game Contest
« Reply #8 on: Mar 6th, 2012, 2:07pm »

Sorry I'm unlikely to have time to do an entry... cry
I've been busy organising as Chair our Welsh Society St David's Day celebrations, and will be away travelling quite often over the next month or two.
This is not me, but a link to a photo of our Taunton Mayor getting into the right spirit at our 'do'....
I first met programming Madlibs on Commodore PET in the late 70s.
The bright student I as a teacher learned PET BASIC & 6502 machine code with is now a senior programmer at Nomura Bank. Now why did I stay in teachinghuh

For a quick look at what we've achieved on Rosetta, see this list of completed tasks.
If anyone is hovering, wondering whether to try one, I strongly recommend you do.
User IP Logged

tsh73
Moderator
ImageImageImageImageImage


member is offline

Avatar

Anatoly (real name)


PM

Gender: Male
Posts: 1609
xx Re: Mad Lib Game Contest
« Reply #9 on: Mar 9th, 2012, 12:18am »

Combining Dan's WORD$ trick with strrep(...) using INSTR (like in last meerkat' example):
Code:

temp$="<name> went for a walk in the park. <he or she> found a <noun>. <name> decided to take it home."

while word$(temp$,2,"<")<>""
    what$=word$(word$(temp$,2,"<"),1,">")
    input "";what$;": ";rep$
    temp$=strrep$(temp$,"<";what$;">",rep$)
wend

print temp$

function strrep$(str$,what$,with$)
     k   = instr(str$,what$)
    while k
       str$ = left$(str$,k - 1) + with$ + mid$(str$,k + len(what$))
       k   = instr(str$,what$)
    wend
    strrep$ = str$
end function
 
« Last Edit: Mar 9th, 2012, 12:18am by tsh73 » User IP Logged

The existence of bug reports means somebody is using the software and actually cares to report back to you that he is having a problem with it, instead of just deleting it from their hard disk.
(Janusz Marcin Gorycki)
meerkat
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 18
xx Re: Mad Lib Game Contest
« Reply #10 on: Mar 11th, 2012, 08:45am »

A couple changes to cut some lines from my previous code.
Code:
temp$="<name> went for a walk in the park. <he or she> found a <noun>. <name> decided to take it home."
k = instr(temp$,"<")
while k
    replace$   = mid$(temp$,k,instr(temp$,">")-k + 1)
    print "Replace:";replace$;" with:"; :input with$
    while k
        temp$  = left$(temp$,k-1) + with$ + mid$(temp$,k + len(replace$))
        k      = instr(temp$,replace$,k)
    wend
k = instr(temp$,"<")
wend
print temp$
wait
 


hth
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls