Post code that you're proud of!

I just read the thread with Cameronhimself asking if he should post his development prgress on his game and it gave me an idea. This thread is now for posting code you are particularly proud of. I believe it will give people a chance to show off and give others ideas for their own code or even get suggestions about what they’ve already written. It is open to authors of all experience levels. Let me start off.

First I need to post a bit of code which uses hyperlinks to post a list of various actions the player can perform on the objects in the world so you know whats up with the other examples I share.

[code]Include Inline Hyperlinks by Erik Temple.

A thing has some text called acts. The acts of a thing is usually “blank”. Understand “act [something]” and “[something]” as acting. Acting is an action applying to one thing. Check acting: if the acts of the noun is “blank”, say “There wasn’t anything I could think to do with [the noun].” instead. Carry out acting: say “[acts of the noun]”. Report acting: do nothing.

The lantern is a switched off device in the Bedroom. It is fixed in place. The description of the lantern is “The lantern was nothing special, simply a glass gas lamp made by the local smith and bolted to the wall.”
After switching on the lantern: say "I reached up to the nob which controlled the gas flow, giving it a slight twist. After a quick tap on the starter the flame burst into life. ".
the acts of the lantern is “[link]light[as]light lantern[end link]”.[/code]
Fairly simple but the backbone of how I want to write this thing.

This one I already posted in another thread but I’m still proud of it, it’s an example of having the first time a room is visiting having a description in which the player is more involved the first run through (terrible explanation I know)

[code]The Bedroom is a room; "[first time]I woke up in a suden flash, cold sweat coatexd my body but I felt unbeleavably hot. I quickly shoved my sheets away and sat up. It was still deep in the middle of the night so it took a while for my eyes to adjust, it felt like I just came out of a brightly lit room. The first thing I did was check on my brother, to my releaf he was still fast asleep; snoring soundly. His bed sat against the wall seperated from mine by a wooden [link]end-table[end link] with a [link]glass of water[end link] on top and a [link]lantern[end link] hanging on the wall above.

Now that I was sure I hadn’t bothered him I took a closer look around.[only] [if visited]My brother was sleeping in the bed accross from mine, separated by a wooden [link]end-table[end link] with a [link]glass of water[end link] on top and a [link]lantern[end link] hanging on the wall above.[end if]Faint starlight streamed from the one [link]window[end link] in the room,[if the window is closed] firmly shut[otherwise] wide open[end if]. Next to it stood my [link]bookshelf[end link]. [first time]Everything in the room was technically both of ours but my brother had never been much one for reading.[only]Siting across the bookshelf stood the small circular [link]table[as]center table[end link] with two chairs. The [link]book[end link] I had been reading and a candle sat ontop. [if unvisited]Finally my eyes settled on the [link]metal sheet[end link] hanging on the closed door opposite my bed. Engraved on it was he Celdar national chant.[otherwise]Hanging on the door opposite my bed was a metal shit engraved with the Celdar national chant."[/code]

So go ahead and show us what you got.

fine. here. (noob here too, and I’m actually proud of the simple code below because it took a few tweaks to get it right. Again, simple, but I did it ALL BY MY SELF!!!11)

instead of talking to the ninth floor attendant: if the player is not informed: say "'Would you like to go to the lobby?' he asks."; if the player consents: say "'Okay.'"; wait for the SPACE key; clear the screen; move the player to Near the Elevator; otherwise: say "You tell the attendant you want to go to your room on the fourth floor. He silently acquiesces."; wait for the SPACE key; clear the screen; move player to Fourth Floor Elevator; otherwise: say "It's getting late so you ask the attendant to take you to the fourth floor so you can go to your hotel room."; wait for the SPACE key; clear the screen; move player to Fourth Floor Elevator.

Not that this might not be a good idea for many, but it’s hard for me to comply because the code I’m most proud of is in the areas where I have attempted the weirdest solutions, and I attempted those solutions in order to more gracefully make particular turns in the storytelling. So it’s almost as if you have asked specifically for the most spoilery parts of my source code. Maybe it’s just an artifact of my personal process – I try to marry my most interesting coding ideas to my most interesting story ideas, to make them inextricable from each other: that’s when I get excited.

I agree, so I chose code that’s not too spoilery, but it’s also not the code I’m proudest of cuz that would be too spoilery.

Yeah. I suppose I could fill in some blanks with substitute story but it seems like too muchwork.

I gave in cuz I think it might help out us noobs to see lots and lots of codes, if the thread ends up taking off, of course.

The code I’m so proud of (and this tells a long story about how much of a noob I am) is the learning machine in Apocalypse, plus all the hundreds of communication interchanges of LOGAN. I cannot post it because it’s a megabyte of code :stuck_out_tongue:

c’mon, give us a taste!!! :laughing:

Probably the coolest thing I can think of is my Japanese parser :slight_smile:

Maybe it doesn’t count because it hasn’t been worked into a full parser, but I’m fond of this code for movement on a coordinate system with special rooms scattered around it.

Also, I should be ashamed of the following, but I’m not (if only for the title):

[spoiler][code]“I have no huwikaz and I must zucimut” by Matt Weiner

Include Procedural Randomness by Aaron Reed.

Use American dialect.

Section - The Long Corridor

The Long Corridor is a room. “You are in a seemingly infinitely long north-south corridor. (Latitude: [latitude]).” The oddly named object is a privately-named thing in the long corridor. Understand “xxxxxxxxx” as the oddly named object.

The latitude is a number that varies. When play begins: now the latitude is a random number between 500 and 27000.

For printing the name of the oddly named object:
say the name associated with the latitude.

The description of the oddly named object is “It looks like the [name associated with the latitude plus 1000] 1000 steps north of here.”

Instead of taking the oddly named object: say “What? You can’t take the [oddly named object]!”

Instead of going north in the corridor:
say “You walk north.”;
increment the latitude;
try looking.

Instead of going south in the corridor:
say “You walk south.”;
decrement the latitude;
try looking.

Section - The Worst Random Name Generator Ever

Table of Consonants
consonant (indexed text)
“b”
“c”
“d”
“f”
“g”
“h”
“j”
“k”
“l”
“m”
“n”
“p”
“qu”
“r”
“s”
“t”
“v”
“w”
“x”
“z”

Table of Vowels
vowel (indexed text)
“a”
“e”
“i”
“o”
“u”
“y”

To decide what indexed text is the name associated with (germ - a number):
set seed to germ;
let string be indexed text;
now string is “[random consonant][random vowel][random consonant][random vowel][random consonant][random vowel][random consonant]”;
decide on string.

To say random consonant:
let n be a procedurally random number between 1 and the number of rows in the Table of Consonants;
choose row n in the Table of Consonants;
say the consonant entry.

To say random vowel:
let n be a procedurally random number between 1 and the number of rows in the Table of Vowels;
choose row n in the Table of Vowels;
say the vowel entry.

Section - Dynamic Parsing

[This is where the dynamic parsing happens, such as it is.]

After reading a command:
if the player’s command includes “xxxxxxxxx”:
say “There’s no xxxxxxxxx here.”;
reject the player’s command;
otherwise:
let T be indexed text;
let T be the player’s command;
if T matches the text “[the name associated with the latitude]”:
replace the text “[the name associated with the latitude]” in T with “xxxxxxxxx”;
change the text of the player’s command to T.[/code][/spoiler]

I realize that it would be easier to use an understand-as instead of the after reading a command, but I’m glad I was able to get this to work at all.

This bit of code stands out for me.

I haven’t done IF in a while (read: 1.5 years) and have just gotten halfway through implementing a new game in the past few days. I needed an elevator to separate out the different sections of the game, but I wanted to make it a room instead of a container/vehicle.

Since my NPCs don’t use the elevator, I don’t need to check what floor the elevator is on before the player re-enters it, but were I to have more interesting NPC interaction I’d likely have to fix that.

[spoiler][code]A transport is a kind of room. The Elevator is a transport. “[one of]The elevator is an ornate thing, with gold-colored gates to the north that reveal some measure of elegance. [or][stopping][if the floor of the elevator is 0]A panel above the gates tells you that you are on the bottom floor.[else if the floor of the elevator is 1]A panel above the gates tells you that you are on the middle floor.[else if the floor of the elevator is 2]A panel above the gates tells you that you are on the top floor.[else]A panel above the gates tells you that you are on the basement.[end if] On one wall are two buttons, one pointing up and the other pointing down.”.

A transport has a number called the floor. The Elevator has floor 1.

A button is a kind of thing.

A thing can be pushable or not-pushable. A thing is usually not-pushable. A button is usually pushable. A button can be working or broken. A button is usually working.

To push is an action applying to one thing. Understand “push [something]” as pushing. Understand “press [something]” as pushing.

Check pushing:
if the noun is not-pushable:
say “Pushing that would serve no purpose.” instead;
if the noun is broken:
say “The [noun] is currently broken; pushing it does absolutely nothing.” instead.

The panel is scenery in the Elevator. The UpButton is a button. The UpButton is part of the panel. The DownButton is a button. The DownButton is part of the panel.

The description of the panel is “A panel shows which floor the elevator is currently on. It indicates that the elevator is on the [if the floor of the elevator is 0]bottom floor[else if the floor of the elevator is 1]middle floor[else if the floor of the elevator is 2]top floor[else if the floor of the elevator is -1]basement[end if].”

The description of the UpButton is “A simple triangle pointing upwards.[if the floor of the elevator is 2 or the UpButton is broken] It is currently unusable.[end if]”.

The description of the DownButton is “A simple triangle pointing downwards.[if the floor of the elevator is -1 or the DownButton is broken] It is currently unusable.[otherwise if the floor of the elevator is 0 and can-access-basement is false] It is currently unusable.[end if]”.

The UpButton is broken.

can-access-basement is a truth state that varies. can-access-basement is false.

After pushing the UpButton:
if the floor of the elevator is not 2:
say “The elevator jolts and begins to rise.”;
now the floor of the elevator is the floor of the elevator plus one;
try looking;
else:
say “You press the button, but nothing happens.”

After pushing the DownButton:
if the floor of the elevator is not 0:
say “The elevator jolts and begins to descend.”;
now the floor of the elevator is the floor of the elevator minus one;
try looking;
else if can-access-basement is false:
say “You press the button, but nothing happens.”;
else if the floor of the elevator is -1:
say “You press the button, but nothing happens.”;
else:
say “The elevator jolts and begins to descend.”;
now the floor of the elevator is the floor of the elevator minus one;
try looking;

Understand “up”, “up button”, and “button” as the UpButton. The printed name of the UpButton is “up button”.

Understand “down”, “down button”, and “button” as the DownButton. The printed name of the DownButton is “down button”.

Instead of going up when the location is the Elevator:
try pushing the UpButton instead.

Instead of going down when the location is the Elevator:
try pushing the DownButton instead.

Instead of going nowhere when the location is the Elevator:
say “In the elevator, the only exit is through the gates to the north.”

Instead of going north when the location is the Elevator:
say “You open the gates and step forward.”;
if the floor of the elevator is 0:
move the player to Floor0;
else if the floor of the elevator is 1:
move the player to Floor1;
else if the floor of the elevator is 2:
move the player to Floor2;
else if the floor of the elevator is -1:
move the player to Basement.

Instead of going south when the location is Floor1 or the location is Floor2 or the location is Floor0 or the location is Basement:
say “You open the elevator gates and step into the elevator.”;
move the player to the Elevator.[/code][/spoiler]

This was pretty much the first thing I wrote, so finishing the implementation made me very, very happy.