http://qs321.pair.com?node_id=1067575

I'm new to Perl. So, take it easy on me. I made this little game to practice.

## Version: 3.15.3 ## Purpose: Text based adventure game use 5.14.1; use warnings; my ($command,$currentRoom,$standing,$light,$sectorBSeal,$sectorCSeal,$ +lighter,$controlRoomDoorLock,$doctorWillisSaved,$controlRoomPressureS +tabilization,$uniform,$escapePodsLock,$keyCard,$closetKey); my (@saveArray,@loadArray); use constant HAVE => 1; use constant DONT_HAVE => 0; use constant FALSE => 0; use constant TRUE => 1; use constant BEDROOM => 1; use constant HALLWAY => 2; use constant LAB => 3; use constant CONTROL_ROOM => 4; use constant DATAFILEOUT => './savegamefile.txt'; use constant DATAFILEIN => './savegamefile.txt'; $| = 1; sub main { initializeValues(); mainMenu(); gameIntro(); bedroomCommand(); hallwayCommand(); labratoryCommand(); computerCommand(); labNotesCommand(); sectorACommand(); controlRoomCommand(); areYouSure(); utilityRoomCommand(); inventory(); endingA(); endingB(); endingC(); playAgain(); saveGame(); writeGame(); loadGame(); startFromLoad(); quitMenu(); } main(); sub initializeValues { $uniform = DONT_HAVE; $keyCard = DONT_HAVE; $lighter = DONT_HAVE; $closetKey = DONT_HAVE; $standing = FALSE; $light = FALSE; $controlRoomPressureStabilization = FALSE; $controlRoomDoorLock = TRUE; $escapePodsLock = TRUE; $sectorBSeal = FALSE; $sectorCSeal = FALSE; $doctorWillisSaved = FALSE; $currentRoom = BEDROOM; } sub mainMenu { use constant NEW_GAME => 1; use constant LOAD_GAME => 2; use constant QUIT_GAME => 3; system "cls"; my $hereDoc = <<'END'; ########################################################### MARTIAN MYSTERY VERSION 3.4.6 CREATOR: TYLER BURROWS ########################################################### MAIN MENU 1:.................................................NEW GAME 2:................................................LOAD GAME 3:................................................QUIT GAME ########################################################### PLEASE CHOOSE FROM THE MENU END print $hereDoc; print "\n\n>:"; chomp ($command = <STDIN>); if ($command =~ /^[a-zA-Z]/) { print "\n\nYou may only use numeric entries. Try again."; sleep 3; mainMenu(); } elsif ($command == NEW_GAME) { print "Your adventure begins.\n............................"; sleep 2; gameIntro(); } elsif ($command == LOAD_GAME) { print "\n\nLoading your last game\n........................."; sleep 2; loadGame(); } elsif ($command == QUIT_GAME) { print "\n\nQuitting Game.\n.............\nGoodbye."; sleep 3; die; } else { print "\n\nCommand not recognized. Choose from the menu."; sleep 3; mainMenu(); } } sub gameIntro { system"cls"; print "The year is 2021."; sleep 3; print "\nYou are a field scientist living in humanity's\nfirst per +manent habitat on the planet mars."; sleep 4; print "\nIt is the pinnacle of technology, costing US taxpayers ov +er\n14.7 billion dollars and taking 7 years to complete."; sleep 4; print "\nIt contains millions of dollars worth of equipment and su +pplies\nand houses over 350 full time residents, mainly comprised of +research scientists like yourself."; sleep 4; print "\n\n\n\t\t\tWelcome to Cydonia."; sleep 3; system "cls"; print "\n\nYou awaken with a start as you feel the entire room sha +ke."; sleep 3; print "\n\n\t\t\tERRRN\a"; sleep 3; print "\n\n\t\t\tERRRN\a"; sleep 3; print "\n\n\t\t\tERRRN\a"; sleep 3; print "\n\nThe habitat's alarms begin to sound,\nblaring loudly in + your ear from above your bed.\nThe red light flashes bright in your +face,\nburning your eyes as you open them."; sleep 3; bedroomCommand(); } sub bedroomCommand { print "\n\n>:"; chomp ($command = <STDIN>); $command =~ tr/A-Z/a-z/; if ($command =~ "inventory" or $command =~ "open inventory") { $currentRoom = BEDROOM; inventory(); } elsif ($command =~ "look") { if ($light == FALSE) { print "It is very dark other than the flashing red alarm l +ight on the ceiling.\nThere is a lamp on your nightstand.\n"; sleep 2; bedroomCommand(); } elsif ($light == TRUE) { print "The sleeping quarters are quite small.\nOnly a bed, + a nightstand, a desk, a large pressure-seal door, and a dresser are +in the room"; if ($uniform == DONT_HAVE) { print "The chair in front of the desk has your uniform + slung over it."; } if ($keyCard == DONT_HAVE) { print "\nYour keycard sits on the desk"; } if ($lighter == DONT_HAVE) { print " next to an old lighter\n"; } print "There are no paintings or posters on the wall, just + steel painted white.\n"; sleep 5; } } elsif ($command =~ "turn on" or $command =~ "turn on the lamp" or +$command =~ "turn on lamp" or $command =~ "turn on the light" or $com +mand =~ "turn on light") { if ($light == FALSE) { if ($standing == FALSE) { print "You reach over in bed and turn on the switch.\n +The light flickers on.\n\n"; print "The sleeping quarters are quite small.\nOnly a +bed, a nightstand, a desk, a large pressure-seal door, and a dresser +are in the room"; if ($uniform == DONT_HAVE) { print " The chair in front of the desk has your un +iform slung over it."; } if ($keyCard == DONT_HAVE) { print "\nYour keycard sits on the desk"; } if ($lighter == DONT_HAVE) { print " next to an old lighter\n"; } print "There are no paintings or posters on the wall, +just steel painted white.\n\n"; sleep 5; $light = TRUE; bedroomCommand(); } } elsif ($light == TRUE) { print "The light is already on.\n\n"; sleep 2; bedroomCommand(); } } elsif ($command =~ "nightstand" or $command =~ "night stand") { print "\n\nThere's nothing on here but a lamp. What a bummer.\ +n\n"; sleep 2; bedroomCommand(); } elsif ($command =~ "get out of bed" or $command =~ "stand" or $com +mand =~ "get up") { if ($standing == FALSE) { if ($light == TRUE) { print "You roll out of bed and stand up facing the door.\n +\n"; sleep 2; $standing = TRUE; bedroomCommand(); } elsif ($light == FALSE) { print "\n\nI can't really see anything.\nI should turn + on the light before I get up.\n\n"; sleep 3; bedroomCommand(); } } elsif ($standing == TRUE) { print "You are already standing.\n\n"; sleep 2; bedroomCommand(); } } elsif ($command =~ "keycard" or $command =~ "take keycard" or $com +mand =~ "pick up keycard" or $command =~ "grab keycard") { if ($light == FALSE) { print "\n\nWhat keycard? I can't see anything."; sleep 2; bedroomCommand(); } elsif ($standing == FALSE) { print "You can't reach it from the bed..\n\n"; sleep 2; bedroomCommand(); } elsif ($light == TRUE) { if ($keyCard == HAVE) { print "You already have it.\n\n"; sleep 2; bedroomCommand(); } elsif ($keyCard == DONT_HAVE) { print "You pick up the keycard.\nThis will probably co +me in handy.\n\n"; sleep 2; $keyCard = HAVE; bedroomCommand(); } } } elsif ($command =~ "uniform" or $command =~ "put on uniform" or $c +ommand =~ "wear uniform" or $command =~ "take uniform") { if ($light == FALSE) { print "\n\nWhat uniform? I can't see anything."; sleep 2; bedroomCommand(); } elsif ($standing == FALSE) { print "You can't reach it from the bed..\n\n"; sleep 2; bedroomCommand(); } elsif ($light == TRUE) { if ($uniform == HAVE) { print "You're already wearing your uniform.\n\n"; sleep 2; bedroomCommand(); } elsif ($uniform == DONT_HAVE) { print "You put on your uniform, a dark jumpsuit with a whi +te lab coat.\n\n"; sleep 2; $uniform = HAVE; bedroomCommand(); } } } elsif ($command =~ "lighter" or $command =~ "pick up lighter" or $ +command =~ "take lighter" or $command =~ "grab lighter") { if ($light == FALSE) { print "\n\nWhat lighter? I can't see anything.\n\n"; sleep 2; bedroomCommand(); } elsif ($standing == FALSE) { print "You can't reach it from the bed..\n\n"; sleep 2; bedroomCommand(); } elsif ($light == TRUE) { if ($lighter == HAVE) { print "You already grabbed that.\n\n"; sleep 3; bedroomCommand(); } elsif ($lighter == DONT_HAVE) { print "You pick up the old lighter.\n\n"; $lighter = HAVE; sleep 3; bedroomCommand(); } } } elsif ($command =~ "dresser" or $command =~ "check dresser" or $co +mmand =~ "open dresser") { if ($light == FALSE) { print "\n\nWhat keycard? I can't see anything.\n\n"; sleep 2; bedroomCommand(); } elsif ($standing == FALSE) { print "You can't reach it from the bed..\n\n"; sleep 2; bedroomCommand(); } elsif ($light == TRUE) { print "There doesn't seem to be anything of use in here.\n\n"; sleep 3; bedroomCommand(); } } elsif ($command =~ "turn off the light") { print "\n\nI'm not afraid of the dark but I really feel like\n +I should get going.\n\n"; sleep 3; bedroomCommand(); } elsif ($command =~ "door" or $command =~ "open door" or $command = +~ "exit" or $command =~ "leave") { if ($standing == FALSE) { print "You can't reach it from the bed..\n\n"; sleep 2; bedroomCommand(); } elsif ($uniform == HAVE and $standing == TRUE) { print "You open the door and walk out into the hallway.\n\ +n"; sleep 2; system "cls"; $currentRoom = HALLWAY; hallwayCommand(); } elsif ($uniform == DONT_HAVE) { print "You can't help but feeling you're forgetting someth +ing...\nWhatever it is, you feel naked without it....\n\n"; sleep 2; bedroomCommand(); } } elsif ($command =~ "save game" or $command =~ "save progress") { saveGame(); } elsif ($command =~ "quit") { quitMenu(); } else { print "Command not recognized. Try again."; sleep 2; bedroomCommand(); } } sub hallwayCommand { print "\n>:"; chomp ($command = <STDIN>); $command =~ tr/A-Z/a-z/; if ($command =~ "look") { print "\n\nYou are in a hallway\nThere are two doors.\nOne lea +ding to your bedroom\nand the other leading to a utility closet.\n A +sign on the wall indicates\nthe research lab is located to the right\ +nand the control room is located to the left."; sleep 4; hallwayCommand(); } elsif ($command =~ "inventory" or $command =~ "open inventory") { $currentRoom = HALLWAY; inventory(); } elsif ($command =~ "utility" or $command =~ "closet" or $command = +~ "open utility closet" or $command =~ "go into utility closet") { if ($closetKey == DONT_HAVE) { print "\n\nYou try to open the utility closet door but find it + to be locked.\nThe key has to be around here somewhere.\n\n"; sleep 3; hallwayCommand(); } else { print "\n\nYou use the closet key to unlock the door\nand proc +eed into the utility room.\n\n"; sleep 4; system "cls"; utilityRoomCommand(); } } elsif ($command =~ "bedroom" or $command =~ "back to bedroom" or $ +command =~ "go in bedroom") { print "\n\nYou open the door to your bedroom and\nproceed back + inside."; sleep 3; system "cls"; $currentRoom = BEDROOM; bedroomCommand(); } elsif ($command =~ "control room" or $command =~ "left" or $comman +d =~ "go to control room" or $command =~ "go left" or $command =~ "tu +rn left") { print "\n\nYou walk down the hallway to the left.\nA short dis +tance later you see a pressure locked door\nwith a sign above it read +ing 'Control Room Access'\n"; sleep 4; if ($controlRoomPressureStabilization == TRUE and $controlRoom +DoorLock == FALSE) { print "\nYou twist open the pressure seal, open the door, +and proceed inside."; sleep 4; system "cls"; $currentRoom = CONTROL_ROOM; controlRoomCommand(); } else { print "\nYou try to open the door, but it\nappears to be s +ealed.\n\n"; sleep 4; hallwayCommand(); } } elsif ($command =~ "go to lab" or $command =~ "go right" or $comma +nd =~ "turn right" or $command =~ "right" or $command =~ "lab") { print "You walk down the hallway to the right.\nYou notice qui +te a bit of steam coming from this direction.\nAfter turning a corner +, you see the research lab.\nThe door hangs open and a steam or gas s +eeps out.\nYou walk in, noticing you seem to feel a little lightheade +d.\n"; sleep 8; system "cls"; $currentRoom = LAB; labCommand(); } elsif ($command =~ "save game" or $command =~ "save progress") { saveGame(); } elsif ($command =~ "quit") { quitMenu(); } else { print "\n\nCommand not recognized. Try again.\n\n"; sleep 3; hallwayCommand(); } } sub utilityRoomCommand { print "\n>:"; chomp ($command = <STDIN>); $command =~ tr/A-Z/a-z/; if ($command =~ "look") { print "\n\nThe shelves and supplies have been knocked all over + the room.\nHowever, you see a pair of legs sticking out from\nunder +one of the shelves. It looks like Dr. Willis.\n\n"; sleep 5; utilityRoomCommand(); } elsif ($command =~ "dr. willis" or $command =~ "willis" or $comman +d =~ "help" or $command =~ "legs") { print "\n\nYou run over to the shelves and, using all your str +ength, lift the\nshelf off of Dr. Willis. You pull him to the center\ +nof the room and assess his vital signs. His breathing\nand heart-rat +e are fine. He's just unconscious.\nThere's nothing I can really do n +ow.\nI'll come back to him later.\nThere's nothing else of use in her +e.\nSo, I better move on for now.\nYou step back into the hallway.\n\ +n"; sleep 10; system"cls"; hallwayCommand(); } elsif ($command =~ "leave" or $command =~ "use door" or $command = +~ "hallway") { print "\n\nThere's something terrible happening right in front + of me\nJust look! I can't leave now.\n\n"; sleep 3; utilityRoomCommand(); } elsif ($command =~ "save game" or $command =~ "save progress") { print "\n\nI can't do that here.\n\n"; sleep 3; utilityRoomCommand(); } elsif ($command =~ "quit") { print "\n\nI can't do that here.\n\n"; sleep 3; utilityRoomCommand(); } else { print "\n\nCommand not recognized. Please try again.\n\n"; sleep 2; utilityRoomCommand(); } } sub labCommand { print ">:"; chomp ($command = <STDIN>); $command =~ tr/A-Z/a-z/; if ($command =~ "look") { print "\n\nThe whole labratory is in dissaray.\nShelves are kn +ocked over. There appears to be a liquid leaking from\none of the pip +es on the wall, accompanied by a hissing noise\nand a steam-like gas +rising from leak\nMost of the computer terminals in this room are\nde +stroyed, but there is one that appears\nto be functioning. There is a + table across\nfrom the computers with research equipment on it.\nIt +is mostly beakers and test tubes."; print "However,\nthere is one notebook laying open"; if ($closetKey == DONT_HAVE) { print " and a key on the table.\n"; } sleep 5; labCommand(); } elsif ($command =~ "inventory" or $command =~ "open inventory") { inventory(); } elsif ($command =~ "lighter" or $command =~ "use lighter" or $comm +and =~ "light lighter") { endingC(); } elsif ($command =~ "computer" or $command =~ "use computer" or $co +mmand =~ "go to computer") { print "\n\nYou walk over to the computer terminal.\nThe comput +er's screensaver is active.\nThe keyboard and mouse appear to be inta +ct.\nYou hit the space bar a few times\nand the system wakes up displ +aying a prompt."; print "\n\nPlease swipe your keycard now..."; sleep 5; if ($keyCard == HAVE) { print "\n\nYou swipe your id card."; sleep 3; system "cls"; computerCommand(); } else { print "\n\nYou don't have the keycard required.\nSeems lik +e I might have left it in my bedroom"; sleep 3; labCommand(); } } elsif ($command =~ "notebook" or $command =~ "look at notebook" or + $command =~ "take notebook" or $command =~ "get notebook") { print "\n\nYou walk over to the table and look\nat the noteboo +k. There are sloppily drawn\ncalculations all over the notebook.\nThe +y appear to be attempting to predict the trajectory\nof several objec +ts in relation to the current\nposition of mars and the location\nof +the Cydonia research station. The calculations all\npoint to an impac +t...\n\n"; sleep 5; labCommand(); } elsif ($command =~ "key" or $command =~ "pick up key" or $command +=~ "take key" or $command =~ "grab key") { print "\n\nYou walk over to the table\nand pick up the key. Th +e label keychain attached\nto it says 'utility closet'\n\n"; sleep 3; $closetKey = HAVE; labCommand(); } elsif ($command =~ "leave" or $command =~ "door" or $command =~ "h +allway" or $command =~ "go back") { print "\n\nYou walk back through the door into the hallway."; sleep 3; system "cls"; $currentRoom = HALLWAY; hallwayCommand(); } elsif ($command =~ "save game" or $command =~ "save progress") { saveGame(); } elsif ($command =~ "quit") { quitMenu(); } else { print "\n\nCommand unrecognized. Please try again.\n\n"; sleep 2; labCommand(); } } sub computerCommand { system "cls"; use constant LAB_NOTES => 1; use constant SYSTEM_STATUS => 2; use constant SECURITY_OPTIONS => 3; use constant OPEN_COMMUNICATIONS => 4; use constant QUIT => 5; print "\t\tCYDONIA RESEARCH CONSOLE 14.B\n\t\tPLEASE CHOOSE FROM T +HE FOLLOWING MENU\n 1:..........................................VIEW LAB NOTES\n 2:......................................VIEW SYSTEM STATUS\n 3:........................................SECURITY OPTIONS\n 4:.....................................OPEN COMMUNICATIONS\n 5:....................................................QUIT\n"; print ">:"; chomp ($command = <STDIN>); if ($command =~ /^[a-zA-Z]/) { print "\n\nYou may only use numeric entries. Try again."; sleep 3; computerCommand(); } elsif ($command == LAB_NOTES) { labNotesCommand(); } elsif ($command == SYSTEM_STATUS) { print "..............\n"; sleep 1; print "....\n"; sleep 1; print "Querying system status. Please wait."; sleep 2; print "\n\nOxygen converters: Stable/Running\nMain Power: part +ial/Sector C offline\nAuxilary Power: Offline\nCommunications: Offlin +e\nEscape Pods: 1 and 3 active. 2, 5, and 4 offline.\n"; sleep 6; computerCommand(); } elsif ($command == SECURITY_OPTIONS) { securityOptions(); } elsif ($command == OPEN_COMMUNICATIONS) { print "\n\nOpening communications\n.................."; sleep 2; print "\nConnection established. Syncronizing Satellites."; sleep 1; print "\n..........................\n\n"; sleep 1; print "Outgoing communications currently offline\ndue to solar + cycle position."; sleep 3; print "Message Received from Earth communications"; print "\n............................\n"; sleep 3; print "\nMESSAGE DATE: Today\nURGENCY: 10\n Research Post Cydonia, our sensors indicate that\n there have been a series of catastrophic asteroid\n strikes in your sector. System sensors indicate\n air seal leaks in sectors B and C. In all\n possibility, you may all be dead. If any of you\n can, please respond. If communications are down\n immediatly proceed to escape pods for evacuation.\n More asteroid strikes are eminent in the next few\n hours. Godspeed."; sleep 7; computerCommand(); } elsif ($command == QUIT) { print "\n\nTerminating Login\n..................\n\n"; sleep 3; system "cls"; labCommand(); } } sub labNotesCommand { use constant APR_16 => 1; use constant APR_19 => 2; use constant MAY_1 => 3; use constant MAY_5 => 4; use constant TODAY => 5; use constant QUIT => 6; system "cls"; print "\t\t CYDONIA RESEARCH LAB NOTES\n\t\tCHOOSE ENTRY TO ACCESS +:\n 1:......................................04/16/23\n 2:......................................04/19/23\n 3:......................................05/01/23\n 4:......................................05/05/23\n 5:.........................................Today\n 6:.......................................Go Back\n"; print ">:"; chomp ($command = <STDIN>); if ($command =~ /^[a-zA-Z]/) { print "\n\nYou may only use numeric entries. Try again."; sleep 3; labNotesCommand(); } elsif ($command == APR_16) { print "\n\n\t\tEntry from 04/16/23:\nUser: Doctor Tennant\n\n Our first ever entry! My team and I landed earlier today at\n 09:17 EST. My coleagues are Dr. Smith: mechanical engineer,\n Dr. Willis: thermodynamic engineer, Dr. Margo: doctor of\n astrobiology and physics, and of course myself Dr. Tennant: MD +\n and doctor of biology. We entered the Martian atmosphere aroun +d\n 09:00 EST. At this point, around 09:08 EST, our landing probe\ +n encountered a 5 or 6 mile high Martian dust cyclone, commonly\ +n reffered to as a Dust Devil. Luckily, we were able to release\ +n the parachute in time to avoid being pulled in and our\n retrorockets allowed us to redirect around the cyclone. We\n are currently establishing contact with the communications\n satellites and expect to be able to radio a full report\n to Earth soon.\n"; sleep 15; labNotesCommand(); } elsif ($command == APR_19) { print "\n\n\t\tEntry from 04/19/23:\nUser: Doctor Tennant\n\n We had a bit of a scare today. An X-5 solar flare interfered\n with out communications satellites, causing us to lose contact +\n with mission support for over six hours. Our initial concern\n was that the satellite had been damaged and would need to be\n repaired somehow. However, we were able initiate a restart of the satellites systems using a launchable data probe.\n The LAKJSDKFJA;SLDL22332323XXXEWR5E2KLLL;AKLJDFKDKK\n\n DATA CORRUPT\nEND FILE"; sleep 8; labNotesCommand(); } elsif ($command == MAY_1){ print "\n\n\t\tEntry from 05/01/23:\nUser: Doctor Tennant\n\n" +; print "We've been on Mars for almost a month now. Our initial\ +n set up and equipment tests are almost completed. Today, we\n launched an expedition to the 'Face of Mars' in the Cydonia region. A region once thought to hold proof of former life\n on Mars, this ancient formation strongly resembles a large fac +e\n when viewed from above and at the correct angle. We took rock\ +n samples from several parts of the formation and created a 3-d\ +n mapped rendering of it, using a sonar program. We found severa +l\n pockets of ice hidden several meters below the surface all aro +und\n the face.\n"; sleep 10; labNotesCommand(); } elsif ($command == MAY_5) { print "\n\n\t\tEntry from 05/05/23:\nUser: Doctor Tennant\n\n" +; print "My tests have finally completed on our rock and water s +amples\n taken from the 'Face of Mars' region of Cydonia. In the rock\n samples we found detectable amounts the building\n blocks for life. However, it was the water that yielded the\n truly interesting results. There appears to be, and keep in\n mind we have not confirmed that no cross-contamination\n occured with the sample, some form of microbial life present\n in the frozen water deposits. It isn't like any organism\n I've ever observed before. It seems to be a sulfur based\n organism, that replicates incredibly quickly and is\n resistant to almost any poisonous compound\n"; sleep 13; labNotesCommand(); } elsif ($command == TODAY) { print "\n\n\t\tEntry from today:\nUser: Doctor Margo\n\n FILE NOT FOUND\nFILE MAY BE CORRUPT OR OPEN IN ANOTHER LOCATIO +N\n ERROR 19456."; sleep 4; labNotesCommand(); } elsif ($command == QUIT) { print "\n\nGoing back to menu...."; sleep 2; computerCommand(); } else { print "\n\nInvalid entry. Please choose from the menu."; sleep 3; system "cls"; labNotesCommand(); } } sub securityOptions { use constant CONTROL_ROOM_DOORS => 1; use constant ESCAPE_POD_LOCK => 2; use constant SECTOR_C_CONTROL => 3; use constant SECTOR_B_CONTROL => 4; use constant SECTOR_A_CONTROL => 5; use constant GO_BACK => 6; system "cls"; print "\t\t CYDONIA RESEARCH SECURITY MENU\n\t\tCHOOSE ENTRY TO AC +CESS:\n 1:..............................CONTROL ROOM DOORS\n 2:........................ESCAPE POD LOCK OVERRIDE\n 3:................................SECTOR C CONTROL\n 4:................................SECTOR B CONTROL\n 5:................................SECTOR A CONTROL\n 6:.........................................GO BACK\n"; print ">:"; chomp ($command = <STDIN>); if ($command =~ /^[a-zA-Z]/) { print "\n\nYou may only use numeric entries. Try again."; sleep 3; securityOptions(); } elsif ($command == CONTROL_ROOM_DOORS) { use constant LOCK => 1; use constant UNLOCK => 2; print "\n\nControl Room doors are currently: Locked\n Enter 1 to lock or 2 to unlock:"; chomp ($command = <STDIN>); if ($command =~ /^[a-zA-Z]/) { print "\n\nYou may only use numeric entries. Returning to +menu."; sleep 3; securityOptions(); } elsif ($command == LOCK) { print "\n\nThe door is now locked."; sleep 2; $controlRoomDoorLock = TRUE; securityOptions(); } elsif ($command == UNLOCK) { if ($controlRoomPressureStabilization == FALSE) { print "Cannot unlock the door at this time.\nSensors i +ndicate a dangerous pressure leak."; sleep 2; securityOptions(); } elsif ($controlRoomPressureStabilization == TRUE) { print "\nThe door is now unlocked."; sleep 2; $controlRoomDoorLock = FALSE; securityOptions(); } } else { print "\n\nInvalid command. Use 1 or 2."; sleep 3; securityOptions(); } } elsif ($command == ESCAPE_POD_LOCK) { use constant LOCK => 1; use constant UNLOCK => 2; print "\n\nEscape Pod doors are currently: Locked\n Enter 1 to lock or 2 to unlock:"; chomp ($command = <STDIN>); if ($command =~ /^[a-zA-Z]/) { print "\n\nYou may only use numeric entries. Returning to +menu."; sleep 3; securityOptions(); } elsif ($command == LOCK) { print "\n\nThe door is now locked."; sleep 2; $escapePodsLock = TRUE; securityOptions(); } elsif ($command == UNLOCK) { print "\nThe door is now unlocked."; sleep 2; $escapePodsLock = FALSE; securityOptions(); } } elsif ($command == SECTOR_C_CONTROL) { print "\n\nERROR\nSector C is currently Offline. Unable to acc +ess controls.\nReturning to menu."; sleep 3; securityOptions(); } elsif ($command == SECTOR_B_CONTROL) { print "\n\nERROR\nSector B is currently Offline. Unable to acc +ess controls.\nReturning to menu."; sleep 3; securityOptions(); } elsif ($command == SECTOR_A_CONTROL) { sectorACommand(); } elsif ($command == GO_BACK) { print "\n\nGoing back to main menu...."; sleep 3; system "cls"; computerCommand(); } } sub sectorACommand { use constant SEAL_SECTOR_B => 1; use constant SEAL_SECTOR_C => 2; use constant PRESSURE_REPORT => 3; use constant GO_BACK => 4; system "cls"; print "\t\t CYDONIA RESEARCH SECTOR A CONTROL\n\t\tCHOOSE ENTRY TO + ACCESS:\n 1:...................................SEAL SECTOR B\n 2:...................................SEAL SECTOR C\n 3:.................................PRESSURE REPORT\n 4:.........................................GO BACK\n"; print ">:"; chomp ($command = <STDIN>); if ($command =~ /^[a-zA-Z]/) { print "\n\nYou may only use numeric entries. Try again."; sleep 3; sectorACommand(); } elsif ($command == SEAL_SECTOR_B) { print "\n\nSector B sealing\n."; sleep 1; print "..........."; sleep 1; print "...........\n"; sleep 1; print "Sector B Sealed."; $sectorBSeal = TRUE; sleep 2; if ($sectorCSeal == TRUE) { $controlRoomPressureStabilization = TRUE; } sectorACommand(); } elsif ($command == SEAL_SECTOR_C) { print "\n\nSector C sealing\n"; sleep 1; print "........."; sleep 1; print ".........\n"; sleep 1; print "Sector C Sealed."; sleep 2; $sectorCSeal = TRUE; if ($sectorBSeal == TRUE) { $controlRoomPressureStabilization = TRUE; } sectorACommand(); } elsif ($command == PRESSURE_REPORT) { if ($sectorBSeal = FALSE) { print "\n\nSector B: Pressure Leak. CRITICAL."; } if ($sectorCSeal = FALSE) { print "\n\nSector C: Pressure Leak. CRITICAL."; } if ($sectorBSeal = TRUE and $sectorCSeal = TRUE) { print "\n\nPressure stabilized.\nSectors B and C sealed."; $controlRoomPressureStabilization = TRUE; sleep 3; } sleep 2; sectorACommand(); } elsif ($command == GO_BACK) { print "Going back to the security menu...."; sleep 3; system "cls"; securityOptions(); } else { print "\n\nCommand not recognized. Choose from the menu."; sleep 3; sectorACommand(); } } sub controlRoomCommand { print "\n>:"; chomp ($command = <STDIN>); $command =~ tr/A-Z/a-z/; if ($command =~ "look") { print "\n\nThe control room consists of a central hub with\nco +ntrols and computers and the circular\nwalls of the room are covered +in monitors\nterminals and gauges. Everything appears to be\nfunction +al. There are four escape pods along the far\nwall. None of which are + deployed. There is a computer terminal\nwith a visible alert message + in the middle hub of the room. There is also a\ncircular window to t +he outside adjacent to the escape pods."; sleep 5; controlRoomCommand(); } elsif ($command =~ "inventory" or $command =~ "open inventory") { $currentRoom = CONTROL_ROOM; inventory(); } elsif ($command =~ "window" or $command =~ "look out window") { print "\n\nYou walk over to the window and peer out.\nYou were +n't quite ready for what you see, however.\nYou feel your heart drop +as you look through\nthe window at the charred remains of sectors B a +nd C of the station.\nA large impact crator seems to point to some ki +nd of large asteroid.\nYou can't imagine anyone surviving in that wre +ckage..."; sleep 4; controlRoomCommand(); } elsif ($command =~ "computer" or $command =~ "use computer" or $co +mmand =~ "go to computer") { print "\n\nYou walk over to the computer to get a closer\nlook + at the message. It is the system monitoring\nthe vital signs of all +the crew members.\nDr. Smith and Dr. Margo's vital signs are non-exis +tent...\nHowever, mine seem fine and Dr. Willis' are weak but stable. +\n"; sleep 7; controlRoomCommand(); } elsif ($command =~ "leave" or $command =~ "use door" or $command = +~ "hallway" or $command =~ "door") { print "\n\nYou use the door and walk back out into the hallway +."; $currentRoom = HALLWAY; sleep 3; hallwayCommand(); } elsif ($command =~ "escape pod" or $command =~ "use escape pod" or + $command =~ "escape" or $command =~ "get in escape pod") { if ($escapePodsLock == FALSE) { areYouSure(); } elsif ($escapePodsLock == TRUE) { print "\n\nThe locks on the escape pods are currently enga +ged.\nIf I remember correctly, I can disable those from the labratory +."; sleep 3; controlRoomCommand(); } } elsif ($command =~ "save game" or $command =~ "save progress") { saveGame(); } elsif ($command =~ "quit") { quitMenu(); } else { print "Command not recognized. Try again.\n\n"; sleep 3; controlRoomCommand(); } } sub areYouSure { print "\n\nDo you really want to abandon the Cydonia research stat +ion\nand return to Earth? If I'm missing anything, I won't very well\ +nbe able to go back and get it...\n\n"; print ">:"; chomp ($command = <STDIN>); if ($command =~ /^[1-9]/) { print "\n\nPlease choose yes or no."; sleep 2; areYouSure(); } $command =~ tr/A-Z/a-z/; if ($command =~ "yes" or $command =~ "y") { if ($doctorWillisSaved == TRUE) { endingA(); } else { endingB(); } } elsif ($command =~ "no" or $command =~"n") { print "\n\nReturning to control room."; sleep 2; controlRoomCommand(); } else { print "\n\nCommand not recognized. Returning to control Room.\ +n"; sleep 3; controlRoomCommand(); } } sub inventory { system "cls"; print "#################################################\n"; print "############AVAILABLE INVENTORY##################\n"; print "#################################################"; if ($uniform == HAVE) { print "\n\nOne slightly uncomfortable uniform."; } if ($keyCard == HAVE) { print "\n\nYour access keycard."; } if ($lighter == HAVE) { print "\n\nOne old lighter."; } if ($closetKey == HAVE) { print "\n\nThe key to the utility closet."; } sleep 5; if ($currentRoom == BEDROOM) { bedroomCommand(); } elsif ($currentRoom == HALLWAY) { hallwayCommand(); } elsif ($currentRoom == LAB) { labCommand(); } elsif ($currentRoom == CONTROL_ROOM) { controlRoomCommand(); } } sub endingA { print "\n\nYou prepare the escape pods for launch. One for yoursel +f\n and one for Doctor Willis. You quickly run back to the utility ro +om\nand carry him into the command room. You lay him down\nin one of +the escape pods, activate its distress beacon, set its coordinates\nf +or Earth, close the doors, and send him home. You then prepare your o +wn pod as you watch\nDoctor Willis' pod shoot into the air out the ob +servation window.\nYou secure the door, lock in your coordinates, and + initiate the launch protocol.\nYou feel yourself pulled down hard as + the escape pod's thrusters\nshoot you into the Martian atmosphere. A +s you depart,\nyou look back, seeing the wreckage of B and C sections + scattered across the landscape.\nThe grief of the loss of your other + team mates grips the pit\nof your stomach tightly. You watch a 'Dust + Devil' swings past the pod,\nwhirling dust around the windows as Cyd +onia wane in the distance.\nYou say a silent goodbye to your fallen c +omrads, taking comfort\nin the fact you were at least able to save so +meone.\nYou close your eyes and lean your head back. 'What a trip' Yo +u think to yourself."; sleep 18; print "\n\nMission Successful."; sleep 3; playAgain(); } sub endingB { print "\n\nYou prepare your escape pod for launch. You get inside, + secure the doors, set your\ncoordinates for Earth, and intiate the l +aunch protocol. You feel the thrusters kick in as you are pulled back + into your seat in the pod.\nYou are lifted high into the Martian atm +osphere. As you ascend, you\nlook back at the shattered remains of se +ctors B and C of the research station.\nYou feel a sickening sadness +as the deaths of your comrads sinks in.\n'If only there was something + I could have done. If I could have just saved one of them...'\nYou s +igh and lean back into your seat, closing your eyes. 'It's going to b +e a long ride home' you think to yourself."; sleep 18; print "\n\nMission Successful...Sort of...You might have missed so +mething...hint hint."; sleep 3; playAgain(); } sub endingC { print "\n\nYou flick open the lighter and, after a few failed trie +s,\nit lights. However, since the labratory is currently\nleaking liq +uid oxygen and massive amounts of it are\nbeing converted to gas from + the heat of the room.\nThe flame causes a massive explosion. Time se +ems to slow down\nas you see the flame grow and you feel consumed wit +h heat.\nYour body is torn asunder into millions of little pieces\nas + the explosive force ruptures the entire habitat.\nThis causes a pres +sure leak resulting in every little piece of you\n,along with all the + lab equipment,being blown\ninto space. The report of the habitat's d +estruction\nand all its crew members' deaths won't even reach Earth\n +for another 14 minutes. Only one thought passes through your mind\nin + your final seconds. 'Why the hell\nwould anyone bring a lighter to a + pressurized oxygen habitat?"; print "\n\n\t\t\tGAME OVER"; sleep 18; playAgain(); } sub playAgain { use constant YES => 'y'; use constant NO => 'n'; system "cls"; print "\n\nWould you like to play again? Y or N?"; print "\n>:"; chomp ($command = <STDIN>); if ($command =~ /^[1-9]/) { print "\n\nNumerical entries not allowed. Use Y or N."; sleep 3; playAgain(); } $command =~ tr/A-Z/a-z/; if ($command == YES) { main(); } elsif ($command == NO) { print "\n\nThank you for playing!\nGoodbye!"; sleep 2; die; } else { print "\n\nCommand not recognized. Use Y or N."; sleep 3; playAgain(); } } sub saveGame { @saveArray = (); $saveArray[0] = $uniform; $saveArray[1] = $keyCard; $saveArray[2] = $lighter; $saveArray[3] = $closetKey; $saveArray[4] = $standing; $saveArray[5] = $light; $saveArray[6] = $controlRoomDoorLock; $saveArray[7] = $controlRoomPressureStabilization; $saveArray[8] = $sectorBSeal; $saveArray[9] = $sectorCSeal; $saveArray[10] = $doctorWillisSaved; $saveArray[11] = $currentRoom; writeGame(); } sub writeGame { my $OUT; my $size = @saveArray; open ($OUT, '+>', DATAFILEOUT); for (my $i = 0; $i < $size; $i++) { print ($OUT "$saveArray[$i]"); print ($OUT "\n"); } close $OUT; print "\n\n\t\tGame successfully saved. Goodbye"; sleep 3; mainMenu(); } sub loadGame { use constant MAX_INVENTORY => 12; my $IN; my $counter = 0; @loadArray = (); open ($IN, '<', DATAFILEIN); while (<$IN>) { chomp ($loadArray[$counter] = $_); $counter++; } close $IN; startFromLoad(); } sub startFromLoad { $uniform = $loadArray[0]; $keyCard = $loadArray[1]; $lighter = $loadArray[2]; $closetKey = $loadArray[3]; $standing = $loadArray[4]; $light = $loadArray[5]; $controlRoomDoorLock = $loadArray[6]; $controlRoomPressureStabilization = $loadArray[7]; $sectorBSeal = $loadArray[8]; $sectorCSeal = $loadArray[9]; $doctorWillisSaved = $loadArray[10]; $currentRoom = $loadArray[11]; system "cls"; if ($currentRoom == BEDROOM) { print "\n\nLoad Successful\nCurrent Room: Bedroom\n"; sleep 3; bedroomCommand(); } elsif ($currentRoom == HALLWAY){ print "\n\nLoad Successful\nCurrent Room: Hallway\n"; sleep 3; hallwayCommand(); } elsif ($currentRoom == LAB) { print "\n\nLoad Successful\nCurrent Room: Labratory\n"; sleep 3; labCommand(); } elsif ($currentRoom == CONTROL_ROOM) { print "\n\nLoad Successful\nCurrent Room: Control Room\n"; sleep 3; controlRoomCommand(); } } sub quitMenu { use constant YES => 'y'; use constant NO => 'n'; print "\n\nDo you wish to quit without saving? y or n?\n"; chomp ($command = <STDIN>); $command =~ tr/A-Z/a-z/; if ($command =~ /^[1-9]/) { print "\n\nYou may only enter letters in this field. Y for yes + or N for no."; sleep 3; quitMenu(); } elsif ($command == YES) { print "\n\nQuitting Game.\n...................."; sleep 3; die; } elsif ($command == NO) { print "\n\nReturning to game. Type 'save game' to save your pr +ogress."; sleep 3; if ($currentRoom == BEDROOM) { print "\n\nReturn Successful\nCurrent Room: Bedroom\n"; sleep 3; bedroomCommand(); } elsif ($currentRoom == HALLWAY){ print "\n\nReturn Successful\nCurrent Room: Hallway\n"; sleep 3; hallwayCommand(); } elsif ($currentRoom == LAB) { print "\n\nReturn Successful\nCurrent Room: Labratory\n"; sleep 3; labCommand(); } elsif ($currentRoom == CONTROL_ROOM) { print "\n\nReturn Successful\nCurrent Room: Control Room\n"; sleep 3; controlRoomCommand(); } } else { print "\n\nCommand not recognized. Use Y for yes and N for no. +"; sleep 3; quitMenu(); } }

Replies are listed 'Best First'.
Re: Text Based Perl Game
by Athanasius (Archbishop) on Dec 18, 2013 at 03:45 UTC

    Hello spoonman2525, and welcome to the Monastery!

    A few further comments:

    1. Anonymous Monk stole my thunder on this one, but definitely consider giving each lexical variable the smallest possible scope. For example, instead of one $command declared at the head of the script (thereby making it effectively global, despite its being declared with my), declare it each time it’s needed:

      sub mainMenu { ... chomp (my $command = <STDIN>); ...
    2. Also consider using lexical filehandles: open (my $OUT, '+>', DATAFILEOUT).

    3. The pragma use constant QUIT => 5; is actually implemented as a function, which is why you are getting warnings like this:

      Constant subroutine main::QUIT redefined at C:/Perl/Strawberry/strawbe +rry-perl-5.18.1.1-32bit-portable/perl/lib/constant.pm line 140.

      In other words, anything declared with use constant is a global. It’s better to give each constant a unique name:

      use constant QUIT_COMPUTER_COMMAND => 5;
    4. Reserve die for error conditions. For normal termination, prefer exit.

    5. In Perl, it’s really not necessary to define constants for TRUE and FALSE, and it’s certainly not necessary to name them explicitly when testing. For example, in sub bedroomCommand, it would be simpler to write:

      if ($light) { ... } else { ... }
    6. Familiarise yourself with Perl’s handy statement modifiers (see perlsyn). Then, instead of this (from sub loadGame):

      while (<$IN>) { chomp ($loadArray[$counter] = $_); $counter++; }

      you can write a single line, like this:

      chomp($loadArray[$counter++] = $_) while <$IN>;

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Text Based Perl Game
by GrandFather (Saint) on Dec 18, 2013 at 03:41 UTC

    There are three places where you:

    if (xxx = yyy) {

    which is not to be what you want. You have correctly used == elsewhere so you know about the difference between equality (==) and assignment (=) operators. The error is particularly bad because you get warnings during game start up that tell you about the problem!

    Your 'main' sub contains a whole bunch of code that will never execute because mainMenu never returns.

    True laziness is hard work
Re: Text Based Perl Game
by roboticus (Chancellor) on Dec 19, 2013 at 12:22 UTC

    spoonman:

    The biggest item I'd like to point out is that your flow control is flawed. You're using subroutines to call themselves and each other without ever returning, causing the stack to get deeper and deeper. I'd suggest instead that you have a control loop like this:

    sub process_command { print "\n\n:"; chomp ($command = <STDIO>); $command =~ tr/A-Z/a-z/; if ($currentRoom == HALLWAY) { hallwayCommand(); } elsif ($currentRoom == BEDROOM) { bedroomCommand(); } ... etcetera ... }

    Then, in the cases where you're calling xxxxxxxCommand(), you'd instead just use:

    $currentRoom = HALLWAY; return;

    Obviously, you can skip the assignment if you're already *in* the hallway.

    It looks like your game will be fun when you get it ironed out! But you're working too hard right now: there's duplicated code in various places. Whenever you see code that's duplicated, or is *similar* to code in other places, you should think about methods to turn it into a subroutine.

    For example:

    if ($uniform == HAVE) { print "\n\nOne slightly uncomfortable uniform."; } if ($keyCard == HAVE) { print "\n\nYour access keycard."; } if ($lighter == HAVE) { print "\n\nOne old lighter."; } if ($closetKey == HAVE) { print "\n\nThe key to the utility closet."; }

    In many (?all?) room, you have similar code. The problem as I see it is that you're individually managing each item by name (i.e., with a separate variable), which is pretty much *forcing* you to write all those duplicated lines of code.

    I'd suggest making a hash hold all your objects, so you can write a subroutine to display what you see, kinda like this:

    # At start of program, set up the starting location and description of + each object my $Objects = ( light => { location=>BEDROOM, description=>'The chair in front of your desk has your uniform s +lung over it', }, keycard => { location=>BEDROOM, description=>'Your keycard sits on the desk', } ... etcetera ... }; sub display_items_in_location { my $loc_to_display = shift; for my $item (keys %Objects) { if ($Objects{$item}{location} == $loc_to_display) { print $Objects{$item}{location}; } } }

    Then in your various rooms, you can display the items with a single subroutine call:

    sub bedroomCommand { ... elsif ($light == TRUE) { print "The sleeping quarters are quite small.... display_items_in_location(BEDROOM); print "There are no paintings or posters on the wall... sleep 5; }

    And when you want to pick something up, you just reference the object in the hash:

    elsif ($command =~ "keycard" or $command =~ "take keycard" ... ... elsif ($light == TRUE) { if ($Objects{keycard}{location} == HAVE) { print "You already have it.\n\n"; sleep 2; return; # from my first comment } else { print "You pick up the keycard.\n..... sleep 2; $Objects{keycard}{location} = HAVE; $Objects{keycard}{description} = 'Your access keycard. +';

    Notice that you may want to change the description as you do things with it, so your inventory doesn't look like it has a desk in it! Also, it can simplify your inventory, since HAVE is just another location.

    There are other things you'll eventually want to tune up, but other monks have caught many of them, so I'll leave them out for now.

    Note: I find that when working to improve on a task, like writing, I try to learn how to fix a couple of things at a time. It's too difficult to learn 10 things at once. So when I was learning to be a writer, I had my editors give me a "top five" list of faults in my writing after they review each article. I'd review my next article with those points in mind, and eventually, I'd learn one well enough that it would fall off the list to be replaced with something else.

    That's when I learned another thing: You'll never be perfect at it, as someone can always come up with a "top five" list of things for you to improve on. Though, when you get really good, they may have to review a *lot* of material to find all five!

    One more point about the "top five" list: Sometimes people will add an item to the list that isn't really a fact but an opinion. If you ever question an item on the list, double-check it with others to check. For example: code indentation and braces are a hotly contended topic. But for most languages, there isn't a "best" way to do it, usually it's just style and opinion. A few languages have relatively firm conventions that you may want to adhere to, such as those primarily used in a single IDE that insists on reformatting to it's convention. While opinions differ on where your braces go, I believe that *consistency* is quite important. Luckily, you're doing quite well on that. It makes the code easier to read and maintain.

    Every once in a while, I get a bit long winded, and that obviously happened today. So to wrap it up, I'd task you with a "top two items":

    • Think about ways to use subroutines to save yourself effort.
    • Practice your program flow handling.

    Keep it up, it looks like you're doing well and having fun. I'm looking forward to updates and future questions.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      The biggest item I'd like to point out is that your flow control is flawed. You're using subroutines to call themselves and each other without ever returning, causing the stack to get deeper and deeper. I'd suggest instead that you have a control loop like this: [...]

      Another solution would be to call the non-returning subs like this:

      goto &hallwayCommand;

      This would prevent the call stack from growing. (See the last two paragraphs of goto for more info.)

        smls:

        It's certainly viable. However, I'm hoping to point him in the direction that will lead to the use of dispatch tables and data-driven architecture to simplify longer term maintenance and story changes.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Text Based Perl Game
by kcott (Archbishop) on Dec 18, 2013 at 14:03 UTC

    G'day spoonman2525,

    Welcome to the monastery.

    Here's another suggestion. You have multiple strings (for printing) that are hundreds of characters in length, wrap many times, and have huge amount of formatting (newlines and tabs) embedded in each string. Without actually running the code, it would be (virtually) impossible to visualise the output format. In addition, I suspect this might become something of a maintenance nightmare: consider a situation where you need to insert a phrase and then need to reposition a dozen or more newline ("\n") characters from the point of insertion to the end of the string.

    I think you'll find the use of heredocs would be much easier. You can use these directly in the code or define them separately elsewhere in the code (such that the code logic is not obscured by large amounts of text).

    Here's an example using part of the code in sub endingA {...}:

    #!/usr/bin/env perl use strict; use warnings; print '-' x 40, "\n"; # (part of) Current code in sub endingA {...} print "\n\nYou prepare the escape pods for launch. One for yourself\n +and one for Doctor Willis. You quickly run back to the utility room\n +and carry him into the command room. You lay him down\nin one of the +escape pods, ...\n"; print '-' x 40, "\n"; print '-' x 40, "\n"; # Easier to read code for sub endingA {...} print <<'EOT'; You prepare the escape pods for launch. One for yourselfand one for Do +ctor Willis. You quickly run back to the utility roomand carry him in +to the command room. You lay him down in one of the escape pods, ... EOT print '-' x 40, "\n"; # Predefined text for use in sub endingA {...} my $endingA_text = <<'EOT'; You prepare the escape pods for launch. One for yourself and one for Doctor Willis. You quickly run back to the utility room and carry him into the command room. You lay him down in one of the escape pods, ... EOT print '-' x 40, "\n"; print $endingA_text; print '-' x 40, "\n";

    Output:

    ---------------------------------------- You prepare the escape pods for launch. One for yourself and one for Doctor Willis. You quickly run back to the utility room and carry him into the command room. You lay him down in one of the escape pods, ... ---------------------------------------- ---------------------------------------- You prepare the escape pods for launch. One for yourself and one for Doctor Willis. You quickly run back to the utility room and carry him into the command room. You lay him down in one of the escape pods, ... ---------------------------------------- ---------------------------------------- You prepare the escape pods for launch. One for yourself and one for Doctor Willis. You quickly run back to the utility room and carry him into the command room. You lay him down in one of the escape pods, ... ----------------------------------------

    Another, possibly better, alternative would be to define all these large tracts of text in an external file.

    -- Ken

Re: Text Based Perl Game
by Anonymous Monk on Dec 18, 2013 at 02:53 UTC
Re: Text Based Perl Game
by VincentK (Beadle) on Dec 26, 2013 at 18:32 UTC
    Hi Spoonman2525.

    I think a more object oriented approach will help you. My examples below are far from polished and perfect, but should still help you.

    For starters, I would abstract a 'player' and a 'room' to a type. This way, you can free them from being locked into a subroutine. This also allows you to setup a central command parser instead of having a set of commands for each room or action.

    Listed below is a skeleton of a more object oriented game. It does not contain all of the detail of your storyline, but it does show how to create a player ( it sets a character name, but you can change this behavior ) and load rooms. Within the current framework a player can pickup and drop items. Moving between rooms (so long as the door is not locked) is also allowed.

    The main game loop also needs a little work. I tried implement a framework that would allow the game to progress instead of halting on waiting for user input from <STDIN>.

    With all that said, I really hope that all of this helps. Best of luck with the game and your learning! I am interested to see what your next version looks like.

    Here is a sample player module.

    Here is a sample game room module.

    Here is a sample game room loadfile. You can play with the Bedroom door by changing 'LOCKED' to 'UNLOCKED'

    Here is a sample commands helpfile.

    Here is the main game script.

      VincentK:

      A couple suggestions:

      1. For player.pm and gameroom.pm, you're allowing the export of the method names, but the method names don't need to be exported. If they were class and/or utility functions (i.e. functions not needing an object reference) then I can see where exporting the name can be useful. But I don't see any value to exporting these method names.
      2. In the main game script, you're using subroutine prototypes, but they're not helpful in these cases. You may want to review the prototypes section of perldoc perlsub.

      Otherwise, it looks pretty good.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        I appreciate the feedback. Thanks.

        I will keep item no. one in mind going forward. As for item no. two, I had a feeling someone was going to point that out. I am so used to having forward declarations, that it kind of looks wrong to me if I don't have them in place. I do understand your point though.

        Thanks again.
Re: Text Based Perl Game
by spoonman2525 (Initiate) on Dec 25, 2013 at 00:29 UTC
    Thanks for all the tips guys. I'm working on implementing them right now, and I'll post a sleeker, better version soon. This is my first programming language; So, I'm really learning to understand the logic of programming. Eventually I want to program in Java or C++. Perl has been a great learning tool for me though.
      Hi there! I played through your game, and was able to get endingB() and endingC() to work, but not endingA(). Is it possible to do so in this version?