#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    31
    Rep Power
    2

    Text based game: modes


    Code:
    #array of directions
    DIRECTIONS = {"north": ( 0, 1, 0),
                  "south": ( 0,-1, 0),
                  "east" : ( 1, 0, 0),
                  "west" : (-1, 0, 0),
                  "up"   : ( 0, 0, 1),
                  "down" : ( 0, 0,-1)}
    
    ALL_DIRECTIONS = list(DIRECTIONS.keys())
    
    class Room:
        def __init__(self,descript,dirs=None,override_dirs=None):
            self.descript = descript
            if dirs == None:
                self.available_dirs = ALL_DIRECTIONS[:]
            else:
                self.available_dirs = dirs
            if override_dirs == None:
                self.override_dirs = {}
            else:
                self.override_dirs = override_dirs
    
    print("You open your eyes and find yourself in on of the C Block computer labs ... or, rather, the remnants of it. There are certain ly no terminals here, just a pile of debris ... it reminds you of the aftermath of the 1997 Year 12 muck up - the one where they set fire to the electricity transformer causing the entire street to be blacked-out for three days. As you stand up, you start to wonder how you got here ... the stench of decay soon changes that thought to one of getting out ...")
    
    #desciptions of every room 
    room1 = ("\nC block lab\nThere is nothing but debris here. You have fond memories of happier hours,\nsitting on stools, hunched over computer terminals. Now, not even the terminals remain.\n")
    room2 = ("\nBalcony\nThe balcony has seen better days. Most of it has collapsed - rusting steel \nbars jut out from crumbling concrete. Oddly enough, the stairs seem ok.\n")
    room3 = ("\nCourtyard\nThe C Block courtyard appears to be locked tight. You notice, however, \nthat someone has sawn off some of the bars of the Western gate.\n")
    room4 = ("\nLoading Bay\nThis is where stores are delivered. The enormous dumpster, you remember \nbeing here, is strangely absent. The concrete her appears to have deep gouges in it.\n")
    room5 = ("\nQuad\nThe Quad is the center of the school. There is a large gray metal box attached \nto the wall of the library.\n")
    room6 = ("\nB Block Lab\nOnce full of gleaming terminals, this large room is now empty except for \nan enormous dumpster that appears to have crashed through the ceiling from the floor above. There is a door east.\n")
    room7 = ("\nB Block Quad\nThis is the B Block Quad. The sun is shining, the birds are singing ... \nIt strikes you as being a wonderful sort of day.\n")
    room8 = ("\nCorridors\nThe crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room9 = ("\nCorridors\nThe crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room10 = ("\nCorridors\nThe crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room11 = ("\nCorridors\nThe crazy grounds committee has planted a row of hedges here ...\n You are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room12 = ("\nYou open your eyes to find you have fallen asleep in Software Design again.\nEverything is as it should be - except somehow better.")
    room13 = ("\nWas it all really just a dream ? Or are you dreaming now Neo ... Who cares !\nSomehow the experience has given you the answer you needed to finish your last programming assignment\nfor the year. That means a well-earned rest is imminent. Or possibly some free time to write that adventure game you always wanted to write.")
    
    class Game:
        
        # Initialises variables
        def __init__(self):
            self.exit = False
            self.map_dict = self.get_maps()
            self.current_loc = [0,0,0]
            self.CurrentMap = self.map_dict[tuple(self.current_loc)]
            self.commands = {"go"   : self.go,
                             "quit" : self.quitgame,
                             "look" : self.look,
                             "exits": self.exits}
            
        #defines where each room is on the map and overides
        def get_maps(self):
            maps = {(0,0,0):Room(room1,["west"]),
                    (-1,0,0):Room(room2,["east","down"]),
                    (-1,0,-1):Room(room3,["west","up"]),
                    (-2,0,-1):Room(room4,["south","east"]),
                    (-2,-1,-1):Room(room5,["north","east","west"]),
                    (-1,-1,-1):Room(room6,["east", "west"]),
                    (0,-1,-1):Room(room7,["north","south","east","west","up","down"]),
                    (-3,-1,-1):Room(room8,["north","south","east","west"],{"south":(-4,-1,-1)}),
                    (-3,0,-1):Room(room9,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-3,-1,-1)}),
                    (-4,0,-1):Room(room10,["north","south","east","west"],{"north":(-3,0,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (-4,-1,-1):Room(room11,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (1,-1,-1):Room(room12,["north","south","east","west","up","down"],{"north":(-5,-5,-5),"south":(-5,-5,-5),"east":(-5,-5,-5),"west":(-5,-5,-5),"up":(-5,-5,-5),"down":(-5,-5,-5)}),
                    (-5,-5,-5):Room(room13,[])}
            return maps
    
        #when the user inputs "look" it just prints output.
        def look(self,args=None):
           if not args:
            print("You see nothing but endless void stretching off in all directions ...")
    
        #when the user inputs exits, the game prints out a list of valid directions where it can go
        def exits(self,args=None):
           if not args:
            print("You can go {}.".format(self.CurrentMap.available_dirs))
        
        def go(self,args):
            def change_it():
                self.CurrentMap = self.map_dict[tuple(self.current_loc)]
                print(self.CurrentMap.descript)
            direct = args[0] if args else None
            if direct:
                if direct in self.CurrentMap.override_dirs:
                    self.current_loc = list(self.CurrentMap.override_dirs[direct])
                    change_it()
                #fixes an error where "sorry, im afraid i cant do that" would appear at wrong times.
                elif direct in DIRECTIONS and "all" in self.CurrentMap.override_dirs:
                    self.current_loc = list(self.CurrentMap.override_dirs["all"])
                    change_it()
                #changes the room coordinations
                elif direct in self.CurrentMap.available_dirs:
                    for i in range(3):
                        self.current_loc[i]+=DIRECTIONS[direct][i]
                    change_it()
                #if no valid directions are input then print this
                else:
                    if direct not in self.CurrentMap.available_dirs:
                        print("Sorry, I'm afraid I can't do that !")
            #if first word is recognised but there is no second input then print this
            else:
                print("Please tell me more ...")
     
        #if user inputs quit then changes self.exit to True which ends the game.
        def quitgame(self,args):
            if not args:
                print("OK ... but a small part of you may never leave until you have personally saved Muirfieland from the clutches of evil .. Bwahahahahahah (sinister laugh) ...")
                self.exit = True
        
        def process_command(self,command):
            com = command.lower().split()
            if com and com[0] in self.commands:
                self.commands[com[0]](com[1:])
            else:
                print("Huh?")
    
        #run the game and defines the user input.
        def run(self):
            while self.exit != True:
                src = input('> ')
                self.process_command(src)
                
    if __name__ == "__main__":
        #runs the game
        game = Game()
        game.run()
    ok soo ive got a code atm, with hardcoded rooms locations etc.
    pretty much i need to add modes, verbose mode and shh mode. here are descriptions
    shh : This enables the shh mode, in which location descriptions are only provided if the player enters a location for the first time. There is no effect on other output, including the output of the location name, which is always output after each command.
    verbose : The default mode. This turns off shh mode, so that locations descriptions are provided after each move to a location, regardless of whether or not that location has been previously visited by the player.

    i know how to do this... but it would essentially mean doubling the the amount of code i have... and i know there has to be a more efficient way of doing it.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Should be straight forward enough. Give your game class a new attribute called self.shh or whatever. Initialize it to False.
    When you give the shh command flip it to True and vice-versa. Now give your rooms another flag called self.seen or something. This will start as False and flip to True if the room has been visited.

    When you enter a room check the Game self.ssh attribute. If it is False just show the room. If it is True check the Rooms self.seen attribute and show or not accordingly.

    Something like that should work.
    -Mek
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Mekire
    Should be straight forward enough. Give your game class a new attribute called self.shh or whatever. Initialize it to False.
    When you give the shh command flip it to True and vice-versa. Now give your rooms another flag called self.seen or something. This will start as False and flip to True if the room has been visited.

    When you enter a room check the Game self.ssh attribute. If it is False just show the room. If it is True check the Rooms self.seen attribute and show or not accordingly.

    Something like that should work.
    -Mek
    ahhhh ok i get what your saying, ill give it a try.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    31
    Rep Power
    2
    hey mekire just a BIG question. this is an embarrasing question but im having trouble getting the game to end when its at "room 13"
    Code:
    def get_maps(self):
            maps = {(0,0,0):Room(room1,["west"]),
                    (-1,0,0):Room(room2,["east","down"]),
                    (-1,0,-1):Room(room3,["west","up"]),
                    (-2,0,-1):Room(room4,["south","east"]),
                    (-2,-1,-1):Room(room5,["north","east","west"]),
                    (-1,-1,-1):Room(room6,["east", "west"]),
                    (0,-1,-1):Room(room7,["north","south","east","west","up","down"]),
                    (-3,-1,-1):Room(room8,["north","south","east","west"],{"south":(-4,-1,-1)}),
                    (-3,0,-1):Room(room9,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-3,-1,-1)}),
                    (-4,0,-1):Room(room10,["north","south","east","west"],{"north":(-3,0,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (-4,-1,-1):Room(room11,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (1,-1,-1):Room(room12,["north","south","east","west","up","down"],{"north":(-5,-5,-5),"south":(-5,-5,-5),"east":(-5,-5,-5),"west":(-5,-5,-5),"up":(-5,-5,-5),"down":(-5,-5,-5)}),
                    (-5,-5,-5):Room(room13,)}
            return maps
    now how come that putting [self.exit = True] in room 13 not work?. which if u can get it to work it would make the whole room.seen = True much easier thankyou.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by jacobace
    hey mekire just a BIG question. this is an embarrasing question but im having trouble getting the game to end when its at "room 13"
    Code:
    def get_maps(self):
            maps = {(0,0,0):Room(room1,["west"]),
                    (-1,0,0):Room(room2,["east","down"]),
                    (-1,0,-1):Room(room3,["west","up"]),
                    (-2,0,-1):Room(room4,["south","east"]),
                    (-2,-1,-1):Room(room5,["north","east","west"]),
                    (-1,-1,-1):Room(room6,["east", "west"]),
                    (0,-1,-1):Room(room7,["north","south","east","west","up","down"]),
                    (-3,-1,-1):Room(room8,["north","south","east","west"],{"south":(-4,-1,-1)}),
                    (-3,0,-1):Room(room9,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-3,-1,-1)}),
                    (-4,0,-1):Room(room10,["north","south","east","west"],{"north":(-3,0,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (-4,-1,-1):Room(room11,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (1,-1,-1):Room(room12,["north","south","east","west","up","down"],{"north":(-5,-5,-5),"south":(-5,-5,-5),"east":(-5,-5,-5),"west":(-5,-5,-5),"up":(-5,-5,-5),"down":(-5,-5,-5)}),
                    (-5,-5,-5):Room(room13,)}
            return maps
    now how come that putting [self.exit = True] in room 13 not work?. which if u can get it to work it would make the whole room.seen = True much easier thankyou.
    Code:
    (-5,-5,-5):Room(room13,[])}
    soz there was a problem there in the room 13 part.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Did you get that room 13 problem sorted then? or still have a problem?

    Implementing the self.seen idea really is pretty trivial. I can show you if you want or you can try it yourself if you prefer.

    -Mek
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Mekire
    Did you get that room 13 problem sorted then? or still have a problem?

    Implementing the self.seen idea really is pretty trivial. I can show you if you want or you can try it yourself if you prefer.

    -Mek
    i think the self.seen thing would be easy... I THINK. but i just can u show me where to put the self.exit = True soo the game ends.. when it reaches floor 13
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Just check if the current location of the player corresponds to the location of the final map and set exit to True if it does.

    python Code:
    #array of directions
    DIRECTIONS = {"north": ( 0, 1, 0),
                  "south": ( 0,-1, 0),
                  "east" : ( 1, 0, 0),
                  "west" : (-1, 0, 0),
                  "up"   : ( 0, 0, 1),
                  "down" : ( 0, 0,-1)}
     
    ALL_DIRECTIONS = list(DIRECTIONS.keys())
     
    class Room:
        def __init__(self,descript,dirs=None,override_dirs=None):
            self.descript = descript
            if dirs == None:
                self.available_dirs = ALL_DIRECTIONS[:]
            else:
                self.available_dirs = dirs
            if override_dirs == None:
                self.override_dirs = {}
            else:
                self.override_dirs = override_dirs
            self.seen = False
     
     
    #desciptions of every room
    room1 = ("\nC block lab","There is nothing but debris here. You have fond memories of happier hours,\nsitting on stools, hunched over computer terminals. Now, not even the terminals remain.\n")
    room2 = ("\nBalcony","The balcony has seen better days. Most of it has collapsed - rusting steel \nbars jut out from crumbling concrete. Oddly enough, the stairs seem ok.\n")
    room3 = ("\nCourtyard","The C Block courtyard appears to be locked tight. You notice, however, \nthat someone has sawn off some of the bars of the Western gate.\n")
    room4 = ("\nLoading Bay","This is where stores are delivered. The enormous dumpster, you remember \nbeing here, is strangely absent. The concrete her appears to have deep gouges in it.\n")
    room5 = ("\nQuad","The Quad is the center of the school. There is a large gray metal box attached \nto the wall of the library.\n")
    room6 = ("\nB Block Lab","Once full of gleaming terminals, this large room is now empty except for \nan enormous dumpster that appears to have crashed through the ceiling from the floor above. There is a door east.\n")
    room7 = ("\nB Block Quad","This is the B Block Quad. The sun is shining, the birds are singing ... \nIt strikes you as being a wonderful sort of day.\n")
    room8 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room9 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room10 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room11 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ...\n You are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room12 = ("","You open your eyes to find you have fallen asleep in Software Design again.\nEverything is as it should be - except somehow better.")
    room13 = ("","Was it all really just a dream ? Or are you dreaming now Neo ... Who cares !\nSomehow the experience has given you the answer you needed to finish your last programming assignment\nfor the year. That means a well-earned rest is imminent. Or possibly some free time to write that adventure game you always wanted to write.")
     
    class Game:
     
        # Initialises variables
        def __init__(self):
            self.exit = False
            self.map_dict = self.get_maps()
            self.current_loc = [0,0,0]
            self.CurrentMap = self.map_dict[tuple(self.current_loc)]
            self.ssh = False
            self.commands = {"go"   : self.go,
                             "quit" : self.quitgame,
                             "look" : self.look,
                             "exits": self.exits,
                             "ssh"  : self.change_ssh,
                             "full" : self.change_full}
     
        #defines where each room is on the map and overides
        def get_maps(self):
            maps = {(0,0,0):Room(room1,["west"]),
                    (-1,0,0):Room(room2,["east","down"]),
                    (-1,0,-1):Room(room3,["west","up"]),
                    (-2,0,-1):Room(room4,["south","east"]),
                    (-2,-1,-1):Room(room5,["north","east","west"]),
                    (-1,-1,-1):Room(room6,["east", "west"]),
                    (0,-1,-1):Room(room7,["north","south","east","west","up","down"]),
                    (-3,-1,-1):Room(room8,["north","south","east","west"],{"south":(-4,-1,-1)}),
                    (-3,0,-1):Room(room9,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-3,-1,-1)}),
                    (-4,0,-1):Room(room10,["north","south","east","west"],{"north":(-3,0,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (-4,-1,-1):Room(room11,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (1,-1,-1):Room(room12,["north","south","east","west","up","down"],{"north":(-5,-5,-5),"south":(-5,-5,-5),"east":(-5,-5,-5),"west":(-5,-5,-5),"up":(-5,-5,-5),"down":(-5,-5,-5)}),
                    (-5,-5,-5):Room(room13,[])}
            return maps
     
        #when the user inputs "look" it just prints output.
        def look(self,args=None):
           if not args:
            print("You see nothing but endless void stretching off in all directions ...")
     
        #when the user inputs exits, the game prints out a list of valid directions where it can go
        def exits(self,args=None):
           if not args:
              print("You can go {}.".format(self.CurrentMap.available_dirs))
     
        def change_ssh(self,args):
            print("Short-hand mode activated.")
            self.ssh = True
        def change_full(self,args):
            print("Verbose mode activated.")
            self.ssh = False
     
        def go(self,args):
            def change_it():
                self.CurrentMap = self.map_dict[tuple(self.current_loc)]
                print(self.CurrentMap.descript[0])
                if not self.CurrentMap.seen or not self.ssh:
                    print(self.CurrentMap.descript[1])
                self.CurrentMap.seen = True
                if self.current_loc == [-5,-5,-5]:
                    self.exit = True
            direct = args[0] if args else None
            if direct:
                if direct in self.CurrentMap.override_dirs:
                    self.current_loc = list(self.CurrentMap.override_dirs[direct])
                    change_it()
                #fixes an error where "sorry, im afraid i cant do that" would appear at wrong times.
                elif direct in DIRECTIONS and "all" in self.CurrentMap.override_dirs:
                    self.current_loc = list(self.CurrentMap.override_dirs["all"])
                    change_it()
                #changes the room coordinations
                elif direct in self.CurrentMap.available_dirs:
                    for i in range(3):
                        self.current_loc[i]+=DIRECTIONS[direct][i]
                    change_it()
                #if no valid directions are input then print this
                else:
                    if direct not in self.CurrentMap.available_dirs:
                        print("Sorry, I'm afraid I can't do that !")
            #if first word is recognised but there is no second input then print this
            else:
                print("Please tell me more ...")
     
        #if user inputs quit then changes self.exit to True which ends the game.
        def quitgame(self,args):
            if not args:
                print("OK ... but a small part of you may never leave until you have personally saved Muirfieland from the clutches of evil .. Bwahahahahahah (sinister laugh) ...")
                self.exit = True
     
        def process_command(self,command):
            com = command.lower().split()
            if com and com[0] in self.commands:
                self.commands[com[0]](com[1:])
            else:
                print("Huh?")
     
        #run the game and defines the user input.
        def run(self):
            print("You open your eyes and find yourself in on of the C Block computer labs ... or, rather, the remnants of it. There are certain ly no terminals here, just a pile of debris ... it reminds you of the aftermath of the 1997 Year 12 muck up - the one where they set fire to the electricity transformer causing the entire street to be blacked-out for three days. As you stand up, you start to wonder how you got here ... the stench of decay soon changes that thought to one of getting out ...")
            while self.exit != True:
                src = input('> ')
                self.process_command(src)
            input("\nWell, that's it folks. Press enter to exit.")
     
    if __name__ == "__main__":
        #runs the game
        game = Game()
        game.run()

    -Mek
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Mekire
    Just check if the current location of the player corresponds to the location of the final map and set exit to True if it does.

    python Code:
    #array of directions
    DIRECTIONS = {"north": ( 0, 1, 0),
                  "south": ( 0,-1, 0),
                  "east" : ( 1, 0, 0),
                  "west" : (-1, 0, 0),
                  "up"   : ( 0, 0, 1),
                  "down" : ( 0, 0,-1)}
     
    ALL_DIRECTIONS = list(DIRECTIONS.keys())
     
    class Room:
        def __init__(self,descript,dirs=None,override_dirs=None):
            self.descript = descript
            if dirs == None:
                self.available_dirs = ALL_DIRECTIONS[:]
            else:
                self.available_dirs = dirs
            if override_dirs == None:
                self.override_dirs = {}
            else:
                self.override_dirs = override_dirs
            self.seen = False
     
     
    #desciptions of every room
    room1 = ("\nC block lab","There is nothing but debris here. You have fond memories of happier hours,\nsitting on stools, hunched over computer terminals. Now, not even the terminals remain.\n")
    room2 = ("\nBalcony","The balcony has seen better days. Most of it has collapsed - rusting steel \nbars jut out from crumbling concrete. Oddly enough, the stairs seem ok.\n")
    room3 = ("\nCourtyard","The C Block courtyard appears to be locked tight. You notice, however, \nthat someone has sawn off some of the bars of the Western gate.\n")
    room4 = ("\nLoading Bay","This is where stores are delivered. The enormous dumpster, you remember \nbeing here, is strangely absent. The concrete her appears to have deep gouges in it.\n")
    room5 = ("\nQuad","The Quad is the center of the school. There is a large gray metal box attached \nto the wall of the library.\n")
    room6 = ("\nB Block Lab","Once full of gleaming terminals, this large room is now empty except for \nan enormous dumpster that appears to have crashed through the ceiling from the floor above. There is a door east.\n")
    room7 = ("\nB Block Quad","This is the B Block Quad. The sun is shining, the birds are singing ... \nIt strikes you as being a wonderful sort of day.\n")
    room8 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room9 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room10 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room11 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ...\n You are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room12 = ("","You open your eyes to find you have fallen asleep in Software Design again.\nEverything is as it should be - except somehow better.")
    room13 = ("","Was it all really just a dream ? Or are you dreaming now Neo ... Who cares !\nSomehow the experience has given you the answer you needed to finish your last programming assignment\nfor the year. That means a well-earned rest is imminent. Or possibly some free time to write that adventure game you always wanted to write.")
     
    class Game:
     
        # Initialises variables
        def __init__(self):
            self.exit = False
            self.map_dict = self.get_maps()
            self.current_loc = [0,0,0]
            self.CurrentMap = self.map_dict[tuple(self.current_loc)]
            self.ssh = False
            self.commands = {"go"   : self.go,
                             "quit" : self.quitgame,
                             "look" : self.look,
                             "exits": self.exits,
                             "ssh"  : self.change_ssh,
                             "full" : self.change_full}
     
        #defines where each room is on the map and overides
        def get_maps(self):
            maps = {(0,0,0):Room(room1,["west"]),
                    (-1,0,0):Room(room2,["east","down"]),
                    (-1,0,-1):Room(room3,["west","up"]),
                    (-2,0,-1):Room(room4,["south","east"]),
                    (-2,-1,-1):Room(room5,["north","east","west"]),
                    (-1,-1,-1):Room(room6,["east", "west"]),
                    (0,-1,-1):Room(room7,["north","south","east","west","up","down"]),
                    (-3,-1,-1):Room(room8,["north","south","east","west"],{"south":(-4,-1,-1)}),
                    (-3,0,-1):Room(room9,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-3,-1,-1)}),
                    (-4,0,-1):Room(room10,["north","south","east","west"],{"north":(-3,0,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (-4,-1,-1):Room(room11,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (1,-1,-1):Room(room12,["north","south","east","west","up","down"],{"north":(-5,-5,-5),"south":(-5,-5,-5),"east":(-5,-5,-5),"west":(-5,-5,-5),"up":(-5,-5,-5),"down":(-5,-5,-5)}),
                    (-5,-5,-5):Room(room13,[])}
            return maps
     
        #when the user inputs "look" it just prints output.
        def look(self,args=None):
           if not args:
            print("You see nothing but endless void stretching off in all directions ...")
     
        #when the user inputs exits, the game prints out a list of valid directions where it can go
        def exits(self,args=None):
           if not args:
              print("You can go {}.".format(self.CurrentMap.available_dirs))
     
        def change_ssh(self,args):
            print("Short-hand mode activated.")
            self.ssh = True
        def change_full(self,args):
            print("Verbose mode activated.")
            self.ssh = False
     
        def go(self,args):
            def change_it():
                self.CurrentMap = self.map_dict[tuple(self.current_loc)]
                print(self.CurrentMap.descript[0])
                if not self.CurrentMap.seen or not self.ssh:
                    print(self.CurrentMap.descript[1])
                self.CurrentMap.seen = True
                if self.current_loc == [-5,-5,-5]:
                    self.exit = True
            direct = args[0] if args else None
            if direct:
                if direct in self.CurrentMap.override_dirs:
                    self.current_loc = list(self.CurrentMap.override_dirs[direct])
                    change_it()
                #fixes an error where "sorry, im afraid i cant do that" would appear at wrong times.
                elif direct in DIRECTIONS and "all" in self.CurrentMap.override_dirs:
                    self.current_loc = list(self.CurrentMap.override_dirs["all"])
                    change_it()
                #changes the room coordinations
                elif direct in self.CurrentMap.available_dirs:
                    for i in range(3):
                        self.current_loc[i]+=DIRECTIONS[direct][i]
                    change_it()
                #if no valid directions are input then print this
                else:
                    if direct not in self.CurrentMap.available_dirs:
                        print("Sorry, I'm afraid I can't do that !")
            #if first word is recognised but there is no second input then print this
            else:
                print("Please tell me more ...")
     
        #if user inputs quit then changes self.exit to True which ends the game.
        def quitgame(self,args):
            if not args:
                print("OK ... but a small part of you may never leave until you have personally saved Muirfieland from the clutches of evil .. Bwahahahahahah (sinister laugh) ...")
                self.exit = True
     
        def process_command(self,command):
            com = command.lower().split()
            if com and com[0] in self.commands:
                self.commands[com[0]](com[1:])
            else:
                print("Huh?")
     
        #run the game and defines the user input.
        def run(self):
            print("You open your eyes and find yourself in on of the C Block computer labs ... or, rather, the remnants of it. There are certain ly no terminals here, just a pile of debris ... it reminds you of the aftermath of the 1997 Year 12 muck up - the one where they set fire to the electricity transformer causing the entire street to be blacked-out for three days. As you stand up, you start to wonder how you got here ... the stench of decay soon changes that thought to one of getting out ...")
            while self.exit != True:
                src = input('> ')
                self.process_command(src)
            input("\nWell, that's it folks. Press enter to exit.")
     
    if __name__ == "__main__":
        #runs the game
        game = Game()
        game.run()

    -Mek

    OMG IM SOO STUPID!!!
    ive been trying to do an if statement like is room == 13... instead o using co-ordinates,,,OMG how could i have been soo stupid. im gonan give a go at doing the modes by my self though but if i can't ill have a look at ur code
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Mekire
    Just check if the current location of the player corresponds to the location of the final map and set exit to True if it does.

    python Code:
    #array of directions
    DIRECTIONS = {"north": ( 0, 1, 0),
                  "south": ( 0,-1, 0),
                  "east" : ( 1, 0, 0),
                  "west" : (-1, 0, 0),
                  "up"   : ( 0, 0, 1),
                  "down" : ( 0, 0,-1)}
     
    ALL_DIRECTIONS = list(DIRECTIONS.keys())
     
    class Room:
        def __init__(self,descript,dirs=None,override_dirs=None):
            self.descript = descript
            if dirs == None:
                self.available_dirs = ALL_DIRECTIONS[:]
            else:
                self.available_dirs = dirs
            if override_dirs == None:
                self.override_dirs = {}
            else:
                self.override_dirs = override_dirs
            self.seen = False
     
     
    #desciptions of every room
    room1 = ("\nC block lab","There is nothing but debris here. You have fond memories of happier hours,\nsitting on stools, hunched over computer terminals. Now, not even the terminals remain.\n")
    room2 = ("\nBalcony","The balcony has seen better days. Most of it has collapsed - rusting steel \nbars jut out from crumbling concrete. Oddly enough, the stairs seem ok.\n")
    room3 = ("\nCourtyard","The C Block courtyard appears to be locked tight. You notice, however, \nthat someone has sawn off some of the bars of the Western gate.\n")
    room4 = ("\nLoading Bay","This is where stores are delivered. The enormous dumpster, you remember \nbeing here, is strangely absent. The concrete her appears to have deep gouges in it.\n")
    room5 = ("\nQuad","The Quad is the center of the school. There is a large gray metal box attached \nto the wall of the library.\n")
    room6 = ("\nB Block Lab","Once full of gleaming terminals, this large room is now empty except for \nan enormous dumpster that appears to have crashed through the ceiling from the floor above. There is a door east.\n")
    room7 = ("\nB Block Quad","This is the B Block Quad. The sun is shining, the birds are singing ... \nIt strikes you as being a wonderful sort of day.\n")
    room8 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room9 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room10 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ... \nYou are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room11 = ("\nCorridors","The crazy grounds committee has planted a row of hedges here ...\n You are surrounded by enormous green conifers ... paths lead off in all directions\n")
    room12 = ("","You open your eyes to find you have fallen asleep in Software Design again.\nEverything is as it should be - except somehow better.")
    room13 = ("","Was it all really just a dream ? Or are you dreaming now Neo ... Who cares !\nSomehow the experience has given you the answer you needed to finish your last programming assignment\nfor the year. That means a well-earned rest is imminent. Or possibly some free time to write that adventure game you always wanted to write.")
     
    class Game:
     
        # Initialises variables
        def __init__(self):
            self.exit = False
            self.map_dict = self.get_maps()
            self.current_loc = [0,0,0]
            self.CurrentMap = self.map_dict[tuple(self.current_loc)]
            self.ssh = False
            self.commands = {"go"   : self.go,
                             "quit" : self.quitgame,
                             "look" : self.look,
                             "exits": self.exits,
                             "ssh"  : self.change_ssh,
                             "full" : self.change_full}
     
        #defines where each room is on the map and overides
        def get_maps(self):
            maps = {(0,0,0):Room(room1,["west"]),
                    (-1,0,0):Room(room2,["east","down"]),
                    (-1,0,-1):Room(room3,["west","up"]),
                    (-2,0,-1):Room(room4,["south","east"]),
                    (-2,-1,-1):Room(room5,["north","east","west"]),
                    (-1,-1,-1):Room(room6,["east", "west"]),
                    (0,-1,-1):Room(room7,["north","south","east","west","up","down"]),
                    (-3,-1,-1):Room(room8,["north","south","east","west"],{"south":(-4,-1,-1)}),
                    (-3,0,-1):Room(room9,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-3,-1,-1)}),
                    (-4,0,-1):Room(room10,["north","south","east","west"],{"north":(-3,0,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (-4,-1,-1):Room(room11,["north","south","east","west"],{"north":(-3,0,-1),"south":(-4,-1,-1),"east":(-4,0,-1),"west":(-4,0,-1)}),
                    (1,-1,-1):Room(room12,["north","south","east","west","up","down"],{"north":(-5,-5,-5),"south":(-5,-5,-5),"east":(-5,-5,-5),"west":(-5,-5,-5),"up":(-5,-5,-5),"down":(-5,-5,-5)}),
                    (-5,-5,-5):Room(room13,[])}
            return maps
     
        #when the user inputs "look" it just prints output.
        def look(self,args=None):
           if not args:
            print("You see nothing but endless void stretching off in all directions ...")
     
        #when the user inputs exits, the game prints out a list of valid directions where it can go
        def exits(self,args=None):
           if not args:
              print("You can go {}.".format(self.CurrentMap.available_dirs))
     
        def change_ssh(self,args):
            print("Short-hand mode activated.")
            self.ssh = True
        def change_full(self,args):
            print("Verbose mode activated.")
            self.ssh = False
     
        def go(self,args):
            def change_it():
                self.CurrentMap = self.map_dict[tuple(self.current_loc)]
                print(self.CurrentMap.descript[0])
                if not self.CurrentMap.seen or not self.ssh:
                    print(self.CurrentMap.descript[1])
                self.CurrentMap.seen = True
                if self.current_loc == [-5,-5,-5]:
                    self.exit = True
            direct = args[0] if args else None
            if direct:
                if direct in self.CurrentMap.override_dirs:
                    self.current_loc = list(self.CurrentMap.override_dirs[direct])
                    change_it()
                #fixes an error where "sorry, im afraid i cant do that" would appear at wrong times.
                elif direct in DIRECTIONS and "all" in self.CurrentMap.override_dirs:
                    self.current_loc = list(self.CurrentMap.override_dirs["all"])
                    change_it()
                #changes the room coordinations
                elif direct in self.CurrentMap.available_dirs:
                    for i in range(3):
                        self.current_loc[i]+=DIRECTIONS[direct][i]
                    change_it()
                #if no valid directions are input then print this
                else:
                    if direct not in self.CurrentMap.available_dirs:
                        print("Sorry, I'm afraid I can't do that !")
            #if first word is recognised but there is no second input then print this
            else:
                print("Please tell me more ...")
     
        #if user inputs quit then changes self.exit to True which ends the game.
        def quitgame(self,args):
            if not args:
                print("OK ... but a small part of you may never leave until you have personally saved Muirfieland from the clutches of evil .. Bwahahahahahah (sinister laugh) ...")
                self.exit = True
     
        def process_command(self,command):
            com = command.lower().split()
            if com and com[0] in self.commands:
                self.commands[com[0]](com[1:])
            else:
                print("Huh?")
     
        #run the game and defines the user input.
        def run(self):
            print("You open your eyes and find yourself in on of the C Block computer labs ... or, rather, the remnants of it. There are certain ly no terminals here, just a pile of debris ... it reminds you of the aftermath of the 1997 Year 12 muck up - the one where they set fire to the electricity transformer causing the entire street to be blacked-out for three days. As you stand up, you start to wonder how you got here ... the stench of decay soon changes that thought to one of getting out ...")
            while self.exit != True:
                src = input('> ')
                self.process_command(src)
            input("\nWell, that's it folks. Press enter to exit.")
     
    if __name__ == "__main__":
        #runs the game
        game = Game()
        game.run()

    -Mek
    sorry, could you explain something for me.
    Code:
    room12 = ("","You open your eyes to find you have fallen asleep in Software Design again.\nEverything is as it should be - except somehow better.")
    room13 = ("","Was it all really just a dream ? Or are you dreaming now Neo ... Who cares !\nSomehow the experience has given you the answer you needed to finish your last programming assignment\nfor the year. That means a well-earned rest is imminent. Or possibly some free time to write that adventure game you always wanted to write.")
    what does the "", att he start of room 12,13 do? and how does does it work?, because i noticed when i deleted that the only output that came on the screen was the letter "y" in room 12 and "w" in room 13.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    If a room is seen you want only the title of the room to appear (in your short-hand mode). To facilitate this I divided the room descriptions in to (title,description) tuples. If a room hasn't been seen we show both; if it has we only show the title.

    If you delete the first part then instead of self.CurrentMap.descript[0] referring to the title, it now refers to the first letter of the description, and thus your result.

    Rooms 12 and 13 don't have titles but we still need a place holder empty string.

    -Mek
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    31
    Rep Power
    2
    Originally Posted by Mekire
    If a room is seen you want only the title of the room to appear (in your short-hand mode). To facilitate this I divided the room descriptions in to (title,description) tuples. If a room hasn't been seen we show both; if it has we only show the title.

    If you delete the first part then instead of self.CurrentMap.descript[0] referring to the title, it now refers to the first letter of the description, and thus your result.

    Rooms 12 and 13 don't have titles but we still need a place holder empty string.

    -Mek
    thankyou for your help. im now going to implement text wrap soo it looks better when its run instead of using /n in the middle of print something. ill be doing this by using importing textwrap.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Your room-7 has directions that break the game by taking you to maps that don't exist; I believe you intended to override all directions but not sure. Also note the default arguments for Room. If you don't pass a second argument (or you pass None in the case of needing a third argument) available_directions defaults to all directions. Similarly if you want to override all directions to one location you can pass:
    Code:
    {"all":(-5,-5,-5)}
    or similar for the third argument.

    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo