import random SIZE = 10 CRITTER = "*" EMPTY = " " debugOn = False # Author: James Tam # This function was created entirely by the above author and is # used with permission. # @copy() # @Argument: a reference to the list that will be copied # @The list must be already created and properly initialized # @prior to calling this function! # @Return value: a complete copy of the list passed as the # @argument. def copy(world): temp = [] for r in range (0,SIZE,1): temp.append([]) for c in range (0,SIZE,1): temp[r].append(world[r][c]) return(temp) # Author: James Tam # This function was created entirely by the above author and is # used with permission. # ''' # @ This function comes the creation of the list with file input. # @ It combines readFromFile() and intialize() into one function. # @ Normally a function should only implement one well define task but in this case # @ the creation of the list is so trivial it may be okay to combine it with file input. # @createListFileRead() # @Argument: None # @Return value: the game world in the form of a 2D list (each element has # @been initialized to values read in from the input file # ''' def createListFileRead(): r = -1 c = -1 world = [] inputFilename = input("Name of input file: ") try: inputFile = open(inputFilename,"r") r = 0 for line in inputFile: world.append([]) c = 0 for ch in line: if (c < SIZE): world[r].append(ch) c = c + 1 r = r + 1 inputFile.close() except IOError: print("Error reading from " + inputFilename) return(world) # Author: James Tam # This function was created entirely by the above author and is # used with permission. # ''' # @display() # @Argument: two references to the 2D list which is game world. # @The list must be already created and properly initialized # @prior to calling this function! # @Return value: None # @Displays each element of the world with each row all on one line # @Each element is bound: above, left, right and below with a bar (to # @make elements easier to see. # @Each list is displayed side by side # # # ''' def display(turn,oldWorld,newWorld): # Displays a row at a time of each list print("Turn #%d" %turn) print("BEFORE\t\t\tAFTER") for r in range (0,SIZE,1): # Row of dashes before each row of old and new list # (Dashes for old list) for i in range (0, SIZE, 1): print("%s" %(" -"), end="") print("#\t",end="") # (Dashes for new list) for i in range (0, SIZE, 1): print("%s" %(" -"), end="") print() # Display one row of old world list for c in range (0,SIZE,1): # Display: A vertical bar and then element (old list) print("|%s" %(oldWorld[r][c]), end = "") # Separate the lists with a number sign and a tab print("", end = "#\t") # Display one row of new world list for c in range (0,SIZE,1): # Display: A vertical bar and then element (new list) print("|%s" %(newWorld[r][c]), end = "") print("|") # Row of dashes after end of last row (old world list) for i in range (0, SIZE, 1): print("%s" %(" -"), end="") print("#\t",end="") # Row of dashes after end of each row (new world list) for i in range (0, SIZE, 1): print("%s" %(" -"), end="") print() # Author: James Tam # This function was created entirely by the above author and is # used with permission. # ''' # @ This function works in conjunction with readFromFile() # @intialize() # @Argument: None # @Return value: the game world in the form of a 2D list (each element # @is set to an exclamation mark). # ''' def initialize(): world = [] for r in range (0, SIZE, 1): world.append ([]) for c in range (0, SIZE, 1): world[r].append ("!") return(world) # ''' # @ This method works in conjunction with initialize. Initialize creates # @ the list with list elements containing a default value '!'. This method # @ relies on the list already being created and sets each list element to # @ a corresponding value read in from the input file e.g. the string at # @ row 2 and column 4 in the input file will initialize the list element # @ at this same location in the 2D list (game world). # @readFromFile() # @Argument: None # @Return value: the game world in the form of a 2D list (each element # @will now be initialized to the values read in from the input file # ''' def readFromFile(): r = -1 c = -1 world = initialize() # Needed to create the 2D list inputFilename = input("Name of input file: ") try: inputFile = open(inputFilename,"r") r = 0 # Read one line at a time from the file into a string for line in inputFile: c = 0 # Iterate 1 char at a time through the string for ch in line: # Including EOL there's 11 characters per line # 10x10 list , exclude the EOL to avoid reading # outside the bounds of the list (10 columns) if (c < SIZE): # Set list element to the single char # read from file world[r][c] = ch # Advance to next element along row c = c + 1 # Entire row has been set to values read in from # file, move to next row r = r + 1 inputFile.close() except IOError: print("Error reading from " + inputFilename) return(world) ################################################################# # Author: James Tam # This function was created entirely by the above author and is # used with permission. def start(): turn = 0 world = readFromFile() newWorld = copy(world) display(turn,world,newWorld) start()