# Author: James Tam # Version: March 9, 2011 import random # CONSTANTS SIZE = 4 FIELD = " " FOREST = "^" WATER = "W" BURNT = "F" ERROR = "!" ''' @ generateElemement @ a random number argument (precondition 1<=number<=100) @ returns a string of length one (an element to populate world @* 50% are empty fields @* 30% are forests @* The remaining 20% consist of water ''' def generateElement(randomNumber): element = ERROR if ((randomNumber >= 1) and (randomNumber <= 50)): element = FIELD elif ((randomNumber >= 51) and (randomNumber <= 80)): element = FOREST elif ((randomNumber >= 81) and (randomNumber <= 100)): element = WATER else: element = ERROR return(element) ''' @ initialize() @ no parameters @ returns a fully initialized randomly generated world ''' def initialize(): world = [] r = -1 c = -1 randomNumber = -1 newElement = ERROR for r in range (0,SIZE,1): world.append([]) # Add in new empty row for c in range (0,SIZE,1): randomNumber = random.randrange(1,101) element = generateElement(randomNumber) world[r].append(element) # Add new element to row end return(world) ''' @ display() @ argument: 2D list of char(length 1 string), simulated game world @ return value: none @ @ Shows the current state of the world ''' def display(world): r = -1 c = -1 for r in range (0,SIZE,1): for c in range (0,SIZE,1): print(world[r][c], end="") print() print() ''' @ forestFire() @ argument: 2D list of char(length 1 string), simulated game world @ return value: none @ @ Simulates a massive forest fire, all the forests have been burnt down. ''' def forestFire(world): r = -1 c = -1 for r in range (0,SIZE,1): for c in range (0,SIZE,1): if(world[r][c] == FOREST): world[r][c] = BURNT # MAIN EXECUTION POINT def start(): world = initialize() print("Initial world") display(world) input("Hit enter to continue") print("Post fire world") forestFire(world) display(world) start()