''' File: apr10.py Author: Jeff Kinne Contents: What I plan to do in class today. I prepared this file before class, and will save the actual file we did in class after class. ''' # Plan - work on project for about half the class. # Then - put try/except into maze game so if they try to load game that # does not exist, will not crash. Also, give them choice of what # to call the saved game, maybe where to store it. # Note: HW8 due Thursday. # Next up if we get to it - doing a simple search engine. We'll store # information about all the files on the in-class website and make it so we # can search through them. # Note... # import os # print os.getcwd() # print os.listdir(os.getcwd()) def printWorld(aMap,currRow,currCol): print '\nThe map looks like this.' for row in aMap: s = ''.join(row) print s import random, pickle def mazeGame(): worldMap = ''' **************************** ******* *********** ******* ******** *********** *$ * ****** ************** ****** ****** M @****** ****************************''' mapList = worldMap[1:].split('\n') myMap = [] for row in mapList: myMap.append(list(row)) currRow = 3 currCol = 1 while True: printWorld(myMap,currRow,currCol) choice = raw_input('Which direction to go?\n'+ \ ' (n, s, e, w, or q to quit, v to saVe, l to Load) ') nextRow = currRow nextCol = currCol if choice == 'n': nextRow = currRow - 1 nextCol = currCol elif choice == 's': nextRow = currRow + 1 nextCol = currCol elif choice == 'e': nextRow = currRow nextCol = currCol + 1 elif choice == 'w': nextRow = currRow nextCol = currCol - 1 elif choice == 'q': print 'Quittting...' break elif choice == 'v': print 'Saving the game...' s = pickle.dumps([myMap,currRow,currCol]) f = open('saveGame.txt','w') f.write(s) f.close() elif choice == 'l': print 'Loading saved game...' try: # get string from file. f = open('saveGame.txt') s = f.read() f.close() # convert string back to lists and stuff L = pickle.loads(s) # save it back into our variables. myMap = L[0] currRow = L[1] currCol = L[2] nextRow = currRow nextCol = currCol except IOError: print 'Could not load. No file.' else: print 'Not a valid choice. Try again. ' if myMap[nextRow][nextCol] == '*': print 'You ran into a wall. Stop it. Try again. ' elif myMap[nextRow][nextCol] in [' ','.'] : print 'Moving you...' myMap[currRow][currCol] = '.' myMap[nextRow][nextCol] = '$' currRow = nextRow currCol = nextCol elif myMap[nextRow][nextCol] == '@': print 'You win fabulous prizes and glory.' break elif myMap[nextRow][nextCol] == 'M': # monster, coin flip decides who wins. if random.randint(0,10) == 0: # move into the space, we win. print 'monster vanquished.' myMap[currRow][currCol] = '.' myMap[nextRow][nextCol] = '$' currRow = nextRow currCol = nextCol else: print 'sorry, the monster bested you this time..'